"use strict"; // adpanel:port-mismatch-advisor — diagnosable rejection for the one-sided // brick shape of the explicit-port CSRF rule (adpanel:csrf-port-awareness, // utils/origin-host.js csrfPortMismatch — the ONE shared decision). The rule // rejects a deployment shape it cannot distinguish from the sibling-port // ride: the browser sends an explicit non-default port in the Origin while // the serving Host header carries NO port evidence (panel on a nondefault // public port behind a Host-rewriting proxy that forwards neither a // port-carrying trusted X-Forwarded-Host nor has PANEL_PUBLIC_URL set to the // exact public URL). The rejection itself is correct — the rule must not // soften — but the brick used to be SILENT (bare 403 "CSRF blocked" / socket // "origin-not-allowed"). The trust surfaces call advice() with the values the // rejection actually saw and log the returned string through their own // securityLog; this module owns BOTH the fire condition (callers must not // re-derive it) and the throttle. It changes no rejection semantics and no // wire body — it only makes the rejection loud. // // Fire condition — ONLY the brick shape: parseOriginHostPort(originHeader) // yields an explicit non-default port AND servingPort is null. Null // otherwise: both-explicit (equal or different — a different-explicit pair is // a plain misroute, not this brick), serving-explicit, portless origin, and // unparseable origins carry no diagnosis to give. The origin header is // attacker-controlled, so the throttle mirrors _logCsrfExtraAllowedUse // (index.js): at most one emission per host:port key per window, a global // budget of budgetMax emissions per window, and the key Map capped at mapMax // with clear-on-overflow — all call-driven, no timers. // // DI posture: factory-injected clock and tunables only; the sole repo- // internal binding is the shared parse primitive parseOriginHostPort from // utils/origin-host.js (never a second copy — see the header note on that // module). No require of index.js, db.js or any runtime surface. const { parseOriginHostPort } = require("./origin-host"); const DEFAULT_INTERVAL_MS = 60 * 60 * 1000; const DEFAULT_BUDGET_MAX = 20; const DEFAULT_MAP_MAX = 50; function createPortMismatchAdvisor({ now = Date.now, intervalMs = DEFAULT_INTERVAL_MS, budgetMax = DEFAULT_BUDGET_MAX, mapMax = DEFAULT_MAP_MAX, } = {}) { const lastAdvisedAt = new Map(); let budgetUsed = 0; let budgetResetAt = 0; // → the diagnosis string, or null when the shape is not the brick or the // throttle (per-key or global budget) withholds the emission. function advice({ originHeader, servingPort } = {}) { const pair = parseOriginHostPort(originHeader); if (!pair || pair.port == null) return null; if (servingPort != null) return null; const key = `${pair.host}:${pair.port}`; const ts = now(); if (ts >= budgetResetAt) { budgetResetAt = ts + intervalMs; budgetUsed = 0; } if (budgetUsed >= budgetMax) return null; const last = lastAdvisedAt.get(key) || 0; if (ts - last < intervalMs) return null; // The key embeds attacker-controllable header text; cap the map so it // cannot grow unbounded (the same clear-on-overflow posture as the CSRF // allowlist audit trail this throttle mirrors). if (lastAdvisedAt.size >= mapMax) lastAdvisedAt.clear(); lastAdvisedAt.set(key, ts); budgetUsed++; // The FULL message must survive securityLog's 200-char msg slice // (index.js truncates BEFORE appending " | IP | Path"), so the // attacker-controlled host:port echo is capped and the static text is // budgeted: worst case 8 + 25 + 2 + 164 = 199 chars — diagnosis and BOTH // remedies always land intact. The cap keeps the old slice(0, 24) UTF-16 // bound but cuts in whole CODE POINTS (for..of iterates code points): a // bare UTF-16 slice can split a surrogate pair — a lone \uD83D in the // log line, reachable via the schemeless-fallback path of // parseOriginHostPort whose host is not URL-normalized — and a bare // 24-code-point cap alone could GROW the echo (an astral code point is // 2 UTF-16 units, so 24 astral code points are 49 units with the // ellipsis), pushing the worst case past the 200-unit slice and losing // BOTH remedies. Pure-ASCII input caps at exactly the same 24 chars the // old slice(0, 24) produced. const echoed = `${pair.host}:${pair.port}`; let shown = echoed; if (echoed.length > 24) { shown = ""; for (const ch of echoed) { if (shown.length + ch.length > 24) break; shown += ch; } shown += "…"; } return `Origin "${shown}" rejected: explicit port, serving Host none — CSRF. Fix: set PANEL_PUBLIC_URL to the exact browser scheme+host+port, or proxy_set_header X-Forwarded-Host $http_host;`; } return { advice }; } module.exports = { createPortMismatchAdvisor };