"use strict"; // adpanel:stealth-returnto-backslash — behavior + source pins for the // sanitizeStealthReturnTo backslash bypass. // // Browsers normalize a leading "\" into a second "/", so a returnTo of // "/\evil.com" reached res.redirect(303, ...) as a protocol-relative URL and // left origin — exactly the class the existing "//" check exists to block. // These pins hold: // 1. "/\host" falls back to "/login" (the bypass is closed); // 2. the classic "//host" and "/_stealth/..." rejections still hold; // 3. in-app paths and the non-absolute fallback are unchanged; // 4. the guard lives in the ONE sanitizer all four call sites share. 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"); // Extraction pattern: bound the slice by the next top-level declaration and // evaluate it. sanitizeStealthReturnTo is pure — no sandbox deps needed. function loadSanitizeStealthReturnTo() { const start = indexSource.indexOf("function sanitizeStealthReturnTo("); const end = indexSource.indexOf("function getStealthGateCookieMaxAgeMs(", start); assert.ok(start >= 0 && end > start, "sanitizeStealthReturnTo slice must be bounded"); const fnSrc = indexSource.slice(start, end); return new Function(`${fnSrc}\nreturn sanitizeStealthReturnTo;`)(); } test("backslash bypass closed: /\\host falls back to /login", () => { const sanitize = loadSanitizeStealthReturnTo(); assert.strictEqual(sanitize("/\\evil.com"), "/login"); }); test("existing rejections still hold: //host and /_stealth/ are refused", () => { const sanitize = loadSanitizeStealthReturnTo(); assert.strictEqual(sanitize("//evil.com"), "/login"); assert.strictEqual(sanitize("/_stealth/x"), "/login"); }); test("in-app paths and the non-absolute fallback are unchanged", () => { const sanitize = loadSanitizeStealthReturnTo(); assert.strictEqual(sanitize("/dashboard"), "/dashboard"); assert.strictEqual(sanitize("dashboard"), "/login"); assert.strictEqual(sanitize(""), "/login"); assert.strictEqual(sanitize(null), "/login"); }); test("pin: the shared sanitizer source carries the backslash guard", () => { const start = indexSource.indexOf("function sanitizeStealthReturnTo("); const end = indexSource.indexOf("function getStealthGateCookieMaxAgeMs(", start); assert.ok(start >= 0 && end > start, "sanitizeStealthReturnTo slice must be bounded"); const fnSrc = indexSource.slice(start, end); // String.raw so the pin matches the file's literal two-backslash source text. assert.ok( fnSrc.includes(String.raw`raw.startsWith("/\\")`), 'sanitizeStealthReturnTo must reject a leading backslash alongside "//"' ); });