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,109 @@
|
||||
"use strict";
|
||||
|
||||
// Contract tests for the nodes.js relay's CORS responder with an operator
|
||||
// allowlist configured (NODES_CORS_ORIGINS). The default posture (allowlist
|
||||
// unset → no CORS grant at all, for any Origin) is pinned in
|
||||
// tests/node-relay-cors.test.js.
|
||||
//
|
||||
// With an allowlist set, an Origin listed there is echoed with credentials —
|
||||
// that is the ONE way a cross-origin browser client gets credentialed access,
|
||||
// it is an explicit operator trust decision, and anything not listed must
|
||||
// still get nothing. The allowlist is the only gate: since 2026-10-18 there
|
||||
// is no default same-origin echo to combine with or fall back to (pinned
|
||||
// below — a same-origin Origin not on the list gets nothing either).
|
||||
|
||||
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-allowlist-test-secret-0123456789";
|
||||
}
|
||||
// Parsed at module load by nodes.js — must be set before require().
|
||||
process.env.NODES_CORS_ORIGINS = "https://app.example.com";
|
||||
|
||||
const nodesApp = require("../nodes.js");
|
||||
|
||||
// ---- Exit-code honesty -------------------------------------------------------
|
||||
// Same rationale as tests/node-relay-cross-site-guard-parity.test.js: the
|
||||
// mysql2 pool and nodes.js timers keep the event loop alive after the suite,
|
||||
// so the exit is forced — but the exit code tallies REAL failures via
|
||||
// pinnedTest() instead of a self-managed flag (a flag set after a failed
|
||||
// subtest masks the failure behind exit 0; found by adversarial review,
|
||||
// 2026-10-17 — see SECURITY-FIXES.md), and the TAP output is flushed before
|
||||
// the forced exit so every result stays visible.
|
||||
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 — same rationale as tests/node-relay-cors.test.js.
|
||||
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("allowlisted Origin is echoed with credentials and preflight grant", async () => {
|
||||
await withServer(async (base) => {
|
||||
const res = await fetch(`${base}/health`, { headers: { Origin: "https://app.example.com" } });
|
||||
assert.strictEqual(res.status, 200);
|
||||
assert.strictEqual(res.headers.get("access-control-allow-origin"), "https://app.example.com",
|
||||
"the exact allowlisted origin must be echoed (never a wildcard)");
|
||||
assert.strictEqual(res.headers.get("access-control-allow-credentials"), "true");
|
||||
assert.strictEqual(res.headers.get("access-control-allow-methods"), "GET,POST,PATCH,DELETE,OPTIONS");
|
||||
|
||||
const preflight = await fetch(`${base}/health`, {
|
||||
method: "OPTIONS",
|
||||
headers: { Origin: "https://app.example.com", "Access-Control-Request-Method": "POST" },
|
||||
});
|
||||
assert.strictEqual(preflight.status, 204);
|
||||
assert.strictEqual(preflight.headers.get("access-control-allow-origin"), "https://app.example.com",
|
||||
"preflight for an allowlisted Origin must be granted");
|
||||
});
|
||||
});
|
||||
|
||||
pinnedTest("anything not on the allowlist gets no CORS grant, allowlist or not", async () => {
|
||||
await withServer(async (base) => {
|
||||
// http://app.example.com: scheme differs from the allowlisted origin —
|
||||
// CORS grants are exact-origin, scheme included.
|
||||
// https://app.example.com.evil.example: suffix look-alike must not match.
|
||||
for (const origin of ["https://evil.example", "http://app.example.com", "https://app.example.com.evil.example"]) {
|
||||
const res = await fetch(`${base}/health`, { headers: { Origin: origin } });
|
||||
assert.strictEqual(res.headers.get("access-control-allow-origin"), null,
|
||||
`Origin ${origin} must not receive ACAO`);
|
||||
assert.strictEqual(res.headers.get("access-control-allow-credentials"), null,
|
||||
`Origin ${origin} must not receive Access-Control-Allow-Credentials`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
pinnedTest("with an allowlist set, the same-origin Origin is granted nothing unless listed", async () => {
|
||||
await withServer(async (base) => {
|
||||
// The allowlist is the only gate (2026-10-18 posture): an unlisted Origin
|
||||
// gets no grant, even when it happens to be the request's own. An
|
||||
// operator who wants the panel's own origin endorsed simply lists it.
|
||||
const res = await fetch(`${base}/health`, { headers: { Origin: base } });
|
||||
assert.strictEqual(res.headers.get("access-control-allow-origin"), null,
|
||||
"a same-origin Origin not on the allowlist must not receive ACAO when an allowlist is set");
|
||||
assert.strictEqual(res.headers.get("access-control-allow-credentials"), null);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user