83 releases (24 stable)

3.4.1 Nov 5, 2024
3.3.0 Sep 24, 2024
3.2.0 Jul 8, 2024
3.0.0 Jan 11, 2024
0.0.0 Jun 25, 2020

#17 in Filesystem

Download history 46005/week @ 2024-07-27 37696/week @ 2024-08-03 44616/week @ 2024-08-10 44580/week @ 2024-08-17 39908/week @ 2024-08-24 44234/week @ 2024-08-31 42433/week @ 2024-09-07 44138/week @ 2024-09-14 43177/week @ 2024-09-21 44306/week @ 2024-09-28 53115/week @ 2024-10-05 38956/week @ 2024-10-12 46693/week @ 2024-10-19 49599/week @ 2024-10-26 56309/week @ 2024-11-02 49021/week @ 2024-11-09

208,190 downloads per month
Used in 245 crates (32 directly)

Apache-2.0…

570KB
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

~1.8–10MB
~117K SLoC