tabletcommand-logger 3.2.6 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cspell/project-words.txt +28 -0
- package/CLAUDE.md +150 -0
- package/README.md +111 -1
- package/build/index.js +15 -3
- package/build/index.js.map +1 -1
- package/build/logger-types.js +3 -0
- package/build/logger-types.js.map +1 -0
- package/build/middleware/logger.js +94 -33
- package/build/middleware/logger.js.map +1 -1
- package/build/middleware/status-logger.js +4 -9
- package/build/middleware/status-logger.js.map +1 -1
- package/build/otel.js +54 -0
- package/build/otel.js.map +1 -0
- package/build/pino-logger.js +97 -0
- package/build/pino-logger.js.map +1 -0
- package/build/redaction.js +351 -0
- package/build/redaction.js.map +1 -0
- package/build/test/logger-factory.js +207 -0
- package/build/test/logger-factory.js.map +1 -0
- package/build/test/middleware-logger.js +535 -2
- package/build/test/middleware-logger.js.map +1 -1
- package/build/test/middleware-status-logger.js +58 -0
- package/build/test/middleware-status-logger.js.map +1 -0
- package/build/test/otel-mixin.js +139 -0
- package/build/test/otel-mixin.js.map +1 -0
- package/build/test/redaction.js +293 -0
- package/build/test/redaction.js.map +1 -0
- package/build/test/signature.js +111 -0
- package/build/test/signature.js.map +1 -0
- package/build/winston-logger.js +18 -47
- package/build/winston-logger.js.map +1 -1
- package/definitions/index.d.ts +6 -2
- package/definitions/index.d.ts.map +1 -1
- package/definitions/logger-types.d.ts +38 -0
- package/definitions/logger-types.d.ts.map +1 -0
- package/definitions/middleware/logger.d.ts +4 -3
- package/definitions/middleware/logger.d.ts.map +1 -1
- package/definitions/middleware/status-logger.d.ts +4 -4
- package/definitions/middleware/status-logger.d.ts.map +1 -1
- package/definitions/otel.d.ts +8 -0
- package/definitions/otel.d.ts.map +1 -0
- package/definitions/pino-logger.d.ts +6 -0
- package/definitions/pino-logger.d.ts.map +1 -0
- package/definitions/redaction.d.ts +25 -0
- package/definitions/redaction.d.ts.map +1 -0
- package/definitions/test/logger-factory.d.ts +2 -0
- package/definitions/test/logger-factory.d.ts.map +1 -0
- package/definitions/test/middleware-status-logger.d.ts +2 -0
- package/definitions/test/middleware-status-logger.d.ts.map +1 -0
- package/definitions/test/otel-mixin.d.ts +2 -0
- package/definitions/test/otel-mixin.d.ts.map +1 -0
- package/definitions/test/redaction.d.ts +2 -0
- package/definitions/test/redaction.d.ts.map +1 -0
- package/definitions/test/signature.d.ts +2 -0
- package/definitions/test/signature.d.ts.map +1 -0
- package/definitions/winston-logger.d.ts +2 -13
- package/definitions/winston-logger.d.ts.map +1 -1
- package/eslint.config.mjs +1 -0
- package/package.json +17 -5
- package/src/index.ts +7 -3
- package/src/logger-types.ts +50 -0
- package/src/middleware/logger.ts +105 -37
- package/src/middleware/status-logger.ts +6 -15
- package/src/otel.ts +57 -0
- package/src/pino-logger.ts +110 -0
- package/src/redaction.ts +368 -0
- package/src/test/logger-factory.ts +216 -0
- package/src/test/middleware-logger.ts +582 -2
- package/src/test/middleware-status-logger.ts +61 -0
- package/src/test/otel-mixin.ts +119 -0
- package/src/test/redaction.ts +409 -0
- package/src/test/signature.ts +126 -0
- package/src/winston-logger.ts +2 -72
- package/tabletcommand-logger-4.0.0.tgz +0 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { assert } from "chai";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { Writable } from "node:stream";
|
|
4
|
+
import Module from "node:module";
|
|
5
|
+
import * as otel from "@opentelemetry/api";
|
|
6
|
+
import logger from "../pino-logger";
|
|
7
|
+
import { getOtelTraceContext, resetOtelForTesting } from "../otel";
|
|
8
|
+
|
|
9
|
+
describe("OpenTelemetry Mixin", () => {
|
|
10
|
+
function createCaptureStream(lines: Record<string, unknown>[]) {
|
|
11
|
+
return new Writable({
|
|
12
|
+
write(chunk: Buffer | string, _encoding: string, callback: (error?: Error | null) => void) {
|
|
13
|
+
try {
|
|
14
|
+
lines.push(JSON.parse(chunk.toString()) as Record<string, unknown>);
|
|
15
|
+
} catch {
|
|
16
|
+
// ignore
|
|
17
|
+
}
|
|
18
|
+
callback();
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
it("does not inject trace fields when no span is active", () => {
|
|
24
|
+
const lines: Record<string, unknown>[] = [];
|
|
25
|
+
const log = logger(
|
|
26
|
+
{ name: "test", logToConsole: true },
|
|
27
|
+
createCaptureStream(lines)
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
log?.info("plain message without active span");
|
|
31
|
+
|
|
32
|
+
assert.equal(lines.length, 1);
|
|
33
|
+
assert.isUndefined(lines[0].trace_id);
|
|
34
|
+
assert.isUndefined(lines[0].span_id);
|
|
35
|
+
assert.equal(lines[0].msg, "plain message without active span");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("injects trace_id and span_id when an active span is present", () => {
|
|
39
|
+
const lines: Record<string, unknown>[] = [];
|
|
40
|
+
const log = logger(
|
|
41
|
+
{ name: "test", logToConsole: true },
|
|
42
|
+
createCaptureStream(lines)
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const origGetActiveSpan = otel.trace.getActiveSpan;
|
|
46
|
+
// Mock an active span
|
|
47
|
+
(otel.trace as any).getActiveSpan = () => ({
|
|
48
|
+
spanContext: () => ({
|
|
49
|
+
traceId: "4bf92f3577b34da6a3ce929d0e0e4736",
|
|
50
|
+
spanId: "00f067aa0ba902b7",
|
|
51
|
+
traceFlags: 1,
|
|
52
|
+
}),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
log?.info({ detail: "test" }, "message with active trace span");
|
|
57
|
+
|
|
58
|
+
assert.equal(lines.length, 1);
|
|
59
|
+
assert.equal(lines[0].trace_id, "4bf92f3577b34da6a3ce929d0e0e4736");
|
|
60
|
+
assert.equal(lines[0].span_id, "00f067aa0ba902b7");
|
|
61
|
+
assert.equal(lines[0].trace_flags, "1");
|
|
62
|
+
assert.equal(lines[0].detail, "test");
|
|
63
|
+
} finally {
|
|
64
|
+
(otel.trace as any).getActiveSpan = origGetActiveSpan;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("does not inject all-zero trace IDs when an inactive or NonRecordingSpan is returned", () => {
|
|
69
|
+
const lines: Record<string, unknown>[] = [];
|
|
70
|
+
const log = logger(
|
|
71
|
+
{ name: "test", logToConsole: true },
|
|
72
|
+
createCaptureStream(lines)
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const origGetActiveSpan = otel.trace.getActiveSpan;
|
|
76
|
+
// Mock NonRecordingSpan with invalid all-zero IDs
|
|
77
|
+
(otel.trace as any).getActiveSpan = () => ({
|
|
78
|
+
spanContext: () => ({
|
|
79
|
+
traceId: "00000000000000000000000000000000",
|
|
80
|
+
spanId: "0000000000000000",
|
|
81
|
+
traceFlags: 0,
|
|
82
|
+
}),
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
log?.info({ detail: "test" }, "message with invalid/zero span");
|
|
87
|
+
|
|
88
|
+
assert.equal(lines.length, 1);
|
|
89
|
+
assert.isUndefined(lines[0].trace_id);
|
|
90
|
+
assert.isUndefined(lines[0].span_id);
|
|
91
|
+
assert.isUndefined(lines[0].trace_flags);
|
|
92
|
+
assert.equal(lines[0].detail, "test");
|
|
93
|
+
} finally {
|
|
94
|
+
(otel.trace as any).getActiveSpan = origGetActiveSpan;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("gracefully returns empty object when @opentelemetry/api cannot be resolved", () => {
|
|
99
|
+
const mod = Module as unknown as { _resolveFilename: (request: string, ...args: unknown[]) => string };
|
|
100
|
+
const origResolve = mod._resolveFilename;
|
|
101
|
+
mod._resolveFilename = function (request: string, ...args: unknown[]) {
|
|
102
|
+
if (request === "@opentelemetry/api") {
|
|
103
|
+
const err = new Error("Cannot find module @opentelemetry/api") as Error & { code: string };
|
|
104
|
+
err.code = "MODULE_NOT_FOUND";
|
|
105
|
+
throw err;
|
|
106
|
+
}
|
|
107
|
+
return origResolve.call(this, request, ...args);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
resetOtelForTesting();
|
|
112
|
+
const ctx = getOtelTraceContext();
|
|
113
|
+
assert.deepEqual(ctx, {});
|
|
114
|
+
} finally {
|
|
115
|
+
mod._resolveFilename = origResolve;
|
|
116
|
+
resetOtelForTesting();
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import { assert } from "chai";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { Writable } from "node:stream";
|
|
4
|
+
import logger from "../pino-logger";
|
|
5
|
+
import { MAX_REDACT_DEPTH, redactSetCookie } from "../redaction";
|
|
6
|
+
|
|
7
|
+
describe("Native Redaction Engine", () => {
|
|
8
|
+
function createCaptureStream(lines: Record<string, unknown>[]) {
|
|
9
|
+
return new Writable({
|
|
10
|
+
write(chunk: Buffer | string, _encoding: string, callback: (error?: Error | null) => void) {
|
|
11
|
+
try {
|
|
12
|
+
const str = chunk.toString();
|
|
13
|
+
lines.push(JSON.parse(str) as Record<string, unknown>);
|
|
14
|
+
} catch {
|
|
15
|
+
// ignore
|
|
16
|
+
}
|
|
17
|
+
callback();
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
it("redacts apikey to first 7 characters", () => {
|
|
23
|
+
const lines: Record<string, unknown>[] = [];
|
|
24
|
+
const log = logger(
|
|
25
|
+
{ name: "test", logToConsole: true },
|
|
26
|
+
createCaptureStream(lines)
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
log?.info({ apikey: "abcdef1234567" }, "api call");
|
|
30
|
+
|
|
31
|
+
assert.equal(lines.length, 1);
|
|
32
|
+
assert.equal(lines[0].apikey, "abcdef1");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("redacts camelCase apiKey and personnelApiKey to first 7 characters", () => {
|
|
36
|
+
const lines: Record<string, unknown>[] = [];
|
|
37
|
+
const log = logger(
|
|
38
|
+
{ name: "test", logToConsole: true },
|
|
39
|
+
createCaptureStream(lines)
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
log?.info(
|
|
43
|
+
{
|
|
44
|
+
apiKey: "abcdef1234567",
|
|
45
|
+
personnelApiKey: "abcdef1234567",
|
|
46
|
+
},
|
|
47
|
+
"multi keys"
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
assert.equal(lines.length, 1);
|
|
51
|
+
assert.equal(lines[0].apiKey, "abcdef1");
|
|
52
|
+
assert.equal(lines[0].personnelApiKey, "abcdef1");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("completely masks password and secret fields", () => {
|
|
56
|
+
const lines: Record<string, unknown>[] = [];
|
|
57
|
+
const log = logger(
|
|
58
|
+
{ name: "test", logToConsole: true },
|
|
59
|
+
createCaptureStream(lines)
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
log?.info(
|
|
63
|
+
{
|
|
64
|
+
password: "super-secret-password-123",
|
|
65
|
+
secret: "top-secret-val",
|
|
66
|
+
},
|
|
67
|
+
"credentials test"
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
assert.equal(lines.length, 1);
|
|
71
|
+
assert.equal(lines[0].password, "<redacted>");
|
|
72
|
+
assert.equal(lines[0].secret, "<redacted>");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("redacts nested headers with authorization and cookie", () => {
|
|
76
|
+
const lines: Record<string, unknown>[] = [];
|
|
77
|
+
const log = logger(
|
|
78
|
+
{ name: "test", logToConsole: true },
|
|
79
|
+
createCaptureStream(lines)
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
log?.info(
|
|
83
|
+
{
|
|
84
|
+
headers: {
|
|
85
|
+
authorization: "Bearer abcdef1234567",
|
|
86
|
+
cookie: "session=abcdef1234567; saml=xyz987654321",
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
"headers log"
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
assert.equal(lines.length, 1);
|
|
93
|
+
const headers = lines[0].headers as Record<string, string>;
|
|
94
|
+
assert.equal(headers.authorization, "Bearer abcdef1");
|
|
95
|
+
assert.equal(headers.cookie, "session=<redacted>; saml=<redacted>");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("redacts only the first pair value in set-cookie and preserves attributes", () => {
|
|
99
|
+
const raw =
|
|
100
|
+
"session=SECRET12345; Path=/; Domain=.tabletcommand.com; Max-Age=3600; HttpOnly; Secure; SameSite=None";
|
|
101
|
+
const redactedVal = redactSetCookie(raw);
|
|
102
|
+
|
|
103
|
+
assert.equal(
|
|
104
|
+
redactedVal,
|
|
105
|
+
"session=<redacted>; Path=/; Domain=.tabletcommand.com; Max-Age=3600; HttpOnly; Secure; SameSite=None"
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
// Also verify via pinoCensor in logger
|
|
109
|
+
const lines: Record<string, unknown>[] = [];
|
|
110
|
+
const log = logger(
|
|
111
|
+
{ name: "test", logToConsole: true },
|
|
112
|
+
createCaptureStream(lines)
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
log?.info(
|
|
116
|
+
{
|
|
117
|
+
res: {
|
|
118
|
+
headers: {
|
|
119
|
+
"set-cookie": raw,
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
"res set-cookie test"
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
assert.equal(lines.length, 1);
|
|
127
|
+
const res = lines[0].res as { headers: { "set-cookie": string } };
|
|
128
|
+
assert.equal(
|
|
129
|
+
res.headers["set-cookie"],
|
|
130
|
+
"session=<redacted>; Path=/; Domain=.tabletcommand.com; Max-Age=3600; HttpOnly; Secure; SameSite=None"
|
|
131
|
+
);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("redacts apikey within nested objects", () => {
|
|
135
|
+
const lines: Record<string, unknown>[] = [];
|
|
136
|
+
const log = logger(
|
|
137
|
+
{ name: "test", logToConsole: true },
|
|
138
|
+
createCaptureStream(lines)
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
log?.info(
|
|
142
|
+
{
|
|
143
|
+
payload: {
|
|
144
|
+
account: {
|
|
145
|
+
apikey: "abcdef1234567",
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
"nested object"
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
assert.equal(lines.length, 1);
|
|
153
|
+
const payload = lines[0].payload as { account: { apikey: string } };
|
|
154
|
+
assert.equal(payload.account.apikey, "abcdef1");
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
function nest(levels: number, leaf: Record<string, unknown>): Record<string, unknown> {
|
|
158
|
+
let value = leaf;
|
|
159
|
+
for (let i = 0; i < levels; i++) {
|
|
160
|
+
value = { [`l${i}`]: value };
|
|
161
|
+
}
|
|
162
|
+
return value;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function leafAt(line: Record<string, unknown>, levels: number): Record<string, unknown> {
|
|
166
|
+
let value = line;
|
|
167
|
+
for (let i = levels - 1; i >= 0; i--) {
|
|
168
|
+
value = value[`l${i}`] as Record<string, unknown>;
|
|
169
|
+
}
|
|
170
|
+
return value;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
it(`redacts a secret ${MAX_REDACT_DEPTH} levels deep`, () => {
|
|
174
|
+
assert.equal(MAX_REDACT_DEPTH, 10);
|
|
175
|
+
const lines: Record<string, unknown>[] = [];
|
|
176
|
+
const log = logger({ name: "test", logToConsole: true }, createCaptureStream(lines));
|
|
177
|
+
|
|
178
|
+
log?.info(nest(MAX_REDACT_DEPTH, { password: "super-secret-password-123" }), "depth test");
|
|
179
|
+
|
|
180
|
+
assert.equal(leafAt(lines[0], MAX_REDACT_DEPTH).password, "<redacted>");
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("leaves a secret deeper than MAX_REDACT_DEPTH as the documented ceiling", () => {
|
|
184
|
+
const lines: Record<string, unknown>[] = [];
|
|
185
|
+
const log = logger({ name: "test", logToConsole: true }, createCaptureStream(lines));
|
|
186
|
+
|
|
187
|
+
log?.info(nest(MAX_REDACT_DEPTH + 1, { password: "super-secret-password-123" }), "depth test");
|
|
188
|
+
|
|
189
|
+
assert.equal(leafAt(lines[0], MAX_REDACT_DEPTH + 1).password, "super-secret-password-123");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("matches key names without case, dashes or underscores", () => {
|
|
193
|
+
const lines: Record<string, unknown>[] = [];
|
|
194
|
+
const log = logger({ name: "test", logToConsole: true }, createCaptureStream(lines));
|
|
195
|
+
|
|
196
|
+
log?.info({
|
|
197
|
+
Password: "hunter2hunter2",
|
|
198
|
+
APIKEY: "abcdef1234567",
|
|
199
|
+
"X-API-Key": "abcdef1234567",
|
|
200
|
+
access_token: "abcdef1234567",
|
|
201
|
+
"AUTH-TOKEN": "abcdef1234567",
|
|
202
|
+
}, "case test");
|
|
203
|
+
|
|
204
|
+
const line = lines[0];
|
|
205
|
+
assert.equal(line.Password, "<redacted>");
|
|
206
|
+
assert.equal(line.APIKEY, "abcdef1");
|
|
207
|
+
assert.equal(line["X-API-Key"], "abcdef1");
|
|
208
|
+
assert.equal(line.access_token, "abcdef1");
|
|
209
|
+
assert.equal(line["AUTH-TOKEN"], "abcdef1");
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("cuts the credential names found in massive-tyrion to 7 characters", () => {
|
|
213
|
+
const names = [
|
|
214
|
+
"authToken", "accessToken", "refreshToken", "refresh_token", "idToken", "id_token",
|
|
215
|
+
"sessionToken", "superAdminSessionToken", "deviceToken", "zonehavenToken", "faToken",
|
|
216
|
+
"videoGatewayToken", "x-tc-auth-token", "apiToken", "mark43ApiToken", "authTokenPlainText",
|
|
217
|
+
"departmentApiKey", "agencyApiKey", "watchDutyApiKey", "arcgisApiKey", "freshdeskApiKey",
|
|
218
|
+
"googleApiKey", "webApiKey", "apiKeyReadOnly", "apiKeyManagement", "personnelApiKey",
|
|
219
|
+
"partnerKey", "x-partner-key", "cadKey", "vendorKey", "licenseKey", "licenseCode", "mark43Key",
|
|
220
|
+
"authKey", "accessCode", "joinCode", "jwt", "authHeader", "sessionCookie", "signupKey",
|
|
221
|
+
];
|
|
222
|
+
const lines: Record<string, unknown>[] = [];
|
|
223
|
+
const log = logger({ name: "test", logToConsole: true }, createCaptureStream(lines));
|
|
224
|
+
|
|
225
|
+
log?.info({ body: Object.fromEntries(names.map((name) => [name, "abcdef1234567"])) }, "names");
|
|
226
|
+
|
|
227
|
+
const body = lines[0].body as Record<string, string>;
|
|
228
|
+
for (const name of names) {
|
|
229
|
+
assert.equal(body[name], "abcdef1", name);
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("masks secret material and personal data completely", () => {
|
|
234
|
+
const names = [
|
|
235
|
+
"authSecret", "authSecretPlainText", "secretPlainText", "authKeySecret", "keySecret",
|
|
236
|
+
"clientSecret", "client_secret", "webhookSecret", "jwtSecret", "droneVideoJwtSecret",
|
|
237
|
+
"decryptedSecret", "onetimesecret", "secretAccessKey", "credential", "credentials",
|
|
238
|
+
"privateKey", "sshPrivateKey", "jwtSigningKey", "mailgunSigningKey", "encryptionKey",
|
|
239
|
+
"esriEncryptionKey", "esriEncryptionPassword", "initialPassword", "confirmPassword",
|
|
240
|
+
"pass", "pwd", "auth_pass", "salt", "ssn", "dateOfBirth", "licenseNumber", "doorCode",
|
|
241
|
+
];
|
|
242
|
+
const lines: Record<string, unknown>[] = [];
|
|
243
|
+
const log = logger({ name: "test", logToConsole: true }, createCaptureStream(lines));
|
|
244
|
+
|
|
245
|
+
log?.info({ body: Object.fromEntries(names.map((name) => [name, "abcdef1234567"])) }, "names");
|
|
246
|
+
|
|
247
|
+
const body = lines[0].body as Record<string, string>;
|
|
248
|
+
for (const name of names) {
|
|
249
|
+
assert.equal(body[name], "<redacted>", name);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("keeps flags, settings and pagination cursors readable", () => {
|
|
254
|
+
const lines: Record<string, unknown>[] = [];
|
|
255
|
+
const log = logger({ name: "test", logToConsole: true }, createCaptureStream(lines));
|
|
256
|
+
|
|
257
|
+
log?.info({
|
|
258
|
+
hasAuthSecret: true,
|
|
259
|
+
minPasswordLength: 8,
|
|
260
|
+
useSSHKey: false,
|
|
261
|
+
tokenTTLSeconds: 3600,
|
|
262
|
+
nextPageToken: "cursor-abcdef1234567",
|
|
263
|
+
paginationToken: "cursor-abcdef1234567",
|
|
264
|
+
cacheKey: "dept:1234567890",
|
|
265
|
+
}, "flags");
|
|
266
|
+
|
|
267
|
+
const line = lines[0];
|
|
268
|
+
assert.equal(line.hasAuthSecret, true);
|
|
269
|
+
assert.equal(line.minPasswordLength, 8);
|
|
270
|
+
assert.equal(line.useSSHKey, false);
|
|
271
|
+
assert.equal(line.tokenTTLSeconds, 3600);
|
|
272
|
+
assert.equal(line.nextPageToken, "cursor-abcdef1234567");
|
|
273
|
+
assert.equal(line.paginationToken, "cursor-abcdef1234567");
|
|
274
|
+
assert.equal(line.cacheKey, "dept:1234567890");
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("redacts secrets inside a whole auth object and in arrays", () => {
|
|
278
|
+
const lines: Record<string, unknown>[] = [];
|
|
279
|
+
const log = logger({ name: "test", logToConsole: true }, createCaptureStream(lines));
|
|
280
|
+
|
|
281
|
+
log?.info({
|
|
282
|
+
auth: { username: "alice", password: "hunter2hunter2", sshPrivateKey: "-----BEGIN KEY-----" },
|
|
283
|
+
users: [{ name: "a", authToken: "abcdef1234567" }, { name: "b", pass: "hash", salt: "salt" }],
|
|
284
|
+
}, "auth");
|
|
285
|
+
|
|
286
|
+
const auth = lines[0].auth as Record<string, string>;
|
|
287
|
+
assert.deepEqual(auth, { username: "alice", password: "<redacted>", sshPrivateKey: "<redacted>" });
|
|
288
|
+
const users = lines[0].users as Record<string, string>[];
|
|
289
|
+
assert.deepEqual(users, [
|
|
290
|
+
{ name: "a", authToken: "abcdef1" },
|
|
291
|
+
{ name: "b", pass: "<redacted>", salt: "<redacted>" },
|
|
292
|
+
]);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("redacts what a toJSON object such as a Mongoose document writes", () => {
|
|
296
|
+
const lines: Record<string, unknown>[] = [];
|
|
297
|
+
const log = logger({ name: "test", logToConsole: true }, createCaptureStream(lines));
|
|
298
|
+
class Doc {
|
|
299
|
+
toJSON() {
|
|
300
|
+
return { email: "a@b.c", pass: "hash", token: "abcdef1234567" };
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
log?.info({ user: new Doc() }, "doc");
|
|
305
|
+
|
|
306
|
+
assert.deepEqual(lines[0].user, { email: "a@b.c", pass: "<redacted>", token: "abcdef1" });
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("redacts child bindings and serialized errors", () => {
|
|
310
|
+
const lines: Record<string, unknown>[] = [];
|
|
311
|
+
const log = logger({ name: "test", logToConsole: true }, createCaptureStream(lines));
|
|
312
|
+
const err = Object.assign(new Error("request failed"), {
|
|
313
|
+
config: { headers: { Authorization: "Bearer abcdef1234567", "x-api-key": "abcdef1234567" } },
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
log?.child({ partnerKey: "abcdef1234567" }).error(err, "failed");
|
|
317
|
+
|
|
318
|
+
const line = lines[0];
|
|
319
|
+
assert.equal(line.partnerKey, "abcdef1");
|
|
320
|
+
const config = (line.err as { config: { headers: Record<string, string> } }).config;
|
|
321
|
+
assert.equal(config.headers.Authorization, "Bearer abcdef1");
|
|
322
|
+
assert.equal(config.headers["x-api-key"], "abcdef1");
|
|
323
|
+
assert.equal((line.err as { message: string }).message, "request failed");
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("leaves the caller's object unchanged", () => {
|
|
327
|
+
const lines: Record<string, unknown>[] = [];
|
|
328
|
+
const log = logger({ name: "test", logToConsole: true }, createCaptureStream(lines));
|
|
329
|
+
const payload = { user: { name: "alice", password: "hunter2hunter2" } };
|
|
330
|
+
|
|
331
|
+
log?.info(payload, "payload");
|
|
332
|
+
|
|
333
|
+
assert.equal(payload.user.password, "hunter2hunter2");
|
|
334
|
+
assert.equal((lines[0].user as Record<string, string>).password, "<redacted>");
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it("redacts TitleCase Authorization, Cookie, and Set-Cookie headers", () => {
|
|
338
|
+
const lines: Record<string, unknown>[] = [];
|
|
339
|
+
const log = logger(
|
|
340
|
+
{ name: "test", logToConsole: true },
|
|
341
|
+
createCaptureStream(lines)
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
log?.info(
|
|
345
|
+
{
|
|
346
|
+
headers: {
|
|
347
|
+
Authorization: "Bearer abcdef1234567",
|
|
348
|
+
Cookie: "session=abcdef1234567; saml=xyz987654321",
|
|
349
|
+
},
|
|
350
|
+
},
|
|
351
|
+
"TitleCase headers"
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
log?.info(
|
|
355
|
+
{
|
|
356
|
+
res: {
|
|
357
|
+
headers: {
|
|
358
|
+
"Set-Cookie": "session=SECRET12345; Path=/; Domain=.tabletcommand.com; HttpOnly; Secure",
|
|
359
|
+
},
|
|
360
|
+
},
|
|
361
|
+
},
|
|
362
|
+
"TitleCase Set-Cookie"
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
assert.equal(lines.length, 2);
|
|
366
|
+
const headers = lines[0].headers as Record<string, string>;
|
|
367
|
+
assert.equal(headers.Authorization, "Bearer abcdef1");
|
|
368
|
+
assert.equal(headers.Cookie, "session=<redacted>; saml=<redacted>");
|
|
369
|
+
|
|
370
|
+
const res = lines[1].res as { headers: { "Set-Cookie": string } };
|
|
371
|
+
assert.equal(
|
|
372
|
+
res.headers["Set-Cookie"],
|
|
373
|
+
"session=<redacted>; Path=/; Domain=.tabletcommand.com; HttpOnly; Secure"
|
|
374
|
+
);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it("redacts nested TitleCase Authorization in outgoing HTTP/Axios config payloads", () => {
|
|
378
|
+
const lines: Record<string, unknown>[] = [];
|
|
379
|
+
const log = logger(
|
|
380
|
+
{ name: "test", logToConsole: true },
|
|
381
|
+
createCaptureStream(lines)
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
log?.info(
|
|
385
|
+
{
|
|
386
|
+
err: {
|
|
387
|
+
config: {
|
|
388
|
+
headers: {
|
|
389
|
+
Authorization: "Bearer token12345678",
|
|
390
|
+
},
|
|
391
|
+
},
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
"axios error"
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
log?.info(
|
|
398
|
+
{
|
|
399
|
+
Authorization: "Bearer token99887766",
|
|
400
|
+
},
|
|
401
|
+
"root Authorization"
|
|
402
|
+
);
|
|
403
|
+
|
|
404
|
+
assert.equal(lines.length, 2);
|
|
405
|
+
const err = lines[0].err as { config: { headers: { Authorization: string } } };
|
|
406
|
+
assert.equal(err.config.headers.Authorization, "Bearer token12");
|
|
407
|
+
assert.equal(lines[1].Authorization, "Bearer token99");
|
|
408
|
+
});
|
|
409
|
+
});
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { assert } from "chai";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import ts from "typescript";
|
|
5
|
+
|
|
6
|
+
describe("Strict TypeScript Signatures", () => {
|
|
7
|
+
function getCompileDiagnostics(sourceText: string): string[] {
|
|
8
|
+
const options: ts.CompilerOptions = {
|
|
9
|
+
target: ts.ScriptTarget.ES2022,
|
|
10
|
+
moduleResolution: ts.ModuleResolutionKind.Node10,
|
|
11
|
+
strict: true,
|
|
12
|
+
noEmit: true,
|
|
13
|
+
};
|
|
14
|
+
const defaultHost = ts.createCompilerHost(options);
|
|
15
|
+
const testFileName = path.resolve(process.cwd(), "test-signature-check.ts");
|
|
16
|
+
|
|
17
|
+
const customHost: ts.CompilerHost = {
|
|
18
|
+
...defaultHost,
|
|
19
|
+
getSourceFile: (fileName, languageVersion) => {
|
|
20
|
+
if (fileName === testFileName) {
|
|
21
|
+
return ts.createSourceFile(fileName, sourceText, languageVersion);
|
|
22
|
+
}
|
|
23
|
+
return defaultHost.getSourceFile(fileName, languageVersion);
|
|
24
|
+
},
|
|
25
|
+
fileExists: (fileName) => fileName === testFileName || defaultHost.fileExists(fileName),
|
|
26
|
+
readFile: (fileName) => (fileName === testFileName ? sourceText : defaultHost.readFile(fileName)),
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const program = ts.createProgram([testFileName], options, customHost);
|
|
30
|
+
const sourceFile = program.getSourceFile(testFileName);
|
|
31
|
+
if (!sourceFile) {
|
|
32
|
+
return ["Source file not found"];
|
|
33
|
+
}
|
|
34
|
+
const diags = program.getSemanticDiagnostics(sourceFile);
|
|
35
|
+
return diags.map((d) => ts.flattenDiagnosticMessageText(d.messageText, "\n"));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
it("compiles standard Pino signatures without errors", () => {
|
|
39
|
+
const validSnippet = `
|
|
40
|
+
import { StrictLogger } from "./src/logger-types";
|
|
41
|
+
declare const log: StrictLogger;
|
|
42
|
+
|
|
43
|
+
// Pino convention 1: string message only
|
|
44
|
+
log.info("hello world");
|
|
45
|
+
log.info("user %s count %d", "john", 42);
|
|
46
|
+
|
|
47
|
+
// Pino convention 2: object first, optional message
|
|
48
|
+
log.info({ userId: "user-123" }, "user login");
|
|
49
|
+
log.info({ userId: "user-123" });
|
|
50
|
+
|
|
51
|
+
// Errors
|
|
52
|
+
log.error(new Error("network failure"), "failed to reach remote");
|
|
53
|
+
log.error(new Error("network failure"));
|
|
54
|
+
log.warn({ warning: true }, "alert message");
|
|
55
|
+
log.debug({ debugData: 123 });
|
|
56
|
+
`;
|
|
57
|
+
|
|
58
|
+
const diags = getCompileDiagnostics(validSnippet);
|
|
59
|
+
assert.deepEqual(diags, [], "Expected valid Pino signatures to compile cleanly with 0 diagnostics");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("fails compilation when legacy Winston inverted call (msg, obj) is used", () => {
|
|
63
|
+
const invalidSnippet = `
|
|
64
|
+
import { StrictLogger } from "./src/logger-types";
|
|
65
|
+
declare const log: StrictLogger;
|
|
66
|
+
|
|
67
|
+
// Legacy Winston call: string first followed by object
|
|
68
|
+
log.info("user login", { userId: "user-123" });
|
|
69
|
+
`;
|
|
70
|
+
|
|
71
|
+
const diags = getCompileDiagnostics(invalidSnippet);
|
|
72
|
+
assert.isAbove(diags.length, 0, "Expected compilation to fail for inverted call");
|
|
73
|
+
assert.match(
|
|
74
|
+
diags.join("\n"),
|
|
75
|
+
/not assignable to parameter of type 'LogArgument'/,
|
|
76
|
+
"Expected error to indicate the object argument is not a LogArgument"
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("fails compilation when legacy inverted error call (msg, Error) is used", () => {
|
|
81
|
+
const invalidSnippet = `
|
|
82
|
+
import { StrictLogger } from "./src/logger-types";
|
|
83
|
+
declare const log: StrictLogger;
|
|
84
|
+
|
|
85
|
+
// Legacy inverted call: message first, Error object second
|
|
86
|
+
log.error("something failed", new Error("disk full"));
|
|
87
|
+
`;
|
|
88
|
+
|
|
89
|
+
const diags = getCompileDiagnostics(invalidSnippet);
|
|
90
|
+
assert.isAbove(diags.length, 0, "Expected compilation to fail for inverted error call");
|
|
91
|
+
assert.match(
|
|
92
|
+
diags.join("\n"),
|
|
93
|
+
/not assignable to parameter of type 'LogArgument'/,
|
|
94
|
+
"Expected error to indicate the Error argument is not a LogArgument"
|
|
95
|
+
);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("accepts a caught unknown error first on error, warn and fatal", () => {
|
|
99
|
+
const snippet = `
|
|
100
|
+
import { StrictLogger } from "./src/logger-types";
|
|
101
|
+
declare const log: StrictLogger;
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
JSON.parse("{");
|
|
105
|
+
} catch (err) {
|
|
106
|
+
log.error(err, "parse failed");
|
|
107
|
+
log.warn(err, "parse failed for %s", "input");
|
|
108
|
+
log.fatal(err);
|
|
109
|
+
}
|
|
110
|
+
`;
|
|
111
|
+
|
|
112
|
+
assert.deepEqual(getCompileDiagnostics(snippet), []);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("still rejects the inverted call when the second argument is unknown", () => {
|
|
116
|
+
const snippet = `
|
|
117
|
+
import { StrictLogger } from "./src/logger-types";
|
|
118
|
+
declare const log: StrictLogger;
|
|
119
|
+
declare const err: unknown;
|
|
120
|
+
|
|
121
|
+
log.error("something failed", err);
|
|
122
|
+
`;
|
|
123
|
+
|
|
124
|
+
assert.isAbove(getCompileDiagnostics(snippet).length, 0);
|
|
125
|
+
});
|
|
126
|
+
});
|