tabletcommand-logger 3.2.6 → 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.
- package/build/index.js +5 -1
- package/build/index.js.map +1 -1
- package/build/middleware/logger.js +132 -16
- 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/test/middleware-logger.js +253 -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/definitions/index.d.ts +1 -0
- package/definitions/index.d.ts.map +1 -1
- package/definitions/middleware/logger.d.ts +3 -0
- package/definitions/middleware/logger.d.ts.map +1 -1
- package/definitions/middleware/status-logger.d.ts +2 -2
- package/definitions/middleware/status-logger.d.ts.map +1 -1
- package/definitions/test/middleware-status-logger.d.ts +2 -0
- package/definitions/test/middleware-status-logger.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/middleware/logger.ts +146 -15
- package/src/middleware/status-logger.ts +5 -12
- package/src/test/middleware-logger.ts +263 -2
- package/src/test/middleware-status-logger.ts +61 -0
package/definitions/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import _loggerMiddleware from "./middleware/logger";
|
|
|
3
3
|
import _metricsMiddleware, { cleanUpParams } from "./middleware/metrics";
|
|
4
4
|
import _statusLogger from "./middleware/status-logger";
|
|
5
5
|
export { LoggerConfig } from "./winston-logger";
|
|
6
|
+
export { redactHeaders, redactOriginalURL, redactQuery } from "./middleware/logger";
|
|
6
7
|
export declare const logger: typeof _logger;
|
|
7
8
|
export declare const loggerMiddleware: typeof _loggerMiddleware;
|
|
8
9
|
export declare const metricsCleanUpParams: typeof cleanUpParams;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,kBAAkB,CAAC;AACvC,OAAO,iBAAiB,MAAM,qBAAqB,CAAC;AACpD,OAAO,kBAAkB,EAAE,EAAE,aAAa,EAAG,MAAM,sBAAsB,CAAC;AAC1E,OAAO,aAAa,MAAM,4BAA4B,CAAC;AAEvD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,kBAAkB,CAAC;AACvC,OAAO,iBAAiB,MAAM,qBAAqB,CAAC;AACpD,OAAO,kBAAkB,EAAE,EAAE,aAAa,EAAG,MAAM,sBAAsB,CAAC;AAC1E,OAAO,aAAa,MAAM,4BAA4B,CAAC;AAEvD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEpF,eAAO,MAAM,MAAM,gBAAU,CAAC;AAC9B,eAAO,MAAM,gBAAgB,0BAAoB,CAAC;AAClD,eAAO,MAAM,oBAAoB,sBAAgB,CAAC;AAClD,eAAO,MAAM,iBAAiB,2BAAqB,CAAC;AACpD,eAAO,MAAM,YAAY,sBAAgB,CAAC;;;;;;;;AAE1C,wBAME"}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { Logger } from "winston";
|
|
2
2
|
import { NextFunction, Request, Response } from "express";
|
|
3
|
+
import { Query } from "express-serve-static-core";
|
|
4
|
+
export declare function redactHeaders(headers: Request["headers"]): Request["headers"];
|
|
5
|
+
export declare function redactQuery(query: Query): Query;
|
|
3
6
|
export declare function redactOriginalURL(maybeURL?: string): string;
|
|
4
7
|
export default function loggerMiddleware(logger?: Logger): (req: Request, res: Response, next: NextFunction) => void;
|
|
5
8
|
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/middleware/logger.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EACL,YAAY,EACZ,OAAO,EACP,QAAQ,EACT,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/middleware/logger.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EACL,YAAY,EACZ,OAAO,EACP,QAAQ,EACT,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,KAAK,EAAE,MAAM,2BAA2B,CAAC;AA6FlD,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,CAU7E;AAYD,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAS/C;AAaD,wBAAgB,iBAAiB,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAc3D;AAED,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,MAAM,CAAC,EAAE,MAAM,IAClB,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY,UA4BpF"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Logger } from "winston";
|
|
2
2
|
import { NextFunction, Request, Response } from "express";
|
|
3
|
-
import {
|
|
3
|
+
import { redactQuery } from "./logger";
|
|
4
|
+
export { redactQuery };
|
|
4
5
|
export declare function shouldIgnore(req: Request, res: Response): boolean;
|
|
5
6
|
export declare function requestDuration(endTime: Date, startTime?: Date): number;
|
|
6
|
-
export declare function redactQuery(q: Query): import("qs").ParsedQs;
|
|
7
7
|
export default function statusLogger(logger?: Logger): (req: Request, res: Response, next: NextFunction) => void;
|
|
8
8
|
export type StatusLoggerModule = ReturnType<typeof statusLogger>;
|
|
9
9
|
//# sourceMappingURL=status-logger.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"status-logger.d.ts","sourceRoot":"","sources":["../../src/middleware/status-logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,OAAO,EACL,YAAY,EACZ,OAAO,EACP,QAAQ,EACT,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"status-logger.d.ts","sourceRoot":"","sources":["../../src/middleware/status-logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,OAAO,EACL,YAAY,EACZ,OAAO,EACP,QAAQ,EACT,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAoC,WAAW,EAAE,MAAM,UAAU,CAAC;AAEzE,OAAO,EAAE,WAAW,EAAE,CAAC;AAIvB,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAkBjE;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE,IAAI,EAAE,SAAS,CAAC,EAAE,IAAI,GAAG,MAAM,CAMvE;AAED,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,MAAM,CAAC,EAAE,MAAM,IACpB,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY,UAgE9E;AAED,MAAM,MAAM,kBAAkB,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware-status-logger.d.ts","sourceRoot":"","sources":["../../src/test/middleware-status-logger.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -4,6 +4,7 @@ import _metricsMiddleware, { cleanUpParams, } from "./middleware/metrics";
|
|
|
4
4
|
import _statusLogger from "./middleware/status-logger";
|
|
5
5
|
|
|
6
6
|
export { LoggerConfig } from "./winston-logger";
|
|
7
|
+
export { redactHeaders, redactOriginalURL, redactQuery } from "./middleware/logger";
|
|
7
8
|
|
|
8
9
|
export const logger = _logger;
|
|
9
10
|
export const loggerMiddleware = _loggerMiddleware;
|
package/src/middleware/logger.ts
CHANGED
|
@@ -5,26 +5,157 @@ import {
|
|
|
5
5
|
Request,
|
|
6
6
|
Response,
|
|
7
7
|
} from "express";
|
|
8
|
+
import { Query } from "express-serve-static-core";
|
|
8
9
|
|
|
10
|
+
// Each of these authenticates a request on its own, so a log line that keeps the whole
|
|
11
|
+
// value is a live credential. The header stays, cut to a 7-character prefix: enough to
|
|
12
|
+
// tell two keys apart, not enough to replay one. Node lowercases incoming header names.
|
|
13
|
+
const keyHeaders = [
|
|
14
|
+
"authorization",
|
|
15
|
+
"apikey",
|
|
16
|
+
"personnelapikey",
|
|
17
|
+
"x-tc-auth-token",
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
// tabletcommand-session reads each of these from the query string when the header is
|
|
21
|
+
// absent, and accepts either spelling. signupKey only ever arrives in the query.
|
|
22
|
+
const keyParams = [
|
|
23
|
+
"apikey",
|
|
24
|
+
"personnelapikey",
|
|
25
|
+
"signupkey",
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
// A route can wrap a key name in a longer parameter name, such as deviceApiKey, so match
|
|
29
|
+
// the name loosely. Cutting a parameter that holds no credential costs nothing.
|
|
30
|
+
function isKeyParam(name: string): boolean {
|
|
31
|
+
const lower = decodeName(name).toLowerCase();
|
|
32
|
+
return keyParams.some((keyParam) => lower.includes(keyParam));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Express decodes the name before tabletcommand-session reads it, so api%6Bey
|
|
36
|
+
// authenticates as apikey and has to be matched the same way. decodeURIComponent throws
|
|
37
|
+
// on a malformed escape, and this runs in a res "finish" listener where a throw ends the
|
|
38
|
+
// process.
|
|
39
|
+
function decodeName(name: string): string {
|
|
40
|
+
try {
|
|
41
|
+
return decodeURIComponent(name.replace(/\+/g, " "));
|
|
42
|
+
} catch {
|
|
43
|
+
return name;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const redacted = "<redacted>";
|
|
48
|
+
|
|
49
|
+
function keepPrefix(value: string): string {
|
|
50
|
+
return value.substring(0, 7);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Every scheme this service accepts is a short word, such as Bearer or Basic.
|
|
54
|
+
const schemePattern = /^[A-Za-z]{1,20}$/;
|
|
55
|
+
|
|
56
|
+
// A scheme fills the whole prefix on its own, so keep it and take the prefix from the
|
|
57
|
+
// credential behind it. A first word that is not a scheme is part of the credential, so
|
|
58
|
+
// cut the value whole rather than keep that word.
|
|
59
|
+
function redactAuthorization(value: string): string {
|
|
60
|
+
const [scheme, ...rest] = value.split(" ");
|
|
61
|
+
if (rest.length === 0 || !schemePattern.test(scheme)) {
|
|
62
|
+
return keepPrefix(value);
|
|
63
|
+
}
|
|
64
|
+
return `${scheme} ${keepPrefix(rest.join(" "))}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Cookie names answer "did the caller send a session at all", so they stay and the values go.
|
|
68
|
+
function redactCookie(value: string): string {
|
|
69
|
+
return value
|
|
70
|
+
.split(";")
|
|
71
|
+
.map((pair) => pair.split("=")[0].trim())
|
|
72
|
+
.filter((name) => name !== "")
|
|
73
|
+
.map((name) => `${name}=${redacted}`)
|
|
74
|
+
.join("; ");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function redactorFor(name: string): (value: string) => string {
|
|
78
|
+
if (name === "cookie") {
|
|
79
|
+
return redactCookie;
|
|
80
|
+
}
|
|
81
|
+
if (name === "authorization") {
|
|
82
|
+
return redactAuthorization;
|
|
83
|
+
}
|
|
84
|
+
return keepPrefix;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Node only gives a string or an array of strings, so anything else means other
|
|
88
|
+
// middleware wrote to req.headers. Drop it rather than throw: this runs in a res
|
|
89
|
+
// "finish" listener, where an exception ends the process.
|
|
90
|
+
function redactHeaderValue(name: string, value: unknown): string | string[] {
|
|
91
|
+
const redact = redactorFor(name);
|
|
92
|
+
if (_.isArray(value)) {
|
|
93
|
+
return value.map((entry: unknown) => _.isString(entry) ? redact(entry) : redacted);
|
|
94
|
+
}
|
|
95
|
+
if (!_.isString(value)) {
|
|
96
|
+
return redacted;
|
|
97
|
+
}
|
|
98
|
+
return redact(value);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function redactHeaders(headers: Request["headers"]): Request["headers"] {
|
|
102
|
+
const clean = { ...headers };
|
|
103
|
+
for (const [name, value] of Object.entries(clean)) {
|
|
104
|
+
const lower = name.toLowerCase();
|
|
105
|
+
if (_.isUndefined(value) || (lower !== "cookie" && !keyHeaders.includes(lower))) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
clean[name] = redactHeaderValue(lower, value);
|
|
109
|
+
}
|
|
110
|
+
return clean;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function redactQueryValue(value: Query[string]): Query[string] {
|
|
114
|
+
if (_.isString(value)) {
|
|
115
|
+
return keepPrefix(value);
|
|
116
|
+
}
|
|
117
|
+
if (_.isArray(value)) {
|
|
118
|
+
return value.map((entry: unknown) => _.isString(entry) ? keepPrefix(entry) : redacted);
|
|
119
|
+
}
|
|
120
|
+
return redacted;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function redactQuery(query: Query): Query {
|
|
124
|
+
const clean: Query = { ...query };
|
|
125
|
+
for (const [name, value] of Object.entries(clean)) {
|
|
126
|
+
if (_.isUndefined(value) || !isKeyParam(name)) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
clean[name] = redactQueryValue(value);
|
|
130
|
+
}
|
|
131
|
+
return clean;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function redactParamPair(pair: string): string {
|
|
135
|
+
const split = pair.indexOf("=");
|
|
136
|
+
if (split < 0 || !isKeyParam(pair.substring(0, split))) {
|
|
137
|
+
return pair;
|
|
138
|
+
}
|
|
139
|
+
return pair.substring(0, split + 1) + keepPrefix(pair.substring(split + 1));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// req.originalUrl is relative, so this splits the string rather than parsing a URL. A
|
|
143
|
+
// regex over the whole string backtracks for as long as the url, and the url comes from
|
|
144
|
+
// the caller. Splitting on "?" as well as "&" reaches a nested url in a parameter value.
|
|
9
145
|
export function redactOriginalURL(maybeURL?: string): string {
|
|
10
|
-
if (!maybeURL) {
|
|
146
|
+
if (!_.isString(maybeURL) || maybeURL === "") {
|
|
11
147
|
return "";
|
|
12
148
|
}
|
|
13
149
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const prevApiKey = href.searchParams.get("apikey");
|
|
18
|
-
if (prevApiKey && _.isString(prevApiKey)) {
|
|
19
|
-
href.searchParams.set("apikey", prevApiKey.substring(0, 7));
|
|
20
|
-
return href.toString();
|
|
21
|
-
}
|
|
22
|
-
} catch {
|
|
23
|
-
//
|
|
150
|
+
const queryStart = maybeURL.indexOf("?");
|
|
151
|
+
if (queryStart < 0) {
|
|
152
|
+
return maybeURL;
|
|
24
153
|
}
|
|
25
154
|
|
|
26
|
-
|
|
27
|
-
|
|
155
|
+
const path = maybeURL.substring(0, queryStart + 1);
|
|
156
|
+
const parts = maybeURL.substring(queryStart + 1).split(/([?&])/);
|
|
157
|
+
|
|
158
|
+
return path + parts.map((part, index) => index % 2 === 0 ? redactParamPair(part) : part).join("");
|
|
28
159
|
}
|
|
29
160
|
|
|
30
161
|
export default function loggerMiddleware(logger?: Logger) {
|
|
@@ -47,9 +178,9 @@ export default function loggerMiddleware(logger?: Logger) {
|
|
|
47
178
|
// Added as compatibility
|
|
48
179
|
req: {
|
|
49
180
|
body: req.body as unknown,
|
|
50
|
-
headers: req.headers,
|
|
181
|
+
headers: redactHeaders(req.headers),
|
|
51
182
|
method: req.method,
|
|
52
|
-
originalUrl: req.originalUrl
|
|
183
|
+
originalUrl: redactOriginalURL(req.originalUrl)
|
|
53
184
|
},
|
|
54
185
|
status: res.statusCode ? res.statusCode : 0
|
|
55
186
|
});
|
|
@@ -5,10 +5,11 @@ import {
|
|
|
5
5
|
Request,
|
|
6
6
|
Response,
|
|
7
7
|
} from "express";
|
|
8
|
-
import { Query } from "express-serve-static-core";
|
|
9
8
|
import _ from "lodash";
|
|
10
9
|
|
|
11
|
-
import { redactOriginalURL } from "./logger";
|
|
10
|
+
import { redactHeaders, redactOriginalURL, redactQuery } from "./logger";
|
|
11
|
+
|
|
12
|
+
export { redactQuery };
|
|
12
13
|
|
|
13
14
|
const allowedMethods = ["POST"];
|
|
14
15
|
|
|
@@ -40,14 +41,6 @@ export function requestDuration(endTime: Date, startTime?: Date): number {
|
|
|
40
41
|
return endTime.valueOf() - startTime.valueOf();
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
export function redactQuery(q: Query) {
|
|
44
|
-
if (_.isString(q.apikey)) {
|
|
45
|
-
q.apikey = q.apikey.substring(0, 7);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return q;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
44
|
export default function statusLogger(logger?: Logger) {
|
|
52
45
|
return function requestLogger(req: Request, res: Response, next: NextFunction) {
|
|
53
46
|
if (!_.isObject(req._startTime)) {
|
|
@@ -86,13 +79,13 @@ export default function statusLogger(logger?: Logger) {
|
|
|
86
79
|
const cleanReq = _.pick(req, [
|
|
87
80
|
"body",
|
|
88
81
|
"departmentLog",
|
|
89
|
-
"headers",
|
|
90
82
|
"httpVersion",
|
|
91
83
|
"method",
|
|
92
84
|
"originalUrl",
|
|
93
85
|
"path",
|
|
94
86
|
"query",
|
|
95
|
-
])
|
|
87
|
+
]) as Partial<Request>;
|
|
88
|
+
cleanReq.headers = redactHeaders(req.headers);
|
|
96
89
|
cleanReq.originalUrl = redactOriginalURL(req.originalUrl);
|
|
97
90
|
cleanReq.query = redactQuery(req.query);
|
|
98
91
|
|
|
@@ -1,8 +1,169 @@
|
|
|
1
1
|
import { assert } from "chai";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
2
3
|
import { describe, it, describe as context } from "node:test";
|
|
3
|
-
import {
|
|
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
|
});
|
|
@@ -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
|
+
});
|