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,565 @@
|
||||
"use strict";
|
||||
|
||||
// Unit tests for utils/cross-site-read-guard.js — the shared CSRF-read guard
|
||||
// applied by index.js's own file routes AND nodes.js's remote-mode relays.
|
||||
// Route-level wiring (which routes carry it, on both sides) is pinned in
|
||||
// tests/node-relay-cross-site-guard-parity.test.js; this file pins the
|
||||
// middleware's own contract:
|
||||
//
|
||||
// - sec-fetch-site "cross-site" → 403, unless the Origin/Referer host is
|
||||
// operator-endorsed (same hosts the CSRF middleware lets drive WRITES).
|
||||
// - Evidence (Origin/Referer) present while the metadata header is absent
|
||||
// or indecisive → the evidence host must be the panel's own or endorsed,
|
||||
// else 403. This is the old-browser closure of the former unconditional
|
||||
// fail-open on an absent header.
|
||||
// - No evidence at all → cookieless requests PASS (curl, API scripts and
|
||||
// monitor agents); cookie-bearing requests are 403 — the residual the
|
||||
// guard previously failed open on (old browser with its Referer
|
||||
// stripped, same-site sibling that strips its Referer), now closed to
|
||||
// match sameOriginProtection. Browser-attested "same-origin"/"none"
|
||||
// keeps cookie-bearing traffic working; scripted session clients
|
||||
// declare an Origin header instead.
|
||||
// - Origin/Referer "null" → 403 only when the request carries cookies.
|
||||
// - Endorsement sources: panel public host + CSRF_ALLOWED_HOSTS entries
|
||||
// (injected by index.js) and NODES_CORS_ORIGINS (read here directly).
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
// Parsed at module load by the guard — must be set before require().
|
||||
process.env.NODES_CORS_ORIGINS = "https://app.example.com";
|
||||
|
||||
const guard = require("./cross-site-read-guard");
|
||||
const { rejectCrossSiteRead, setCrossOriginTrustContext } = guard;
|
||||
const { createPortMismatchAdvisor } = require("./origin-port-advisor");
|
||||
|
||||
function fakeReq(headers = {}) {
|
||||
return {
|
||||
headers,
|
||||
get(name) {
|
||||
// Mirror express's req.get: case-insensitive header lookup.
|
||||
const key = String(name).toLowerCase();
|
||||
if (key in headers) return headers[key];
|
||||
const upper = Object.keys(headers).find((h) => h.toLowerCase() === key);
|
||||
return upper ? headers[upper] : undefined;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function fakeRes() {
|
||||
const res = {
|
||||
statusCode: null,
|
||||
body: null,
|
||||
nextCalled: false,
|
||||
};
|
||||
res.status = (code) => {
|
||||
res.statusCode = code;
|
||||
return res;
|
||||
};
|
||||
res.json = (payload) => {
|
||||
res.body = payload;
|
||||
return res;
|
||||
};
|
||||
return res;
|
||||
}
|
||||
|
||||
// headers: secFetchSite, origin, referer, cookie, host
|
||||
function run(headers) {
|
||||
const req = fakeReq(headers);
|
||||
const res = fakeRes();
|
||||
const outcome = rejectCrossSiteRead(req, res, () => { res.nextCalled = true; });
|
||||
return { req, res, outcome };
|
||||
}
|
||||
|
||||
function expectBlocked(headers, label) {
|
||||
const { res } = run(headers);
|
||||
assert.strictEqual(res.statusCode, 403, `${label} must be rejected`);
|
||||
assert.deepStrictEqual(res.body, { error: "cross-site request blocked" }, `${label} must carry the guard's error body`);
|
||||
assert.strictEqual(res.nextCalled, false, `${label} must not reach next()`);
|
||||
}
|
||||
|
||||
function expectPassed(headers, label) {
|
||||
const { res } = run(headers);
|
||||
assert.strictEqual(res.statusCode, null, `${label} must not set a status`);
|
||||
assert.strictEqual(res.body, null, `${label} must not write a body`);
|
||||
assert.strictEqual(res.nextCalled, true, `${label} must reach next()`);
|
||||
}
|
||||
|
||||
// ---- Baseline contract -------------------------------------------------------
|
||||
|
||||
test("cross-site requests are rejected with 403 before next()", () => {
|
||||
expectBlocked({ "sec-fetch-site": "cross-site" }, "cross-site");
|
||||
});
|
||||
|
||||
test("the check is case-insensitive on the header value", () => {
|
||||
for (const value of ["Cross-Site", "CROSS-Site", "cross-Site"]) {
|
||||
expectBlocked({ "sec-fetch-site": value }, `site=${value}`);
|
||||
}
|
||||
});
|
||||
|
||||
test("same-origin, same-site and none pass through to next() when cookieless", () => {
|
||||
for (const value of ["same-origin", "same-site", "none"]) {
|
||||
expectPassed({ "sec-fetch-site": value }, `site=${value} with no other headers`);
|
||||
}
|
||||
});
|
||||
|
||||
test("an absent Sec-Fetch-Site header (cookieless non-browser clients) passes through", () => {
|
||||
// curl, API scripts, monitor agents: no Fetch Metadata headers and no
|
||||
// ambient authority — the auth gate decides.
|
||||
expectPassed({}, "headerless");
|
||||
});
|
||||
|
||||
test("evidence-less requests carrying cookies are rejected (the closed residual)", () => {
|
||||
// An old browser navigating with its Referer stripped (Referrer-Policy:
|
||||
// no-referrer) and a same-site sibling that strips its own Referer both
|
||||
// arrive with zero evidence while SameSite=Lax still attaches the session
|
||||
// cookie — byte-identical to a cookie-jar script, which must therefore
|
||||
// declare an Origin (see below), exactly as the CSRF middleware already
|
||||
// requires of it for writes.
|
||||
expectBlocked({ cookie: "connect.sid=whatever" }, "headerless with a session cookie");
|
||||
expectBlocked(
|
||||
{ cookie: "connect.sid=whatever", "sec-fetch-site": "same-site" },
|
||||
"same-site attestation with no evidence host (sibling stripped its Referer)"
|
||||
);
|
||||
expectBlocked(
|
||||
{ cookie: "connect.sid=whatever", "sec-fetch-site": "crosssite" },
|
||||
"malformed site value with a session cookie"
|
||||
);
|
||||
});
|
||||
|
||||
test("browser-attested no-initiator navigations keep cookies working", () => {
|
||||
// Sec-Fetch-Site is computed by the browser and JS cannot forge it
|
||||
// (forbidden header name), so "none" on a cookie-bearing request is a
|
||||
// genuine top-level navigation — the paste-a-URL case every panel allows
|
||||
// by design. Same for an attested same-origin fetch that carries no
|
||||
// Origin/Referer headers.
|
||||
expectPassed(
|
||||
{ cookie: "connect.sid=whatever", "sec-fetch-site": "none" },
|
||||
"address-bar navigation with a session cookie"
|
||||
);
|
||||
expectPassed(
|
||||
{ cookie: "connect.sid=whatever", "sec-fetch-site": "same-origin" },
|
||||
"attested same-origin with a session cookie and no Origin/Referer"
|
||||
);
|
||||
});
|
||||
|
||||
test("scripted session clients declare themselves with an Origin header", () => {
|
||||
// The escape hatch branch 4 already provides: one header, the same
|
||||
// declaration cookie-jar scripts need for any state-changing request.
|
||||
expectPassed(
|
||||
{ cookie: "connect.sid=whatever", origin: "https://panel.example.com", host: "panel.example.com" },
|
||||
"headerless + cookie + same-host Origin"
|
||||
);
|
||||
expectBlocked(
|
||||
{ cookie: "connect.sid=whatever", origin: "https://evil.example", host: "panel.example.com" },
|
||||
"headerless + cookie + foreign Origin"
|
||||
);
|
||||
});
|
||||
|
||||
test("a malformed value fails open (only the exact cross-site token blocks)", () => {
|
||||
// Deliberate: the guard is read-side hardening on top of auth, not an auth
|
||||
// layer — an unknown value must not break legitimate (cookieless) clients.
|
||||
for (const value of ["crosssite", "cross site", "cross_site", ""]) {
|
||||
expectPassed({ "sec-fetch-site": value }, `malformed site=${JSON.stringify(value)}`);
|
||||
}
|
||||
});
|
||||
|
||||
// ---- Old-browser fallback: evidence present, metadata header absent ---------
|
||||
|
||||
test("absent metadata + same-host Origin passes (old browser, same-origin UI)", () => {
|
||||
expectPassed(
|
||||
{ origin: "https://panel.example.com", host: "panel.example.com" },
|
||||
"same-origin Origin without metadata"
|
||||
);
|
||||
});
|
||||
|
||||
test("absent metadata + foreign Origin is now rejected (the old-browser hole)", () => {
|
||||
expectBlocked(
|
||||
{ origin: "https://evil.example", host: "panel.example.com" },
|
||||
"cross-origin Origin without metadata"
|
||||
);
|
||||
});
|
||||
|
||||
test("absent metadata + foreign Referer is rejected, same-host Referer passes", () => {
|
||||
expectBlocked(
|
||||
{ referer: "https://evil.example/attack.html", host: "panel.example.com" },
|
||||
"cross-origin Referer without metadata"
|
||||
);
|
||||
expectPassed(
|
||||
{ referer: "https://panel.example.com/dashboard", host: "panel.example.com" },
|
||||
"same-origin Referer without metadata"
|
||||
);
|
||||
});
|
||||
|
||||
test("hosts are matched without scheme or port (CSRF middleware semantics)", () => {
|
||||
expectPassed(
|
||||
{ origin: "https://panel.example.com:8443", host: "panel.example.com" },
|
||||
"same host, different port"
|
||||
);
|
||||
});
|
||||
|
||||
test("localhost evidence grants the localhost variants, as in the CSRF middleware", () => {
|
||||
expectPassed(
|
||||
{ origin: "http://localhost:3000", host: "127.0.0.1" },
|
||||
"localhost Origin against loopback Host"
|
||||
);
|
||||
});
|
||||
|
||||
test("same-site evidence from a sibling subdomain is held to the host standard", () => {
|
||||
// SameSite=Lax cookies attach to same-site GETs, so a hostile sibling
|
||||
// subdomain must be allowlisted exactly like for state-changing requests.
|
||||
expectBlocked(
|
||||
{ "sec-fetch-site": "same-site", origin: "https://evil.example.com", host: "panel.example.com" },
|
||||
"same-site attestation + non-allowlisted sibling host"
|
||||
);
|
||||
expectPassed(
|
||||
{ "sec-fetch-site": "same-site", origin: "https://app.example.com", host: "panel.example.com" },
|
||||
"same-site attestation + NODES_CORS_ORIGINS host"
|
||||
);
|
||||
});
|
||||
|
||||
test("a browser-attested same-origin fetch beats a contradictory Origin header", () => {
|
||||
// Mirrors sameOriginProtection: sameOriginFetch bypasses the host check.
|
||||
expectPassed(
|
||||
{ "sec-fetch-site": "same-origin", origin: "https://evil.example", host: "panel.example.com" },
|
||||
"same-origin attestation with foreign Origin"
|
||||
);
|
||||
});
|
||||
|
||||
test("cross-site attestation with an endorsed host passes (reads are not stricter than writes)", () => {
|
||||
// The CSRF middleware accepts cross-site WRITES from CSRF_ALLOWED_HOSTS
|
||||
// hosts; the guard accepts their reads the same way.
|
||||
expectBlocked(
|
||||
{ "sec-fetch-site": "cross-site", origin: "https://evil.example", host: "panel.example.com" },
|
||||
"cross-site attestation + foreign Origin"
|
||||
);
|
||||
expectPassed(
|
||||
{ "sec-fetch-site": "cross-site", origin: "https://app.example.com", host: "panel.example.com" },
|
||||
"cross-site attestation + NODES_CORS_ORIGINS host"
|
||||
);
|
||||
});
|
||||
|
||||
test("opaque null origin: blocked only when it carries cookies", () => {
|
||||
expectPassed(
|
||||
{ origin: "null", host: "panel.example.com" },
|
||||
"null Origin cookieless (no ambient authority — auth gate decides)"
|
||||
);
|
||||
expectBlocked(
|
||||
{ origin: "null", host: "panel.example.com", cookie: "connect.sid=whatever" },
|
||||
"null Origin with cookies"
|
||||
);
|
||||
expectPassed(
|
||||
{ origin: "null", host: "panel.example.com", cookie: "connect.sid=whatever", "sec-fetch-site": "same-origin" },
|
||||
"null Origin with cookies but browser-attested same-origin"
|
||||
);
|
||||
});
|
||||
|
||||
test("unparseable Origin with no other evidence is treated as no evidence", () => {
|
||||
// Matches the CSRF middleware's parse-based behavior: garbage in, no
|
||||
// evidence out; fail-open stays confined to the no-evidence branch.
|
||||
expectPassed({ origin: " " }, "whitespace Origin");
|
||||
});
|
||||
|
||||
// ---- Injected trust context (what index.js provides in production) ----------
|
||||
|
||||
test("panel public host and CSRF_ALLOWED_HOSTS entries endorse cross-origin reads", () => {
|
||||
setCrossOriginTrustContext({
|
||||
panelPublicHost: "panel.example.com",
|
||||
extraAllowedHosts: ["portal.example.com", "*.apps.example.com"],
|
||||
});
|
||||
try {
|
||||
// The serving Host header is NOT the panel public host (proxy rewrites
|
||||
// Host to an upstream name) — the injected public host still endorses.
|
||||
expectPassed(
|
||||
{ origin: "https://panel.example.com", host: "upstream.internal" },
|
||||
"PANEL_PUBLIC_URL host endorsement"
|
||||
);
|
||||
expectPassed(
|
||||
{ referer: "https://portal.example.com/integration", host: "upstream.internal" },
|
||||
"CSRF_ALLOWED_HOSTS entry endorsement"
|
||||
);
|
||||
expectPassed(
|
||||
{ origin: "https://a.apps.example.com", host: "upstream.internal" },
|
||||
"wildcard CSRF_ALLOWED_HOSTS entry endorsement"
|
||||
);
|
||||
expectBlocked(
|
||||
{ origin: "https://still-evil.example", host: "upstream.internal" },
|
||||
"non-endorsed host stays blocked with injected context"
|
||||
);
|
||||
} finally {
|
||||
// Reset so later tests run with the standalone (Host-header-only) context.
|
||||
setCrossOriginTrustContext({});
|
||||
}
|
||||
});
|
||||
|
||||
test("guard honors the CSRF read allowlist for cross-site-attested foreign hosts after injection", () => {
|
||||
setCrossOriginTrustContext({ extraAllowedHosts: ["portal.example.com"] });
|
||||
try {
|
||||
expectPassed(
|
||||
{ "sec-fetch-site": "cross-site", origin: "https://portal.example.com", host: "panel.example.com" },
|
||||
"cross-site attestation + CSRF_ALLOWED_HOSTS host"
|
||||
);
|
||||
} finally {
|
||||
setCrossOriginTrustContext({});
|
||||
}
|
||||
});
|
||||
|
||||
test("NODES_CORS_ORIGINS matches at host level: a sibling origin may drive a request it cannot read", () => {
|
||||
// The allowlist names https://app.example.com; the guard (host-level) also
|
||||
// passes http://app.example.com — but the CORS layer echoes ACAO only for
|
||||
// the exact origin, so the sibling origin cannot actually read responses.
|
||||
expectPassed(
|
||||
{ origin: "http://app.example.com", host: "panel.example.com" },
|
||||
"sibling scheme of a NODES_CORS_ORIGINS entry"
|
||||
);
|
||||
});
|
||||
|
||||
// ---- adpanel:port-mismatch-advisor wiring (the third shared-rule rejection
|
||||
// point) ---------------------------------------------------------------
|
||||
//
|
||||
// The guard's port-mismatch rejection is the ONLY encounter cookie-bearing
|
||||
// GET/HEAD reads have with the explicit-port rule (reads skip
|
||||
// sameOriginProtection entirely), so it must be as diagnosable as the
|
||||
// middleware's 403 branch and the socket handshake — without turning the
|
||||
// rejection into anything but a rejection. The tests below use the REAL
|
||||
// advisor factory (the same one index.js binds) wrapped with a recorder, so
|
||||
// both the fire condition and the evidence forwarding are pinned.
|
||||
|
||||
// Wraps a fresh real advisor with a recorder: returns { advisor, seen,
|
||||
// logged } — seen collects the evidence objects the guard forwarded, logged
|
||||
// the lines logAdvice received.
|
||||
function recordingAdvisor() {
|
||||
const real = createPortMismatchAdvisor();
|
||||
const seen = [];
|
||||
const logged = [];
|
||||
const advisor = {
|
||||
advice(evidence) {
|
||||
seen.push(evidence);
|
||||
return real.advice(evidence);
|
||||
},
|
||||
};
|
||||
return { advisor, seen, logged };
|
||||
}
|
||||
|
||||
test("the port-mismatch rejection forwards its evidence to the injected advisor and logs the advice", () => {
|
||||
const { advisor, seen, logged } = recordingAdvisor();
|
||||
setCrossOriginTrustContext({
|
||||
portMismatchAdvisor: advisor,
|
||||
logAdvice: (line, req) => logged.push({ line, req }),
|
||||
});
|
||||
try {
|
||||
// The brick shape: cookie-bearing read, explicit non-default origin
|
||||
// port, portless serving Host header — rejected by the shared rule, and
|
||||
// the rejection must be LOUD (a misconfigured Deployment-D operator gets
|
||||
// diagnosed on writes AND now on reads).
|
||||
const { req, res } = run({
|
||||
cookie: "connect.sid=whatever",
|
||||
origin: "https://panel.example.com:8443",
|
||||
host: "panel.example.com",
|
||||
});
|
||||
assert.strictEqual(res.statusCode, 403, "the brick shape must still be rejected");
|
||||
assert.deepStrictEqual(
|
||||
res.body,
|
||||
{ error: "cross-site request blocked" },
|
||||
"the 403 body stays byte-identical"
|
||||
);
|
||||
assert.strictEqual(res.nextCalled, false);
|
||||
assert.deepStrictEqual(
|
||||
seen,
|
||||
[{ originHeader: "https://panel.example.com:8443", servingPort: null }],
|
||||
"the advisor must see exactly the evidence the rejection saw"
|
||||
);
|
||||
assert.strictEqual(logged.length, 1, "the returned advice must be logged");
|
||||
assert.ok(
|
||||
typeof logged[0].line === "string" && logged[0].line.includes("PANEL_PUBLIC_URL"),
|
||||
"the advice carries the diagnosis with the two remedies"
|
||||
);
|
||||
assert.strictEqual(logged[0].req, req, "logAdvice receives the request");
|
||||
} finally {
|
||||
setCrossOriginTrustContext({});
|
||||
}
|
||||
});
|
||||
|
||||
test("the advisor sees the Referer when the Referer is the evidence header", () => {
|
||||
const { advisor, seen, logged } = recordingAdvisor();
|
||||
setCrossOriginTrustContext({
|
||||
portMismatchAdvisor: advisor,
|
||||
logAdvice: (line) => logged.push(line),
|
||||
});
|
||||
try {
|
||||
const { res } = run({
|
||||
cookie: "connect.sid=whatever",
|
||||
referer: "https://panel.example.com:8443/dashboard",
|
||||
host: "panel.example.com",
|
||||
});
|
||||
assert.strictEqual(res.statusCode, 403);
|
||||
assert.deepStrictEqual(
|
||||
seen,
|
||||
[{ originHeader: "https://panel.example.com:8443/dashboard", servingPort: null }],
|
||||
"the forwarded header must be the SAME one that produced the guard's originPair (Origin preferred, else Referer)"
|
||||
);
|
||||
assert.strictEqual(logged.length, 1);
|
||||
} finally {
|
||||
setCrossOriginTrustContext({});
|
||||
}
|
||||
});
|
||||
|
||||
test("non-brick shapes stay silent (the advisor owns the fire condition)", () => {
|
||||
const { advisor, seen, logged } = recordingAdvisor();
|
||||
setCrossOriginTrustContext({
|
||||
portMismatchAdvisor: advisor,
|
||||
logAdvice: (line) => logged.push(line),
|
||||
});
|
||||
try {
|
||||
// Both-explicit different ports: a plain misroute, not the brick — the
|
||||
// branch still forwards the evidence, but the real advisor withholds.
|
||||
expectBlocked(
|
||||
{ cookie: "connect.sid=whatever", origin: "https://panel.example.com:8443", host: "panel.example.com:9999" },
|
||||
"both-explicit different ports"
|
||||
);
|
||||
// Plain foreign-host rejection: never reaches the port-mismatch branch.
|
||||
expectBlocked(
|
||||
{ "sec-fetch-site": "cross-site", origin: "https://evil.example", host: "panel.example.com" },
|
||||
"foreign host"
|
||||
);
|
||||
assert.deepStrictEqual(
|
||||
seen,
|
||||
[{ originHeader: "https://panel.example.com:8443", servingPort: "9999" }],
|
||||
"only the port-mismatch branch calls the advisor"
|
||||
);
|
||||
assert.deepStrictEqual(logged, [], "no advice, no log — nothing outside the brick shape");
|
||||
} finally {
|
||||
setCrossOriginTrustContext({});
|
||||
}
|
||||
});
|
||||
|
||||
test("an advisor that throws cannot alter or break the rejection (best-effort logging)", () => {
|
||||
const logged = [];
|
||||
setCrossOriginTrustContext({
|
||||
portMismatchAdvisor: {
|
||||
advice() { throw new Error("advisor exploded"); },
|
||||
},
|
||||
logAdvice: (line) => logged.push(line),
|
||||
});
|
||||
try {
|
||||
expectBlocked(
|
||||
{ cookie: "connect.sid=whatever", origin: "https://panel.example.com:8443", host: "panel.example.com" },
|
||||
"throwing advisor"
|
||||
);
|
||||
assert.deepStrictEqual(logged, []);
|
||||
} finally {
|
||||
setCrossOriginTrustContext({});
|
||||
}
|
||||
});
|
||||
|
||||
test("without the injected advisor the brick rejection is unchanged (nodes.js posture)", () => {
|
||||
// The afterEach reset leaves the standalone context: nodes.js binds the
|
||||
// guard WITHOUT the dep, so the rejection here must be byte-identical to
|
||||
// the pre-advisor guard — same 403, same body, no throw.
|
||||
expectBlocked(
|
||||
{ cookie: "connect.sid=whatever", origin: "https://panel.example.com:8443", host: "panel.example.com" },
|
||||
"brick shape without an injected advisor"
|
||||
);
|
||||
});
|
||||
|
||||
test("index.js wires the shared advisor into the guard's trust context (structural pin)", () => {
|
||||
const src = fs.readFileSync(path.join(__dirname, "..", "index.js"), "utf8");
|
||||
const at = src.indexOf("crossSiteReadGuard.setCrossOriginTrustContext(");
|
||||
assert.notStrictEqual(at, -1, "index.js must configure the guard's trust context");
|
||||
const win = src.slice(at, at + 1200);
|
||||
assert.ok(
|
||||
win.includes("portMismatchAdvisor: _PORT_MISMATCH_ADVISOR"),
|
||||
"index.js must inject the shared _PORT_MISMATCH_ADVISOR instance into the read guard — cookie-bearing reads reach the guard's port-mismatch branch, not sameOriginProtection's"
|
||||
);
|
||||
assert.ok(
|
||||
win.includes("logAdvice: (line, req) => securityLog(line, req)"),
|
||||
"index.js must route the guard's advice through the same securityLog sink the other two surfaces use"
|
||||
);
|
||||
});
|
||||
|
||||
// ---- App-wide default-secure read gate (createApiReadGate) ------------------
|
||||
|
||||
const { createApiReadGate } = guard;
|
||||
|
||||
function runGate(gateFn, headers, { method = "GET", path = "/anything" } = {}) {
|
||||
const req = fakeReq(headers);
|
||||
req.method = method;
|
||||
req.path = path;
|
||||
const res = fakeRes();
|
||||
gateFn(req, res, () => { res.nextCalled = true; });
|
||||
return res;
|
||||
}
|
||||
|
||||
// Mirrors the exemption list index.js mounts (telemetry-polling family;
|
||||
// the /api/status/ metrics history routes are enumerated exactly — no
|
||||
// prefix pattern — so future routes under that namespace stay guarded).
|
||||
const apiGate = createApiReadGate({
|
||||
exemptPaths: ["/me", "/me/status", "/servers/statuses", "/server-info", "/status/metrics/day", "/status/metrics/summary", "/status/snapshot"],
|
||||
exemptPatterns: [/^\/server\/[^/]+\/status$/, /^\/server\/[^/]+\/node-status$/],
|
||||
});
|
||||
|
||||
test("non-GET/HEAD methods pass through untouched (writes belong to the CSRF middleware)", () => {
|
||||
for (const method of ["POST", "PUT", "DELETE", "PATCH", "OPTIONS"]) {
|
||||
const res = runGate(apiGate, { cookie: "connect.sid=x" }, { method });
|
||||
assert.strictEqual(res.nextCalled, true, `${method} must skip the read gate`);
|
||||
assert.strictEqual(res.statusCode, null, `${method} must not be answered by the read gate`);
|
||||
}
|
||||
});
|
||||
|
||||
test("HEAD is gated like GET (existence/size oracle via status and Content-Length)", () => {
|
||||
const res = runGate(apiGate, { cookie: "connect.sid=x" }, { method: "HEAD", path: "/settings/security" });
|
||||
assert.strictEqual(res.statusCode, 403, "cookie-bearing evidence-less HEAD must be rejected");
|
||||
});
|
||||
|
||||
test("exempt telemetry paths and patterns skip the guard even with cookies and no evidence", () => {
|
||||
for (const p of ["/me", "/me/status", "/servers/statuses", "/server-info", "/server/web1/status", "/server/web1/node-status", "/status/metrics/day", "/status/metrics/summary", "/status/snapshot"]) {
|
||||
const res = runGate(apiGate, { cookie: "connect.sid=x" }, { path: p });
|
||||
assert.strictEqual(res.nextCalled, true, `${p} (documented telemetry polling) must skip the guard`);
|
||||
}
|
||||
});
|
||||
|
||||
test("exempt matching is exact: near-misses and sub-paths stay guarded", () => {
|
||||
for (const p of ["/me/keys", "/servers/statuses-extra", "/server-info/abc", "/settings/database/pgadmin/status", "/server/web1/statuses"]) {
|
||||
const res = runGate(apiGate, { cookie: "connect.sid=x" }, { path: p });
|
||||
assert.strictEqual(res.statusCode, 403, `${p} must NOT match the telemetry exemptions`);
|
||||
}
|
||||
});
|
||||
|
||||
test("no prefix inheritance: a future route under /api/status/ is guarded by default", () => {
|
||||
// The metrics history routes are enumerated exactly — a NEW route added
|
||||
// under that namespace must not silently inherit the telemetry exemption.
|
||||
for (const p of ["/status/whatever", "/status/metrics/week", "/status/export"]) {
|
||||
const res = runGate(apiGate, { cookie: "connect.sid=x" }, { path: p });
|
||||
assert.strictEqual(res.statusCode, 403, `${p} must be guarded — the /api/status/ exemption is exact-path only`);
|
||||
}
|
||||
});
|
||||
|
||||
test("cookie-bearing evidence-less GETs outside the exemptions are rejected", () => {
|
||||
const res = runGate(apiGate, { cookie: "connect.sid=x" }, { path: "/settings/security" });
|
||||
assert.strictEqual(res.statusCode, 403);
|
||||
assert.deepStrictEqual(res.body, { error: "cross-site request blocked" });
|
||||
assert.strictEqual(res.nextCalled, false);
|
||||
});
|
||||
|
||||
test("cookieless GETs outside the exemptions pass to the auth gate (non-browser contract)", () => {
|
||||
const res = runGate(apiGate, {}, { path: "/settings/security" });
|
||||
assert.strictEqual(res.nextCalled, true);
|
||||
assert.strictEqual(res.statusCode, null);
|
||||
});
|
||||
|
||||
test("browser-attested navigations and same-origin fetches with cookies still pass", () => {
|
||||
for (const site of ["none", "same-origin"]) {
|
||||
const res = runGate(apiGate, { cookie: "connect.sid=x", "sec-fetch-site": site }, { path: "/settings/security" });
|
||||
assert.strictEqual(res.nextCalled, true, `site=${site} must pass the gate`);
|
||||
}
|
||||
});
|
||||
|
||||
test("a gate without exemptions guards every cookie-bearing GET/HEAD", () => {
|
||||
const bare = createApiReadGate();
|
||||
const res = runGate(bare, { cookie: "connect.sid=x" }, { path: "/me" });
|
||||
assert.strictEqual(res.statusCode, 403, "no exemptions means no telemetry carve-out");
|
||||
const head = runGate(bare, { cookie: "connect.sid=x" }, { method: "HEAD", path: "/me" });
|
||||
assert.strictEqual(head.statusCode, 403, "HEAD is gated like GET with no exemptions");
|
||||
});
|
||||
Reference in new issue
Block a user