A missing asset is answered with the app, any method serves a file, and web-root containment is a string prefix #1311

Closed
opened 2026-11-10 01:25:03 +00:00 by jaylouisw · 0 comments
Owner

Found by pointing at the static server the same request-level lens used for #1307 and #1309. Three defects, and one
of them was pinned by a test that asserted the wrong thing.

1. A missing file is answered with the app

GET /nope.js   -> 200  text/html; charset=utf-8   <!DOCTYPE html> ...
GET /nope.png  -> 200  text/html

serveStatic() falls back to index.html for anything it cannot read. This app has no client-side routes - it
is one page with tabs - so the fallback serves no deep link that matters and instead hides every wrong filename.
In a browser the only report is Uncaught SyntaxError: Unexpected token '<', in a file the reader can no longer
identify. A missing favicon.ico reads as a broken app.

2. The method is ignored

POST /index.html    -> 200 (the document)
DELETE /index.html  -> 200

A client that POSTs can conclude its write landed. HEAD returns a body as well, including on the gzip branch
where res.end(gz) writes the compressed bytes.

3. Containment is a string prefix, not a path boundary

if (filePath.indexOf(WEB_DIR) !== 0) { 403 }

indexOf(WEB_DIR) === 0 also accepts …/web-backup/…, a sibling whose name merely starts with web. Nothing
exploits it today - the URL parser collapses .. before the path is joined - but a check that is only safe because
another component happens to clean up first is not a check, and the next directory added beside web/ inherits the
hole.

A test was pinning the defect

test/server-static-cache.test.js asserted:

ok(missing.status === 200, 'a missing asset falls back to the app rather than 404');
ok(contentType.indexOf('text/html') === 0, 'and is served as HTML');

So the behaviour was not merely untested, it was specified, in the wrong direction. That is why it survived.

What must be true instead

  • a path with a file extension that does not exist is a 404 text/plain, and never the document; a path with no
    extension still opens the app (deep links keep working);
  • POST/PUT/DELETE/PATCH on a static path is 405 with Allow: GET, HEAD; HEAD returns the headers a
    GET would send and no body, on every branch including gzip;
  • containment is filePath === WEB_DIR || filePath.startsWith(WEB_DIR + path.sep);
  • the old assertions are corrected rather than deleted, so the change of intent is visible in the diff.
Found by pointing at the static server the same request-level lens used for #1307 and #1309. Three defects, and one of them was pinned by a test that asserted the wrong thing. ## 1. A missing file is answered with the app ``` GET /nope.js -> 200 text/html; charset=utf-8 <!DOCTYPE html> ... GET /nope.png -> 200 text/html ``` `serveStatic()` falls back to `index.html` for anything it cannot read. This app has **no client-side routes** - it is one page with tabs - so the fallback serves no deep link that matters and instead hides every wrong filename. In a browser the only report is `Uncaught SyntaxError: Unexpected token '<'`, in a file the reader can no longer identify. A missing `favicon.ico` reads as a broken app. ## 2. The method is ignored ``` POST /index.html -> 200 (the document) DELETE /index.html -> 200 ``` A client that POSTs can conclude its write landed. `HEAD` returns a body as well, including on the gzip branch where `res.end(gz)` writes the compressed bytes. ## 3. Containment is a string prefix, not a path boundary ```js if (filePath.indexOf(WEB_DIR) !== 0) { 403 } ``` `indexOf(WEB_DIR) === 0` also accepts `…/web-backup/…`, a sibling whose name merely starts with `web`. Nothing exploits it today - the URL parser collapses `..` before the path is joined - but a check that is only safe because another component happens to clean up first is not a check, and the next directory added beside `web/` inherits the hole. ## A test was pinning the defect `test/server-static-cache.test.js` asserted: ```js ok(missing.status === 200, 'a missing asset falls back to the app rather than 404'); ok(contentType.indexOf('text/html') === 0, 'and is served as HTML'); ``` So the behaviour was not merely untested, it was **specified**, in the wrong direction. That is why it survived. ## What must be true instead * a path with a file extension that does not exist is a `404 text/plain`, and never the document; a path with no extension still opens the app (deep links keep working); * `POST`/`PUT`/`DELETE`/`PATCH` on a static path is `405` with `Allow: GET, HEAD`; `HEAD` returns the headers a `GET` would send and no body, on every branch including gzip; * containment is `filePath === WEB_DIR || filePath.startsWith(WEB_DIR + path.sep)`; * the old assertions are corrected rather than deleted, so the change of intent is visible in the diff.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
jaylouisw/packetforge#1311
No description provided.