#std #api #network #file

cap-std

Capability-based version of the Rust standard library

64 releases (5 stable)

1.0.4 Jan 18, 2023
1.0.3 Dec 24, 2022
1.0.1 Nov 10, 2022
0.26.1 Sep 29, 2022
0.0.0 Jun 25, 2020

#52 in Network programming

Download history 14965/week @ 2022-10-07 10745/week @ 2022-10-14 12462/week @ 2022-10-21 9616/week @ 2022-10-28 10525/week @ 2022-11-04 12154/week @ 2022-11-11 7821/week @ 2022-11-18 9815/week @ 2022-11-25 8407/week @ 2022-12-02 9264/week @ 2022-12-09 11850/week @ 2022-12-16 4578/week @ 2022-12-23 6840/week @ 2022-12-30 10960/week @ 2023-01-06 9246/week @ 2023-01-13 10851/week @ 2023-01-20

38,668 downloads per month
Used in 69 crates (16 directly)

Apache-2.0…

495KB
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–5.5MB
~92K SLoC