
















@@ -47,6 +47,8 @@ afterAll(async () => {
4747});
48484949describe("enableConsoleCapture", () => {
50+const secret = "sk-testsecret1234567890abcd";
51+5052it("swallows EIO from stderr writes", () => {
5153setLoggerOverride({ level: "info", file: tempLogPath() });
5254vi.spyOn(process.stderr, "write").mockImplementation(() => {
@@ -123,6 +125,50 @@ describe("enableConsoleCapture", () => {
123125expect(stdoutWrite).toHaveBeenCalledWith('{\n "ok": true\n}\n');
124126});
125127128+it("redacts credentials before forwarding console output", () => {
129+setLoggerOverride({ level: "info", file: tempLogPath() });
130+const log = vi.fn();
131+console.log = log;
132+enableConsoleCapture();
133+134+console.log("apiKey:", secret);
135+136+expect(log).toHaveBeenCalledTimes(1);
137+const line = String(log.mock.calls[0]?.[0] ?? "");
138+expect(line).toContain("apiKey:");
139+expect(line).not.toContain(secret);
140+});
141+142+it("redacts credentials before writing forced stderr console output", () => {
143+setLoggerOverride({ level: "info", file: tempLogPath() });
144+const stderrWrite = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
145+routeLogsToStderr();
146+enableConsoleCapture();
147+148+console.error(`Authorization: Bearer ${secret}`);
149+150+expect(stderrWrite).toHaveBeenCalledTimes(1);
151+const line = String(stderrWrite.mock.calls[0]?.[0] ?? "");
152+expect(line).toContain("Authorization: Bearer");
153+expect(line).not.toContain(secret);
154+});
155+156+it("redacts credentials when timestamp prefixing console output", () => {
157+setLoggerOverride({ level: "info", file: tempLogPath() });
158+const warn = vi.fn();
159+console.warn = warn;
160+setConsoleTimestampPrefix(true);
161+enableConsoleCapture();
162+163+console.warn(`token=${secret}`);
164+165+expect(warn).toHaveBeenCalledTimes(1);
166+const line = String(warn.mock.calls[0]?.[0] ?? "");
167+expect(line).toMatch(/^(?:\d{2}:\d{2}:\d{2}|\d{4}-\d{2}-\d{2}T)/);
168+expect(line).toContain("token=");
169+expect(line).not.toContain(secret);
170+});
171+126172it.each([
127173{ name: "stdout", stream: process.stdout },
128174{ name: "stderr", stream: process.stderr },
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。