Media Export logoMediaLisp
← iOS reference and overrides

docs/filesystem-policy.md

Unmodified engine source document, commit af8e0b5f6d97ab379337d3539fd8dbe0d6337fd8. Markdown source is displayed as text to preserve exact content.

# Filesystem policy for embedding hosts

`Interp::set_root(directory)` restricts subsequent engine path operations to
an existing directory. Configure it between evaluations, before guest file streams
are opened. `root()` returns its canonical path; `clear_root()` restores ordinary
path behavior. A failed `set_root` leaves the previous policy intact.

This policy assumes an app-owned directory whose namespace the host controls.
It does not resist concurrent path/symlink replacement, exclude pre-existing hard
links/mounts/special files, revoke open streams or confine subprocesses. Deny
process and environment capabilities for untrusted source. Give guests only
trusted host streams. It is not an OS sandbox.

With a root, relative names resolve under it. Absolute names within it work too.
Each existing component resolves through symlinks before later parent components;
missing components are allowed for new files/directories. Escapes, broken links,
empty paths and ambiguous Windows drive-relative paths fail with Stream errors.
Containment uses path components, so a sibling called `root-other` is outside
`root`. A host must control namespace changes throughout the check and operation.

OPEN, PROBE-FILE, DELETE-FILE, directory creation/enumeration, raw-byte writes,
ABSOLUTE-PATH, both publication modes and every file-loading route use the policy.
DELETE and publication operate on checked directory entries, preserving symlinks
rather than deleting/renaming their targets. Enumeration rejects a listing with
an escaping or broken link before following it for metadata. Publication checks
both operands before modifying either; no-replace remains exclusive.
PATH-JOIN only composes strings and provides no access by itself.

Ordinary LOAD becomes root-relative. RUSTLISP-LOAD-RELATIVE remains relative to
the active physical source file and uses the existing loader and source stack.
Host `load_file` bypasses the guest LOAD capability but still respects the root.
Capabilities remain independently necessary: files off still denies OPEN inside
the root. No root preserves existing cwd-relative behavior. No process cwd changes.

```rust
use rustlisp::{Capabilities, Interp};
fn example() -> rustlisp::EvalResult<()> {
let mut interp = Interp::standard()?;
interp.set_root("app-documents")?; // host creates and controls this directory
interp.set_capabilities(Capabilities {
    files: true, load: true, ..Capabilities::NONE
});
// This host load is restricted to app-documents, too.
interp.load_file("lesson.lisp")?;
Ok(())
}
```

Configure sessions on their interpreter's owner thread; the control handle holds
no filesystem policy or Lisp values. Cancellation still cannot preempt native
file I/O. Device hosts must test actual process memory and ordinary-stack behavior;
the interpreter's approximate heap-byte limit is not a total RSS limit.

The implementation audit maps each boundary to its existing operation:

| Boundary | Policy application |
|---|---|
| OPEN / WITH-OPEN-FILE | After option validation, before File/OpenOptions |
| LOAD / LOAD-RELATIVE / host load_file | Shared load_path, before source stack/read |
| PROBE-FILE / ABSOLUTE-PATH | Before canonicalization/path result |
| DELETE-FILE | Checked final entry before unlink |
| ENSURE-DIRECTORIES-EXIST | MAKE-DIRECTORIES before create_dir_all |
| DIRECTORY-ENTRIES | Directory before read_dir, each entry before metadata |
| WRITE-BYTES | Before OpenOptions/create/truncate |
| PUBLISH-FILE, replace and no-replace | Both checked entries before rename/link/unlink |
| MCP load_file | Interpreter root policy, including after reset |

Engine diagnostic logs are host-configured destinations. Process execution has
its own capability and can access arbitrary paths; it must remain denied for
confinement. Pure path composition and embedded prelude loading do not access
guest-selected files.