From 5fcef587e0db9452900fb8b2ca43891c82f0a4ab Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 23 Oct 2026 17:47:12 +0800 Subject: [PATCH 1/2] fix(log): mask userinfo in non-http scheme URLs in logs/diagnostics/publish (#4191) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit maskUrlsInText / maskUrlsForPublish / maskUrlsForDiagnostics all shared one entry regex that only matched https?://, so socks5://user:pass@host (self-relay forwarding addresses), ws/wss, ftp etc. carried their inline credentials verbatim into logs, diagnostic blobs and public publish text. The single-URL maskers already strip userinfo/query/hash via new URL for every scheme; only the entry find-regex was too narrow. Shared const EMBEDDED_URL_RE now matches any RFC-3986 scheme://… form in all three fns. No leading word-boundary on purpose: a word-char-glued URL (xhttps://…) must stay masked exactly as before - over-matching is safe, under-matching leaks. 3 regression tests added. Behavior change: non-http scheme URLs are now host-masked where they were previously passed through. --- src/log-mask.ts | 33 ++++++++++++++++++++++----------- tests/log-mask.test.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/log-mask.ts b/src/log-mask.ts index 4792109b1f..0dbd1c184b 100644 --- a/src/log-mask.ts +++ b/src/log-mask.ts @@ -105,10 +105,20 @@ export function maskUrlForLog(url: string): string { return `${u.protocol}//${host}${u.pathname}`; } -/** Mask every http(s) URL embedded anywhere in an arbitrary string (request - * paths like /bili/http://relay.internal/v1/..., error text). */ +// #4191: find ANY scheme://… URL embedded in free text, not just http(s). +// maskUrlForLog already strips userinfo/query/hash via new URL for every +// scheme, but the entry regex had to actually LET non-http schemes through — +// socks5://user:pass@host (self-relay forwarding addresses), ws/wss, ftp etc. +// carried their inline credentials verbatim into logs / diagnostics / publish +// blobs. No leading \b on purpose: a word-char-glued URL (xhttps://…) must +// stay masked exactly as before; over-matching is safe, under-matching leaks. +const EMBEDDED_URL_RE = /[a-z][a-z0-9+.-]*:\/\/[^\s"'<>]+/gi; + +/** Mask every scheme://… URL embedded anywhere in an arbitrary string + * (request paths like /bili/http://relay.internal/v1/..., error text, + * socks5://user:pass@host forwarding addresses — #4191). */ export function maskUrlsInText(text: string): string { - return text.replace(/https?:\/\/[^\s"'<>]+/gi, (m) => maskUrlForLog(m)); + return text.replace(EMBEDDED_URL_RE, (m) => maskUrlForLog(m)); } /** #3811: publish-path URL masking for artifacts that land in a PUBLIC GitHub @@ -129,12 +139,12 @@ function maskUrlForPublish(url: string): string { return `${u.protocol}//${host}${u.pathname}`; } -/** #3811: mask the host of every http(s) URL embedded in an arbitrary string, - * unconditionally (see maskUrlForPublish). Catches URLs wherever they appear - * in serialized text — object values AND object keys (the providers table - * uses upstream URLs as keys). */ +/** #3811: mask the host of every scheme://… URL embedded in an arbitrary string, + * unconditionally (see maskUrlForPublish; any scheme since #4191). Catches URLs + * wherever they appear in serialized text — object values AND object keys (the + * providers table uses upstream URLs as keys). */ export function maskUrlsForPublish(text: string): string { - return text.replace(/https?:\/\/[^\s"'<>]+/gi, (m) => maskUrlForPublish(m)); + return text.replace(EMBEDDED_URL_RE, (m) => maskUrlForPublish(m)); } /** Mask a URL for DIAGNOSTIC payloads (#3651): same public/private rule as @@ -160,10 +170,11 @@ export function maskUrlForDiagnostics(url: string): string { } /** Text-level twin of maskUrlForDiagnostics (#3651): masks every embedded - * http(s) URL with the loopback-exempt rule; unparseable fragments pass - * through (the IP scrubber neutralizes bare IP literals separately). */ + * scheme://… URL with the loopback-exempt rule (any scheme since #4191); + * unparseable fragments pass through (the IP scrubber neutralizes bare IP + * literals separately). */ export function maskUrlsForDiagnostics(text: string): string { - return text.replace(/https?:\/\/[^\s"'<>]+/gi, (m) => { + return text.replace(EMBEDDED_URL_RE, (m) => { try { void new URL(m); return maskUrlForDiagnostics(m); diff --git a/tests/log-mask.test.ts b/tests/log-mask.test.ts index d8ff08cbfb..af69ec3033 100644 --- a/tests/log-mask.test.ts +++ b/tests/log-mask.test.ts @@ -79,6 +79,38 @@ test("maskUrlsInText: masks URLs embedded in arbitrary strings", () => { assert.equal(maskUrlsInText("no urls here"), "no urls here"); }); +test("maskUrlsInText: non-http userinfo schemes are masked too (#4191)", () => { + assert.equal(maskUrlsInText(" socks5://u:p@10.0.0.1:1080 "), " socks5://:1080 "); + assert.equal( + maskUrlsInText("forward ws://user:pass@relay.internal:8080/ws failed"), + "forward ws://:8080/ws failed", + ); + assert.equal(maskUrlsInText("wss://a:b@host.example/x"), "wss:///x"); + assert.equal(maskUrlsInText("ftp://user:pass@ftp.example/pub"), "ftp:///pub"); + // userinfo/query/hash dropped, path kept; a pathless non-special scheme keeps no trailing "/" + assert.equal(maskUrlsInText("socks5h://x:y@r.internal/a/b?c=d#f"), "socks5h:///a/b"); + assert.equal(maskUrlsInText("socks5://10.0.0.1:1080"), "socks5://:1080"); + assert.equal(maskUrlsInText("junk http:// trailing stays"), "junk http:// trailing stays"); + assert.equal(maskUrlsInText("mailto:x@y.com stays"), "mailto:x@y.com stays"); +}); + +test("maskUrlsInText: word-char-glued URL stays masked — no leading \\b in the entry regex (#4191)", () => { + // A leading \b would skip these (word char → no boundary) and leak the host; + // over-masking is safe, under-masking is not. Pins the unbounded pattern. + assert.ok(!maskUrlsInText("_https://foo.com/bar underscore").includes("foo.com")); + assert.ok(!maskUrlsInText("xhttps://foo.com/bar glued").includes("foo.com")); +}); + +test("maskUrlsForDiagnostics: non-http userinfo schemes are masked too (#4191)", () => { + assert.equal(maskUrlsForDiagnostics(" socks5://u:p@10.0.0.1:1080 "), " socks5://:1080 "); + assert.equal( + maskUrlsForDiagnostics("forward ws://user:pass@relay.internal:8080/ws failed"), + "forward ws://:8080/ws failed", + ); + // loopback exemption applies per-scheme: bili's own local proxy stays verbatim + assert.equal(maskUrlsForDiagnostics("probe ws://127.0.0.1:9000/acp"), "probe ws://127.0.0.1:9000/acp"); +}); + test("maskUrlForDiagnostics: loopback exempt (identifies bili's own proxy), non-public still masked (#3651)", () => { assert.equal(maskUrlForDiagnostics("http://127.0.0.1:18787/v1/messages"), "http://127.0.0.1:18787/v1/messages"); assert.equal(maskUrlForDiagnostics("http://localhost:9090/acp"), "http://localhost:9090/acp"); From 6b8a47718ac85951fb90057f14429fd7caea8019 Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 23 Oct 2026 18:46:38 +0800 Subject: [PATCH 2/2] test(log-mask): pin maskUrlsForPublish on the public boundary for non-http schemes (#4191) Adds the one surface the original three #4191 test blocks did not cover directly: maskUrlsForPublish. It uses different always-mask logic (isPublicApiHost, ignoring BILI_LOG_MASK_HOSTS=0), so it is pinned separately rather than relying on transitive coverage via maskUrlsInText/maskUrlsForDiagnostics. --- tests/log-mask.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/log-mask.test.ts b/tests/log-mask.test.ts index af69ec3033..8134537274 100644 --- a/tests/log-mask.test.ts +++ b/tests/log-mask.test.ts @@ -25,6 +25,7 @@ import { maskUrlForDiagnostics, maskUrlForLog, maskUrlsForDiagnostics, + maskUrlsForPublish, maskUrlsInText, redactSecretsInText, setMaskHostsEnabled, @@ -111,6 +112,17 @@ test("maskUrlsForDiagnostics: non-http userinfo schemes are masked too (#4191)", assert.equal(maskUrlsForDiagnostics("probe ws://127.0.0.1:9000/acp"), "probe ws://127.0.0.1:9000/acp"); }); +test("maskUrlsForPublish: non-http userinfo schemes masked on the PUBLIC boundary (#4191)", () => { + // unlike the log/diagnostics paths, publish masks non-public hosts unconditionally (ignores BILI_LOG_MASK_HOSTS=0) + assert.equal(maskUrlsForPublish(" socks5://u:p@10.0.0.1:1080 "), " socks5://:1080 "); + assert.equal( + maskUrlsForPublish("forward ws://user:pass@relay.internal:8080/ws failed"), + "forward ws://:8080/ws failed", + ); + assert.equal(maskUrlsForPublish("ftp://user:pass@ftp.example/pub"), "ftp:///pub"); + assert.equal(maskUrlsForPublish("https://api.anthropic.com/v1/messages"), "https://api.anthropic.com/v1/messages"); +}); + test("maskUrlForDiagnostics: loopback exempt (identifies bili's own proxy), non-public still masked (#3651)", () => { assert.equal(maskUrlForDiagnostics("http://127.0.0.1:18787/v1/messages"), "http://127.0.0.1:18787/v1/messages"); assert.equal(maskUrlForDiagnostics("http://localhost:9090/acp"), "http://localhost:9090/acp");