"use strict"; // adpanel:preauth-session-ttl — pins the short-lifetime behavior for // unauthenticated sessions. Every cookieless GET /login writes a session // (login CSRF token, bot-proof seed, captcha state); this middleware caps // that entry's lifetime instead of letting it inherit the 365-day // authenticated maxAge. Uses the real express-session Cookie class so the // maxAge/originalMaxAge coupling exercised here is the production one // (setting maxAge routes through the expires setter, which refreshes // originalMaxAge — what connect-redis and session-file-store read). const test = require("node:test"); const assert = require("node:assert"); const fs = require("node:fs"); const path = require("node:path"); const Cookie = require("express-session/session/cookie"); const { DEFAULT_PREAUTH_SESSION_MAX_AGE_MS, resolvePreauthSessionMaxAgeMs, createPreauthSessionTtlMiddleware, } = require("./preauth-session"); const AUTHED_MAX_AGE_MS = 365 * 24 * 60 * 60 * 1000; const MIN_MS = 5 * 60 * 1000; const MAX_MS = 24 * 60 * 60 * 1000; // Reads of the maxAge getter are live (expires - now), so compare loosely. const SLOP_MS = 2000; function makeSessionCookie() { return new Cookie({ maxAge: AUTHED_MAX_AGE_MS, sameSite: "lax", secure: "auto", httpOnly: true, }); } function runMiddleware(req, middleware = createPreauthSessionTtlMiddleware({ maxAgeMs: DEFAULT_PREAUTH_SESSION_MAX_AGE_MS })) { let nextCalled = false; middleware(req, {}, () => { nextCalled = true; }); assert.ok(nextCalled, "middleware must always call next()"); } test("anonymous session is capped to the 30-minute preauth lifetime (maxAge and originalMaxAge)", () => { const req = { session: { user: null, cookie: makeSessionCookie() } }; runMiddleware(req); assert.ok( Math.abs(req.session.cookie.maxAge - DEFAULT_PREAUTH_SESSION_MAX_AGE_MS) < SLOP_MS, `anonymous maxAge should be ~${DEFAULT_PREAUTH_SESSION_MAX_AGE_MS}, got ${req.session.cookie.maxAge}` ); assert.ok( Math.abs(req.session.cookie.originalMaxAge - DEFAULT_PREAUTH_SESSION_MAX_AGE_MS) < SLOP_MS, "originalMaxAge (read by session-file-store) must follow the short maxAge" ); }); test("anonymous session without a pre-existing maxAge is still capped", () => { // Sessions created before any store write may carry a cookie whose maxAge // was never materialized; the cap must apply to them too. const req = { session: { user: null, cookie: new Cookie({ httpOnly: true }) } }; runMiddleware(req); assert.ok(Math.abs(req.session.cookie.maxAge - DEFAULT_PREAUTH_SESSION_MAX_AGE_MS) < SLOP_MS); }); test("authenticated session is left untouched at the 365-day lifetime", () => { const req = { session: { user: "admin@example.com", cookie: makeSessionCookie() } }; runMiddleware(req); assert.ok( req.session.cookie.maxAge > AUTHED_MAX_AGE_MS - SLOP_MS, `authenticated session must keep ~365 days, got ${req.session.cookie.maxAge}` ); assert.strictEqual(req.session.cookie.originalMaxAge, AUTHED_MAX_AGE_MS); }); test("missing req.session and cookie-less session do not throw", () => { runMiddleware({}); runMiddleware({ session: null }); runMiddleware({ session: { user: null, cookie: null } }); }); test("environment override is honored, garbage falls back to the default", () => { assert.strictEqual( resolvePreauthSessionMaxAgeMs({ PREAUTH_SESSION_MAX_AGE_MS: "2700000" }), 45 * 60 * 1000, "an in-range value (milliseconds) must be honored verbatim" ); assert.strictEqual(resolvePreauthSessionMaxAgeMs({}), DEFAULT_PREAUTH_SESSION_MAX_AGE_MS); assert.strictEqual(resolvePreauthSessionMaxAgeMs({ PREAUTH_SESSION_MAX_AGE_MS: "" }), DEFAULT_PREAUTH_SESSION_MAX_AGE_MS); assert.strictEqual(resolvePreauthSessionMaxAgeMs({ PREAUTH_SESSION_MAX_AGE_MS: "not-a-number" }), DEFAULT_PREAUTH_SESSION_MAX_AGE_MS); }); test("environment override is clamped to 5 minutes through 24 hours", () => { assert.strictEqual(resolvePreauthSessionMaxAgeMs({ PREAUTH_SESSION_MAX_AGE_MS: "0" }), MIN_MS); assert.strictEqual(resolvePreauthSessionMaxAgeMs({ PREAUTH_SESSION_MAX_AGE_MS: "999" }), MIN_MS); assert.strictEqual( resolvePreauthSessionMaxAgeMs({ PREAUTH_SESSION_MAX_AGE_MS: String(100 * 24 * 60 * 60 * 1000) }), MAX_MS ); // The clamp must never produce a lifetime the 370-day tombstone cannot // outlive (24h << 365d). assert.ok(MAX_MS < AUTHED_MAX_AGE_MS); }); // adpanel:env-int-clamp — the resolver must delegate to the ONE shared // clamped-int helper (floor policy): a private parseInt copy here is exactly // the drift that made BCRYPT_ROUNDS="0" and PREAUTH_SESSION_MAX_AGE_MS="0" // behave differently across the campaign modules. test("the resolver delegates to the shared clamped-int helper (no private env-parse copy)", () => { const src = fs.readFileSync(path.join(__dirname, "preauth-session.js"), "utf8"); assert.ok(src.includes("resolveClampedInt("), "the resolver must consume utils/clamped-int.js"); assert.ok(!src.includes("parseInt(String("), "no private parseInt resolver copy may remain"); });