forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredaction.test.ts
More file actions
112 lines (89 loc) · 3.48 KB
/
Copy pathredaction.test.ts
File metadata and controls
112 lines (89 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { describe, it, expect, vi } from "vitest";
import { ConsoleRuntimeLogger } from "../../src/logger/runtime-logger.js";
describe("ConsoleRuntimeLogger redaction", () => {
it("redacts default sensitive keys (secret, token, password, apiKey, authorization)", () => {
const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {});
const logger = new ConsoleRuntimeLogger();
logger.info("test", {
userId: "u1",
apiToken: "abc-123",
password: "hunter2",
apiKey: "sk-live-xxx",
authorization: "Bearer xyz",
secretKey: "my-secret",
normalField: "visible"
});
const loggedMeta = infoSpy.mock.calls[0]![1] as Record<string, unknown>;
expect(loggedMeta.userId).toBe("u1");
expect(loggedMeta.normalField).toBe("visible");
expect(loggedMeta.apiToken).toBe("[REDACTED]");
expect(loggedMeta.password).toBe("[REDACTED]");
expect(loggedMeta.apiKey).toBe("[REDACTED]");
expect(loggedMeta.authorization).toBe("[REDACTED]");
expect(loggedMeta.secretKey).toBe("[REDACTED]");
infoSpy.mockRestore();
});
it("accepts custom redactKeys pattern", () => {
const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {});
const logger = new ConsoleRuntimeLogger({ redactKeys: /^ssn$/i });
logger.info("test", {
ssn: "123-45-6789",
name: "Alice",
token: "visible-token"
});
const loggedMeta = infoSpy.mock.calls[0]![1] as Record<string, unknown>;
expect(loggedMeta.ssn).toBe("[REDACTED]");
expect(loggedMeta.name).toBe("Alice");
expect(loggedMeta.token).toBe("visible-token");
infoSpy.mockRestore();
});
it("redacts nested objects recursively", () => {
const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {});
const logger = new ConsoleRuntimeLogger();
logger.info("nested test", {
user: {
name: "Bob",
credentials: {
password: "secret",
token: "tok-123"
}
},
safe: "value"
});
const loggedMeta = infoSpy.mock.calls[0]![1] as Record<string, unknown>;
expect(loggedMeta.user).toEqual({
name: "Bob",
credentials: {
password: "[REDACTED]",
token: "[REDACTED]"
}
});
expect(loggedMeta.safe).toBe("value");
infoSpy.mockRestore();
});
it("does not redact arrays", () => {
const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {});
const logger = new ConsoleRuntimeLogger();
logger.info("array test", { tags: ["a", "b"], secret: "hidden" });
const loggedMeta = infoSpy.mock.calls[0]![1] as Record<string, unknown>;
expect(loggedMeta.tags).toEqual(["a", "b"]);
expect(loggedMeta.secret).toBe("[REDACTED]");
infoSpy.mockRestore();
});
it("handles undefined metadata gracefully", () => {
const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {});
const logger = new ConsoleRuntimeLogger();
expect(() => logger.info("no meta")).not.toThrow();
expect(infoSpy.mock.calls[0]![1]).toEqual({});
infoSpy.mockRestore();
});
it("applies redaction to error() as well", () => {
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
const logger = new ConsoleRuntimeLogger();
logger.error("fail", { apiKey: "leaked", detail: "oops" });
const loggedMeta = errorSpy.mock.calls[0]![1] as Record<string, unknown>;
expect(loggedMeta.apiKey).toBe("[REDACTED]");
expect(loggedMeta.detail).toBe("oops");
errorSpy.mockRestore();
});
});