124 lines
6.3 KiB
JavaScript
124 lines
6.3 KiB
JavaScript
"use strict";
|
|
|
|
// adpanel:session-account-binding — unit tests for the ONE shared
|
|
// "this session still belongs to this account row" predicate. The HTTP session
|
|
// middleware and the Socket.IO handshake must both reject on exactly this
|
|
// shape; see tests/session-account-binding-pins.test.js for the source pins.
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert");
|
|
|
|
const { sessionAccountBindingMismatch, credentialGenerationMismatch } = require("./session-account-binding");
|
|
|
|
test("a legacy session without accountId/accountEpoch passes the binding", () => {
|
|
// Sessions minted before the binding existed must keep working — they are
|
|
// covered by the pending-session-revocation sweep, not by this predicate.
|
|
const session = { user: "admin@example.com" };
|
|
assert.strictEqual(sessionAccountBindingMismatch(session, { id: 7, session_epoch: 3 }), false);
|
|
assert.strictEqual(sessionAccountBindingMismatch({ cookie: {} }, { id: 7 }), false);
|
|
});
|
|
|
|
test("a fully bound session whose id and epoch match the resolved row passes", () => {
|
|
assert.strictEqual(
|
|
sessionAccountBindingMismatch({ accountId: 7, accountEpoch: 3 }, { id: 7, session_epoch: 3 }),
|
|
false
|
|
);
|
|
// The original inline comparison used Number() coercion on the row fields,
|
|
// so numeric strings from a driver that returns strings keep passing.
|
|
assert.strictEqual(
|
|
sessionAccountBindingMismatch({ accountId: 7, accountEpoch: 3 }, { id: "7", session_epoch: "3" }),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("an id mismatch is rejected (session bound to a different account row)", () => {
|
|
const session = { accountId: 7, accountEpoch: 3 };
|
|
assert.strictEqual(sessionAccountBindingMismatch(session, { id: 8, session_epoch: 3 }), true);
|
|
});
|
|
|
|
test("an epoch mismatch is rejected (account deleted, then recreated with the same email)", () => {
|
|
const session = { accountId: 7, accountEpoch: 3 };
|
|
assert.strictEqual(sessionAccountBindingMismatch(session, { id: 7, session_epoch: 4 }), true);
|
|
});
|
|
|
|
test("a null or missing user row is rejected (fail closed)", () => {
|
|
// No predicate caller can pass a session without a resolved user row — the
|
|
// guard exists so a future caller cannot turn an unresolved row into an
|
|
// implicit pass.
|
|
assert.strictEqual(sessionAccountBindingMismatch({ accountId: 7, accountEpoch: 3 }, null), true);
|
|
assert.strictEqual(sessionAccountBindingMismatch({ accountId: 7, accountEpoch: 3 }, undefined), true);
|
|
});
|
|
|
|
test("a missing session is rejected (fail closed)", () => {
|
|
assert.strictEqual(sessionAccountBindingMismatch(null, { id: 7, session_epoch: 3 }), true);
|
|
assert.strictEqual(sessionAccountBindingMismatch(undefined, { id: 7, session_epoch: 3 }), true);
|
|
});
|
|
|
|
test("the epoch leg stays inert when the row carries no session_epoch (exact legacy semantics)", () => {
|
|
// Number.isFinite(undefined) is false: a row backfilled without an epoch
|
|
// must not fail the epoch leg — matching the original inline HTTP predicate.
|
|
assert.strictEqual(
|
|
sessionAccountBindingMismatch({ accountId: 7, accountEpoch: 3 }, { id: 7 }),
|
|
false
|
|
);
|
|
});
|
|
|
|
// adpanel:credential-generation — the strict proof-vs-live generation
|
|
// comparison. Different epochs on comparable sides always mismatch; an
|
|
// uncomparable side is "cannot decide", which callers pair with the
|
|
// unconditional fingerprint re-check.
|
|
test("credentialGenerationMismatch rejects a proof minted before a credential bump", () => {
|
|
assert.strictEqual(credentialGenerationMismatch(3, 4), true);
|
|
assert.strictEqual(credentialGenerationMismatch(4, 3), true);
|
|
});
|
|
|
|
test("credentialGenerationMismatch accepts only an exact generation match", () => {
|
|
assert.strictEqual(credentialGenerationMismatch(3, 3), false);
|
|
assert.strictEqual(credentialGenerationMismatch(2147483646, 2147483646), false);
|
|
});
|
|
|
|
test("credentialGenerationMismatch treats absent/NULL/non-numeric as uncomparable, never as 0", () => {
|
|
assert.strictEqual(credentialGenerationMismatch(undefined, 4), false);
|
|
assert.strictEqual(credentialGenerationMismatch(3, undefined), false);
|
|
assert.strictEqual(credentialGenerationMismatch(null, 4), false);
|
|
assert.strictEqual(credentialGenerationMismatch(3, null), false);
|
|
assert.strictEqual(credentialGenerationMismatch("abc", 4), false); // non-numeric proved side
|
|
assert.strictEqual(credentialGenerationMismatch(3, "abc"), false); // non-numeric live side
|
|
assert.strictEqual(credentialGenerationMismatch(0, 0), false);
|
|
});
|
|
|
|
test("credentialGenerationMismatch coerces numeric-string pairs consistently", () => {
|
|
// Numeric strings are comparable (matching the binding predicate's deliberate
|
|
// Number() tolerance for drivers that return strings); equality on value.
|
|
assert.strictEqual(credentialGenerationMismatch("3", "3"), false);
|
|
assert.strictEqual(credentialGenerationMismatch("3", "4"), true);
|
|
assert.strictEqual(credentialGenerationMismatch("3", 4), true);
|
|
});
|
|
|
|
test("credentialGenerationMismatch denies a legacy generation against any bumped one", () => {
|
|
// 0 is the schema DEFAULT ("no generation") and what legacy binds coerce to:
|
|
// the security-relevant direction is 0 vs nonzero, which must DENY.
|
|
assert.strictEqual(credentialGenerationMismatch(0, 5), true);
|
|
assert.strictEqual(credentialGenerationMismatch(5, 0), true);
|
|
assert.strictEqual(credentialGenerationMismatch(0, 2147483646), true);
|
|
});
|
|
|
|
test("credentialGenerationMismatch keeps non-finite numbers uncomparable", () => {
|
|
assert.strictEqual(credentialGenerationMismatch(Infinity, 3), false);
|
|
assert.strictEqual(credentialGenerationMismatch(3, Infinity), false);
|
|
assert.strictEqual(credentialGenerationMismatch(NaN, 3), false);
|
|
assert.strictEqual(credentialGenerationMismatch(-Infinity, -Infinity), false);
|
|
});
|
|
|
|
test("credentialGenerationMismatch documents Number()'s value-like coercions", () => {
|
|
// Number()'s grammar makes " " and "" read as 0 and booleans as 1/0 — these
|
|
// sources are unreachable (rows are BIGINT via mapUserRow's Number(), the
|
|
// session store is server-side, minted records write Number()), the deny
|
|
// direction holds regardless, and the fingerprint leg is authoritative. The
|
|
// pins below lock the comparable-by-value semantics the comment documents.
|
|
assert.strictEqual(credentialGenerationMismatch("", 1), true);
|
|
assert.strictEqual(credentialGenerationMismatch("", 0), false);
|
|
assert.strictEqual(credentialGenerationMismatch(true, 1), false);
|
|
assert.strictEqual(credentialGenerationMismatch(true, 2), true);
|
|
});
|