Capability-gated I/O (roadmap #21)
A Trine program can only touch what it is handed. Nothing is reachable by default: no files, no environment variables, no input beyond what the user grants on the command line.
fn summarize(d): # (Dir) -> Result[Str, Str]: reads files in d, nothing else
let names = list_dir(d)?
...
fn main(sys): # (Sys) -> Unit: can do what the user granted
match dir(sys, "notes"):
Ok(d) => print(summarize(readonly(d)))
Err(e) => print(e) # no permission for 'notes' (run with --allow-read=notes ...)
trine run notes.tr --allow-read=notes -- first-arg second-arg
The model
Authority is a value. main may take one parameter, sys, of type Sys: the root capability. Nothing else can create a Sys. No function can reach the outside world without being handed a Sys, a Dir, or a function that holds one, and its type says which. You can read a function's reach straight off its signature:
| signature | can do |
|---|---|
(Str) -> Int | compute and print, nothing else |
(Dir) -> Result[Str, Str] | read and write files inside that directory (if it's writable) |
(Sys) -> Unit | arguments, environment, stdin, granted directories, programs and hosts |
(Conn) -> Str | talk over a connection it is given, nothing else |
((Str) -> Str) -> Str | whatever the function it's given can do |
This holds because:
- there is no global or ambient authority: no
open("/etc/passwd"); SysandDircan't implement traits (an error), so a generic function can't get at a capability hidden in a type variable;- capabilities can't be compared, hashed (they aren't
Keys), or forged.
trine caps file.tr prints every function's reach:
word_count : (Str) -> Int
none: computes and prints only
summarize : (Dir) -> Result[Str, Str]
files in directories it is given [uses list_dir, read_file]
main : (Sys) -> Unit
system (arguments, environment, input, opening granted directories) [uses args, dir, env, read_file, readonly]
3 of 5 function(s) can't reach the outside world (no Sys, Dir or function parameters)
This is the property that makes Trine a good host for code written by AI agents or pulled in from elsewhere: you review the signature, not the body.
Operations
| operation | type | notes |
|---|---|---|
args(sys) | (Sys) -> List[Str] | everything after -- |
env(sys, name) | (Sys, Str) -> Result[Str, Str] | only names granted by --allow-env |
read_line(sys) | (Sys) -> Option[Str] | without the newline (\r\n too); None at end of input |
read_all(sys) | (Sys) -> Str | invalid UTF-8 bytes become U+FFFD |
dir(sys, path) | (Sys, Str) -> Result[Dir, Str] | only inside a granted directory; writable if granted for writing |
read_file(d, name) | (Dir, Str) -> Result[Str, Str] | must be valid UTF-8 |
write_file(d, name, s) | (Dir, Str, Str) -> Result[Unit, Str] | creates or replaces |
list_dir(d) | (Dir) -> Result[List[Str], Str] | sorted (deterministic) |
subdir(d, name) | (Dir, Str) -> Result[Dir, Str] | narrower; keeps d's write permission |
readonly(d) | (Dir) -> Dir | the same directory, read-only |
now(sys) | (Sys) -> Float | seconds since 1970 (wall clock) |
seed(sys) | (Sys) -> Int | 64 random bits from the OS, to seed the generator |
random(state) | (Int) -> Int | the next generator state (SplitMix64); pure: no capability, same sequence in every engine |
random_below(state, n) | (Int, Int) -> Int | the state as a number in 0..n-1 |
append_file(d, name, s) | (Dir, Str, Str) -> Result[Unit, Str] | adds to the end; creates the file |
open_lines(d, name) | (Dir, Str) -> Result[Lines, Str] | a file to read a line at a time (for large files) |
next_line(r) | (Lines) -> Option[Str] | the next line without its \n/\r\n (invalid UTF-8 replaced); None at the end, which closes the file |
run(sys, cmd) | (Sys, List[Str]) -> Result[(Int, Str, Str), Str] | runs cmd[0] (found on the PATH; no shell) with the rest as arguments and empty input; (exit status, output, error output). A signal gives 128 + its number |
run_input(sys, cmd, s) | (Sys, List[Str], Str) -> Result[(Int, Str, Str), Str] | the same, with s as its standard input |
py_start(sys) | (Sys) -> Result[Py, Str] | a Python process for any importable module (needs --allow-python; library python.tr, docs/PYTHON.md) |
net(sys) | (Sys) -> Net | the network part of sys alone: a Net can do http and tcp_connect (to granted hosts) and nothing else, so tasks may hold one (M4) |
tcp_connect(sys, h, p) | (Sys, Str, Int) -> Result[Conn, Str] | a TCP connection to host h, port p; reads and writes give up after 60 seconds |
tcp_send(c, s) | (Conn, Str) -> Result[Unit, Str] | sends s (UTF-8) |
tcp_read_line(c) | (Conn) -> Result[Option[Str], Str] | the next line (without \n/\r\n; invalid UTF-8 replaced); None when the other side has closed |
tcp_read_all(c) | (Conn) -> Result[Str, Str] | everything until the other side closes |
tcp_close(c) | (Conn) -> Unit | closes now (a connection also closes when no longer used) |
http(sys, m, url, hs, b) | (Sys, Str, Str, Map[Str, Str], Str) -> Result[(Int, Map[Str, Str], Str), Str] | an HTTP request: method (GET, POST, ...), URL, headers, body; gives the status, the headers (names in lower case; repeated ones joined with , ) and the body (UTF-8, invalid bytes replaced). Any status is Ok: a 404 is an answer, not a failure |
The clock and the OS entropy need Sys because they make a program nondeterministic (and so stop trine run from switching to native code mid-run, like input does). The generator itself is an ordinary function: s = random(s), then random_below(s, 6) + 1 is a die roll; seed it with a constant for reproducible runs, or with seed(sys).
Not for secrets. SplitMix64 is fast and well spread but predictable: a few outputs reveal the rest. Use it for games, simulations, sampling and tests, never for tokens, passwords, keys or anything an attacker shouldn't guess; seed(sys) can also fall back to the clock when the OS source fails (SECURITY.md). For secrets, ask the OS through a granted program (run(sys, ["openssl", "rand", "-hex", "32"])) or Python's secrets module (PYTHON.md).
Failures are values (Err), not crashes: the messages say what to grant (no permission to read $HOME (run with --allow-env=HOME)).
Library functions built on these (docs/LIBRARY.md): read_lines(d, name), each_line(r) and stdin_lines(sys) (generators: for line in stdin_lines(sys)), http_get(sys, url) and http_post(sys, url, body, content_type) (the body of a 2xx response, an Err otherwise). eprint(x) (standard error) and exit(code) need no capability: they can't reach anything a program couldn't already affect by printing or stopping.
HTTP
http:// is spoken directly (HTTP/1.1, one request per connection, chunked bodies decoded); https:// goes through the system's curl (on Windows 10+, macOS and most Linux systems), which also honours the usual proxy settings. Either way the host must be granted, and a URL can't smuggle in a second request: methods are upper-case letters, header names and values can't contain line breaks, and URLs can't contain spaces or control characters. Redirects are not followed (the next host may not be granted): a 301/302 comes back with its location header.
Processes
run starts the program itself (execvp / CreateProcess), never a shell, so arguments are passed as they are: run(sys, ["git", "log", "--oneline", user_text]) can't be turned into a second command. The program inherits the environment and the current directory. It is outside Trine's checks once started (it can do whatever the user can), which is why each program must be granted by name.
Grants
--allow-read=DIR read files under DIR
--allow-write=DIR read and write files under DIR
--allow-env read any environment variable
--allow-env=A,B read only $A and $B
--allow-run=git,ls run these programs (matched by the name as the program gives it)
--allow-run run any program
--allow-net=H,H:P connect to these hosts (any port, or port P)
--allow-net connect to any host
-- ARGS... program arguments (after `--`, nothing is a grant)
Not available in WebAssembly builds: processes and the network (the operations give an Err saying so).
Grants work identically in all three engines and through the run cache (trine run passes them to cached native builds).
Confinement
A Dir can only be narrowed, never widened. A name is refused, with 'NAME' is outside the directory, when it:
- is absolute (
/etc/passwd; on Windows also\\x,C:x,C:\\x); - climbs out (
../x,sub/../../x), whether or not the target exists, so nothing about the outside is revealed; - on Windows: contains a backslash or a
:(another separator, a drive, an alternate data stream); - resolves through a symbolic link to somewhere outside (in a file name or any directory on the way).
Writes never follow a symbolic link at the last step (O_NOFOLLOW; on Windows the link itself is opened with FILE_FLAG_OPEN_REPARSE_POINT and refused), and reads open the resolved path the same way. On WASI the host's preopened directories are an outer fence: run a WebAssembly build with --dir D for each directory granted with --allow-read=D / --allow-write=D. dir(sys, p) doesn't reveal whether an ungranted path exists. Printing a Dir shows the path as the program wrote it.
Known limit. Names are resolved, then opened. A process that can change the directory tree between those two steps (replacing an intermediate directory with a link) could redirect an operation. Closing that race needs openat2(RESOLVE_BENEATH) on Linux; it is on the list for S5 (threat model). Trine's own code can't race itself: there is no concurrency yet.
Interaction with fast trine run
trine run interprets a fresh program while building it natively, then switches to the native build mid-run by replaying it and skipping output already printed (#43). Replay is only correct for deterministic programs, so once a program reads input or touches files, it stays in the interpreter for the rest of that run. The next run uses the cached native build from the start.
What's next
- Capability-scoped packages (#47): an imported module gets only the capabilities its importer passes. Needs modules (#18).
- Narrower network grants (a URL prefix rather than a host) and a TCP listener.
- Effect handlers (#20) can then intercept capability operations, e.g. to fake a file system in tests.