Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/log-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand Down
44 changes: 44 additions & 0 deletions tests/log-mask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
maskUrlForDiagnostics,
maskUrlForLog,
maskUrlsForDiagnostics,
maskUrlsForPublish,
maskUrlsInText,
redactSecretsInText,
setMaskHostsEnabled,
Expand Down Expand Up @@ -79,6 +80,49 @@ 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://<private-host>:1080 ");
assert.equal(
maskUrlsInText("forward ws://user:pass@relay.internal:8080/ws failed"),
"forward ws://<private-host>:8080/ws failed",
);
assert.equal(maskUrlsInText("wss://a:b@host.example/x"), "wss://<private-host>/x");
assert.equal(maskUrlsInText("ftp://user:pass@ftp.example/pub"), "ftp://<private-host>/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://<private-host>/a/b");
assert.equal(maskUrlsInText("socks5://10.0.0.1:1080"), "socks5://<private-host>: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://<private-host>:1080 ");
assert.equal(
maskUrlsForDiagnostics("forward ws://user:pass@relay.internal:8080/ws failed"),
"forward ws://<private-host>: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("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://<private-host>:1080 ");
assert.equal(
maskUrlsForPublish("forward ws://user:pass@relay.internal:8080/ws failed"),
"forward ws://<private-host>:8080/ws failed",
);
assert.equal(maskUrlsForPublish("ftp://user:pass@ftp.example/pub"), "ftp://<private-host>/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");
Expand Down
Loading