Files
Kelx/tests/logout-ssh-immediate-revoke.test.js
2026-11-30 16:32:37 +00:00

95 lines
5.4 KiB
JavaScript

"use strict";
// adpanel:logout-ssh-immediate-revoke — source pins for the logout SSH
// teardown block. Reads index.js as text, like the other audit tests.
//
// Load-bearing ordering: POST /logout must tear down the user's live SSH
// terminal tunnels and evict any cached minted terminal token BETWEEN the
// awaited db-proxy token revocation and the session-destroy choreography —
// the tunnels are authenticated pivots into the internal network, and their
// revocation must not wait for the 60s credential sweep. A future refactor
// that moves or silently drops the block degrades logout revocation to that
// sweep window; this pin exists so the drop cannot happen quietly.
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 indexOfOrThrow(needle, from = 0) {
const idx = indexSource.indexOf(needle, from);
assert.ok(idx >= 0, `expected source pin not found: ${needle}`);
return idx;
}
function countOccurrencesIn(haystack, needle) {
let count = 0;
let pos = 0;
for (;;) {
const idx = haystack.indexOf(needle, pos);
if (idx < 0) return count;
count += 1;
pos = idx + needle.length;
}
}
function logoutHandlerRegion() {
// adpanel:logout-reliability — the route now mounts its dedicated limiter
// first (app.post("/logout", logoutRateLimiter, async ...)); the anchor
// tracks the mounted chain while the pinned intent (the handler region's
// revocation choreography, bounded by the next route registration) is
// byte-identical.
const start = indexOfOrThrow('app.post("/logout", logoutRateLimiter, async (req, res) => {');
const end = indexSource.indexOf('app.get("/api/settings/security"', start);
assert.ok(end > start, "the /logout handler region must be bounded by the next route registration");
return { start, end, body: indexSource.slice(start, end) };
}
test("the adpanel:logout-ssh-immediate-revoke tag comment and teardown block exist inside POST /logout", () => {
const { body } = logoutHandlerRegion();
const tagIdx = body.indexOf("// adpanel:logout-ssh-immediate-revoke —");
assert.ok(tagIdx >= 0, "the adpanel:logout-ssh-immediate-revoke tag comment must document the block inside the handler");
const blockIdx = body.indexOf("if (logoutUser) {", tagIdx);
assert.ok(blockIdx > tagIdx, "the teardown block (guarded by logoutUser) must follow its tag comment");
const blockBody = body.slice(blockIdx, body.indexOf("req.session.destroy(", blockIdx));
assert.ok(blockBody.includes("destroyLiveSshTerminalSockets(logoutUser)"), "the block must drop this user's live SSH terminal tunnels");
assert.ok(blockBody.includes("evictSshTerminalTokenCacheForUser(logoutUser)"), "the block must evict this user's cached terminal tokens");
});
test("the SSH teardown runs AFTER the db-proxy revocation and BEFORE the session destroy", () => {
const { body, start } = logoutHandlerRegion();
const revokeIdx = body.indexOf("await dbProxy.revokeAllForUser(logoutUser);");
assert.ok(revokeIdx >= 0, "the awaited db-proxy revocation must exist in the handler");
const sshCallIdx = body.indexOf("destroyLiveSshTerminalSockets(logoutUser)");
assert.ok(sshCallIdx >= 0, "the tunnel teardown call must exist in the handler");
assert.ok(sshCallIdx > revokeIdx, `the SSH teardown (${start + sshCallIdx}) must follow the db-proxy immediate revocation (${start + revokeIdx})`);
const destroyIdx = body.indexOf("req.session.destroy(");
assert.ok(destroyIdx > sshCallIdx, `the SSH teardown (${start + sshCallIdx}) must precede req.session.destroy (${start + destroyIdx}) — revocation while the session record is still readable`);
// The token-cache eviction keeps the same revoke→destroy window beside the
// tunnel teardown (a minted-but-unused token must not outlive logout either).
const evictIdx = body.indexOf("evictSshTerminalTokenCacheForUser(logoutUser)");
assert.ok(evictIdx > revokeIdx && evictIdx < destroyIdx, "the token-cache eviction sits in the same revoke→destroy window");
});
test("the teardown helpers are the ONE shared implementations, called exactly once per logout", () => {
const { body } = logoutHandlerRegion();
assert.strictEqual(countOccurrencesIn(body, "destroyLiveSshTerminalSockets(logoutUser)"), 1, "exactly one tunnel teardown call site in POST /logout");
assert.strictEqual(countOccurrencesIn(body, "evictSshTerminalTokenCacheForUser(logoutUser)"), 1, "exactly one token eviction call site in POST /logout");
// The helpers must still be defined in index.js (the account-delete path
// shares them — no parallel copies).
indexOfOrThrow("function destroyLiveSshTerminalSockets(");
indexOfOrThrow("function evictSshTerminalTokenCacheForUser(");
});
test("the teardown is failure-tolerant: logout completes even if the sweep throws", () => {
const { body } = logoutHandlerRegion();
const blockIdx = body.indexOf("// adpanel:logout-ssh-immediate-revoke —");
const blockBody = body.slice(blockIdx, body.indexOf("req.session.destroy(", blockIdx));
assert.ok(blockBody.includes("try {"), "the teardown block is wrapped in try/catch (a sweep failure must not wedge logout)");
assert.ok(blockBody.includes('console.warn("[security] Failed to tear down live SSH terminal tunnels on logout:"'), "a teardown failure is logged loudly and logout still completes");
});