#std #api #network #file

cap-std

Capability-based version of the Rust standard library

51 releases (25 breaking)

new 0.25.2 Jun 29, 2022
0.24.4 May 26, 2022
0.24.2 Mar 16, 2022
0.22.0 Dec 16, 2021
0.0.0 Jun 25, 2020

#50 in Network programming

Download history 5494/week @ 2022-03-15 5218/week @ 2022-03-22 9921/week @ 2022-03-29 10441/week @ 2022-04-05 7865/week @ 2022-04-12 10158/week @ 2022-04-19 9986/week @ 2022-04-26 8047/week @ 2022-05-03 9360/week @ 2022-05-10 7911/week @ 2022-05-17 5797/week @ 2022-05-24 7174/week @ 2022-05-31 6455/week @ 2022-06-07 4249/week @ 2022-06-14 7512/week @ 2022-06-21 6932/week @ 2022-06-28

27,017 downloads per month
Used in 42 crates (15 directly)

Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT

485KB
10K 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–6MB
~112K SLoC