"use strict"; // adpanel:session-account-binding — source pins for the ONE shared // session/account binding predicate. The writeup's finding #7: the Socket.IO // handshake authenticated a session without the account id/epoch binding and // without the revoked-sid check that the HTTP session middleware enforces, so // a socket could outlive the account row it was minted for (deleted-then- // recreated account with the same email) or keep speaking after a teardown. // These pins hold the consolidation in place: both auth surfaces must call // sessionAccountBindingMismatch( (never a re-inlined copy), the handshake must // enforce it after the user lookup, and the old inline accountIdMismatch // constant must stay gone from index.js. It reads index.js as text, like the // other audit tests. 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 indexSource = fs.readFileSync(path.join(ROOT, "index.js"), "utf8"); test("the HTTP session middleware binds through the shared predicate", () => { const at = indexSource.indexOf('if (req._staticAsset || !req.session?.user) return next();'); assert.notStrictEqual(at, -1, "expected the account-binding/suspension middleware"); const end = indexSource.indexOf("// Middleware to ensure session TTL is refreshed", at); assert.ok(end > at, "expected the end of the binding/suspension middleware"); const region = indexSource.slice(at, end); assert.ok( region.includes("sessionAccountBindingMismatch(req.session, user)"), "the HTTP middleware must decide the binding through the shared predicate" ); assert.ok( region.includes("teardownStaleAccountSession(req, res, { respond: true })"), "a binding mismatch must tear the session down and answer (respond: true)" ); // adpanel:session-account-binding — the branch ORDER inside this middleware // is itself security-relevant and is pinned here by character offset, not // just by presence: the zombie branch (the account row behind the session // is gone) must resolve first — there is no row to compare a binding // against and no suspension state to leak; the shared-predicate binding // check second; the suspension gate LAST. The middleware's own comment // states the constraint: the binding is "enforced BEFORE the suspension // branch so a stale session never learns the recreated account's // suspension state". A swap of any two branches keeps every string the // presence pins above (and every other pin in this file) true, so only // these offsets catch it. const zombieAt = region.indexOf("if (!user) {"); const respondFalseAt = region.indexOf("teardownStaleAccountSession(req, res, { respond: false })"); const bindingAt = region.indexOf("sessionAccountBindingMismatch(req.session, user)"); const respondTrueAt = region.indexOf("teardownStaleAccountSession(req, res, { respond: true })"); const suspendedAt = region.indexOf("if (!user?.suspended) return next();"); assert.notStrictEqual(zombieAt, -1, "expected the zombie (missing account row) branch"); assert.notStrictEqual(respondFalseAt, -1, "the zombie branch must tear the stale session down silently (respond: false)"); assert.notStrictEqual(bindingAt, -1, "expected the shared-predicate binding check"); assert.notStrictEqual(respondTrueAt, -1, "expected the binding branch's answering teardown (respond: true)"); assert.notStrictEqual(suspendedAt, -1, "expected the suspension gate"); assert.ok(respondFalseAt > zombieAt, "the respond:false teardown belongs to the zombie branch"); assert.ok(respondFalseAt < bindingAt, "the zombie case must be fully handled before the binding decision"); assert.ok( zombieAt < bindingAt, "the zombie branch must run before the binding check — a missing row has no binding to compare against" ); assert.ok( bindingAt < suspendedAt, "the binding check must run before the suspension gate — a stale session must never learn the recreated account's suspension state" ); assert.ok(respondTrueAt > bindingAt, "the respond:true teardown belongs to the binding branch"); assert.ok(respondTrueAt < suspendedAt, "the binding teardown must complete before the suspension gate is reached"); }); test("the Socket.IO handshake enforces the binding and revoked-sid checks after the user lookup", () => { const start = indexSource.indexOf("io.use((socket, next) => {"); assert.notStrictEqual(start, -1, "expected the socket handshake middleware"); const end = indexSource.indexOf("const consoleHistory = new Map();", start); const region = indexSource.slice(start, end > start ? end : start + 8000); const lookupAt = region.indexOf("findUserByEmail(email)"); const bindingAt = region.indexOf("sessionAccountBindingMismatch(socket.request.session, user)"); const revokedAt = region.indexOf('isSessionSidRevoked(String(socket.request.sessionID || "")'); assert.notStrictEqual(lookupAt, -1, "expected the user lookup in the handshake"); assert.notStrictEqual(bindingAt, -1, "the handshake must enforce the shared account binding"); assert.notStrictEqual(revokedAt, -1, "the handshake must reject revoked sids"); assert.ok(bindingAt > lookupAt, "the binding check must come after the user lookup"); assert.ok(revokedAt > bindingAt, "the revoked-sid check must come after the binding check"); assert.ok( region.includes('{ failClosed: false }'), "the socket-side revoked-sid check must fail open on store errors (no response object to tear down with)" ); assert.ok( !region.includes("req.session.destroy(") && !region.includes(".destroy("), "the socket path must not destroy sessions — teardown belongs to the next HTTP request" ); }); test("the old inline binding constants are gone from index.js", () => { assert.ok( !indexSource.includes("const accountIdMismatch"), "the inline id-mismatch constant must not come back — the shared predicate is the one home" ); assert.ok( !indexSource.includes("const accountEpochMismatch"), "the inline epoch-mismatch constant must not come back — the shared predicate is the one home" ); }); test("the predicate is defined exactly once, in the shared module", () => { const moduleSrc = fs.readFileSync(path.join(ROOT, "utils", "session-account-binding.js"), "utf8"); assert.match(moduleSrc, /function sessionAccountBindingMismatch\(session, user\)/, "expected the shared predicate definition"); const inIndex = (indexSource.match(/function sessionAccountBindingMismatch\(/g) || []).length; assert.strictEqual(inIndex, 0, "index.js must not carry its own copy of the predicate"); });