Skip to content

Permissions and untrusted input

A program can read files, open network connections and start other programs. Library code should not be able to do any of that behind your back, so in Brass these abilities are values:

  • fs.files() gives file access, net.access() network access, and os.processes() the right to start programs.
  • They can only be made in the program’s main file. Everything else, including packages and plugins, gets them as parameters.
  • Each can be narrowed before it is handed on: files.within(&"data"), access.public_only() (no private or local addresses), processes.only(&["git"]).
import fs
fn load_notes(files: &fs.Files, name: &Str) -> Str ! fs.IoError {
return try files.read(&("notes/" + *name))
}
let files = fs.files().within(&"notes")
let text = load_notes(&files, &"today.txt") catch ""
print(f"{text.len()} bytes")

A function’s signature now tells you what it can touch. load_notes can read files below notes/, and nothing else; it cannot reach the network at all. In any file other than the main one, calling fs.read directly, or making a new permission with fs.files(), is a compile error:

error: outside the program's main file, files are used through a permission:
take `files: &fs.Files` and call `files.read(...)`

Text that comes from outside, such as an HTTP request, a response body or the output of another program, arrives as Untrusted<Str>. It cannot be used where a Str is expected until it goes through a check that says what was verified, or unchecked() for when you really mean to take it as is.

fn serve(requested: &Untrusted<Str>) -> Str {
match requested.path_inside(&"/srv/www") {
Some(p) => return "serving " + p
None => return "refused"
}
}
print(serve(&Untrusted.new("/docs/index.html")))
print(serve(&Untrusted.new("/../../etc/passwd")))
print(serve(&Untrusted.new("/%2e%2e/secret")))
let user = Untrusted.new("<script>")
print(user.html_escaped())
match user.as_name() {
Some(n) => print("name " + n)
None => print("not a plain name")
}
serving /srv/www/docs/index.html
refused
refused
&lt;script&gt;
not a plain name

Other checks: only(allowed_bytes), to_i64(), is(text), starts_with(prefix), and checked(fn) for a check of your own.