589 lines
30 KiB
JavaScript
589 lines
30 KiB
JavaScript
"use strict";
|
|
|
|
// adpanel:static-prefix-extension-test / adpanel:stealth-prefix-cover —
|
|
// behavior + source pins for the two pre-auth static surface divergences the
|
|
// audit found:
|
|
//
|
|
// F1 (extension test): the pre-auth static classification served ANY file
|
|
// under the directory prefixes (/css//css-ext//js//images//fonts//
|
|
// /webfonts//vendor/ — /uploads/ already had its own rasters-only test)
|
|
// while the block comment above the constants claimed "a stray
|
|
// .json/.html/.map dropped there later falls through to the auth gate".
|
|
// Live today: public/js/socket.io.min.js.map and
|
|
// public/vendor/monaco-editor/*/PROVENANCE.md were world-readable pre-auth,
|
|
// and any future file dropped under a prefix dir would be too. The fix
|
|
// makes the code match its own documented claim: the non-/uploads/ branch
|
|
// is exactly the PRE_AUTH_STATIC_EXT allowlist on the DECODED path — the
|
|
// allowlist itself is unchanged, prefix membership grants nothing extra.
|
|
//
|
|
// F2 (stealth prefix cover): under STEALTH_MODE (installer default ON) the
|
|
// gate decoyed only the 20 exact STEALTH_ASSET_PATHS entries + the
|
|
// /auth-assets/ leg, so a pre-gate probe pulled the ~17MB vendored tree
|
|
// (/vendor/monaco-editor/0.55.1/** etc.) with distinguishable 200s while
|
|
// the covered set entries decoyed 404 — a fingerprint for an unauthenticated
|
|
// prober. The fix extends the classification (compose, not replace: same
|
|
// decode → send-normalize → UP_PATH mirror → favicon exclusion) with a leg
|
|
// covering the SAME PRE_AUTH_STATIC_PREFIXES the static layer draws from:
|
|
// everything the static layer could serve decoys pre-gate; with stealth
|
|
// access or stealth off the gate calls next() and static serving applies
|
|
// the extension test + /uploads/ rasters narrowing.
|
|
//
|
|
// F3 (dotfiles): send 0.19.0's legacy undefined-dotfiles behavior only
|
|
// hides a dotfile BASENAME — a dotfile/dotdir in a MIDDLE segment is
|
|
// treated as 'allow' and served (pinned against the installed send below).
|
|
// publicStaticHandler now sets dotfiles:"ignore"; public/ contains no
|
|
// dotfile segments at all (asserted against the real tree), so no legit
|
|
// asset breaks.
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
const http = require("node:http");
|
|
|
|
const ROOT = path.join(__dirname, "..");
|
|
const INDEX_JS = path.join(ROOT, "index.js");
|
|
const indexSource = fs.readFileSync(INDEX_JS, "utf8");
|
|
|
|
function indexOfOrThrow(needle, from = 0) {
|
|
const idx = indexSource.indexOf(needle, from);
|
|
assert.ok(idx >= 0, `expected source pin not found: ${needle}`);
|
|
return idx;
|
|
}
|
|
|
|
// Extraction (same technique as stealth-classification-normalize.test.js):
|
|
// stitch the static-surface constants (prefixes + both extension allowlists,
|
|
// sliced as one block), the stealth asset classifier (set + helper) and
|
|
// safeDecodePath into one evaluatable scope. index.js binds require("path")
|
|
// at module top; new Function bodies see no module scope, so the same module
|
|
// object is handed in as a parameter. The classifier's prefix-cover leg and
|
|
// safeDecodePath are defined textually later in index.js but only run at
|
|
// request time — the stitched scope reproduces exactly that binding.
|
|
function loadHardeningScope() {
|
|
const staticStart = indexOfOrThrow("const PRE_AUTH_STATIC_PREFIXES = [");
|
|
const staticEnd = indexOfOrThrow("const publicStaticHandler", staticStart);
|
|
assert.ok(staticEnd > staticStart, "static constants slice must be bounded");
|
|
const setStart = indexOfOrThrow("const STEALTH_ASSET_PATHS = new Set([");
|
|
const setEnd = indexSource.indexOf("]);", setStart);
|
|
assert.ok(setEnd > setStart, "STEALTH_ASSET_PATHS slice must be bounded");
|
|
const fnStart = indexOfOrThrow("function isStealthProtectedAssetPath(");
|
|
const fnEnd = indexOfOrThrow("function isStealthProtectedLoginPath(", fnStart);
|
|
assert.ok(fnEnd > fnStart, "isStealthProtectedAssetPath slice must be bounded");
|
|
const decodeStart = indexOfOrThrow("function safeDecodePath(p)");
|
|
const decodeEnd = indexOfOrThrow("function servePublicStatic(", decodeStart);
|
|
assert.ok(decodeEnd > decodeStart, "safeDecodePath slice must be bounded");
|
|
const src =
|
|
indexSource.slice(staticStart, staticEnd) + "\n" +
|
|
indexSource.slice(setStart, setEnd + 3) + "\n" +
|
|
indexSource.slice(fnStart, fnEnd) + "\n" +
|
|
indexSource.slice(decodeStart, decodeEnd);
|
|
return new Function("path", `${src}\nreturn { PRE_AUTH_STATIC_PREFIXES, PRE_AUTH_STATIC_EXT, PRE_AUTH_UPLOADS_EXT, isStealthProtectedAssetPath, STEALTH_ASSET_PATHS, safeDecodePath };`)(path);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Source pins
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("source: the pre-auth static test is the extension allowlist on the DECODED path, uniformly", () => {
|
|
const mwStart = indexOfOrThrow("app.use(async (req, res, next) => {\n try {\n if (req.method !== \"GET\" && req.method !== \"HEAD\") return next();");
|
|
const mwEnd = indexSource.indexOf("});", mwStart);
|
|
assert.ok(mwEnd > mwStart, "static classification middleware slice must be bounded");
|
|
const mwSrc = indexSource.slice(mwStart, mwEnd);
|
|
assert.ok(
|
|
mwSrc.includes("const p = safeDecodePath(req.path);"),
|
|
"classification must run on the decoded path (express.static/send decode " +
|
|
"once before serving — `logo%2Esvg` must classify as logo.svg)"
|
|
);
|
|
// The exact expression: no prefix OR-leg may bypass the extension test.
|
|
const exprIdx = mwSrc.indexOf("const isAssetPath = isUploadsPath");
|
|
assert.ok(exprIdx >= 0, "isAssetPath expression must be present");
|
|
const expr = mwSrc.slice(exprIdx, mwSrc.indexOf(";", exprIdx) + 1);
|
|
assert.strictEqual(
|
|
expr,
|
|
'const isAssetPath = isUploadsPath\n ? PRE_AUTH_UPLOADS_EXT.test(p)\n : PRE_AUTH_STATIC_EXT.test(p);',
|
|
"the non-/uploads/ branch must be exactly the PRE_AUTH_STATIC_EXT test — " +
|
|
"a prefix OR-leg here served ANY extension under the prefix dirs"
|
|
);
|
|
assert.ok(mwSrc.includes("adpanel:static-prefix-extension-test"));
|
|
assert.ok(
|
|
!mwSrc.includes("PRE_AUTH_STATIC_PREFIXES.some"),
|
|
"the classification middleware must not consult the prefix list at all — " +
|
|
"with the extension test governing every spelling, prefix membership " +
|
|
"grants nothing (the list's consumers are the stealth cover and the " +
|
|
"fast-path mirror)"
|
|
);
|
|
});
|
|
|
|
test("source: the static allowlist is unchanged and the handler sets dotfiles:'ignore'", () => {
|
|
assert.ok(
|
|
indexSource.includes('const PRE_AUTH_STATIC_EXT = /\\.(css|js|mjs|png|jpg|jpeg|gif|ico|svg|webp|avif|woff|woff2|ttf|eot|webm|mp4)$/i;'),
|
|
"PRE_AUTH_STATIC_EXT must stay byte-identical — the fix extends its APPLICATION, not the allowlist"
|
|
);
|
|
assert.ok(
|
|
indexSource.includes('const PRE_AUTH_UPLOADS_EXT = /\\.(png|jpg|jpeg|gif|webp)$/i;'),
|
|
"PRE_AUTH_UPLOADS_EXT (the /uploads/ rasters narrowing) must stay byte-identical"
|
|
);
|
|
const prefixesStart = indexOfOrThrow("const PRE_AUTH_STATIC_PREFIXES = [");
|
|
const prefixesEnd = indexSource.indexOf("];", prefixesStart);
|
|
assert.strictEqual(
|
|
indexSource.slice(prefixesStart, prefixesEnd + 2),
|
|
'const PRE_AUTH_STATIC_PREFIXES = [\n "/css/",\n "/css-ext/",\n "/js/",\n "/images/",\n "/fonts/",\n "/webfonts/",\n "/vendor/",\n "/uploads/"\n];',
|
|
"the prefix list must keep its exact contents (shared with the stealth cover)"
|
|
);
|
|
const handlerStart = indexOfOrThrow("const publicStaticHandler = express.static(path.join(__dirname, \"public\"), {");
|
|
const handlerEnd = indexSource.indexOf("});", handlerStart);
|
|
const handlerSrc = indexSource.slice(handlerStart, handlerEnd);
|
|
assert.ok(
|
|
handlerSrc.includes('dotfiles: "ignore"'),
|
|
"express.static must set dotfiles:'ignore' — send's legacy undefined " +
|
|
"behavior SERVES middle-segment dotfiles (pinned below)"
|
|
);
|
|
assert.ok(handlerSrc.includes("adpanel:static-prefix-extension-test"));
|
|
});
|
|
|
|
test("source: the stealth classifier composes the prefix cover AFTER the set, on the same normalized form", () => {
|
|
const fnStart = indexOfOrThrow("function isStealthProtectedAssetPath(");
|
|
const fnEnd = indexOfOrThrow("function isStealthProtectedLoginPath(", fnStart);
|
|
const fnSrc = indexSource.slice(fnStart, fnEnd);
|
|
// The pre-existing composition pins still hold (decode → send-normalize →
|
|
// UP_PATH mirror → spelling-parity fold → favicon exclusion → legs).
|
|
// 2026-11-13 (adpanel:stealth-asset-spelling-parity): the legs now run on
|
|
// the case-folded form (`caseFolded`, trailing slash kept) and the
|
|
// exact-match set / root-level leg run on the one-trailing-slash-stripped
|
|
// form (`resolved`) — updated consciously from the raw `p` pins.
|
|
assert.ok(fnSrc.includes('const sendNormalized = path.posix.normalize("." + "/" + decoded);'));
|
|
assert.ok(fnSrc.includes('const p = "/" + sendNormalized;'));
|
|
assert.ok(
|
|
fnSrc.includes("const caseFolded = p.toLowerCase();") &&
|
|
fnSrc.includes('const resolved = caseFolded.replace(/\\/$/, "");'),
|
|
"the spelling-parity fold (case + exactly-one-trailing-slash strip) must " +
|
|
"compose AFTER the send-normalize (adpanel:stealth-asset-spelling-parity)"
|
|
);
|
|
const upPathIdx = fnSrc.indexOf("/(?:^|[\\\\/])\\.\\.(?:[\\\\/]|$)/.test(sendNormalized)");
|
|
const prefixLegIdx = fnSrc.indexOf("PRE_AUTH_STATIC_PREFIXES.some");
|
|
const setLegIdx = fnSrc.indexOf("STEALTH_ASSET_PATHS.has(resolved)");
|
|
const faviconIdx = fnSrc.indexOf('caseFolded === "/auth-assets/favicon"');
|
|
const rootLegIdx = fnSrc.indexOf("PRE_AUTH_STATIC_EXT.test(resolved)");
|
|
assert.ok(upPathIdx >= 0 && setLegIdx >= 0 && faviconIdx >= 0 && rootLegIdx >= 0, "existing legs must stay");
|
|
assert.ok(prefixLegIdx > upPathIdx && prefixLegIdx > faviconIdx,
|
|
"the prefix leg must run AFTER the UP_PATH mirror (root-escaping traversals " +
|
|
"stay unprotected beside send's swallowed 403) and after the favicon exclusion");
|
|
assert.ok(
|
|
/return caseFolded\.startsWith\("\/auth-assets\/"\) \|\| STEALTH_ASSET_PATHS\.has\(resolved\) \|\|\n\s*PRE_AUTH_STATIC_PREFIXES\.some\(\(prefix\) => caseFolded\.startsWith\(prefix\)\) \|\|\n\s*\(resolved\.indexOf\("\/", 1\) === -1 && PRE_AUTH_STATIC_EXT\.test\(resolved\)\);/.test(fnSrc),
|
|
"the return must COMPOSE the four legs — the /auth-assets/ leg, the " +
|
|
"exact-match set (spelling-parity folded, branding-media entries covered per " +
|
|
"adpanel:branding-media-stealth-cover), the prefix legs (slash-KEPT folded " +
|
|
"form so prefix-root spellings stay covered) and the root-level " +
|
|
"allowlisted-extension leg on the SAME shared PRE_AUTH_STATIC_EXT const"
|
|
);
|
|
assert.ok(fnSrc.includes("adpanel:stealth-prefix-cover"));
|
|
assert.ok(fnSrc.includes("adpanel:stealth-asset-decoded-match"));
|
|
assert.ok(fnSrc.includes("adpanel:stealth-asset-normalized-match"));
|
|
assert.ok(fnSrc.includes("adpanel:stealth-asset-spelling-parity"));
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Inventory pins (real public/ tree — the no-regression assertions)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("inventory: every file under a static prefix carries an allowlisted extension except the two pinned document files", () => {
|
|
const { PRE_AUTH_STATIC_PREFIXES, PRE_AUTH_STATIC_EXT } = loadHardeningScope();
|
|
const PUBLIC_DIR = path.join(ROOT, "public");
|
|
const files = [];
|
|
const walk = (dir) => {
|
|
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
const full = path.join(dir, e.name);
|
|
if (e.isDirectory()) walk(full);
|
|
else files.push("/" + path.relative(PUBLIC_DIR, full).split(path.sep).join("/"));
|
|
}
|
|
};
|
|
walk(PUBLIC_DIR);
|
|
assert.ok(files.length >= 300, `inventory must cover the real public/ tree (got ${files.length})`);
|
|
// No dotfile segments anywhere in public/ — dotfiles:"ignore" cannot break
|
|
// a referenced asset, and nothing may start writing one.
|
|
for (const f of files) {
|
|
assert.strictEqual(
|
|
f.split("/").some((seg) => seg.startsWith(".")), false,
|
|
`no dotfile segment expected in public/: ${f}`
|
|
);
|
|
}
|
|
const underPrefix = files.filter((f) => PRE_AUTH_STATIC_PREFIXES.some((p) => f.startsWith(p)));
|
|
assert.ok(underPrefix.length >= 250, `the prefix dirs must hold the vendored tree (got ${underPrefix.length})`);
|
|
// The only on-disk exceptions — both are document/debug artifacts the block
|
|
// comment has always claimed fall through to the auth gate. If one of these
|
|
// moves (e.g. a monaco version bump), update the pin consciously.
|
|
const exceptions = new Set([
|
|
"/js/socket.io.min.js.map",
|
|
"/vendor/monaco-editor/0.55.1/PROVENANCE.md",
|
|
]);
|
|
for (const f of exceptions) {
|
|
assert.ok(files.includes(f), `${f} must still exist on disk (update the pin if it moved)`);
|
|
}
|
|
const offenders = underPrefix.filter((f) => !PRE_AUTH_STATIC_EXT.test(f) && !exceptions.has(f));
|
|
assert.deepStrictEqual(
|
|
offenders, [],
|
|
"every prefix file must carry an allowlisted extension — a new non-allowlisted " +
|
|
"extension under a prefix is pre-auth-invisible by design and must be added " +
|
|
"consciously to the allowlist or moved out of the static prefixes"
|
|
);
|
|
});
|
|
|
|
test("inventory: every same-origin asset path referenced by views/ and public/js still classifies servable", () => {
|
|
const { PRE_AUTH_STATIC_PREFIXES, PRE_AUTH_STATIC_EXT } = loadHardeningScope();
|
|
const refRe = /["'`](\/(?:css|css-ext|js|images|fonts|webfonts|vendor|uploads)\/[A-Za-z0-9_.-]+\.(?:css|js|mjs|png|jpg|jpeg|gif|ico|svg|webp|avif|woff|woff2|ttf|eot|webm|mp4|map|md|html|json|txt|bak|env))["'`]/g;
|
|
const refs = new Set();
|
|
const sources = [];
|
|
const viewsDir = path.join(ROOT, "views");
|
|
for (const dir of [viewsDir, path.join(viewsDir, "partials"), path.join(ROOT, "public", "js")]) {
|
|
if (!fs.existsSync(dir)) continue;
|
|
for (const f of fs.readdirSync(dir)) {
|
|
if (!/\.(ejs|js)$/.test(f)) continue;
|
|
sources.push(path.join(dir, f));
|
|
}
|
|
}
|
|
assert.ok(sources.length >= 10, "must scan the real views + public/js sources");
|
|
for (const file of sources) {
|
|
const src = fs.readFileSync(file, "utf8");
|
|
for (const m of src.matchAll(refRe)) refs.add(m[1]);
|
|
}
|
|
assert.ok(refs.size >= 20, `must extract real asset references (got ${refs.size})`);
|
|
const broken = [...refs].filter((r) => !PRE_AUTH_STATIC_EXT.test(r));
|
|
assert.deepStrictEqual(
|
|
broken, [],
|
|
"every referenced asset must carry an allowlisted extension — the extension " +
|
|
"test may not break a single referenced asset (non-allowlisted references " +
|
|
"found; move the file, add the extension consciously, or fix the reference)"
|
|
);
|
|
// and each referenced prefix path must exist on disk (query-stripped) —
|
|
// the references are plain string literals in the scanned sources
|
|
const onDisk = new Set();
|
|
const walk = (dir) => {
|
|
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
const full = path.join(dir, e.name);
|
|
if (e.isDirectory()) walk(full);
|
|
else onDisk.add("/" + path.relative(path.join(ROOT, "public"), full).split(path.sep).join("/"));
|
|
}
|
|
};
|
|
walk(path.join(ROOT, "public"));
|
|
const missing = [...refs].filter((r) => !onDisk.has(r));
|
|
assert.deepStrictEqual(
|
|
missing, [],
|
|
"every referenced static-prefix asset must exist on disk (catches renamed files)"
|
|
);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Behavioral harnesses: replicate the production stack with the REAL
|
|
// extracted pieces — stealth gate (decoy on classification, pass on access)
|
|
// → pre-auth static classification (extracted consts + the pinned
|
|
// expression) → express.static with the production options → fallthrough.
|
|
// Raw request lines (no client-side URL normalization).
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function buildFixtureDir() {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "adpanel-static-prefix-"));
|
|
fs.mkdirSync(path.join(dir, "vendor", "monaco-editor", "0.55.1"), { recursive: true });
|
|
fs.mkdirSync(path.join(dir, "vendor", "fontawesome", "css"), { recursive: true });
|
|
fs.mkdirSync(path.join(dir, "js"), { recursive: true });
|
|
fs.mkdirSync(path.join(dir, "css"), { recursive: true });
|
|
fs.mkdirSync(path.join(dir, "images"), { recursive: true });
|
|
fs.mkdirSync(path.join(dir, "uploads"), { recursive: true });
|
|
fs.mkdirSync(path.join(dir, "js", ".hidden-dir"), { recursive: true });
|
|
fs.writeFileSync(path.join(dir, "vendor", "monaco-editor", "0.55.1", "loader.js"), "LOADER-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "vendor", "monaco-editor", "0.55.1", "PROVENANCE.md"), "PROVENANCE-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "vendor", "fontawesome", "css", "all.min.css"), "FACSS-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "js", "app.js"), "APPJS-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "js", "socket.io.min.js.map"), "SOURCEMAP-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "js", "notes.txt"), "NOTES-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "js", "README"), "README-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "js", ".hidden-dir", "x.js"), "HIDDEN-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "css", "app.css"), "CSS-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "images", "pic.png"), "PNG-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "uploads", "avatar.png"), "AVATAR-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "uploads", "notes.txt"), "UPLOADTXT-CONTENT");
|
|
fs.writeFileSync(path.join(dir, ".secret"), "SECRET-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "dashboard.html"), "DASHBOARD-CONTENT");
|
|
// root-level (no-prefix) allowlisted-extension files — the
|
|
// adpanel:stealth-asset-spelling-parity root-level cover mirrors the static
|
|
// layer's extension test, which needs no prefix at the root
|
|
fs.writeFileSync(path.join(dir, "dashboard.css"), "DASHCSS-CONTENT");
|
|
fs.writeFileSync(path.join(dir, "style.css"), "STYLECSS-CONTENT");
|
|
return dir;
|
|
}
|
|
|
|
// Mirrors index.js's stealth gate → static classification → express.static
|
|
// order. hasStealthAccess is reduced to a boolean (cookie/session semantics
|
|
// are covered by the gate's own tests); isAuthenticated likewise. index.js
|
|
// mounts express.static EXACTLY ONCE, reached only through the classification
|
|
// middleware's two serve branches (isAssetPath / isAuthenticated) — the
|
|
// harness reproduces that shape, so a fallthrough here never re-enters
|
|
// express.static.
|
|
function buildApp(dir, { stealthActive, hasAccess, authenticated }) {
|
|
const express = require("express");
|
|
const { PRE_AUTH_STATIC_EXT, PRE_AUTH_UPLOADS_EXT, isStealthProtectedAssetPath, safeDecodePath } = loadHardeningScope();
|
|
const app = express();
|
|
if (stealthActive) {
|
|
app.use((req, res, next) => {
|
|
const pathName = String(req.path || "").trim();
|
|
if (!isStealthProtectedAssetPath(pathName)) return next();
|
|
if (hasAccess) return next();
|
|
// mirror sendStealthNotFound: 404, empty body
|
|
res.setHeader("X-Stealth-Gate", "decoy");
|
|
return res.status(404).type("text/plain").send("");
|
|
});
|
|
}
|
|
const publicStaticHandler = express.static(dir, { maxAge: "30d", etag: true, lastModified: true, immutable: true, dotfiles: "ignore" });
|
|
app.use(async (req, res, next) => {
|
|
if (req.method !== "GET" && req.method !== "HEAD") return next();
|
|
const p = safeDecodePath(req.path);
|
|
const isUploadsPath = p.startsWith("/uploads/");
|
|
// the exact expression pinned against index.js source above
|
|
const isAssetPath = isUploadsPath
|
|
? PRE_AUTH_UPLOADS_EXT.test(p)
|
|
: PRE_AUTH_STATIC_EXT.test(p);
|
|
if (isAssetPath) return publicStaticHandler(req, res, next);
|
|
if (authenticated) return publicStaticHandler(req, res, next);
|
|
return next();
|
|
});
|
|
app.use((req, res) => res.status(404).send("FALLTHROUGH-404"));
|
|
return app;
|
|
}
|
|
|
|
function startServer(app) {
|
|
const server = app.listen(0, "127.0.0.1");
|
|
return new Promise((resolve) => server.once("listening", () => resolve(server)));
|
|
}
|
|
|
|
function rawGet(server, requestPath) {
|
|
const port = server.address().port;
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.get({ host: "127.0.0.1", port, path: requestPath }, (res) => {
|
|
let body = "";
|
|
res.on("data", (c) => (body += c));
|
|
res.on("end", () => resolve({ status: res.statusCode, body, headers: res.headers }));
|
|
});
|
|
req.on("error", reject);
|
|
});
|
|
}
|
|
|
|
test("behavior F2: pre-gate probes under stealth decoy 404 for EVERYTHING under the static prefixes", async () => {
|
|
const dir = buildFixtureDir();
|
|
const app = buildApp(dir, { stealthActive: true, hasAccess: false, authenticated: false });
|
|
let server;
|
|
try {
|
|
server = await startServer(app);
|
|
for (const spelling of [
|
|
"/vendor/monaco-editor/0.55.1/loader.js", // the finding's probe: was 200
|
|
"/vendor/monaco-editor/0.55.1/PROVENANCE.md",
|
|
"/js/socket.io.min.js.map", // was the live 196KB pre-auth leak
|
|
"/js/notes.txt", // dropped-file scenario
|
|
"/js/app.js",
|
|
"/css/app.css",
|
|
"/images/pic.png",
|
|
"/uploads/avatar.png",
|
|
"/vendor/fontawesome/css/all.min.css", // 20-entry set member — decoy unchanged
|
|
"/js/.hidden-dir/x.js", // middle-segment dotdir
|
|
"/v%65ndor/monaco-editor/0.55.1/loader.js", // encoded prefix spelling
|
|
"/js//app.js", // normalizes onto the prefix
|
|
"/js/does-not-exist.js", // missing file — uniform decoy
|
|
"/js/notes", // extensionless
|
|
// adpanel:stealth-asset-spelling-parity (2026-11-13) — root-level
|
|
// (no-prefix) allowlisted-extension spellings were served pre-gate with
|
|
// REAL bytes pre-fix; the root-level cover decoys them and every
|
|
// spelling variant that resolves onto them
|
|
"/dashboard.css",
|
|
"/style.css",
|
|
"//dashboard.css", // send-normalizes onto the root file
|
|
"/dashboard%2Ecss", // encoded extension decodes onto it
|
|
"/DASHBOARD.CSS", // fs case-sensitive ENOENT — uniform decoy
|
|
"/dashboard.css/", // strict:false-optional slash — uniform decoy
|
|
]) {
|
|
const r = await rawGet(server, spelling);
|
|
assert.strictEqual(r.status, 404, spelling);
|
|
assert.strictEqual(r.headers["x-stealth-gate"], "decoy", spelling);
|
|
assert.strictEqual(r.body, "", spelling);
|
|
}
|
|
} finally {
|
|
if (server) server.close();
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("behavior F2: with stealth access the gate passes and static serving applies the extension test", async () => {
|
|
const dir = buildFixtureDir();
|
|
const app = buildApp(dir, { stealthActive: true, hasAccess: true, authenticated: false });
|
|
let server;
|
|
try {
|
|
server = await startServer(app);
|
|
const served = [
|
|
["/vendor/monaco-editor/0.55.1/loader.js", "LOADER-CONTENT"],
|
|
["/css/app.css", "CSS-CONTENT"],
|
|
["/js/app.js", "APPJS-CONTENT"],
|
|
["/images/pic.png", "PNG-CONTENT"],
|
|
["/uploads/avatar.png", "AVATAR-CONTENT"], // /uploads/ rasters narrowing preserved
|
|
["/vendor/fontawesome/css/all.min.css", "FACSS-CONTENT"],
|
|
// adpanel:stealth-asset-spelling-parity — gated callers pass the gate
|
|
// BEFORE any decoy, so canonical root-level assets serve unchanged
|
|
["/dashboard.css", "DASHCSS-CONTENT"],
|
|
["/style.css", "STYLECSS-CONTENT"],
|
|
];
|
|
for (const [spelling, body] of served) {
|
|
const r = await rawGet(server, spelling);
|
|
assert.strictEqual(r.status, 200, spelling);
|
|
assert.strictEqual(r.body, body, spelling);
|
|
assert.strictEqual(r.headers["x-stealth-gate"], undefined, spelling);
|
|
}
|
|
// non-allowlisted extensions fall through to the auth gate even gated —
|
|
// the extension test governs every spelling, prefix or not
|
|
for (const spelling of [
|
|
"/js/socket.io.min.js.map",
|
|
"/vendor/monaco-editor/0.55.1/PROVENANCE.md",
|
|
"/js/notes.txt",
|
|
"/uploads/notes.txt", // /uploads/ narrowing (not a raster)
|
|
"/js/README", // extensionless
|
|
]) {
|
|
const r = await rawGet(server, spelling);
|
|
assert.strictEqual(r.status, 404, spelling);
|
|
assert.strictEqual(r.headers["x-stealth-gate"], undefined, spelling);
|
|
assert.strictEqual(r.body, "FALLTHROUGH-404", spelling);
|
|
}
|
|
// dotfiles never serve — middle-segment dotdir and root dotfile
|
|
for (const spelling of ["/js/.hidden-dir/x.js", "/.secret"]) {
|
|
const r = await rawGet(server, spelling);
|
|
assert.strictEqual(r.status, 404, spelling);
|
|
assert.strictEqual(r.headers["x-stealth-gate"], undefined, spelling);
|
|
assert.strictEqual(r.body, "FALLTHROUGH-404", spelling);
|
|
}
|
|
// a non-prefix, non-asset path still falls through (unprotected class unchanged)
|
|
const dash = await rawGet(server, "/dashboard.html");
|
|
assert.strictEqual(dash.status, 404);
|
|
assert.strictEqual(dash.body, "FALLTHROUGH-404");
|
|
} finally {
|
|
if (server) server.close();
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("behavior F1: stealth OFF — allowlisted assets serve pre-auth, non-allowlisted extensions fall through", async () => {
|
|
const dir = buildFixtureDir();
|
|
const app = buildApp(dir, { stealthActive: false, hasAccess: false, authenticated: false });
|
|
let server;
|
|
try {
|
|
server = await startServer(app);
|
|
const served = [
|
|
["/vendor/monaco-editor/0.55.1/loader.js", "LOADER-CONTENT"],
|
|
["/css/app.css", "CSS-CONTENT"],
|
|
["/js/app.js", "APPJS-CONTENT"],
|
|
["/uploads/avatar.png", "AVATAR-CONTENT"],
|
|
// stealth off → no gate → canonical root-level assets serve pre-auth
|
|
// exactly as before the root-level cover existed (it only classifies)
|
|
["/dashboard.css", "DASHCSS-CONTENT"],
|
|
["/style.css", "STYLECSS-CONTENT"],
|
|
];
|
|
for (const [spelling, body] of served) {
|
|
const r = await rawGet(server, spelling);
|
|
assert.strictEqual(r.status, 200, spelling);
|
|
assert.strictEqual(r.body, body, spelling);
|
|
}
|
|
for (const spelling of [
|
|
"/js/socket.io.min.js.map", // the fix: was 200 pre-auth
|
|
"/vendor/monaco-editor/0.55.1/PROVENANCE.md",
|
|
"/js/notes.txt",
|
|
"/js/README",
|
|
"/uploads/notes.txt",
|
|
]) {
|
|
const r = await rawGet(server, spelling);
|
|
assert.strictEqual(r.status, 404, spelling);
|
|
assert.strictEqual(r.body, "FALLTHROUGH-404", spelling);
|
|
}
|
|
} finally {
|
|
if (server) server.close();
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("behavior F1: the fix narrows the PRE-AUTH surface only — authenticated callers still reach the whole tree", async () => {
|
|
const dir = buildFixtureDir();
|
|
const app = buildApp(dir, { stealthActive: false, hasAccess: false, authenticated: true });
|
|
let server;
|
|
try {
|
|
server = await startServer(app);
|
|
// the isAuthenticated branch serves what the extension test refuses pre-auth
|
|
for (const [spelling, body] of [
|
|
["/js/socket.io.min.js.map", "SOURCEMAP-CONTENT"],
|
|
["/js/notes.txt", "NOTES-CONTENT"],
|
|
["/js/README", "README-CONTENT"],
|
|
["/js/app.js", "APPJS-CONTENT"],
|
|
]) {
|
|
const r = await rawGet(server, spelling);
|
|
assert.strictEqual(r.status, 200, spelling);
|
|
assert.strictEqual(r.body, body, spelling);
|
|
}
|
|
// dotfile segments stay 404 even authenticated — dotfiles:"ignore" is a
|
|
// handler-level option (no legit dotfile exists in public/)
|
|
const dotAuthed = await rawGet(server, "/js/.hidden-dir/x.js");
|
|
assert.strictEqual(dotAuthed.status, 404);
|
|
assert.strictEqual(dotAuthed.body, "FALLTHROUGH-404");
|
|
} finally {
|
|
if (server) server.close();
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("behavior F1: the DECODED path governs — an encoded spelling of a non-allowlisted extension falls through too", async () => {
|
|
const dir = buildFixtureDir();
|
|
const app = buildApp(dir, { stealthActive: false, hasAccess: false, authenticated: false });
|
|
let server;
|
|
try {
|
|
server = await startServer(app);
|
|
// send decodes once and serves by the decoded name; the classification
|
|
// must see the same decoded extension: %2Emap → '.map' — refused
|
|
const r = await rawGet(server, "/js/socket.io.min%2Emap");
|
|
assert.strictEqual(r.status, 404);
|
|
assert.strictEqual(r.body, "FALLTHROUGH-404");
|
|
// ...and the encoded spelling of an allowlisted asset still serves
|
|
const ok = await rawGet(server, "/js/app%2Ejs");
|
|
assert.strictEqual(ok.status, 200);
|
|
assert.strictEqual(ok.body, "APPJS-CONTENT");
|
|
} finally {
|
|
if (server) server.close();
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("framework contract: send 0.19.0's default dotfiles behavior SERVES middle-segment dotfiles — the option closes it", async () => {
|
|
const express = require("express");
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "adpanel-dotfiles-"));
|
|
try {
|
|
fs.mkdirSync(path.join(dir, "js", ".stash"), { recursive: true });
|
|
fs.writeFileSync(path.join(dir, "js", ".stash", "x.js"), "HIDDEN-CONTENT");
|
|
fs.writeFileSync(path.join(dir, ".rootdot"), "ROOTDOT-CONTENT");
|
|
const rawGetLocal = async (staticOptions, requestPath) => {
|
|
const app = express();
|
|
app.use(express.static(dir, staticOptions));
|
|
app.use((req, res) => res.status(404).send("FALLTHROUGH-404"));
|
|
const server = await startServer(app);
|
|
try {
|
|
return await rawGet(server, requestPath);
|
|
} finally {
|
|
server.close();
|
|
}
|
|
};
|
|
// legacy default (undefined): dotfile BASENAME hidden, middle segment served
|
|
const legacyMiddle = await rawGetLocal({}, "/js/.stash/x.js");
|
|
assert.strictEqual(legacyMiddle.status, 200, "undefined dotfiles serves middle-segment dotdirs (the quirk)");
|
|
assert.strictEqual(legacyMiddle.body, "HIDDEN-CONTENT");
|
|
const legacyRoot = await rawGetLocal({}, "/.rootdot");
|
|
assert.strictEqual(legacyRoot.status, 404, "undefined dotfiles hides a dotfile basename");
|
|
// production option: every dotfile segment 404s
|
|
const hardenedMiddle = await rawGetLocal({ dotfiles: "ignore" }, "/js/.stash/x.js");
|
|
assert.strictEqual(hardenedMiddle.status, 404);
|
|
assert.strictEqual(hardenedMiddle.body, "FALLTHROUGH-404");
|
|
const hardenedRoot = await rawGetLocal({ dotfiles: "ignore" }, "/.rootdot");
|
|
assert.strictEqual(hardenedRoot.status, 404);
|
|
} finally {
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|