Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,160 @@
|
||||
"use strict";
|
||||
|
||||
// Contract tests for the nodes.js relay's CORS responder (applyCors), default
|
||||
// posture: ZERO reflection. History for reviewers, so nobody re-derives this
|
||||
// surface wrong:
|
||||
//
|
||||
// 1. A security review described this surface as "reflects any Origin with
|
||||
// Access-Control-Allow-Credentials: true" — it never did: applyCors has
|
||||
// gated every echo since the initial commit (a6c5c37), first behind an
|
||||
// exact same-origin check OR an explicit NODES_CORS_ORIGINS allowlist
|
||||
// (never a wildcard). The same-origin echo was provably inert — a
|
||||
// browser-computed Origin equals the request's own scheme+host exactly
|
||||
// when the request is same-origin, where CORS is meaningless — but it
|
||||
// was still a dynamic reflection branch carrying credentials: true, and
|
||||
// any future widening of its origin computation (X-Forwarded-Host trust,
|
||||
// suffix matching, …) would have silently turned it into a credentialed
|
||||
// cross-origin grant.
|
||||
// 2. On 2026-10-18 that branch was REMOVED (see SECURITY-FIXES.md, "nodes.js
|
||||
// CORS: default posture tightened to zero reflection"). The allowlist is
|
||||
// now the ONLY way any Origin receives a CORS grant; with
|
||||
// NODES_CORS_ORIGINS unset the relay emits no Access-Control-* response
|
||||
// header at all. Same-origin callers need nothing (browsers do not
|
||||
// consult CORS on same-origin requests); cross-origin callers need an
|
||||
// explicit operator endorsement (allowlist posture pinned in
|
||||
// tests/node-relay-cors-allowlist.test.js).
|
||||
//
|
||||
// Also pinned here:
|
||||
// - Vary: Origin on every response through the router (ACAO presence
|
||||
// depends on the request Origin under the allowlist posture; caches must
|
||||
// not replay one origin's CORS headers to another).
|
||||
// - A request crafted to look maximally same-origin-shaped (Origin matching
|
||||
// the Host header, client-facing multi-hop X-Forwarded-Proto) still gets
|
||||
// NO grant — pinning that no same-origin computation exists to widen.
|
||||
//
|
||||
// This file runs with NODES_CORS_ORIGINS UNSET (the default posture).
|
||||
|
||||
const test = require("node:test");
|
||||
const { after } = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
const http = require("node:http");
|
||||
|
||||
if (!process.env.SESSION_SECRET || process.env.SESSION_SECRET.length < 32) {
|
||||
process.env.SESSION_SECRET = "adpanel-node-relay-cors-test-secret-0123456789";
|
||||
}
|
||||
delete process.env.NODES_CORS_ORIGINS;
|
||||
|
||||
const nodesApp = require("../nodes.js");
|
||||
|
||||
// ---- Exit-code honesty -------------------------------------------------------
|
||||
// Requiring nodes.js starts the mysql2 pool and batch timers, which keep the
|
||||
// event loop alive after the suite finishes — the process must be exited
|
||||
// forcibly (same note as node-relay-cross-site-guard-parity.test.js). The
|
||||
// exit code tallies REAL failures via pinnedTest(): node:test marks a test
|
||||
// failed when its callback rejects, and the wrapper counts the rejection
|
||||
// before rethrowing. A self-managed "passed" flag cannot do this — await
|
||||
// t.test() resolves even when the subtest failed, so a flag set after the
|
||||
// last subtest masks a failure behind exit 0 (this exact masking was found
|
||||
// by adversarial review, 2026-10-17 — see SECURITY-FIXES.md). process.exit()
|
||||
// also truncates buffered TAP on pipes, hence the flush delay before exiting.
|
||||
let failedTests = 0;
|
||||
function pinnedTest(name, fn) {
|
||||
return test(name, async (t) => {
|
||||
try {
|
||||
await fn(t);
|
||||
} catch (err) {
|
||||
failedTests += 1;
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
}
|
||||
after(async () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 250));
|
||||
process.exit(failedTests > 0 ? 1 : 0);
|
||||
});
|
||||
|
||||
// Teardown runs INSIDE the tallied body (try/finally), not in a t.after()
|
||||
// hook: a hook failure fires after the body, outside pinnedTest's try/catch,
|
||||
// so a failed close would print `not ok` while the tally stays 0 — invisible
|
||||
// to the exit code in direct node-file mode (under `node --test` the parent
|
||||
// runner catches it; the tally must not depend on that).
|
||||
async function withServer(fn) {
|
||||
const server = http.createServer(nodesApp);
|
||||
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
const base = `http://127.0.0.1:${server.address().port}`;
|
||||
try {
|
||||
return await fn(base);
|
||||
} finally {
|
||||
await new Promise((r) => server.close(r));
|
||||
}
|
||||
}
|
||||
|
||||
pinnedTest("default posture: a foreign Origin gets no CORS grant at all", async () => {
|
||||
await withServer(async (base) => {
|
||||
const res = await fetch(`${base}/health`, { headers: { Origin: "https://evil.example" } });
|
||||
assert.strictEqual(res.status, 200);
|
||||
assert.strictEqual(res.headers.get("access-control-allow-origin"), null,
|
||||
"a non-endorsed Origin must not receive Access-Control-Allow-Origin");
|
||||
assert.strictEqual(res.headers.get("access-control-allow-credentials"), null,
|
||||
"no ACAO must mean no Access-Control-Allow-Credentials either");
|
||||
});
|
||||
});
|
||||
|
||||
pinnedTest("default posture: even a genuine same-origin request gets no grant", async () => {
|
||||
await withServer(async (base) => {
|
||||
// The echo that USED to fire here was inert by construction (same-origin
|
||||
// requests never consult CORS), so removing it must be invisible to every
|
||||
// real client. Pin the removal: without NODES_CORS_ORIGINS the relay
|
||||
// emits no Access-Control-* header even for the request's own origin.
|
||||
const res = await fetch(`${base}/health`, { headers: { Origin: base } });
|
||||
assert.strictEqual(res.status, 200);
|
||||
assert.strictEqual(res.headers.get("access-control-allow-origin"), null,
|
||||
"no allowlist means no grant — not even for the request's own origin");
|
||||
assert.strictEqual(res.headers.get("access-control-allow-credentials"), null,
|
||||
"no ACAO must mean no Access-Control-Allow-Credentials either");
|
||||
});
|
||||
});
|
||||
|
||||
pinnedTest("default posture: a same-origin-shaped request via a multi-hop X-Forwarded-Proto chain still gets no grant", async () => {
|
||||
await withServer(async (base) => {
|
||||
// Craft everything a same-origin computation would need: Origin matching
|
||||
// the Host header, with the client-facing scheme taken from the first
|
||||
// XFP entry of an edge-TLS proxy chain. The old echo would have fired
|
||||
// here; its removal must hold for exactly this maximally-favorable shape,
|
||||
// so no future "same-origin" widening has anywhere to land.
|
||||
const httpsOrigin = base.replace(/^http:/, "https:");
|
||||
const res = await fetch(`${base}/health`, {
|
||||
headers: { Origin: httpsOrigin, "X-Forwarded-Proto": "https,http" },
|
||||
});
|
||||
assert.strictEqual(res.status, 200);
|
||||
assert.strictEqual(res.headers.get("access-control-allow-origin"), null,
|
||||
"no same-origin computation may remain to be widened into a grant");
|
||||
assert.strictEqual(res.headers.get("access-control-allow-credentials"), null);
|
||||
});
|
||||
});
|
||||
|
||||
pinnedTest("every response through the router carries Vary: Origin", async () => {
|
||||
await withServer(async (base) => {
|
||||
for (const headers of [{}, { Origin: "https://evil.example" }, { Origin: base }]) {
|
||||
const res = await fetch(`${base}/health`, { headers });
|
||||
const vary = (res.headers.get("vary") || "").split(",").map((s) => s.trim().toLowerCase());
|
||||
assert.ok(vary.includes("origin"),
|
||||
`Vary must include Origin (request headers: ${JSON.stringify(headers)})`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
pinnedTest("preflight for any Origin answers 204 without any CORS grant", async () => {
|
||||
await withServer(async (base) => {
|
||||
for (const origin of ["https://evil.example", base]) {
|
||||
const res = await fetch(`${base}/health`, {
|
||||
method: "OPTIONS",
|
||||
headers: { Origin: origin, "Access-Control-Request-Method": "GET" },
|
||||
});
|
||||
assert.strictEqual(res.status, 204);
|
||||
assert.strictEqual(res.headers.get("access-control-allow-origin"), null,
|
||||
"preflight must not grant a non-endorsed Origin (including the request's own)");
|
||||
assert.strictEqual(res.headers.get("access-control-allow-credentials"), null);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user