Repository navigation
fix(files): serve a rendered page's files and media from the descriptor that was checked - #1892
Conversation
…or that was checked A rendered HTML page's own pictures, scripts and stylesheets, and the videos it plays from /api/fs/page-media, were checked by path and then read again by path, so a folder swapped for a link between the check and the read could put another file's bytes into the page or under its media address. Each such file is now opened once (O_NOFOLLOW, O_NONBLOCK), must be a regular file whose path still names it, and is proven per platform before any byte is read or an address minted: on Linux the descriptor's own /proc/self/fd path must lie in the page's folder or an OVIS_MEDIA_ROOTS folder with no hidden name (unreadable or deleted fails closed); on macOS every folder from that root down must be owned by this user and writable by no group or other user; Windows keeps the post-open identity recheck only (documented); other platforms authorize nothing. The page's folder is resolved before the page is opened. A media address reopens and re-checks on every request, plays only while the file has the identity, size and modification time its render saw, and streams from its descriptor in bounded positional reads with 200, HEAD, a single-range 206 and 416 (RFC 9110: another unit, several ranges or If-Range send the whole file), closing the descriptor exactly once. Ported from the unpushed fix/descriptor-bound-page-files branch, without its screenshot-stabilizing commits. Fixes #1609
…es come from A page's folder was resolved before the page was opened, but a folder swapped for a link around that lookup and put back before the open still made the link's target the folder the page's files were allowed from. The page read must now be the file of its name in that folder (same device and inode), or it is refused. A HEAD ignores Range and describes the whole file (RFC 9110 defines a range for GET alone), and an empty file is sent whole whatever range is asked. The failing-fstat test now fails the page's files and not the page itself, so it checks what it names.
…nly the inode check refuses it
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 35 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
CodeRabbit was rate-limited on this PR, so no CodeRabbit review ran. The PR changes the Round 1 (on the port,
Round 2 (on
Round 3 (on
Known residuals, stated in DESIGN.md
Not run: native Windows. The Windows test PC has been offline, and CI's Windows job does not run |
|
Merge pass gate review of 10bc4cb: one blocker. The page document can still come from outside its folder. Assets and media hold up. Blocker (reproduced): a folder swapped right after it is resolved still serves an outside page
Fix: open the page with the same Holds
Windows has no descriptor-path check (documented as outside the single-user threat model). The merge pass re-checks the fix commit, looking only at the changed range. |
The page's folder was fixed before the page was opened, but the page itself was still opened by path: a folder swapped for a link to another folder just after it was resolved, and left so, served that folder's page (the merge pass's gate reproduced an outside page answered with 200). The page's own path is now resolved together with its folder, before the open, and the page read must be that file: on Linux the kernel's path of its descriptor (an unreadable one fails closed), elsewhere its path resolved again after the open. A page that is itself a link is the file it leads to and keeps taking its files from the link's folder, as before; a test now holds that.
|
Thanks. The blocker is reproduced and fixed in 3fbf8d6. What changed:
This runs alongside the existing check that the page is the file of its name in the folder (same device and inode). A folder swapped for a link just after it is resolved, and left so, now gets 404. DESIGN.md says this too. Why not
Mutations, each reverted afterwards:
The CI failures on 10bc4cb were runner faults, not tests: three jobs ended with "The self-hosted runner lost communication with the server" before any step ran. 3fbf8d6 is pushed and starts a fresh run. Before the push, all three ran green on it through the shared heavy queue:
|
|
Merge pass gate re-check of 3fbf8d6: the reported swap is fixed, but the fix opens a new window between its two lookups. Fixed:
Still open: a swap between the two lookups serves the outside page. The code is at
The gate reproduced this with a real rename and a real symlink, between the two real lookups. The tests miss it because the Fix: resolve the folder once, then check the opened descriptor against that single result, not against a second
A second path lookup can always be raced. Only the descriptor's own path, compared with the first resolution, closes this. Please also move the test's swap hook so it runs between the folder resolution and the open. Holds
|
…its folder The previous commit resolved the page's folder and then the page's own path in two lookups, so a folder swapped for a link between them still served another folder's page (the merge pass reproduced it with a real rename and symlink). The folder is now looked up once; the page is opened as the file of its name in that folder (O_NOFOLLOW) and proven from its descriptor alone: on Linux its /proc/self/fd path must be that path; elsewhere that name must be a file with the descriptor's device and inode that still resolves to itself. An HTML file that is itself a symlink is therefore no longer drawn as a page (its Source view still shows it), noted in the changelog fragment. The swap test's hook now runs between the folder's lookup and the open, and also covers the other platforms' samples.
|
You're right, and thanks for the exact window. The second
One behaviour change, now in the changelog fragment: an HTML file that is itself a symlink is no longer drawn as Page. Its Source view still shows it, because the Page | Source toggle stays in the header. Seen from its descriptor, such a page can't be told from a folder swapped for a link, so refusing it is the cost of not doing a second lookup. If you'd rather keep symlinked pages, say so and I'll find another way. Mutations, each reverted afterwards:
Tests:
The earlier CI on 3fbf8d6 ended cancelled or failed on the self-hosted runners, not in a test step. The push starts a fresh run. |
…up leads to The previous commit stopped drawing an HTML file that is itself a symlink, to avoid a second path lookup. Symlinked pages are a normal setup, so the page's path is now resolved once (a link to the file it leads to), that file is opened with O_NOFOLLOW and proven from its descriptor as before (Linux: /proc/self/fd path; elsewhere: lstat identity and a realpath sample), and the page's files come from that file's folder. A linked page therefore takes its files from the folder it leads to, the only folder its one lookup proves; the changelog says so.
|
Merge pass gate re-check, round 3 (06b4e6c): the round 2 gap is closed, but the change reopens a schedule the previous commit refused. Closed. A swap right after the folder lookup, with the link left in place, is now refused from the descriptor. The new test reaches that point, and it fails if the descriptor guard is removed. 77 tests pass. Normal pages, their assets and legitimate symlinked parent folders still render, and descriptors are closed. Reopened: a swap that is put back before the open serves the outside page (
The same schedule gets a 404 on 3fbf8d6, because its second lookup no longer agreed. The test at Fix: keep the descriptor proof, and also check, after the open, that the requested path still resolves to the opened file ( Note (not blocking): off Linux, the check is a sample: it can still race between |
…e opened With one lookup, a folder that was a link only while the page's path was looked up, and was put back before the open, made that lookup another folder's page (the merge pass reproduced it on 06b4e6c with a real rename and symlink). After the descriptor proof, the asked path must still resolve to the opened file, so a swap at any one moment fails one of the two checks; a stable symlinked page still draws. The swap-and-restore test asserts the 404 and the absence of the other folder's page again, and a new test opens through a link put back before the later checks, which only the descriptor proof refuses (pageOpened seam).
|
Round 3 is fixed in 959af71 (the push also carries 19ea1bd). Thanks for keeping the proof honest. 19ea1bd, symlinked pages draw again. The owner's rule is usability over hardening, and symlinked pages are a normal setup:
959af71, your round 3 fix. After the descriptor proof, the asked path must still resolve to the opened file (
Mutations, each reverted, and each fails exactly the test named:
Results:
Your note is taken: off Linux, the page check remains a sample, as DESIGN.md says. |
|
Merge pass review-work report. Overall: PASSED after four gate rounds.
What 959af71 does
Gate evidence (round 4)
Assets and media (from round 1, unchanged): inlined images, scripts and stylesheets, Accepted by the documented single-user contract
All required checks are green and no review thread is open. CodeRabbit did not review this PR (rate-limited or skipped). The reviews above stand in for it. |
What
When the file viewer draws an HTML page an agent wrote (Page), the server puts the page's own pictures, scripts and stylesheets into it and gives a video too large for that a short-lived address under
/api/fs/page-media/. Until now both checked a path and then read it again by path (readFileSync,Bun.file). A folder swapped for a link between the check and the read could put another file's bytes into the page, where its script can read them, or under its video address.Now each of those files is opened once and read only from that descriptor, after the opened file itself was shown to lie where the page may reach (
openAuthorizedinserver/file-view.ts):The open:
O_RDONLY | O_NONBLOCK | O_NOFOLLOW. It must be a regular file with a nonzero inode, and the requested path, resolved again, must still name it (same device and inode).Linux: the kernel's path of the descriptor (
/proc/self/fd/<fd>) must lie in the page's folder or anOVIS_MEDIA_ROOTSfolder, with no hidden name below it. An unreadable path, or one marked(deleted), is refused, and nothing falls back to a path check.macOS: every folder from that root down to the file's own must be a real folder owned by the server's user and writable by no group or other user. This does not claim protection against the same user.
Windows: the second resolution and identity check only. Anyone allowed to write a folder on the way is outside the single-user model; documented, with no ACL claims.
Other platforms put no files into the page and mint no addresses.
The page itself: its path is resolved once, before the open; a page that is a symlink resolves to the file it leads to. That file is opened (
O_NOFOLLOW) and proven from its descriptor, never by a second lookup:/proc/self/fdpath must be that path.After the open, the asked path must still resolve to that file. The page's files come from that file's folder. A symlinked page still draws, taking its files from the folder it leads to, the only folder its one lookup proves (main took them from the link's folder).
Embedded files are read from their descriptor and rechecked for size and modification time.
A media address is minted only from a checked descriptor. Every request reopens and re-checks the file, and compares identity, size and modification time with what the render saw. It then streams from the descriptor in 256 KB positional reads, never the whole file. Answers are 200, HEAD, a single-range 206 and 416, per RFC 9110 section 14: another unit, several ranges, a malformed range, an
If-Rangeor a HEAD get the whole file, and so does an empty file. The descriptor is closed exactly once, on end, error or client cancel.server/file-routes.tspasses the request on, so Range and HEAD reach the stream.What users see does not change. Root, hidden-name and symlink rules are not loosened (links into allowed folders keep working), and the sandbox, CSP and cross-site gates are untouched. DESIGN.md states each platform's guarantee and its limits.
docs/guide.mdadds the macOS ownership rule toOVIS_MEDIA_ROOTS.This ports the two fix commits of the unpushed
fix/descriptor-bound-page-filesbranch onto current main, without its twelve screenshot-stabilizing commits. Review findings are fixed here as well, in a second commit. The page's folder used to be resolved after the page was read; it is now resolved first, and the page read must be the file of its name in that folder. Range and HEAD handling now follows RFC 9110.Security change: this needs a real review (it is the file-serving authorization path, and changes the
/api/fs/page-mediaHTTP answers).Fixes #1609
How it was verified
bun test ./server/file-view.test.tson Linux: 61 pass, 0 fail. The new tests cover:/procunreadable or(deleted);If-Range, an empty file, and close-once on cancel during a read./proccheck fails the three swap tests;(deleted)check fails the fail-closed test;fstateach fail their test;requestinfile-routes.tsfailspage-media.contract(206 expected, 200 received).darwin(fresh clone on the test MacBook Air): 57 pass, 3 skipped (the Linux-only swap tests), 0 fail.curl -iusing the page's own fetch headers:Content-Rangebytes for a closed and a suffix range;bytes */9437184;scripts/file-viewer-regression.tsin Chrome: a new step plays and seeks a rendered page's 9 MB recording from its address, refuses it once lapsed and once revoked, and checks every descriptor closed once. It passes on the final commit, where the whole script reports 62 PASS lines.bun run check faston the final commit: 6,421 pass, 0 fail.bun run check run bun test --timeout 15000 ./server/api.contract.test.ts ./server/page-media.contract.test.tson the isolated herdr: 160 pass, 0 fail.omo-native-code-reviewerand Codex (Sol 6.1, xhigh), over three rounds. Both reproduced the page-folder races fixed above; Codex did so with a real concurrent folder exchange, with nothing mocked. The last round approved after rerunning that race for 10 seconds with no hooks:page.htmlof its own in the hidden folder, 0 of 241,195 did.Not run: native Windows. The Windows test PC has been offline since before this work, and CI's Windows job does not run these tests.