security: static serving follows symlinks out of the root #106

Closed
opened 2026-09-25 14:02:12 +00:00 by chrnx · 1 comment
Owner

Kept internal under SECURITY.md: this one is exploitable. The GitHub advisory goes out with the fix.

The traversal guard in staticResolver (src/views.ts:118) compares resolved paths lexically, and then stat and readFile follow symlinks. A link inside public/ that points outside it, left there by a build step, a dependency or an upload, serves whatever it points at.

Fix: take fs.promises.realpath of the root once and of the target on each hit, and refuse any target outside the real root. Test: public/leak -> /etc/hosts gives a 404.

Kept internal under SECURITY.md: this one is exploitable. The GitHub advisory goes out with the fix. The traversal guard in `staticResolver` (src/views.ts:118) compares resolved paths lexically, and then `stat` and `readFile` follow symlinks. A link inside `public/` that points outside it, left there by a build step, a dependency or an upload, serves whatever it points at. **Fix:** take `fs.promises.realpath` of the root once and of the target on each hit, and refuse any target outside the real root. **Test:** `public/leak -> /etc/hosts` gives a 404.
chrnx added the bugsecurity labels 2026-09-25 16:06:52 +00:00
Author
Owner

Fixed in 26.11.0-rc.1 (53660ad). From the changelog:

  • static no longer serves files outside its root through a symlink (#106). The traversal guard compared paths lexically, and the file read then followed symlinks, so public/leak -> /etc/hosts was served. Containment now compares real paths: the requested file, and a directory's index.html, must resolve beneath the root's real path. Links that stay inside the root, and a root that is itself a symlink, keep working. A link that resolves outside the root, or a dangling link, is a 404 — the same miss as a file that does not exist, rendered through onError like any other, and 405/204 where the path has routes. It is deliberately not a 403, so a probe cannot tell "exists outside the root" from "does not exist". The content-type still follows the requested name, not the link target. The root's real path is resolved on the first request and cached, so re-pointing a root symlink while the app runs needs a restart to take effect. A link swapped inside the root between the check and the read can still escape; exploiting that needs write access to the served directory.
Fixed in **26.11.0-rc.1** (`53660ad`). From the changelog: - **`static` no longer serves files outside its root through a symlink** (#106). The traversal guard compared paths lexically, and the file read then followed symlinks, so `public/leak -> /etc/hosts` was served. Containment now compares real paths: the requested file, and a directory's `index.html`, must resolve beneath the root's real path. Links that stay inside the root, and a root that is itself a symlink, keep working. A link that resolves outside the root, or a dangling link, is a **404** — the same miss as a file that does not exist, rendered through `onError` like any other, and `405`/`204` where the path has routes. It is deliberately not a `403`, so a probe cannot tell "exists outside the root" from "does not exist". The content-type still follows the requested name, not the link target. The root's real path is resolved on the first request and cached, so **re-pointing a root symlink while the app runs needs a restart** to take effect. A link swapped inside the root between the check and the read can still escape; exploiting that needs write access to the served directory.
chrnx closed this issue 2026-11-18 01:04:00 +00:00
Sign in to join this conversation.