tabletcommand-logger 3.2.5 → 3.2.7

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.
Files changed (36) hide show
  1. package/build/index.js +5 -1
  2. package/build/index.js.map +1 -1
  3. package/build/middleware/logger.js +132 -16
  4. package/build/middleware/logger.js.map +1 -1
  5. package/build/middleware/status-logger.js +5 -10
  6. package/build/middleware/status-logger.js.map +1 -1
  7. package/build/test/middleware-logger.js +259 -8
  8. package/build/test/middleware-logger.js.map +1 -1
  9. package/build/test/middleware-metrics.js +12 -12
  10. package/build/test/middleware-metrics.js.map +1 -1
  11. package/build/test/middleware-status-logger.js +58 -0
  12. package/build/test/middleware-status-logger.js.map +1 -0
  13. package/definitions/index.d.ts +1 -0
  14. package/definitions/index.d.ts.map +1 -1
  15. package/definitions/middleware/logger.d.ts +3 -0
  16. package/definitions/middleware/logger.d.ts.map +1 -1
  17. package/definitions/middleware/status-logger.d.ts +2 -2
  18. package/definitions/middleware/status-logger.d.ts.map +1 -1
  19. package/definitions/test/middleware-logger.d.ts +1 -1
  20. package/definitions/test/middleware-logger.d.ts.map +1 -1
  21. package/definitions/test/middleware-metrics.d.ts +1 -1
  22. package/definitions/test/middleware-metrics.d.ts.map +1 -1
  23. package/definitions/test/middleware-status-logger.d.ts +2 -0
  24. package/definitions/test/middleware-status-logger.d.ts.map +1 -0
  25. package/definitions/types/express.aug.d.ts.map +1 -1
  26. package/eslint.config.mjs +25 -8
  27. package/gulpfile.js +6 -2
  28. package/package.json +5 -9
  29. package/src/index.ts +1 -0
  30. package/src/middleware/logger.ts +146 -15
  31. package/src/middleware/status-logger.ts +5 -12
  32. package/src/test/middleware-logger.ts +264 -3
  33. package/src/test/middleware-metrics.ts +2 -1
  34. package/src/test/middleware-status-logger.ts +61 -0
  35. package/src/tsconfig.json +4 -1
  36. package/src/types/express.aug.ts +0 -1
@@ -1,8 +1,169 @@
1
- import "mocha";
2
1
  import { assert } from "chai";
3
- import { redactOriginalURL } from "../middleware/logger";
2
+ import { EventEmitter } from "node:events";
3
+ import { describe, it, describe as context } from "node:test";
4
+ import { Request, Response } from "express";
5
+ import { Logger } from "winston";
6
+ import loggerMiddleware, { redactHeaders, redactOriginalURL, redactQuery } from "../middleware/logger";
7
+
8
+ // cspell:ignore tcmobile
4
9
 
