# ADR-0006: Static file confinement

URL: https://scalws.com/docs/adr/0006-static-file-confinement/
Source: `docs/adr/0006-static-file-confinement.md` in the ScalWS repository

* Status: Accepted (2026-10-05), revised 2026-10-05 (openat2 implemented)

## Decision
1. Percent-decode the request path once. Reject NUL, backslash, decoded `/` inside a
   segment (`%2f`), and any `..` segment. Collapse `.` and empty segments.
2. Deny any segment starting with `.` except the first segment `.well-known`.
3. **Linux:** open the result with `openat2(RESOLVE_BENEATH | RESOLVE_NO_MAGICLINKS)`
   relative to a directory fd of the root opened at prepare time, with
   `O_NONBLOCK | O_NOCTTY | O_CLOEXEC`. The kernel guarantees confinement for the whole
   resolution, so a concurrently swapped symlink cannot escape (closes the TOCTOU window
   of the canonicalise approach), and opening a FIFO cannot block. File type and size
   come from `fstat` on the opened fd.
4. **Fallback, Linux** (kernels without `openat2`; seccomp policies that reject it): walk
   `rel` one component at a time from the root fd with `openat(O_NOFOLLOW)` (`O_PATH`
   directories, the leaf as in 3), never reopening a path; any symbolic link on the way
   is refused. A concurrent swap cannot leave the root (revised 2026-10-26: the earlier
   canonicalise-then-open fallback could be raced, security audit of panel mode).
   **Other OSes:** canonicalise `root/rel` and require it to stay beneath the canonical
   root (checked, not race-free).
5. Directories serve configured index files (same lookup); never list.

## Consequences
* With `RESOLVE_BENEATH`, **absolute symlinks are refused even if they point inside the
  root**; relative symlinks that stay inside the root work. The Linux fallback is
  stricter still: it refuses every symbolic link. Both are documented for operators.
* Non-blocking hot path (Linux): the lookup is first attempted on the async worker with
  `RESOLVE_CACHED` (5.12+; `EAGAIN` unless every component is in the dentry cache) and
  small files are read with `preadv2(RWF_NOWAIT)` (`EAGAIN` unless in the page cache).
  Anything that would block is redone on the blocking pool. Large files stream in 256 KiB
  positional reads, inline when cached, otherwise one blocking-pool hop per chunk; no
  thread is held between chunks, so slow clients cannot pin blocking threads.
* On overlayfs (e.g. a container's writable layer) `RESOLVE_CACHED` typically returns
  `EAGAIN`, so the blocking path is used: correct but slower. Serve document roots from a
  real filesystem (ext4/xfs, or a volume in containers).
