"use strict"; // Tests for the URL log-sanitizer. Contract under test: // - credential-bearing query parameters (the ssh-terminal ?token= is the // concrete case) never survive into a URL destined for persistent logs; // - everything else — path, parameter order, encoding, non-sensitive // values — is preserved byte-for-byte; // - hostile input (non-strings, malformed escapes, "=" inside values) can // never crash a logging path. const test = require("node:test"); const assert = require("node:assert"); const { sanitizeUrlForLogging, SENSITIVE_QUERY_KEYS } = require("./url-sanitize"); test("redacts the ssh-terminal token in place", () => { assert.strictEqual( sanitizeUrlForLogging("/ssh-terminal/?token=ABC.123.def"), "/ssh-terminal/?token=[REDACTED]" ); }); test("redacts only sensitive parameters, preserving order and neighbors", () => { assert.strictEqual( sanitizeUrlForLogging("/x?a=1&token=s&b=2"), "/x?a=1&token=[REDACTED]&b=2" ); }); test("redacts every occurrence of a repeated sensitive parameter", () => { assert.strictEqual( sanitizeUrlForLogging("/x?token=a&token=b"), "/x?token=[REDACTED]&token=[REDACTED]" ); }); test("matching is case-insensitive on the parameter name", () => { assert.strictEqual(sanitizeUrlForLogging("/x?Token=x"), "/x?Token=[REDACTED]"); assert.strictEqual(sanitizeUrlForLogging("/x?PASSWORD=hunter2"), "/x?PASSWORD=[REDACTED]"); }); test("covers the whole denylist, not just token", () => { for (const key of SENSITIVE_QUERY_KEYS) { assert.strictEqual( sanitizeUrlForLogging(`/x?${key}=v`), `/x?${key}=[REDACTED]`, `expected "${key}" to be redacted` ); } }); test("a URL with no query string is returned as-is", () => { assert.strictEqual(sanitizeUrlForLogging("/ssh-terminal/"), "/ssh-terminal/"); assert.strictEqual(sanitizeUrlForLogging("/a/b/c"), "/a/b/c"); }); test("empty and non-string input passes through unchanged", () => { assert.strictEqual(sanitizeUrlForLogging(""), ""); assert.strictEqual(sanitizeUrlForLogging(123), 123); assert.strictEqual(sanitizeUrlForLogging(null), null); assert.strictEqual(sanitizeUrlForLogging(undefined), undefined); }); test("percent-encoded sensitive names are still recognized", () => { // %74oken === "token" assert.strictEqual(sanitizeUrlForLogging("/x?%74oken=x"), "/x?%74oken=[REDACTED]"); }); test("values containing '=' are split on the first '=' only", () => { assert.strictEqual( sanitizeUrlForLogging("/x?token=abc=def"), "/x?token=[REDACTED]" ); // Non-sensitive value with '=' inside passes through untouched. assert.strictEqual(sanitizeUrlForLogging("/x?a=b=c"), "/x?a=b=c"); }); test("malformed percent-escapes in a name do not throw", () => { // %C3%28 is an invalid UTF-8 sequence; decodeURIComponent throws on it. const url = "/x?token%C3%28=x&a=1"; assert.doesNotThrow(() => sanitizeUrlForLogging(url)); assert.strictEqual(sanitizeUrlForLogging(url), "/x?token%C3%28=x&a=1"); // The raw-name fallback can only fire when the raw lowercase name itself is // a denylist entry — impossible for a "%"-bearing name, since no denylist // key contains "%". So a malformed escape that defeats decoding passes the // pair through untouched (which matches what the query parser on the // server sees: the same undecoded name). assert.strictEqual(sanitizeUrlForLogging("/x?token%ZZ=x"), "/x?token%ZZ=x"); }); test("benign parameters are never redacted", () => { assert.strictEqual(sanitizeUrlForLogging("/x?code=abc&state=def"), "/x?code=abc&state=def"); assert.strictEqual(sanitizeUrlForLogging("/x?page=2&sort=country_code"), "/x?page=2&sort=country_code"); }); test("encoding of untouched parts is preserved byte-for-byte", () => { const url = "/x?a%20b=1%2F2&c=3"; assert.strictEqual(sanitizeUrlForLogging(url), url); }); test("a bare valueless sensitive flag is redacted whole", () => { assert.strictEqual(sanitizeUrlForLogging("/x?token"), "/x?[REDACTED]"); assert.strictEqual(sanitizeUrlForLogging("/x?debug&token&b=2"), "/x?debug&[REDACTED]&b=2"); assert.strictEqual(sanitizeUrlForLogging("/x?debug"), "/x?debug"); }); test("mixed-case escapes and letters in a name are still recognized", () => { // %54 === "T"; decodeURIComponent accepts both hex cases. assert.strictEqual(sanitizeUrlForLogging("/x?%54oKeN=x"), "/x?%54oKeN=[REDACTED]"); assert.strictEqual(sanitizeUrlForLogging("/x?SeSsIoNiD=x"), "/x?SeSsIoNiD=[REDACTED]"); }); test("a raw '?' inside a value does not hide a later sensitive name", () => { assert.strictEqual( sanitizeUrlForLogging("/x?next=/y?a=b&token=s"), "/x?next=/y?a=b&token=[REDACTED]" ); }); test("an encoded '&' stays inside the value and does not split the pair", () => { assert.strictEqual( sanitizeUrlForLogging("/x?token=a%26b&x=1"), "/x?token=[REDACTED]&x=1" ); }); test("fragments are dropped, never logged", () => { // A fragment after a sensitive value is swallowed by the redaction anyway. assert.strictEqual(sanitizeUrlForLogging("/x?token=s#frag"), "/x?token=[REDACTED]"); // A sensitive name buried after a "#" must not slip past query parsing. assert.strictEqual(sanitizeUrlForLogging("/x?a=1#b?token=s"), "/x?a=1"); // Client-side URLs put credentials in the fragment (this panel's #sid=..., // OAuth implicit-flow tokens) — they must not survive into a log line. assert.strictEqual(sanitizeUrlForLogging("/x#access_token=abc"), "/x"); assert.strictEqual(sanitizeUrlForLogging("/x?a=1#keep"), "/x?a=1"); }); test("regression: a full-length ssh-terminal token leaves no trace in the output", () => { const secret = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"; assert.strictEqual(secret.length, 64); const out = sanitizeUrlForLogging(`/ssh-terminal/?token=${secret}`); assert.strictEqual(out, "/ssh-terminal/?token=[REDACTED]"); assert.ok(!out.includes(secret), "sanitized URL must not contain the token value"); });