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
14 changes: 8 additions & 6 deletions src/log-mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,15 @@ export function maskUrlForDiagnostics(url: string): string {
try {
const u = new URL(url);
// Node keeps the brackets in hostname for bracketed IPv6 ([::1]).
// Loopback is returned VERBATIM (not re-serialized): URL.pathname
// normalizes pathless URLs by appending "/", which would drift
// byte-pinned local-origin lines for no security benefit — loopback
// identifies bili's own proxy, and secrets are scrubbed separately
// by redactSecretsInText upstream of every call site.
// Keep clean loopback URLs verbatim: URL.pathname would add "/" to
// pathless local-origin lines. The exemption covers host visibility,
// never userinfo/query/hash; generic text redaction cannot recognize
// arbitrary passwords or every query parameter carrying a secret.
const h = u.hostname.replace(/^\[|\]$/g, "").toLowerCase();
if (h === "localhost" || isLoopbackIp(h)) return url;
if (h === "localhost" || isLoopbackIp(h)) {
if (!u.username && !u.password && !u.search && !u.hash) return url;
return `${u.protocol}//${u.host}${u.pathname}`;
}
} catch {
// unparseable — fall through to the generic masker
}
Expand Down
39 changes: 39 additions & 0 deletions tests/log-mask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { SessionStore, _setStoreForTest } from "../src/persist.ts";
import { _setForTest as setRegistryForTest } from "../src/registry.ts";
import { closeLogger, configureLogger, log as loggerLog, setLogCapture } from "../src/logger.ts";
import { formatUpstreamError } from "../src/upstream-proxy.ts";
import { logHostEvent } from "../src/agent/host-log.js";
import { defaultLogFile } from "../src/paths.js";
import {
isMaskHostsEnabled,
isPublicApiHost,
Expand Down Expand Up @@ -140,6 +142,43 @@ test("maskUrlsForDiagnostics: text-level twin — loopback URLs kept, others mas
assert.equal(maskUrlsForDiagnostics("junk http:// trailing stays"), "junk http:// trailing stays");
});

for (const origin of ["http://localhost:9090", "ws://127.0.0.1:9000", "socks5://[::1]:1080"]) {
test(`loopback URL visibility never exposes credentials: ${origin}`, () => {
const u = new URL(origin);
const sensitive = `${u.protocol}//alice:local-secret@${u.host}/proxy?q=private-query#private-fragment`;
const safe = `${u.protocol}//${u.host}/proxy`;
assert.equal(maskUrlForDiagnostics(sensitive), safe);
assert.equal(maskUrlsForDiagnostics(`dial ${sensitive} failed`), `dial ${safe} failed`);
assert.equal(maskUrlForDiagnostics(origin), origin, "clean pathless local origins remain byte-identical");
const prev = isMaskHostsEnabled();
try {
setMaskHostsEnabled(false);
assert.equal(maskUrlForDiagnostics(sensitive), safe, "host visibility cannot disable credential masking");
} finally {
setMaskHostsEnabled(prev);
}
});
}

test("host-event file output drops loopback URL secrets while retaining the local endpoint", async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-loopback-log-"));
const prev = process.env.XDG_STATE_HOME;
process.env.XDG_STATE_HOME = dir;
try {
logHostEvent("info", "[test-host]", "dial http://alice:local-secret@localhost:9090/proxy?q=private-query#private-fragment failed");
logHostEvent("info", "[test-host]", "healthy http://localhost:9090");
const output = fs.readFileSync(defaultLogFile(), "utf8");
assert.match(output, /dial http:\/\/localhost:9090\/proxy failed/);
assert.match(output, /healthy http:\/\/localhost:9090\n/);
for (const secret of ["alice", "local-secret", "private-query", "private-fragment"]) {
assert.ok(!output.includes(secret), `${secret} must not reach the paste-able log`);
}
} finally {
if (prev === undefined) delete process.env.XDG_STATE_HOME; else process.env.XDG_STATE_HOME = prev;
await rmrf(dir);
}
});

test("maskHostPortForLog: CONNECT targets", () => {
assert.equal(maskHostPortForLog("relay.internal:8443"), "<private-host>:8443");
assert.equal(maskHostPortForLog("api.anthropic.com:443"), "api.anthropic.com:443");
Expand Down
Loading