Spaces:
Paused
Paused
Claude Claude Sonnet 5 commited on
Fix the dev server's broken path-traversal guard
Browse filesfilePath.startsWith(ROOT) with no trailing separator let a sibling directory
that merely shares ROOT as a string prefix (e.g. SpaceExploration-RTS-evil)
sail through. Extracted the check into a pure, exported resolveSafePath(root,
pathname): normalizes the joined path before comparing, and requires an
exact match or a match followed by the platform path separator. server.listen()
is now guarded to only bind when the file is run directly, so importing it
for tests has no side effects. Dev-only tool (not shipped to players), but
worth fixing correctly.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dmxcvm178TEt2wVXrX5Cj1
- test/serve.test.js +49 -0
- tools/serve.js +30 -11
test/serve.test.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { test } from "node:test";
|
| 2 |
+
import assert from "node:assert/strict";
|
| 3 |
+
import { sep } from "node:path";
|
| 4 |
+
import { resolveSafePath } from "../tools/serve.js";
|
| 5 |
+
|
| 6 |
+
// resolveSafePath() is the path-traversal guard behind `npm start`'s dev server: given the
|
| 7 |
+
// project root and a request pathname, it returns the on-disk path to serve, or null if the
|
| 8 |
+
// request would escape the root. These are regression tests for a broken version of that guard
|
| 9 |
+
// that used a bare `filePath.startsWith(ROOT)` prefix check β which a sibling directory whose
|
| 10 |
+
// name happens to start with the same string sails right through (no separator boundary), and
|
| 11 |
+
// which (if it ran before normalization) could also be fooled by literal ".." segments.
|
| 12 |
+
|
| 13 |
+
const ROOT = `${sep}home${sep}user${sep}SpaceExploration-RTS`;
|
| 14 |
+
|
| 15 |
+
test("resolveSafePath serves an ordinary in-root request", () => {
|
| 16 |
+
assert.equal(resolveSafePath(ROOT, "/index.html"), `${ROOT}${sep}index.html`);
|
| 17 |
+
assert.equal(resolveSafePath(ROOT, "/engine/loop.js"), `${ROOT}${sep}engine${sep}loop.js`);
|
| 18 |
+
});
|
| 19 |
+
|
| 20 |
+
test("resolveSafePath defaults \"/\" to index.html", () => {
|
| 21 |
+
assert.equal(resolveSafePath(ROOT, "/"), `${ROOT}${sep}index.html`);
|
| 22 |
+
});
|
| 23 |
+
|
| 24 |
+
test("resolveSafePath rejects a sibling directory that merely shares ROOT as a string prefix", () => {
|
| 25 |
+
// "/home/user/SpaceExploration-RTS-evil/secret.txt".startsWith("/home/user/SpaceExploration-RTS")
|
| 26 |
+
// is true, so a naive prefix check lets this through. This is the exact shape of the bug.
|
| 27 |
+
assert.equal(resolveSafePath(ROOT, "/../SpaceExploration-RTS-evil/secret.txt"), null);
|
| 28 |
+
});
|
| 29 |
+
|
| 30 |
+
test("resolveSafePath rejects plain \"..\" traversal out of the root", () => {
|
| 31 |
+
assert.equal(resolveSafePath(ROOT, "/../../../../etc/passwd"), null);
|
| 32 |
+
assert.equal(resolveSafePath(ROOT, "/..%2f..%2fetc/passwd".replace(/%2f/gi, "/")), null);
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
test("resolveSafePath rejects percent-encoded traversal", () => {
|
| 36 |
+
assert.equal(resolveSafePath(ROOT, "/%2e%2e/%2e%2e/etc/passwd"), null);
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
test("resolveSafePath allows a request that resolves to ROOT itself", () => {
|
| 40 |
+
// "/.." from ROOT lands one directory *above* ROOT (ROOT's parent) β outside the root, so it
|
| 41 |
+
// must be rejected. "/subdir/.." folds back down to ROOT exactly, so the `filePath === root`
|
| 42 |
+
// branch of the check (as opposed to the `startsWith(root + sep)` branch) must allow it.
|
| 43 |
+
assert.equal(resolveSafePath(ROOT, "/.."), null);
|
| 44 |
+
assert.equal(resolveSafePath(ROOT, "/subdir/.."), ROOT);
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
test("resolveSafePath allows traversal that stays within the root", () => {
|
| 48 |
+
assert.equal(resolveSafePath(ROOT, "/engine/../index.html"), `${ROOT}${sep}index.html`);
|
| 49 |
+
});
|
tools/serve.js
CHANGED
|
@@ -13,11 +13,11 @@
|
|
| 13 |
|
| 14 |
import { createServer } from "node:http";
|
| 15 |
import { readFile } from "node:fs/promises";
|
| 16 |
-
import { join, normalize, extname } from "node:path";
|
| 17 |
import { fileURLToPath } from "node:url";
|
| 18 |
import { dirname } from "node:path";
|
| 19 |
|
| 20 |
-
const ROOT = join(dirname(fileURLToPath(import.meta.url)), ".."); // project root (tools/ is one level down)
|
| 21 |
const PORT = Number(process.argv[2]) || Number(process.env.PORT) || 8080;
|
| 22 |
|
| 23 |
// Content types for everything the game actually serves. The .js entry is the whole point β
|
|
@@ -36,14 +36,29 @@ const MIME = {
|
|
| 36 |
".map": "application/json; charset=utf-8",
|
| 37 |
};
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
const server = createServer(async (req, res) => {
|
| 40 |
try {
|
| 41 |
// Strip the query string, default "/" to index.html, and resolve WITHIN the root β a
|
| 42 |
// request can't escape the project directory via "../" traversal.
|
| 43 |
-
|
| 44 |
-
if (
|
| 45 |
-
const filePath = normalize(join(ROOT, path));
|
| 46 |
-
if (!filePath.startsWith(ROOT)) { res.writeHead(403).end("Forbidden"); return; }
|
| 47 |
|
| 48 |
const body = await readFile(filePath);
|
| 49 |
res.writeHead(200, {
|
|
@@ -57,8 +72,12 @@ const server = createServer(async (req, res) => {
|
|
| 57 |
}
|
| 58 |
});
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
import { createServer } from "node:http";
|
| 15 |
import { readFile } from "node:fs/promises";
|
| 16 |
+
import { join, normalize, extname, sep } from "node:path";
|
| 17 |
import { fileURLToPath } from "node:url";
|
| 18 |
import { dirname } from "node:path";
|
| 19 |
|
| 20 |
+
const ROOT = normalize(join(dirname(fileURLToPath(import.meta.url)), "..")); // project root (tools/ is one level down)
|
| 21 |
const PORT = Number(process.argv[2]) || Number(process.env.PORT) || 8080;
|
| 22 |
|
| 23 |
// Content types for everything the game actually serves. The .js entry is the whole point β
|
|
|
|
| 36 |
".map": "application/json; charset=utf-8",
|
| 37 |
};
|
| 38 |
|
| 39 |
+
// Resolve a request path to an on-disk path, or null if it would escape `root`. Exported (and
|
| 40 |
+
// taking `root` as a parameter) so it can be unit-tested without booting a real HTTP server.
|
| 41 |
+
//
|
| 42 |
+
// Two things have to both hold for this to be safe:
|
| 43 |
+
// 1. `normalize()` collapses any ".." segments in the joined path BEFORE we compare it against
|
| 44 |
+
// root, so a request can't smuggle a traversal past the check.
|
| 45 |
+
// 2. The comparison itself is boundary-aware β `startsWith(root)` alone is broken, because e.g.
|
| 46 |
+
// "/home/user/RTS-evil/x" starts with the string "/home/user/RTS" with no separator between
|
| 47 |
+
// them. Requiring an exact match OR a match followed by path.sep closes that hole.
|
| 48 |
+
export function resolveSafePath(root, pathname) {
|
| 49 |
+
let path = decodeURIComponent(pathname);
|
| 50 |
+
if (path === "/") path = "/index.html";
|
| 51 |
+
const filePath = normalize(join(root, path));
|
| 52 |
+
const withinRoot = filePath === root || filePath.startsWith(root + sep);
|
| 53 |
+
return withinRoot ? filePath : null;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
const server = createServer(async (req, res) => {
|
| 57 |
try {
|
| 58 |
// Strip the query string, default "/" to index.html, and resolve WITHIN the root β a
|
| 59 |
// request can't escape the project directory via "../" traversal.
|
| 60 |
+
const filePath = resolveSafePath(ROOT, new URL(req.url, "http://localhost").pathname);
|
| 61 |
+
if (!filePath) { res.writeHead(403).end("Forbidden"); return; }
|
|
|
|
|
|
|
| 62 |
|
| 63 |
const body = await readFile(filePath);
|
| 64 |
res.writeHead(200, {
|
|
|
|
| 72 |
}
|
| 73 |
});
|
| 74 |
|
| 75 |
+
// Only bind a port when this file is run directly (`node tools/serve.js` / `npm start`), not when
|
| 76 |
+
// it's imported β e.g. by a test β for `resolveSafePath`.
|
| 77 |
+
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
|
| 78 |
+
server.listen(PORT, () => {
|
| 79 |
+
console.log(`Stellar Frontier β serving ${ROOT}`);
|
| 80 |
+
console.log(` open http://localhost:${PORT}/`);
|
| 81 |
+
console.log(" stop Ctrl+C");
|
| 82 |
+
});
|
| 83 |
+
}
|