#std #api #network #file

cap-std

Capability-based version of the Rust standard library

76 releases (17 stable)

2.0.0 Jun 30, 2023
1.0.16 Jun 29, 2023
1.0.15 May 16, 2023
1.0.9 Mar 29, 2023
0.0.0 Jun 25, 2020

#24 in Filesystem

Download history 16176/week @ 2023-08-11 18437/week @ 2023-08-18 18886/week @ 2023-08-25 16081/week @ 2023-09-01 18087/week @ 2023-09-08 17991/week @ 2023-09-15 16706/week @ 2023-09-22 19902/week @ 2023-09-29 16995/week @ 2023-10-06 17632/week @ 2023-10-13 24015/week @ 2023-10-20 22757/week @ 2023-10-27 21794/week @ 2023-11-03 22242/week @ 2023-11-10 17557/week @ 2023-11-17 15266/week @ 2023-11-24

81,017 downloads per month
Used in 160 crates (23 directly)

Apache-2.0…

525KB
11K 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

~0.4–12MB
~100K SLoC