Permissions and the frontmatter
Every program states what it may touch, in the frontmatter, in the same place a reader looks for its name. Nothing is granted by default.
The frontmatter keys
| key | meaning |
|---|---|
midad |
the language version; "0.1" is the only one. Required. |
module |
this file's module name, used by importers |
entry |
the function the runtime calls after exec (main by default) |
imports |
other .md files this one uses |
persist |
a file to save state blocks to and load them from |
static |
a directory served for every path no route matched |
dependencies |
parsed and unused; there is no package manager |
permissions |
the grants, below |
An unknown key is an error (E0005) that lists the ones it accepts, so a typo
does not silently do nothing. mdx: is the pre-rename spelling of midad: and
is still accepted; a file with both keys is E0006.
The grants
permissions:
fs_read: ["config.json", "data/"]
fs_write: ["out.txt"]
net: ["https://example.com"]
env: ["HOME"]
ffi: ["libm.so.6"]
listen: ["127.0.0.1:8787"]
db: ["app.db"]
ai: false
eval: false
| grant | covers |
|---|---|
fs_read |
read_file, fs.read, and reading for persist |
fs_write |
write_file, fs.write, and writing for persist |
net |
http_get, by scheme and host |
env |
env.get |
ffi |
native: blocks, by library name |
listen |
the address the HTTP server binds |
db |
db.open, by file or DSN |
ai |
ai:intent blocks |
eval |
eval_source |
Each one is checked twice: statically by midad check, and again at run time
against the actual target. The static check alone is not a sandbox, and the
language says so.
How a target is matched
A literal target must match an entry exactly, or sit under a directory
entry, or the list must hold "*". A computed target, such as a path that
came from a function argument, needs either "*" or a directory grant, because
the checker cannot know what the path will be:
---
midad: "0.1"
module: computed
permissions:
fs_read: ["config.json"]
---
```exec
const path = "config.json";
print(str(read_file(path)));
```
× E0300: `read_file` with a computed target needs `fs_read: ["*"]` or aThe fix is to grant the directory (fs_read: ["data/"]) or the wildcard, or to
write the literal at the call. The message names both options.
A route needs listen (E0303), persist needs both fs_read and fs_write
for its file (E0304), a native: block needs its library in ffi (E0301)
and an ai:intent needs ai: true (E0302). static: serves a directory over
HTTP, so it is a filesystem read: it needs an fs_read grant that covers that
directory, or * (E0305). Without that rule a program that declares no
filesystem access could publish the whole machine with one line of frontmatter.
Directory grants, and what the runtime does with them
fs_read: ["data/"] (or "data/**", the same thing) allows every path under
data/. The interpreter decides each call on real paths: the target, or for a
write to a file that does not exist yet its parent directory, is canonicalized
and must sit inside the canonical grant directory. .. and a symlink that
leaves the directory are refused, and so is a path that cannot be resolved or
contains a NUL. A relative grant is relative to the directory the program runs
in.
An exact grant (fs_read: ["/srv/app/config.json"]) names one file and is
decided the same way: the grant's parent directory is canonicalized and its own
name kept, and the target must resolve to exactly that. A symlink standing at
the granted name is refused rather than followed; a granted file that does not
exist yet can still be written; one that does not exist answers None on read.
Not covered: a file replaced by a symlink between the check and the open. That is a race the current code does not close.
Wildcards, and why a native build refuses them
"*" is honoured by the interpreter, which checks every call at run time. It is
refused by the native backends, which bake the allowed paths into the binary and
cannot enforce a set that has no members. The message names the grant:
a native build cannot enforce `fs_read: ["*"]`: grant exact paths or hosts, or
run the program interpreted
The same rule reaches a level up: a computed target needs "*" or a directory
grant to pass the checker, and a directory grant is refused by a native build
too. A program that reads a path chosen at run time runs interpreted only.
Network
net names a scheme and host and covers every path under it, not another host.
http_get is an in-process client, not a call out to curl, and it refuses any
address that resolves to a private, loopback, link-local, multicast or reserved
range, for IPv4, IPv6 and v4-mapped IPv6. "*" never allows one. A program that
needs a local service names it in net as an IP literal or localhost. The
checked address is pinned for the request, so a second DNS answer cannot swap
it. Redirects are followed by hand, three hops, each hop checked against net.
A body over 16 MiB, a 4xx or 5xx, or any failure answers None.
persist
persist: "state.json"
permissions:
fs_read: ["state.json"]
fs_write: ["state.json"]
The interpreter loads the state blocks from that file at startup and writes
them back after every run. An imported module's state is stored under its merged
name (module__name); the entry file's own state keeps its name.
A native build refuses a program with persist: by name. Nothing in the native
runtime persists, and running anyway would silently lose the state.
eval_source
eval_source(text) runs a string as a midad program and answers a map with its
output. It needs eval: true, and the evaluated code gets no permissions of its
own: it runs with the program's grants, and only with them.
listen
A route needs listen, and the address in the frontmatter is the one the server
binds unless --listen names another, which must also be granted. A request
that finds every interpreter instance busy gets 503.
What the model does not protect against
ffiis not a boundary. Once a native library is granted, its code runs with the process's full rights.- A wildcard in the interpreter is a wildcard. It is checked per call, but it allows every path.
- The static check is not the sandbox. The runtime check is. Both are needed; neither replaces the other.
- Nothing here sandboxes a program you did not read. It bounds what it can reach through the builtins, which is the case worth caring about: running a small program you got from somewhere else without handing it your machine.
Next: routes and serving.