78 releases (19 stable)

3.0.0 Jan 11, 2024
2.0.1 Jan 2, 2024
2.0.0 Jun 30, 2023
1.0.16 Jun 29, 2023
0.0.0 Jun 25, 2020

#19 in Filesystem

Download history 16074/week @ 2023-12-21 16939/week @ 2023-12-28 26699/week @ 2024-01-04 33435/week @ 2024-01-11 32942/week @ 2024-01-18 32231/week @ 2024-01-25 34649/week @ 2024-02-01 29612/week @ 2024-02-08 31696/week @ 2024-02-15 33315/week @ 2024-02-22 33093/week @ 2024-02-29 31547/week @ 2024-03-07 30173/week @ 2024-03-14 31182/week @ 2024-03-21 31736/week @ 2024-03-28 24980/week @ 2024-04-04

123,962 downloads per month
Used in 195 crates (26 directly)

Apache-2.0…

565KB
12K SLoC

cap-std

Capability-based version of the Rust standard library

Github Actions CI Status crates.io page docs.rs docs

This crate provides a capability-based version of std, providing sandboxed filesystem, networking, and clock APIs. See the toplevel README.md for more information about sandboxing using capability-based security.

The filesystem module cap_std::fs, the networking module cap_std::net, and the time module cap_std::time currently support Linux, macOS, FreeBSD, and Windows. WASI support is in development, though not yet usable.

Example usage of Dir for filesystem access:

use std::io;
use cap_std::fs::Dir;

/// Open files relative to `dir`.
fn dir_example(dir: &Dir) -> io::Result<()> {
    // This works (assuming symlinks don't lead outside of `dir`).
    let file = dir.open("the/thing.txt")?;

    // This fails, since `..` leads outside of `dir`.
    let hidden = dir.open("../hidden.txt")?;

    // This fails, as creating symlinks to absolute paths is not permitted.
    dir.symlink("/hidden.txt", "misdirection.txt")?;

    // However, even if the symlink had succeeded, or, if there is a
    // pre-existing symlink to an absolute directory, following a
    // symlink which would lead outside the sandbox also fails.
    let secret = dir.open("misdirection.txt")?;

    Ok(())
}

Example usage of Pool for network access:

use std::io;
use cap_std::net::Pool;

/// Open network addresses within `pool`.
fn pool_example(pool: &Pool) -> io::Result<()> {
    // Connect to an address. This succeeds only if the given address and
    // port are present in `pool`.
    let stream = pool.connect_tcp_stream("localhost:3333")?;

    Ok(())
}

Dependencies

~2–12MB
~125K SLoC