155 lines
6.2 KiB
JavaScript
155 lines
6.2 KiB
JavaScript
"use strict";
|
|
|
|
// Tests for the peer-admin fail-closed fix on the session revoke route.
|
|
//
|
|
// POST /api/settings/sessions/revoke classifies the revoke target before the
|
|
// step-up: a target belonging to a DIFFERENT admin must always get a fresh
|
|
// step-up that neither consumes nor mints a grace grant (a window hijacked
|
|
// after one legitimate revoke could otherwise log a peer admin out of every
|
|
// device — and kill their persistent logins — with zero further
|
|
// verification). The classification used to default to "not another admin"
|
|
// and only flipped when findUserByEmail succeeded, so a transient DB
|
|
// error/timeout (which findUserByEmail raises, e.g. via its 8s withTimeout)
|
|
// silently downgraded a peer-admin target to the grace-allowed path with a
|
|
// grant scoped to the peer admin's own email — a condition an attacker
|
|
// holding a hijacked admin session and one unscoped grant could help induce
|
|
// under load.
|
|
//
|
|
// The classification is now a tri-state ("self" | "user" | "admin" |
|
|
// "unknown") that fails CLOSED: inside the differing-email branch the
|
|
// pre-lookup state is "unknown", only a successful lookup may set
|
|
// "admin"/"user" (a not-found row cannot be an admin), the catch block
|
|
// cannot weaken it, and "unknown" rides the strictest path — allowGrace:
|
|
// false so no grant can be consumed, persistGrant: false so none can be
|
|
// minted, and graceState is never computed for the request.
|
|
//
|
|
// index.js boots servers on require, so route-level behavior is locked with
|
|
// source assertions (same technique as audit-l3-l5-access-stepup.test.js).
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
const ROOT = path.join(__dirname, "..");
|
|
const INDEX_JS = path.join(ROOT, "index.js");
|
|
|
|
const indexSource = fs.readFileSync(INDEX_JS, "utf8");
|
|
|
|
function countOccurrences(haystack, needle) {
|
|
return haystack.split(needle).length - 1;
|
|
}
|
|
|
|
function sliceBetween(source, startMarker, endMarker) {
|
|
const start = source.indexOf(startMarker);
|
|
assert.ok(start >= 0, `start marker not found: ${startMarker}`);
|
|
const end = source.indexOf(endMarker, start + startMarker.length);
|
|
assert.ok(end > start, `end marker not found after start: ${endMarker}`);
|
|
return source.slice(start, end);
|
|
}
|
|
|
|
const REVOKE_ROUTE = sliceBetween(
|
|
indexSource,
|
|
'app.post("/api/settings/sessions/revoke"',
|
|
"const PANEL_INFO_FILE"
|
|
);
|
|
|
|
const CLASSIFICATION_BLOCK = sliceBetween(
|
|
REVOKE_ROUTE,
|
|
"// Peer-admin protection:",
|
|
"const stepUpOptions = targetIsOtherAdmin"
|
|
);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Tri-state classification
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("classification is a tri-state that defaults to unknown for differing-email targets", () => {
|
|
assert.ok(
|
|
REVOKE_ROUTE.includes('let targetClassification = "self";'),
|
|
"classification must start at self (own session / same email keeps the grace feature)"
|
|
);
|
|
assert.ok(
|
|
CLASSIFICATION_BLOCK.includes("if (targetEmailHint && targetEmailHint !== currentUserEmail) {"),
|
|
"classification must only run for a target with a different email"
|
|
);
|
|
assert.equal(
|
|
countOccurrences(CLASSIFICATION_BLOCK, 'targetClassification = "unknown";'),
|
|
1,
|
|
"classification must become unknown before the lookup, exactly once"
|
|
);
|
|
assert.ok(
|
|
CLASSIFICATION_BLOCK.includes(
|
|
'targetClassification = targetUserHint && targetUserHint.admin ? "admin" : "user";'
|
|
),
|
|
"only a successful lookup may classify the target as admin or user"
|
|
);
|
|
});
|
|
|
|
test("lookup failure cannot weaken the classification (fail closed)", () => {
|
|
const catchBody = sliceBetween(
|
|
CLASSIFICATION_BLOCK,
|
|
"catch (lookupErr) {",
|
|
"}"
|
|
);
|
|
assert.ok(
|
|
catchBody.includes("console.warn("),
|
|
"lookup failures must still be logged"
|
|
);
|
|
assert.ok(
|
|
!catchBody.includes("targetClassification"),
|
|
"the catch block must not assign a weaker classification on lookup failure"
|
|
);
|
|
assert.ok(
|
|
!REVOKE_ROUTE.includes("let targetIsOtherAdmin = false;"),
|
|
"the old fail-open boolean default must not return"
|
|
);
|
|
assert.ok(
|
|
REVOKE_ROUTE.includes(
|
|
'const targetIsOtherAdmin = targetClassification === "admin" || targetClassification === "unknown";'
|
|
),
|
|
"an unknown classification must be handled exactly like a peer admin"
|
|
);
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Invariant: unknown classification mints/consumes/consults no grant
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("unknown classification forbids grace consumption, minting and graceState", () => {
|
|
const strictBranch = sliceBetween(
|
|
REVOKE_ROUTE,
|
|
"const stepUpOptions = targetIsOtherAdmin",
|
|
"const stepUp = await requireDestructiveActionStepUp"
|
|
);
|
|
assert.ok(
|
|
strictBranch.includes("{ allowGrace: false, persistGrant: false }"),
|
|
"peer-admin/unknown targets must force a fresh step-up that consumes no grant and mints none"
|
|
);
|
|
assert.ok(
|
|
REVOKE_ROUTE.includes(
|
|
"const graceState = targetIsOtherAdmin ? null : await getVerifiedSessionRevokeGraceState(req, currentUser);"
|
|
),
|
|
"graceState must never be computed when the classification is not proven non-admin"
|
|
);
|
|
});
|
|
|
|
test("classification happens before the step-up and grace is read after it", () => {
|
|
const classificationIdx = REVOKE_ROUTE.indexOf('let targetClassification = "self";');
|
|
const stepUpIdx = REVOKE_ROUTE.indexOf("const stepUp = await requireDestructiveActionStepUp");
|
|
const graceStateIdx = REVOKE_ROUTE.indexOf("const graceState = targetIsOtherAdmin");
|
|
assert.ok(classificationIdx >= 0 && stepUpIdx > classificationIdx, "classification must precede the step-up");
|
|
assert.ok(graceStateIdx > stepUpIdx, "graceState must be read only after the step-up succeeded");
|
|
});
|
|
|
|
test("the comment documents the fail-closed invariant", () => {
|
|
assert.ok(
|
|
CLASSIFICATION_BLOCK.includes("fails CLOSED"),
|
|
"peer-admin comment must state the fail-closed behavior"
|
|
);
|
|
assert.ok(
|
|
CLASSIFICATION_BLOCK.includes("no grace grant can be"),
|
|
"peer-admin comment must state that an unknown classification mints, consumes and consults no grant"
|
|
);
|
|
});
|