"use strict"; // adpanel:local-captcha-retired — pins the retirement of the local grid // captcha machinery (2026-11 cleanup). The login surface's ONLY captcha flow // is the external provider (reCAPTCHA/hCaptcha/Turnstile via // /api/captcha/verify + verifyExternalCaptcha + the hostname pin); the // retired local-grid identifiers (showLocalCaptcha / captchaQuestion / // shouldShowLocal / localCaptcha / clearLocalCaptcha / // lastExternalCaptchaToken) must not come back — not in the challenge // builder's shape, not in the wire contract, not as session keys. // // The zero-reference pins judge REAL CODE: line comments are stripped first, // because the adpanel:local-captcha-retired note at // buildPendingLoginCaptchaChallenge names the removed identifiers on purpose // (auditor greppability — the same convention tests/login-dead-assets.test.js // uses for the removed jsDelivr host). 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 THIS_FILE = path.join(__dirname, "local-captcha-retired.test.js"); const RETIRED_IDENTIFIERS = [ "showLocalCaptcha", "captchaQuestion", "shouldShowLocal", "clearLocalCaptcha", "localCaptcha", "lastExternalCaptchaToken", ]; // First-party source, repo-wide. Vendored trees (node_modules, public/vendor, // grommunio-web) are third-party and were never part of the local-captcha // surface. function collectFirstPartySources() { const files = []; const pushJs = (dir) => { for (const name of fs.readdirSync(dir)) { const full = path.join(dir, name); const stat = fs.statSync(full); if (stat.isFile() && name.endsWith(".js")) files.push(full); } }; for (const name of ["index.js", "nodes.js", "db.js"]) { const full = path.join(ROOT, name); if (fs.existsSync(full)) files.push(full); } for (const dir of ["routes", "services", "utils", "public/js"]) { const full = path.join(ROOT, dir); if (fs.existsSync(full)) pushJs(full); } for (const name of fs.readdirSync(path.join(ROOT, "tests"))) { if (name.endsWith(".js") && name !== "local-captcha-retired.test.js") { files.push(path.join(ROOT, "tests", name)); } } for (const name of fs.readdirSync(path.join(ROOT, "views"))) { if (name.endsWith(".ejs")) files.push(path.join(ROOT, "views", name)); } return files; } // Strip line comments (line-anchored, so "https://" URLs survive) and EJS // comment blocks. Template strings whose lines happen to start with "//" are // not expected to carry the identifiers (the repo's dead-assets precedent // accepts the same trade). function stripComments(source, isEjs) { let out = source; if (isEjs) out = out.replace(/<%#[\s\S]*?%>/g, ""); return out.replace(/^[ \t]*\/\/.*$/gm, ""); } function scanRetiredIdentifiers() { const hits = []; for (const file of collectFirstPartySources()) { const isEjs = file.endsWith(".ejs"); const code = stripComments(fs.readFileSync(file, "utf8"), isEjs); for (const identifier of RETIRED_IDENTIFIERS) { if (code.includes(identifier)) { hits.push(`${path.relative(ROOT, file)}: ${identifier}`); } } } return hits; } const indexSource = fs.readFileSync(path.join(ROOT, "index.js"), "utf8"); const loginEjs = fs.readFileSync(path.join(ROOT, "views", "login.ejs"), "utf8"); // Extract a balanced `{ ... }` object literal after an anchor (string-aware), // mirroring tests/login-wire-contract.test.js's helper. function extractObjectLiteralAfter(source, anchor, label) { const at = source.indexOf(anchor); assert.notStrictEqual(at, -1, `anchor not found: ${label}`); let depth = 0; let inStr = null; for (let i = at; i < source.length; i++) { const ch = source[i]; if (inStr) { if (ch === "\\") { i++; continue; } if (ch === inStr) inStr = null; continue; } if (ch === "'" || ch === '"' || ch === "`") { inStr = ch; continue; } if (ch === "{") depth++; else if (ch === "}") { depth--; if (depth === 0) return source.slice(at, i + 1); } } assert.fail(`unbalanced object literal after anchor: ${label}`); } // --------------------------------------------------------------------------- // The retirement pins // --------------------------------------------------------------------------- test("no retired local-captcha identifier survives anywhere in first-party source", () => { const hits = scanRetiredIdentifiers(); assert.deepStrictEqual( hits, [], `retired local-captcha identifiers reappeared: ${hits.join("; ")}` ); }); test("the challenge builder carries only the LIVE external-captcha fields", () => { const fnStart = indexSource.indexOf("function buildPendingLoginCaptchaChallenge(req) {"); assert.notStrictEqual(fnStart, -1, "buildPendingLoginCaptchaChallenge must exist"); const returnStart = indexSource.indexOf("return {", fnStart); const builder = extractObjectLiteralAfter(indexSource.slice(returnStart), "return {", "builder return"); for (const live of ["captchaRequired", "useExternalCaptcha", "externalCaptchaAutoStart"]) { assert.ok(new RegExp(`\\b${live}\\b`).test(builder), `the challenge builder must keep producing "${live}"`); } // The retirement note sits at the builder site and names the constraint. const before = indexSource.slice(Math.max(0, fnStart - 600), fnStart); assert.ok( before.includes("// adpanel:local-captcha-retired —"), "the adpanel:local-captcha-retired note must document the constraint at the builder site" ); }); test("the wire contract carries no local-captcha field on either login builder", () => { const sendErrorAt = indexSource.indexOf("const sendError = (status, message, options = {}) => {"); assert.notStrictEqual(sendErrorAt, -1); const sendErrorBody = indexSource.slice(sendErrorAt, indexSource.indexOf("const sendOk = () => {", sendErrorAt)); const payloadAt = indexSource.indexOf("const jsonPayload = {", sendErrorAt); assert.notStrictEqual(payloadAt, -1, "sendCaptchaChallenge's jsonPayload must exist"); const payload = extractObjectLiteralAfter(indexSource.slice(payloadAt), "const jsonPayload = {", "jsonPayload"); for (const [label, body] of [["sendError JSON", sendErrorBody], ["sendCaptchaChallenge JSON", payload]]) { for (const live of ["captchaRequired", "useExternalCaptcha"]) { assert.ok(new RegExp(`\\b${live}\\b`).test(body), `${label} must still carry "${live}"`); } for (const retired of ["showLocalCaptcha", "shouldShowLocal", "captchaQuestion"]) { assert.ok(!body.includes(retired), `${label} must not carry the retired "${retired}"`); } } assert.ok(!loginEjs.includes("showLocalCaptcha"), "login.ejs must not read a local-captcha field"); }); test("the LIVE external-captcha flow stays intact", () => { // The verify route + provider verification + its limiter. assert.ok( indexSource.includes('app.post("/api/captcha/verify", captchaVerifyRateLimiter,'), "the /api/captcha/verify route must stay mounted with its limiter" ); assert.ok( indexSource.includes("verifyExternalCaptcha(token, clientIp)"), "the provider verification call must stay" ); assert.ok( (indexSource.match(/captchaHostnameGuard\.check\(/g) || []).length >= 3, "the hostname pin guard must stay on every provider branch" ); // The pending-login machinery (session+IP+UA fingerprint binding + TTL). assert.ok( indexSource.includes("req.session.pendingCaptchaLogin = {") && indexSource.includes("requestFingerprint: getPendingCaptchaLoginRequestFingerprint(req),") && indexSource.includes("expiresAt: now + PENDING_CAPTCHA_LOGIN_TTL_MS,"), "beginPendingCaptchaLogin must keep binding the pending login to the request fingerprint and TTL" ); assert.ok( indexSource.includes("req.session.captchaRequired = true;") && indexSource.includes("req.session.captchaSolved = false;"), "the solved/required flags of the external flow must stay" ); // The arming latch (shared with the external flow). assert.ok( indexSource.includes("function flagLoginCaptchaRequired(req) {") && indexSource.includes("sess.loginCaptchaRequired = true;") && indexSource.includes("function clearAllLoginCaptchaArming() {"), "the arming latch and its settings-toggle sweep must stay" ); // The invariant: the only captcha is the external one. assert.strictEqual( (indexSource.match(/ANY_CAPTCHA_ENABLED = EXTERNAL_CAPTCHA_ENABLED;/g) || []).length, 1, "ANY_CAPTCHA_ENABLED === EXTERNAL_CAPTCHA_ENABLED must remain the single invariant assignment" ); // The challenge render path still routes on the external fields. assert.ok( loginEjs.includes("data.captchaRequired") && loginEjs.includes("data.useExternalCaptcha"), "the client must keep routing on captchaRequired/useExternalCaptcha" ); });