5
10
  describe("Logger Middleware", function() {
11
+ context("Redact Headers", () => {
12
+ // Sample values only. Every one is made up.
13
+ const sample = "abcdef1234567";
14
+ const other = "1234567abcdef";
15
+
16
+ it("cuts every key header to 7 characters", function() {
17
+ const sut = redactHeaders({
18
+ authorization: `Bearer ${sample}`,
19
+ apikey: sample,
20
+ personnelapikey: sample,
21
+ "x-tc-auth-token": sample,
22
+ });
23
+ assert.deepEqual(sut, {
24
+ authorization: "Bearer abcdef1",
25
+ apikey: "abcdef1",
26
+ personnelapikey: "abcdef1",
27
+ "x-tc-auth-token": "abcdef1",
28
+ });
29
+ });
30
+
31
+ it("keeps the authorization scheme and cuts the credential behind it", function() {
32
+ const sut = redactHeaders({ authorization: `Basic ${sample}` });
33
+ assert.deepEqual(sut, { authorization: "Basic abcdef1" });
34
+ });
35
+
36
+ it("cuts an authorization header that carries no scheme", function() {
37
+ const sut = redactHeaders({ authorization: sample });
38
+ assert.deepEqual(sut, { authorization: "abcdef1" });
39
+ });
40
+
41
+ // The first word was kept whole as a scheme, which logged a credential that holds a
42
+ // space.
43
+ it("cuts an authorization credential that holds a space", function() {
44
+ const sut = redactHeaders({ authorization: "abcdef1234567 7654321fedcba" });
45
+ assert.deepEqual(sut, { authorization: "abcdef1" });
46
+ });
47
+
48
+ it("keeps the cookie names and drops the values", function() {
49
+ const sut = redactHeaders({
50
+ cookie: `seneca-login=${sample}; saml=${other}`,
51
+ });
52
+ assert.deepEqual(sut, {
53
+ cookie: "seneca-login=<redacted>; saml=<redacted>",
54
+ });
55
+ });
56
+
57
+ it("skips the empty segment of a cookie that ends in a semicolon", function() {
58
+ const sut = redactHeaders({ cookie: "seneca-login=abc;" });
59
+ assert.deepEqual(sut, { cookie: "seneca-login=<redacted>" });
60
+ });
61
+
62
+ it("returns an empty cookie unchanged", function() {
63
+ const sut = redactHeaders({ cookie: "" });
64
+ assert.deepEqual(sut, { cookie: "" });
65
+ });
66
+
67
+ it("matches the header name without case", function() {
68
+ const sut = redactHeaders({
69
+ Cookie: `seneca-login=${sample}`,
70
+ ApiKey: sample,
71
+ } as unknown as Record<string, string>);
72
+ assert.deepEqual(sut, {
73
+ Cookie: "seneca-login=<redacted>",
74
+ ApiKey: "abcdef1",
75
+ });
76
+ });
77
+
78
+ it("redacts every value of a repeated header", function() {
79
+ const sut = redactHeaders({ apikey: [sample, other] });
80
+ assert.deepEqual(sut, { apikey: ["abcdef1", "1234567"] });
81
+ });
82
+
83
+ it("leaves the headers that identify the client", function() {
84
+ const sut = redactHeaders({
85
+ "user-agent": "TCMobile 3.0.3 b216",
86
+ "x-tc-bundle-identifier": "com.tabletcommand.tcmobile",
87
+ });
88
+ assert.deepEqual(sut, {
89
+ "user-agent": "TCMobile 3.0.3 b216",
90
+ "x-tc-bundle-identifier": "com.tabletcommand.tcmobile",
91
+ });
92
+ });
93
+
94
+ // This runs in a res "finish" listener, where a throw ends the process.
95
+ it("drops a value that is not a string instead of throwing", function() {
96
+ const sut = redactHeaders({
97
+ apikey: 12345,
98
+ cookie: { name: "seneca-login" },
99
+ authorization: null,
100
+ personnelapikey: [sample, 12345],
101
+ } as unknown as Record<string, string>);
102
+ assert.deepEqual(sut, {
103
+ apikey: "<redacted>",
104
+ cookie: "<redacted>",
105
+ authorization: "<redacted>",
106
+ personnelapikey: ["abcdef1", "<redacted>"],
107
+ });
108
+ });
109
+
110
+ it("leaves the request headers alone", function() {
111
+ const headers = { apikey: sample, cookie: `seneca-login=${sample}` };
112
+ redactHeaders(headers);
113
+ assert.deepEqual(headers, { apikey: sample, cookie: `seneca-login=${sample}` });
114
+ });
115
+ });
116
+
117
+ context("Redact Query", () => {
118
+ const sample = "abcdef1234567";
119
+
120
+ it("cuts every key parameter to 7 characters", function() {
121
+ const sut = redactQuery({
122
+ apikey: sample,
123
+ personnelApiKey: sample,
124
+ signupKey: sample,
125
+ });
126
+ assert.deepEqual(sut, {
127
+ apikey: "abcdef1",
128
+ personnelApiKey: "abcdef1",
129
+ signupKey: "abcdef1",
130
+ });
131
+ });
132
+
133
+ it("leaves the parameters that are not credentials", function() {
134
+ const sut = redactQuery({ v: "4", shared: "1" });
135
+ assert.deepEqual(sut, { v: "4", shared: "1" });
136
+ });
137
+
138
+ it("cuts a parameter that wraps a key name", function() {
139
+ const sut = redactQuery({ deviceApiKey: sample, apikeyHash: sample });
140
+ assert.deepEqual(sut, { deviceApiKey: "abcdef1", apikeyHash: "abcdef1" });
141
+ });
142
+
143
+ it("does not throw on a malformed escape in a parameter name", function() {
144
+ const sut = redactQuery({ "%ZZ": "4" });
145
+ assert.deepEqual(sut, { "%ZZ": "4" });
146
+ });
147
+
148
+ it("cuts every value of a repeated parameter", function() {
149
+ const sut = redactQuery({ apikey: [sample, "7654321fedcba"] });
150
+ assert.deepEqual(sut, { apikey: ["abcdef1", "7654321"] });
151
+ });
152
+
153
+ it("drops a value that is not a string", function() {
154
+ const sut = redactQuery({ apikey: { nested: sample } });
155
+ assert.deepEqual(sut, { apikey: "<redacted>" });
156
+ });
157
+
158
+ // redactQuery used to write the short key back onto req.query, which every handler
159
+ // after the logger reads.
160
+ it("leaves the request query alone", function() {
161
+ const query = { apikey: sample };
162
+ redactQuery(query);
163
+ assert.deepEqual(query, { apikey: sample });
164
+ });
165
+ });
166
+
6
167
  context("Redact Original URL", () => {
7
168
  const url = "http://local:3442/incident?";
8
169
  it("apikey is last", function() {
@@ -21,9 +182,109 @@ describe("Logger Middleware", function() {
21
182
 
22
183
  it("apikey is short", function() {
23
184
  const prevApiKey = "apikey=1&";
24
- const expApiKey = "apikey=1";
185
+ const expApiKey = "apikey=1&";
25
186
  const redacted = redactOriginalURL(url + prevApiKey);
26
187
  assert.equal(redacted, url + expApiKey);
27
188
  });
189
+
190
+ // req.originalUrl is what both loggers pass in, and it is always relative.
191
+ it("cuts the apikey of a relative url", function() {
192
+ const redacted = redactOriginalURL("/api/v1/incident?apikey=abcdef1234567");
193
+ assert.equal(redacted, "/api/v1/incident?apikey=abcdef1");
194
+ });
195
+
196
+ it("cuts every key parameter, in either spelling", function() {
197
+ const redacted = redactOriginalURL(
198
+ "/api/v2/personnel?personnelApiKey=abcdef1234567&signupkey=1234567abcdef&v=4"
199
+ );
200
+ assert.equal(redacted, "/api/v2/personnel?personnelApiKey=abcdef1&signupkey=1234567&v=4");
201
+ });
202
+
203
+ it("cuts more than one key in the same url", function() {
204
+ const redacted = redactOriginalURL("/api/v2/personnel?apikey=abcdef1234567&personnelApiKey=1234567abcdef");
205
+ assert.equal(redacted, "/api/v2/personnel?apikey=abcdef1&personnelApiKey=1234567");
206
+ });
207
+
208
+ it("cuts a parameter that wraps a key name", function() {
209
+ const redacted = redactOriginalURL("/api/v1/incident?departmentApikey=abcdef1234567&apikeyHash=abcdef1234567");
210
+ assert.equal(redacted, "/api/v1/incident?departmentApikey=abcdef1&apikeyHash=abcdef1");
211
+ });
212
+
213
+ it("leaves a parameter whose value holds a key name", function() {
214
+ const redacted = redactOriginalURL("/api/v1/incident?sort=apikey&v=4");
215
+ assert.equal(redacted, "/api/v1/incident?sort=apikey&v=4");
216
+ });
217
+
218
+ // Express decodes the name, so api%6Bey reaches tabletcommand-session as apikey.
219
+ it("cuts a key parameter whose name is encoded", function() {
220
+ const redacted = redactOriginalURL("/api/v1/incident?api%6Bey=abcdef1234567");
221
+ assert.equal(redacted, "/api/v1/incident?api%6Bey=abcdef1");
222
+ });
223
+
224
+ it("cuts the key of a url nested in a parameter value", function() {
225
+ const redacted = redactOriginalURL("/login?next=/api/v1/incident?apikey=abcdef1234567");
226
+ assert.equal(redacted, "/login?next=/api/v1/incident?apikey=abcdef1");
227
+ });
228
+
229
+ it("leaves a url that carries no query", function() {
230
+ assert.equal(redactOriginalURL("/api/v1/incident"), "/api/v1/incident");
231
+ });
232
+
233
+ it("leaves a parameter that carries no value", function() {
234
+ assert.equal(redactOriginalURL("/api/v1/incident?apikey&v=4"), "/api/v1/incident?apikey&v=4");
235
+ });
236
+
237
+ // decodeURIComponent throws on a malformed escape, and this runs where a throw ends
238
+ // the process.
239
+ it("does not throw on a malformed escape in a parameter name", function() {
240
+ assert.equal(redactOriginalURL("/api/v1/incident?%ZZ=1&apikey=abcdef1234567"),
241
+ "/api/v1/incident?%ZZ=1&apikey=abcdef1");
242
+ });
243
+
244
+ // A regex over the whole url backtracked for as long as the url.
245
+ it("redacts a long url without backtracking", function() {
246
+ const start = process.hrtime.bigint();
247
+ redactOriginalURL("/x?" + "apikey".repeat(20000));
248
+ const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6;
249
+ assert.isBelow(elapsedMs, 100);
250
+ });
251
+
252
+ it("returns an empty string for a missing url", function() {
253
+ assert.equal(redactOriginalURL(), "");
254
+ assert.equal(redactOriginalURL(""), "");
255
+ });
256
+ });
257
+
258
+ context("Access Log", () => {
259
+ function logLine(req: Partial<Request>): Record<string, unknown> {
260
+ let line: Record<string, unknown> = {};
261
+ const logger = {
262
+ info: (msg: Record<string, unknown>) => {
263
+ line = msg;
264
+ },
265
+ } as unknown as Logger;
266
+ const res = new EventEmitter() as unknown as Response;
267
+ res.statusCode = 200;
268
+
269
+ loggerMiddleware(logger)(req as Request, res, () => undefined);
270
+ res.emit("finish");
271
+ return line;
272
+ }
273
+
274
+ it("redacts both copies of the url", function() {
275
+ const line = logLine({
276
+ method: "GET",
277
+ originalUrl: "/api/v1/incident?apikey=abcdef1234567",
278
+ headers: { apikey: "abcdef1234567" },
279
+ });
280
+
281
+ assert.equal(line.url, "/api/v1/incident?apikey=abcdef1");
282
+ assert.deepEqual(line.req, {
283
+ body: undefined,
284
+ headers: { apikey: "abcdef1" },
285
+ method: "GET",
286
+ originalUrl: "/api/v1/incident?apikey=abcdef1",
287
+ });
288
+ });
28
289
  });
29
290
  });
@@ -1,7 +1,8 @@
1
- import "mocha";
2
1
  import { assert } from "chai";
2
+ import { describe, it, describe as context } from "node:test";
3
3
  import { Request } from "express";
4
4
  import MetricsModule, { cleanUpParams } from "../middleware/metrics";
5
+
5
6
  const metrics = MetricsModule();
6
7
 
7
8
  describe("Metrics Middleware", function() {
@@ -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 { Logger } from "winston";
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 Logger;
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
+ });
package/src/tsconfig.json CHANGED
@@ -21,6 +21,9 @@
21
21
  "resolveJsonModule": true,
22
22
  "sourceMap": true,
23
23
  "strict": true,
24
- "target": "es2018"
24
+ "target": "es2019",
25
+ "types": [
26
+ "node"
27
+ ]
25
28
  }
26
29
  }
@@ -2,7 +2,6 @@
2
2
  export { };
3
3
 
4
4
  declare global {
5
- // eslint-disable-next-line @typescript-eslint/no-namespace
6
5
  namespace Express {
7
6
  interface Request {
8
7
  statsdKey: string;