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
|
@@ -1,8 +1,194 @@
|
|
|
1
1
|
import { assert } from "chai";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
import http from "node:http";
|
|
2
4
|
import { describe, it, describe as context } from "node:test";
|
|
3
|
-
import {
|
|
5
|
+
import { Writable } from "node:stream";
|
|
6
|
+
import { Request, Response } from "express";
|
|
7
|
+
import { StrictLogger } from "../logger-types";
|
|
8
|
+
import loggerMiddleware, { redactHeaders, redactOriginalURL, redactQuery } from "../middleware/logger";
|
|
9
|
+
import logger from "../pino-logger";
|
|
10
|
+
|
|
11
|
+
// cspell:ignore tcmobile
|
|
4
12
|
|
|
5
13
|
describe("Logger Middleware", function() {
|
|
14
|
+
context("Redact Headers", () => {
|
|
15
|
+
// Sample values only. Every one is made up.
|
|
16
|
+
const sample = "abcdef1234567";
|
|
17
|
+
const other = "1234567abcdef";
|
|
18
|
+
|
|
19
|
+
it("cuts every key header to 7 characters", function() {
|
|
20
|
+
const sut = redactHeaders({
|
|
21
|
+
authorization: `Bearer ${sample}`,
|
|
22
|
+
apikey: sample,
|
|
23
|
+
personnelapikey: sample,
|
|
24
|
+
"x-tc-auth-token": sample,
|
|
25
|
+
});
|
|
26
|
+
assert.deepEqual(sut, {
|
|
27
|
+
authorization: "Bearer abcdef1",
|
|
28
|
+
apikey: "abcdef1",
|
|
29
|
+
personnelapikey: "abcdef1",
|
|
30
|
+
"x-tc-auth-token": "abcdef1",
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("keeps the authorization scheme and cuts the credential behind it", function() {
|
|
35
|
+
const sut = redactHeaders({ authorization: `Basic ${sample}` });
|
|
36
|
+
assert.deepEqual(sut, { authorization: "Basic abcdef1" });
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("cuts an authorization header that carries no scheme", function() {
|
|
40
|
+
const sut = redactHeaders({ authorization: sample });
|
|
41
|
+
assert.deepEqual(sut, { authorization: "abcdef1" });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// The first word was kept whole as a scheme, which logged a credential that holds a
|
|
45
|
+
// space.
|
|
46
|
+
it("cuts an authorization credential that holds a space", function() {
|
|
47
|
+
const sut = redactHeaders({ authorization: "abcdef1234567 7654321fedcba" });
|
|
48
|
+
assert.deepEqual(sut, { authorization: "abcdef1" });
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("keeps the cookie names and drops the values", function() {
|
|
52
|
+
const sut = redactHeaders({
|
|
53
|
+
cookie: `seneca-login=${sample}; saml=${other}`,
|
|
54
|
+
});
|
|
55
|
+
assert.deepEqual(sut, {
|
|
56
|
+
cookie: "seneca-login=<redacted>; saml=<redacted>",
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("skips the empty segment of a cookie that ends in a semicolon", function() {
|
|
61
|
+
const sut = redactHeaders({ cookie: "seneca-login=abc;" });
|
|
62
|
+
assert.deepEqual(sut, { cookie: "seneca-login=<redacted>" });
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("returns an empty cookie unchanged", function() {
|
|
66
|
+
const sut = redactHeaders({ cookie: "" });
|
|
67
|
+
assert.deepEqual(sut, { cookie: "" });
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("redacts set-cookie header leaving attributes intact", function() {
|
|
71
|
+
const sutString = redactHeaders({
|
|
72
|
+
"set-cookie": "session=SECRET_TOKEN; Path=/; Domain=.tabletcommand.com; Max-Age=3600; HttpOnly; Secure; SameSite=None",
|
|
73
|
+
} as unknown as Request["headers"]);
|
|
74
|
+
assert.deepEqual(sutString, {
|
|
75
|
+
"set-cookie": "session=<redacted>; Path=/; Domain=.tabletcommand.com; Max-Age=3600; HttpOnly; Secure; SameSite=None",
|
|
76
|
+
} as unknown as Request["headers"]);
|
|
77
|
+
|
|
78
|
+
const sutArray = redactHeaders({
|
|
79
|
+
"set-cookie": [
|
|
80
|
+
"session=SECRET123; Path=/; HttpOnly; Secure; SameSite=None",
|
|
81
|
+
"remember=VAL456; Path=/; HttpOnly",
|
|
82
|
+
],
|
|
83
|
+
});
|
|
84
|
+
assert.deepEqual(sutArray, {
|
|
85
|
+
"set-cookie": [
|
|
86
|
+
"session=<redacted>; Path=/; HttpOnly; Secure; SameSite=None",
|
|
87
|
+
"remember=<redacted>; Path=/; HttpOnly",
|
|
88
|
+
],
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("matches the header name without case", function() {
|
|
93
|
+
const sut = redactHeaders({
|
|
94
|
+
Cookie: `seneca-login=${sample}`,
|
|
95
|
+
ApiKey: sample,
|
|
96
|
+
} as unknown as Record<string, string>);
|
|
97
|
+
assert.deepEqual(sut, {
|
|
98
|
+
Cookie: "seneca-login=<redacted>",
|
|
99
|
+
ApiKey: "abcdef1",
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("redacts every value of a repeated header", function() {
|
|
104
|
+
const sut = redactHeaders({ apikey: [sample, other] });
|
|
105
|
+
assert.deepEqual(sut, { apikey: ["abcdef1", "1234567"] });
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("leaves the headers that identify the client", function() {
|
|
109
|
+
const sut = redactHeaders({
|
|
110
|
+
"user-agent": "TCMobile 3.0.3 b216",
|
|
111
|
+
"x-tc-bundle-identifier": "com.tabletcommand.tcmobile",
|
|
112
|
+
});
|
|
113
|
+
assert.deepEqual(sut, {
|
|
114
|
+
"user-agent": "TCMobile 3.0.3 b216",
|
|
115
|
+
"x-tc-bundle-identifier": "com.tabletcommand.tcmobile",
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// This runs in a res "finish" listener, where a throw ends the process.
|
|
120
|
+
it("drops a value that is not a string instead of throwing", function() {
|
|
121
|
+
const sut = redactHeaders({
|
|
122
|
+
apikey: 12345,
|
|
123
|
+
cookie: { name: "seneca-login" },
|
|
124
|
+
authorization: null,
|
|
125
|
+
personnelapikey: [sample, 12345],
|
|
126
|
+
} as unknown as Record<string, string>);
|
|
127
|
+
assert.deepEqual(sut, {
|
|
128
|
+
apikey: "<redacted>",
|
|
129
|
+
cookie: "<redacted>",
|
|
130
|
+
authorization: "<redacted>",
|
|
131
|
+
personnelapikey: ["abcdef1", "<redacted>"],
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("leaves the request headers alone", function() {
|
|
136
|
+
const headers = { apikey: sample, cookie: `seneca-login=${sample}` };
|
|
137
|
+
redactHeaders(headers);
|
|
138
|
+
assert.deepEqual(headers, { apikey: sample, cookie: `seneca-login=${sample}` });
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
context("Redact Query", () => {
|
|
143
|
+
const sample = "abcdef1234567";
|
|
144
|
+
|
|
145
|
+
it("cuts every key parameter to 7 characters", function() {
|
|
146
|
+
const sut = redactQuery({
|
|
147
|
+
apikey: sample,
|
|
148
|
+
personnelApiKey: sample,
|
|
149
|
+
signupKey: sample,
|
|
150
|
+
});
|
|
151
|
+
assert.deepEqual(sut, {
|
|
152
|
+
apikey: "abcdef1",
|
|
153
|
+
personnelApiKey: "abcdef1",
|
|
154
|
+
signupKey: "abcdef1",
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("leaves the parameters that are not credentials", function() {
|
|
159
|
+
const sut = redactQuery({ v: "4", shared: "1" });
|
|
160
|
+
assert.deepEqual(sut, { v: "4", shared: "1" });
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("cuts a parameter that wraps a key name", function() {
|
|
164
|
+
const sut = redactQuery({ deviceApiKey: sample, apikeyHash: sample });
|
|
165
|
+
assert.deepEqual(sut, { deviceApiKey: "abcdef1", apikeyHash: "abcdef1" });
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("does not throw on a malformed escape in a parameter name", function() {
|
|
169
|
+
const sut = redactQuery({ "%ZZ": "4" });
|
|
170
|
+
assert.deepEqual(sut, { "%ZZ": "4" });
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("cuts every value of a repeated parameter", function() {
|
|
174
|
+
const sut = redactQuery({ apikey: [sample, "7654321fedcba"] });
|
|
175
|
+
assert.deepEqual(sut, { apikey: ["abcdef1", "7654321"] });
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("drops a value that is not a string", function() {
|
|
179
|
+
const sut = redactQuery({ apikey: { nested: sample } });
|
|
180
|
+
assert.deepEqual(sut, { apikey: "<redacted>" });
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// redactQuery used to write the short key back onto req.query, which every handler
|
|
184
|
+
// after the logger reads.
|
|
185
|
+
it("leaves the request query alone", function() {
|
|
186
|
+
const query = { apikey: sample };
|
|
187
|
+
redactQuery(query);
|
|
188
|
+
assert.deepEqual(query, { apikey: sample });
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
6
192
|
context("Redact Original URL", () => {
|
|
7
193
|
const url = "http://local:3442/incident?";
|
|
8
194
|
it("apikey is last", function() {
|
|
@@ -21,9 +207,403 @@ describe("Logger Middleware", function() {
|
|
|
21
207
|
|
|
22
208
|
it("apikey is short", function() {
|
|
23
209
|
const prevApiKey = "apikey=1&";
|
|
24
|
-
const expApiKey = "apikey=1";
|
|
210
|
+
const expApiKey = "apikey=1&";
|
|
25
211
|
const redacted = redactOriginalURL(url + prevApiKey);
|
|
26
212
|
assert.equal(redacted, url + expApiKey);
|
|
27
213
|
});
|
|
214
|
+
|
|
215
|
+
// req.originalUrl is what both loggers pass in, and it is always relative.
|
|
216
|
+
it("cuts the apikey of a relative url", function() {
|
|
217
|
+
const redacted = redactOriginalURL("/api/v1/incident?apikey=abcdef1234567");
|
|
218
|
+
assert.equal(redacted, "/api/v1/incident?apikey=abcdef1");
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("cuts every key parameter, in either spelling", function() {
|
|
222
|
+
const redacted = redactOriginalURL(
|
|
223
|
+
"/api/v2/personnel?personnelApiKey=abcdef1234567&signupkey=1234567abcdef&v=4"
|
|
224
|
+
);
|
|
225
|
+
assert.equal(redacted, "/api/v2/personnel?personnelApiKey=abcdef1&signupkey=1234567&v=4");
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("cuts more than one key in the same url", function() {
|
|
229
|
+
const redacted = redactOriginalURL("/api/v2/personnel?apikey=abcdef1234567&personnelApiKey=1234567abcdef");
|
|
230
|
+
assert.equal(redacted, "/api/v2/personnel?apikey=abcdef1&personnelApiKey=1234567");
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("cuts a parameter that wraps a key name", function() {
|
|
234
|
+
const redacted = redactOriginalURL("/api/v1/incident?departmentApikey=abcdef1234567&apikeyHash=abcdef1234567");
|
|
235
|
+
assert.equal(redacted, "/api/v1/incident?departmentApikey=abcdef1&apikeyHash=abcdef1");
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("leaves a parameter whose value holds a key name", function() {
|
|
239
|
+
const redacted = redactOriginalURL("/api/v1/incident?sort=apikey&v=4");
|
|
240
|
+
assert.equal(redacted, "/api/v1/incident?sort=apikey&v=4");
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// Express decodes the name, so api%6Bey reaches tabletcommand-session as apikey.
|
|
244
|
+
it("cuts a key parameter whose name is encoded", function() {
|
|
245
|
+
const redacted = redactOriginalURL("/api/v1/incident?api%6Bey=abcdef1234567");
|
|
246
|
+
assert.equal(redacted, "/api/v1/incident?api%6Bey=abcdef1");
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("cuts the key of a url nested in a parameter value", function() {
|
|
250
|
+
const redacted = redactOriginalURL("/login?next=/api/v1/incident?apikey=abcdef1234567");
|
|
251
|
+
assert.equal(redacted, "/login?next=/api/v1/incident?apikey=abcdef1");
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it("leaves a url that carries no query", function() {
|
|
255
|
+
assert.equal(redactOriginalURL("/api/v1/incident"), "/api/v1/incident");
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("leaves a parameter that carries no value", function() {
|
|
259
|
+
assert.equal(redactOriginalURL("/api/v1/incident?apikey&v=4"), "/api/v1/incident?apikey&v=4");
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// decodeURIComponent throws on a malformed escape, and this runs where a throw ends
|
|
263
|
+
// the process.
|
|
264
|
+
it("does not throw on a malformed escape in a parameter name", function() {
|
|
265
|
+
assert.equal(redactOriginalURL("/api/v1/incident?%ZZ=1&apikey=abcdef1234567"),
|
|
266
|
+
"/api/v1/incident?%ZZ=1&apikey=abcdef1");
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
// A regex over the whole url backtracked for as long as the url.
|
|
270
|
+
it("redacts a long url without backtracking", function() {
|
|
271
|
+
const start = process.hrtime.bigint();
|
|
272
|
+
redactOriginalURL("/x?" + "apikey".repeat(20000));
|
|
273
|
+
const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6;
|
|
274
|
+
assert.isBelow(elapsedMs, 100);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("returns an empty string for a missing url", function() {
|
|
278
|
+
assert.equal(redactOriginalURL(), "");
|
|
279
|
+
assert.equal(redactOriginalURL(""), "");
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
context("Access Log", () => {
|
|
284
|
+
function logLine(req: Partial<Request>): Record<string, unknown> {
|
|
285
|
+
let line: Record<string, unknown> = {};
|
|
286
|
+
const logger = {
|
|
287
|
+
info: (msg: Record<string, unknown>) => {
|
|
288
|
+
line = msg;
|
|
289
|
+
},
|
|
290
|
+
} as unknown as StrictLogger;
|
|
291
|
+
const res = new EventEmitter() as unknown as Response;
|
|
292
|
+
res.statusCode = 200;
|
|
293
|
+
|
|
294
|
+
loggerMiddleware(logger)(req as Request, res, () => undefined);
|
|
295
|
+
res.emit("finish");
|
|
296
|
+
return line;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
it("redacts both copies of the url", function() {
|
|
300
|
+
const line = logLine({
|
|
301
|
+
method: "GET",
|
|
302
|
+
originalUrl: "/api/v1/incident?apikey=abcdef1234567",
|
|
303
|
+
headers: { apikey: "abcdef1234567" },
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
assert.equal(line.url, "/api/v1/incident?apikey=abcdef1");
|
|
307
|
+
assert.deepEqual(line.req, {
|
|
308
|
+
body: undefined,
|
|
309
|
+
headers: { apikey: "abcdef1" },
|
|
310
|
+
method: "GET",
|
|
311
|
+
originalUrl: "/api/v1/incident?apikey=abcdef1",
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
context("pino-http integration with real logger", () => {
|
|
317
|
+
it("redacts response set-cookie and authorization headers (H1)", async () => {
|
|
318
|
+
const lines: Record<string, unknown>[] = [];
|
|
319
|
+
const capture = new Writable({
|
|
320
|
+
write(chunk: Buffer | string, _encoding: string, callback: (error?: Error | null) => void) {
|
|
321
|
+
try {
|
|
322
|
+
lines.push(JSON.parse(chunk.toString()) as Record<string, unknown>);
|
|
323
|
+
} catch {
|
|
324
|
+
// ignore
|
|
325
|
+
}
|
|
326
|
+
callback();
|
|
327
|
+
},
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
const log = logger({ name: "test-response-redaction" }, capture);
|
|
331
|
+
const middleware = loggerMiddleware(log as StrictLogger);
|
|
332
|
+
|
|
333
|
+
const server = http.createServer((req, res) => {
|
|
334
|
+
middleware(req as unknown as Request, res as unknown as Response, () => {
|
|
335
|
+
res.setHeader(
|
|
336
|
+
"set-cookie",
|
|
337
|
+
"session=SECRET; Path=/; Domain=.tabletcommand.com; Max-Age=3600; HttpOnly; Secure; SameSite=None"
|
|
338
|
+
);
|
|
339
|
+
res.setHeader("authorization", "Bearer SECRET12345");
|
|
340
|
+
res.statusCode = 200;
|
|
341
|
+
res.end("ok");
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
await new Promise<void>((resolve, reject) => {
|
|
346
|
+
server.listen(0, () => {
|
|
347
|
+
const address = server.address();
|
|
348
|
+
if (!address || typeof address === "string") {
|
|
349
|
+
server.close();
|
|
350
|
+
return reject(new Error("Unable to bind test server"));
|
|
351
|
+
}
|
|
352
|
+
const port = address.port;
|
|
353
|
+
http.get(`http://127.0.0.1:${port}/test`, (res) => {
|
|
354
|
+
res.resume();
|
|
355
|
+
res.on("end", () => {
|
|
356
|
+
server.close(() => resolve());
|
|
357
|
+
});
|
|
358
|
+
}).on("error", (err) => {
|
|
359
|
+
server.close(() => reject(err));
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
assert.equal(lines.length, 1);
|
|
365
|
+
const line = lines[0];
|
|
366
|
+
const resObj = line.res as { statusCode: number; headers: Record<string, unknown> };
|
|
367
|
+
assert.isDefined(resObj);
|
|
368
|
+
assert.equal(resObj.statusCode, 200);
|
|
369
|
+
const setCookie = String(resObj.headers["set-cookie"]);
|
|
370
|
+
assert.include(setCookie, "session=<redacted>");
|
|
371
|
+
assert.notInclude(setCookie, "SECRET");
|
|
372
|
+
assert.include(setCookie, "SameSite=None");
|
|
373
|
+
assert.include(setCookie, "HttpOnly");
|
|
374
|
+
assert.include(setCookie, "Secure");
|
|
375
|
+
assert.include(setCookie, "Path=/");
|
|
376
|
+
assert.include(setCookie, "Domain=.tabletcommand.com");
|
|
377
|
+
assert.include(setCookie, "Max-Age=3600");
|
|
378
|
+
assert.equal(resObj.headers.authorization, "Bearer SECRET1");
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it("redacts credentials in request login body (M2)", async () => {
|
|
382
|
+
const lines: Record<string, unknown>[] = [];
|
|
383
|
+
const capture = new Writable({
|
|
384
|
+
write(chunk: Buffer | string, _encoding: string, callback: (error?: Error | null) => void) {
|
|
385
|
+
try {
|
|
386
|
+
lines.push(JSON.parse(chunk.toString()) as Record<string, unknown>);
|
|
387
|
+
} catch {
|
|
388
|
+
// ignore
|
|
389
|
+
}
|
|
390
|
+
callback();
|
|
391
|
+
},
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
const log = logger({ name: "test-login-body" }, capture);
|
|
395
|
+
const middleware = loggerMiddleware(log as StrictLogger);
|
|
396
|
+
|
|
397
|
+
const server = http.createServer((req, res) => {
|
|
398
|
+
(req as Request).body = {
|
|
399
|
+
username: "alice",
|
|
400
|
+
password: "mySecretPassword123",
|
|
401
|
+
};
|
|
402
|
+
middleware(req as unknown as Request, res as unknown as Response, () => {
|
|
403
|
+
res.statusCode = 200;
|
|
404
|
+
res.end("ok");
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
await new Promise<void>((resolve, reject) => {
|
|
409
|
+
server.listen(0, () => {
|
|
410
|
+
const address = server.address();
|
|
411
|
+
if (!address || typeof address === "string") {
|
|
412
|
+
server.close();
|
|
413
|
+
return reject(new Error("Unable to bind test server"));
|
|
414
|
+
}
|
|
415
|
+
const port = address.port;
|
|
416
|
+
const postReq = http.request(
|
|
417
|
+
`http://127.0.0.1:${port}/login`,
|
|
418
|
+
{ method: "POST" },
|
|
419
|
+
(res) => {
|
|
420
|
+
res.resume();
|
|
421
|
+
res.on("end", () => {
|
|
422
|
+
server.close(() => resolve());
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
);
|
|
426
|
+
postReq.on("error", (err) => {
|
|
427
|
+
server.close(() => reject(err));
|
|
428
|
+
});
|
|
429
|
+
postReq.end();
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
assert.equal(lines.length, 1);
|
|
434
|
+
const line = lines[0];
|
|
435
|
+
const reqObj = line.req as { body: { username: string; password: string } };
|
|
436
|
+
assert.isDefined(reqObj);
|
|
437
|
+
assert.equal(reqObj.body.username, "alice");
|
|
438
|
+
assert.equal(reqObj.body.password, "<redacted>");
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it("redacts secrets inside array of objects in request body (M2)", async () => {
|
|
442
|
+
const lines: Record<string, unknown>[] = [];
|
|
443
|
+
const capture = new Writable({
|
|
444
|
+
write(chunk: Buffer | string, _encoding: string, callback: (error?: Error | null) => void) {
|
|
445
|
+
try {
|
|
446
|
+
lines.push(JSON.parse(chunk.toString()) as Record<string, unknown>);
|
|
447
|
+
} catch {
|
|
448
|
+
// ignore
|
|
449
|
+
}
|
|
450
|
+
callback();
|
|
451
|
+
},
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
const log = logger({ name: "test-array-body" }, capture);
|
|
455
|
+
const middleware = loggerMiddleware(log as StrictLogger);
|
|
456
|
+
|
|
457
|
+
const server = http.createServer((req, res) => {
|
|
458
|
+
(req as Request).body = {
|
|
459
|
+
items: [
|
|
460
|
+
{ apikey: "abcdef1234567" },
|
|
461
|
+
],
|
|
462
|
+
};
|
|
463
|
+
middleware(req as unknown as Request, res as unknown as Response, () => {
|
|
464
|
+
res.statusCode = 200;
|
|
465
|
+
res.end("ok");
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
await new Promise<void>((resolve, reject) => {
|
|
470
|
+
server.listen(0, () => {
|
|
471
|
+
const address = server.address();
|
|
472
|
+
if (!address || typeof address === "string") {
|
|
473
|
+
server.close();
|
|
474
|
+
return reject(new Error("Unable to bind test server"));
|
|
475
|
+
}
|
|
476
|
+
const port = address.port;
|
|
477
|
+
const postReq = http.request(
|
|
478
|
+
`http://127.0.0.1:${port}/items`,
|
|
479
|
+
{ method: "POST" },
|
|
480
|
+
(res) => {
|
|
481
|
+
res.resume();
|
|
482
|
+
res.on("end", () => {
|
|
483
|
+
server.close(() => resolve());
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
);
|
|
487
|
+
postReq.on("error", (err) => {
|
|
488
|
+
server.close(() => reject(err));
|
|
489
|
+
});
|
|
490
|
+
postReq.end();
|
|
491
|
+
});
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
assert.equal(lines.length, 1);
|
|
495
|
+
const line = lines[0];
|
|
496
|
+
const reqObj = line.req as { body: { items: { apikey: string }[] } };
|
|
497
|
+
assert.isDefined(reqObj);
|
|
498
|
+
assert.equal(reqObj.body.items[0].apikey, "abcdef1");
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
it("logs a body parsed after the middleware, and each field once", async () => {
|
|
502
|
+
const raw: string[] = [];
|
|
503
|
+
const capture = new Writable({
|
|
504
|
+
write(chunk: Buffer | string, _encoding: string, callback: (error?: Error | null) => void) {
|
|
505
|
+
raw.push(chunk.toString());
|
|
506
|
+
callback();
|
|
507
|
+
},
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
const log = logger({ name: "test-late-body" }, capture);
|
|
511
|
+
const middleware = loggerMiddleware(log as StrictLogger);
|
|
512
|
+
|
|
513
|
+
const server = http.createServer((req, res) => {
|
|
514
|
+
middleware(req as unknown as Request, res as unknown as Response, () => {
|
|
515
|
+
// Stands in for express.json() mounted after the logger.
|
|
516
|
+
(req as Request).body = { username: "alice", password: "mySecretPassword123" };
|
|
517
|
+
res.statusCode = 404;
|
|
518
|
+
res.end("missing");
|
|
519
|
+
});
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
await new Promise<void>((resolve, reject) => {
|
|
523
|
+
server.listen(0, () => {
|
|
524
|
+
const address = server.address();
|
|
525
|
+
if (!address || typeof address === "string") {
|
|
526
|
+
server.close();
|
|
527
|
+
return reject(new Error("Unable to bind test server"));
|
|
528
|
+
}
|
|
529
|
+
const postReq = http.request(
|
|
530
|
+
`http://127.0.0.1:${address.port}/login?apikey=abcdef1234567`,
|
|
531
|
+
{ method: "POST" },
|
|
532
|
+
(res) => {
|
|
533
|
+
res.resume();
|
|
534
|
+
res.on("end", () => {
|
|
535
|
+
server.close(() => resolve());
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
);
|
|
539
|
+
postReq.on("error", (err) => {
|
|
540
|
+
server.close(() => reject(err));
|
|
541
|
+
});
|
|
542
|
+
postReq.end();
|
|
543
|
+
});
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
assert.equal(raw.length, 1);
|
|
547
|
+
const text = raw[0];
|
|
548
|
+
// Top-level keys only: "method" also appears inside req.
|
|
549
|
+
for (const key of ["status", "url", "remoteAddress", "httpVersion", "req"]) {
|
|
550
|
+
const count = text.split(`"${key}":`).length - 1;
|
|
551
|
+
assert.equal(count, 1, `expected one "${key}" key in ${text}`);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
const line = JSON.parse(text) as Record<string, unknown>;
|
|
555
|
+
assert.equal(line.status, 404);
|
|
556
|
+
assert.equal(line.method, "POST");
|
|
557
|
+
assert.equal(line.url, "/login?apikey=abcdef1");
|
|
558
|
+
const reqObj = line.req as { body: { username: string; password: string }; originalUrl: string };
|
|
559
|
+
assert.equal(reqObj.body.username, "alice");
|
|
560
|
+
assert.equal(reqObj.body.password, "<redacted>");
|
|
561
|
+
assert.equal(reqObj.originalUrl, "/login?apikey=abcdef1");
|
|
562
|
+
assert.equal((line.res as { statusCode: number }).statusCode, 404);
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
it("gives handlers a req.log that writes through the logger", async () => {
|
|
566
|
+
const lines: Record<string, unknown>[] = [];
|
|
567
|
+
const capture = new Writable({
|
|
568
|
+
write(chunk: Buffer | string, _encoding: string, callback: (error?: Error | null) => void) {
|
|
569
|
+
lines.push(JSON.parse(chunk.toString()) as Record<string, unknown>);
|
|
570
|
+
callback();
|
|
571
|
+
},
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
const log = logger({ name: "test-req-log" }, capture);
|
|
575
|
+
const middleware = loggerMiddleware(log as StrictLogger);
|
|
576
|
+
|
|
577
|
+
const server = http.createServer((req, res) => {
|
|
578
|
+
middleware(req as unknown as Request, res as unknown as Response, () => {
|
|
579
|
+
(req as Request).log.info({ step: "handler" }, "inside handler");
|
|
580
|
+
res.end("ok");
|
|
581
|
+
});
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
await new Promise<void>((resolve, reject) => {
|
|
585
|
+
server.listen(0, () => {
|
|
586
|
+
const address = server.address();
|
|
587
|
+
if (!address || typeof address === "string") {
|
|
588
|
+
server.close();
|
|
589
|
+
return reject(new Error("Unable to bind test server"));
|
|
590
|
+
}
|
|
591
|
+
http.get(`http://127.0.0.1:${address.port}/x`, (res) => {
|
|
592
|
+
res.resume();
|
|
593
|
+
res.on("end", () => {
|
|
594
|
+
server.close(() => resolve());
|
|
595
|
+
});
|
|
596
|
+
}).on("error", (err) => {
|
|
597
|
+
server.close(() => reject(err));
|
|
598
|
+
});
|
|
599
|
+
});
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
assert.equal(lines.length, 2);
|
|
603
|
+
assert.equal(lines[0].msg, "inside handler");
|
|
604
|
+
assert.equal(lines[0].service, "test-req-log");
|
|
605
|
+
assert.isDefined(lines[0].reqId);
|
|
606
|
+
assert.equal(lines[0].reqId, lines[1].reqId);
|
|
607
|
+
});
|
|
28
608
|
});
|
|
29
609
|
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { assert } from "chai";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
import { describe, it, describe as context } from "node:test";
|
|
4
|
+
import { Request, Response } from "express";
|
|
5
|
+
import { StrictLogger } from "../logger-types";
|
|
6
|
+
import statusLogger from "../middleware/status-logger";
|
|
7
|
+
|
|
8
|
+
describe("Status Logger Middleware", function() {
|
|
9
|
+
context("Request Log", () => {
|
|
10
|
+
// Sample values only. Every one is made up.
|
|
11
|
+
const sample = "abcdef1234567";
|
|
12
|
+
|
|
13
|
+
function logLine(req: Partial<Request>): Record<string, unknown> {
|
|
14
|
+
let line: Record<string, unknown> = {};
|
|
15
|
+
const logger = {
|
|
16
|
+
info: (msg: Record<string, unknown>) => {
|
|
17
|
+
line = msg;
|
|
18
|
+
},
|
|
19
|
+
} as unknown as StrictLogger;
|
|
20
|
+
const res = new EventEmitter() as unknown as Response;
|
|
21
|
+
res.statusCode = 200;
|
|
22
|
+
res.json = () => res;
|
|
23
|
+
|
|
24
|
+
statusLogger(logger)(req as Request, res, () => undefined);
|
|
25
|
+
res.emit("finish");
|
|
26
|
+
return line;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// server-status authenticates POST /api/v2/personnel by a key it accepts in the query.
|
|
30
|
+
it("cuts the credentials out of the url, the query, and the headers", function() {
|
|
31
|
+
const line = logLine({
|
|
32
|
+
method: "POST",
|
|
33
|
+
originalUrl: `/api/v2/personnel?personnelApiKey=${sample}&v=4`,
|
|
34
|
+
path: "/api/v2/personnel",
|
|
35
|
+
query: { personnelApiKey: sample, v: "4" },
|
|
36
|
+
headers: { "user-agent": "TCMobile 3.0.3 b216", personnelapikey: sample },
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const req = line.req as Record<string, unknown>;
|
|
40
|
+
assert.equal(req.originalUrl, "/api/v2/personnel?personnelApiKey=abcdef1&v=4");
|
|
41
|
+
assert.deepEqual(req.query, { personnelApiKey: "abcdef1", v: "4" });
|
|
42
|
+
assert.deepEqual(req.headers, {
|
|
43
|
+
"user-agent": "TCMobile 3.0.3 b216",
|
|
44
|
+
personnelapikey: "abcdef1",
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("leaves the request query alone for the handlers that run after it", function() {
|
|
49
|
+
const query = { personnelApiKey: sample };
|
|
50
|
+
logLine({
|
|
51
|
+
method: "POST",
|
|
52
|
+
originalUrl: `/api/v2/personnel?personnelApiKey=${sample}`,
|
|
53
|
+
path: "/api/v2/personnel",
|
|
54
|
+
query,
|
|
55
|
+
headers: {},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
assert.deepEqual(query, { personnelApiKey: sample });
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|