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,14 +1,13 @@
|
|
|
1
|
-
import { Logger } from "winston";
|
|
2
|
-
|
|
3
1
|
import {
|
|
4
2
|
NextFunction,
|
|
5
3
|
Request,
|
|
6
4
|
Response,
|
|
7
5
|
} from "express";
|
|
8
|
-
import { Query } from "express-serve-static-core";
|
|
9
6
|
import _ from "lodash";
|
|
7
|
+
import { StrictLogger } from "../logger-types";
|
|
8
|
+
import { redactHeaders, redactOriginalURL, redactQuery } from "./logger";
|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
export { redactQuery };
|
|
12
11
|
|
|
13
12
|
const allowedMethods = ["POST"];
|
|
14
13
|
|
|
@@ -40,15 +39,7 @@ export function requestDuration(endTime: Date, startTime?: Date): number {
|
|
|
40
39
|
return endTime.valueOf() - startTime.valueOf();
|
|
41
40
|
}
|
|
42
41
|
|
|
43
|
-
export function
|
|
44
|
-
if (_.isString(q.apikey)) {
|
|
45
|
-
q.apikey = q.apikey.substring(0, 7);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return q;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export default function statusLogger(logger?: Logger) {
|
|
42
|
+
export default function statusLogger(logger?: StrictLogger | null) {
|
|
52
43
|
return function requestLogger(req: Request, res: Response, next: NextFunction) {
|
|
53
44
|
if (!_.isObject(req._startTime)) {
|
|
54
45
|
req._startTime = new Date();
|
|
@@ -86,13 +77,13 @@ export default function statusLogger(logger?: Logger) {
|
|
|
86
77
|
const cleanReq = _.pick(req, [
|
|
87
78
|
"body",
|
|
88
79
|
"departmentLog",
|
|
89
|
-
"headers",
|
|
90
80
|
"httpVersion",
|
|
91
81
|
"method",
|
|
92
82
|
"originalUrl",
|
|
93
83
|
"path",
|
|
94
84
|
"query",
|
|
95
|
-
])
|
|
85
|
+
]) as Partial<Request>;
|
|
86
|
+
cleanReq.headers = redactHeaders(req.headers);
|
|
96
87
|
cleanReq.originalUrl = redactOriginalURL(req.originalUrl);
|
|
97
88
|
cleanReq.query = redactQuery(req.query);
|
|
98
89
|
|
package/src/otel.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export interface OtelSpanContext {
|
|
2
|
+
trace_id?: string;
|
|
3
|
+
span_id?: string;
|
|
4
|
+
trace_flags?: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// Cached module reference so we only require once
|
|
8
|
+
let otelLoaded = false;
|
|
9
|
+
let otelTrace: typeof import("@opentelemetry/api").trace | null = null;
|
|
10
|
+
|
|
11
|
+
function getOtelTrace(): typeof import("@opentelemetry/api").trace | null {
|
|
12
|
+
if (!otelLoaded) {
|
|
13
|
+
otelLoaded = true;
|
|
14
|
+
try {
|
|
15
|
+
const otel = require("@opentelemetry/api") as typeof import("@opentelemetry/api");
|
|
16
|
+
otelTrace = otel?.trace ?? null;
|
|
17
|
+
} catch {
|
|
18
|
+
otelTrace = null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return otelTrace;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function resetOtelForTesting(): void {
|
|
25
|
+
otelLoaded = false;
|
|
26
|
+
otelTrace = null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function getOtelTraceContext(): OtelSpanContext {
|
|
30
|
+
try {
|
|
31
|
+
const trace = getOtelTrace();
|
|
32
|
+
if (!trace) {
|
|
33
|
+
return {};
|
|
34
|
+
}
|
|
35
|
+
const span = trace.getActiveSpan();
|
|
36
|
+
if (!span) {
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
const spanContext = span.spanContext();
|
|
40
|
+
const isValid =
|
|
41
|
+
typeof trace.isSpanContextValid === "function"
|
|
42
|
+
? trace.isSpanContextValid(spanContext)
|
|
43
|
+
: spanContext?.traceId !== "00000000000000000000000000000000" &&
|
|
44
|
+
spanContext?.spanId !== "0000000000000000";
|
|
45
|
+
|
|
46
|
+
if (spanContext && spanContext.traceId && spanContext.spanId && isValid) {
|
|
47
|
+
return {
|
|
48
|
+
trace_id: spanContext.traceId,
|
|
49
|
+
span_id: spanContext.spanId,
|
|
50
|
+
...(spanContext.traceFlags !== undefined ? { trace_flags: String(spanContext.traceFlags) } : {}),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
} catch {
|
|
54
|
+
// Optional dependency, ignore any errors
|
|
55
|
+
}
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import pino, { LoggerOptions, DestinationStream } from "pino";
|
|
2
|
+
import { LoggerConfig, StrictLogger, LoggerModule } from "./logger-types";
|
|
3
|
+
import { redactSecrets } from "./redaction";
|
|
4
|
+
import { getOtelTraceContext } from "./otel";
|
|
5
|
+
|
|
6
|
+
export { LoggerConfig, StrictLogger, LoggerModule };
|
|
7
|
+
|
|
8
|
+
export function isPinoPrettyAvailable(): boolean {
|
|
9
|
+
try {
|
|
10
|
+
require.resolve("pino-pretty");
|
|
11
|
+
return true;
|
|
12
|
+
} catch {
|
|
13
|
+
try {
|
|
14
|
+
require.resolve("pino-pretty", { paths: [process.cwd()] });
|
|
15
|
+
return true;
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default function logger(
|
|
23
|
+
config: LoggerConfig,
|
|
24
|
+
customDestination?: DestinationStream
|
|
25
|
+
): StrictLogger | null {
|
|
26
|
+
// v4 always writes to stdout. Only return null when the caller explicitly
|
|
27
|
+
// turns every sink off (back-compat with the old winston "no transports" case).
|
|
28
|
+
const explicitlyAllOff =
|
|
29
|
+
config.logToConsole === false &&
|
|
30
|
+
config.logToFile !== true &&
|
|
31
|
+
config.logToDatadog !== true;
|
|
32
|
+
const loggingEnabled = !explicitlyAllOff;
|
|
33
|
+
|
|
34
|
+
if (!loggingEnabled) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const envPretty =
|
|
39
|
+
process.env.PINO_PRETTY !== undefined
|
|
40
|
+
? process.env.PINO_PRETTY === "true"
|
|
41
|
+
: undefined;
|
|
42
|
+
|
|
43
|
+
// Pretty output is opt-in: an unset NODE_ENV must not turn production logs into
|
|
44
|
+
// colorized text that the Datadog Agent cannot parse.
|
|
45
|
+
const isDev = process.env.NODE_ENV === "development" && !process.env.CI;
|
|
46
|
+
const wantPretty = config.pretty ?? envPretty ?? isDev;
|
|
47
|
+
const usePretty = wantPretty && isPinoPrettyAvailable();
|
|
48
|
+
|
|
49
|
+
const base: Record<string, unknown> = {
|
|
50
|
+
...config.extraMeta,
|
|
51
|
+
service: config.name,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const options: LoggerOptions = {
|
|
55
|
+
level: config.level || process.env.LOG_LEVEL || "info",
|
|
56
|
+
base,
|
|
57
|
+
// Redaction walks each log object and each set of child bindings once, matching key
|
|
58
|
+
// names without case. pino's redact paths match one exact key per path, and the
|
|
59
|
+
// wildcard paths needed to reach nested bodies cost ~85µs a line.
|
|
60
|
+
formatters: {
|
|
61
|
+
level: (label: string) => ({ level: label }),
|
|
62
|
+
log: (obj: Record<string, unknown>) => redactSecrets(obj),
|
|
63
|
+
bindings: (bindings: pino.Bindings) => redactSecrets(bindings),
|
|
64
|
+
},
|
|
65
|
+
// formatters.log runs before serializers, so a serialized error is redacted here.
|
|
66
|
+
serializers: {
|
|
67
|
+
// stdSerializers.err builds on a prototype object, so copy its fields to a plain
|
|
68
|
+
// object for the walk to enter.
|
|
69
|
+
err: (err: Error) => redactSecrets({ ...pino.stdSerializers.err(err) }),
|
|
70
|
+
},
|
|
71
|
+
timestamp: pino.stdTimeFunctions.isoTime,
|
|
72
|
+
mixin() {
|
|
73
|
+
return getOtelTraceContext();
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// pino-pretty runs in a worker thread, so a failure there surfaces asynchronously and
|
|
78
|
+
// cannot be caught here. isPinoPrettyAvailable() above is the guard that matters.
|
|
79
|
+
if (usePretty && !customDestination) {
|
|
80
|
+
return redactChildBindings(pino({
|
|
81
|
+
...options,
|
|
82
|
+
transport: {
|
|
83
|
+
target: "pino-pretty",
|
|
84
|
+
options: {
|
|
85
|
+
colorize: true,
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const pinoInstance = customDestination
|
|
92
|
+
? pino(options, customDestination)
|
|
93
|
+
: pino(options, process.stdout);
|
|
94
|
+
|
|
95
|
+
return redactChildBindings(pinoInstance);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// pino applies formatters.bindings to the root logger only; child() resets it. A child is
|
|
99
|
+
// created with Object.create(parent), so overriding child once here covers every
|
|
100
|
+
// descendant, including the ones pino-http creates per request.
|
|
101
|
+
type ChildFn = (this: pino.Logger, bindings: pino.Bindings, options?: pino.ChildLoggerOptions) => pino.Logger;
|
|
102
|
+
|
|
103
|
+
function redactChildBindings(instance: pino.Logger): StrictLogger {
|
|
104
|
+
const pinoChild = instance.child as unknown as ChildFn;
|
|
105
|
+
const child: ChildFn = function child(bindings, childOptions) {
|
|
106
|
+
return pinoChild.call(this, redactSecrets(bindings), childOptions);
|
|
107
|
+
};
|
|
108
|
+
instance.child = child as unknown as pino.Logger["child"];
|
|
109
|
+
return instance as unknown as StrictLogger;
|
|
110
|
+
}
|
package/src/redaction.ts
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import _ from "lodash";
|
|
2
|
+
import { Request } from "express";
|
|
3
|
+
import { Query } from "express-serve-static-core";
|
|
4
|
+
|
|
5
|
+
// Each of these authenticates a request on its own, so a log line that keeps the whole
|
|
6
|
+
// value is a live credential. The header stays, cut to a 7-character prefix: enough to
|
|
7
|
+
// tell two keys apart, not enough to replay one. Node lowercases incoming header names.
|
|
8
|
+
export const keyHeaders = [
|
|
9
|
+
"authorization",
|
|
10
|
+
"apikey",
|
|
11
|
+
"personnelapikey",
|
|
12
|
+
"x-tc-auth-token",
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
// tabletcommand-session reads each of these from the query string when the header is
|
|
16
|
+
// absent, and accepts either spelling. signupKey only ever arrives in the query.
|
|
17
|
+
export const keyParams = [
|
|
18
|
+
"apikey",
|
|
19
|
+
"personnelapikey",
|
|
20
|
+
"signupkey",
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
// A route can wrap a key name in a longer parameter name, such as deviceApiKey, so match
|
|
24
|
+
// the name loosely. Cutting a parameter that holds no credential costs nothing.
|
|
25
|
+
export function isKeyParam(name: string): boolean {
|
|
26
|
+
const lower = decodeName(name).toLowerCase();
|
|
27
|
+
return keyParams.some((keyParam) => lower.includes(keyParam));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Express decodes the name before tabletcommand-session reads it, so api%6Bey
|
|
31
|
+
// authenticates as apikey and has to be matched the same way. decodeURIComponent throws
|
|
32
|
+
// on a malformed escape, and this runs in a res "finish" listener where a throw ends the
|
|
33
|
+
// process.
|
|
34
|
+
export function decodeName(name: string): string {
|
|
35
|
+
try {
|
|
36
|
+
return decodeURIComponent(name.replace(/\+/g, " "));
|
|
37
|
+
} catch {
|
|
38
|
+
return name;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const redacted = "<redacted>";
|
|
43
|
+
|
|
44
|
+
export function keepPrefix(value: string): string {
|
|
45
|
+
return value.substring(0, 7);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Every scheme this service accepts is a short word, such as Bearer or Basic.
|
|
49
|
+
const schemePattern = /^[A-Za-z]{1,20}$/;
|
|
50
|
+
|
|
51
|
+
// A scheme fills the whole prefix on its own, so keep it and take the prefix from the
|
|
52
|
+
// credential behind it. A first word that is not a scheme is part of the credential, so
|
|
53
|
+
// cut the value whole rather than keep that word.
|
|
54
|
+
export function redactAuthorization(value: string): string {
|
|
55
|
+
const [scheme, ...rest] = value.split(" ");
|
|
56
|
+
if (rest.length === 0 || !schemePattern.test(scheme)) {
|
|
57
|
+
return keepPrefix(value);
|
|
58
|
+
}
|
|
59
|
+
return `${scheme} ${keepPrefix(rest.join(" "))}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Cookie names answer "did the caller send a session at all", so they stay and the values go.
|
|
63
|
+
export function redactCookie(value: string): string {
|
|
64
|
+
return value
|
|
65
|
+
.split(";")
|
|
66
|
+
.map((pair) => pair.split("=")[0].trim())
|
|
67
|
+
.filter((name) => name !== "")
|
|
68
|
+
.map((name) => `${name}=${redacted}`)
|
|
69
|
+
.join("; ");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Set-Cookie header contains the cookie pair first, followed by optional attributes
|
|
73
|
+
// (e.g. "session=SECRET; Path=/; Domain=.tabletcommand.com; Max-Age=3600; HttpOnly; Secure; SameSite=None").
|
|
74
|
+
// Redact only the first pair's value while keeping attributes intact for security auditing.
|
|
75
|
+
export function redactSetCookie(value: string): string {
|
|
76
|
+
if (!value || value.trim() === "") {
|
|
77
|
+
return "";
|
|
78
|
+
}
|
|
79
|
+
const parts = value.split(";");
|
|
80
|
+
const first = parts[0].trim();
|
|
81
|
+
if (first === "") {
|
|
82
|
+
return "";
|
|
83
|
+
}
|
|
84
|
+
const equalsIndex = first.indexOf("=");
|
|
85
|
+
const firstRedacted =
|
|
86
|
+
equalsIndex < 0
|
|
87
|
+
? `${first}=${redacted}`
|
|
88
|
+
: `${first.substring(0, equalsIndex).trim()}=${redacted}`;
|
|
89
|
+
|
|
90
|
+
const attributes = parts
|
|
91
|
+
.slice(1)
|
|
92
|
+
.map((attr) => attr.trim())
|
|
93
|
+
.filter((attr) => attr !== "");
|
|
94
|
+
|
|
95
|
+
if (attributes.length === 0) {
|
|
96
|
+
return firstRedacted;
|
|
97
|
+
}
|
|
98
|
+
return [firstRedacted, ...attributes].join("; ");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function redactorFor(name: string): (value: string) => string {
|
|
102
|
+
if (name === "cookie") {
|
|
103
|
+
return redactCookie;
|
|
104
|
+
}
|
|
105
|
+
if (name === "set-cookie") {
|
|
106
|
+
return redactSetCookie;
|
|
107
|
+
}
|
|
108
|
+
if (name === "authorization") {
|
|
109
|
+
return redactAuthorization;
|
|
110
|
+
}
|
|
111
|
+
return keepPrefix;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Node only gives a string or an array of strings, so anything else means other
|
|
115
|
+
// middleware wrote to req.headers. Drop it rather than throw: this runs in a res
|
|
116
|
+
// "finish" listener, where an exception ends the process.
|
|
117
|
+
export function redactHeaderValue(name: string, value: unknown): string | string[] {
|
|
118
|
+
const redact = redactorFor(name);
|
|
119
|
+
if (_.isArray(value)) {
|
|
120
|
+
return value.map((entry: unknown) => (_.isString(entry) ? redact(entry) : redacted));
|
|
121
|
+
}
|
|
122
|
+
if (!_.isString(value)) {
|
|
123
|
+
return redacted;
|
|
124
|
+
}
|
|
125
|
+
return redact(value);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function redactHeaders(headers: Request["headers"]): Request["headers"] {
|
|
129
|
+
const clean = { ...headers };
|
|
130
|
+
for (const [name, value] of Object.entries(clean)) {
|
|
131
|
+
const lower = name.toLowerCase();
|
|
132
|
+
const isCookie = lower === "cookie" || lower === "set-cookie";
|
|
133
|
+
if (_.isUndefined(value) || (!isCookie && !keyHeaders.includes(lower))) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
clean[name] = redactHeaderValue(lower, value);
|
|
137
|
+
}
|
|
138
|
+
return clean;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function redactQueryValue(value: Query[string]): Query[string] {
|
|
142
|
+
if (_.isString(value)) {
|
|
143
|
+
return keepPrefix(value);
|
|
144
|
+
}
|
|
145
|
+
if (_.isArray(value)) {
|
|
146
|
+
return value.map((entry: unknown) => (_.isString(entry) ? keepPrefix(entry) : redacted));
|
|
147
|
+
}
|
|
148
|
+
return redacted;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function redactQuery(query: Query): Query {
|
|
152
|
+
const clean: Query = { ...query };
|
|
153
|
+
for (const [name, value] of Object.entries(clean)) {
|
|
154
|
+
if (_.isUndefined(value) || !isKeyParam(name)) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
clean[name] = redactQueryValue(value);
|
|
158
|
+
}
|
|
159
|
+
return clean;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function redactParamPair(pair: string): string {
|
|
163
|
+
const split = pair.indexOf("=");
|
|
164
|
+
if (split < 0 || !isKeyParam(pair.substring(0, split))) {
|
|
165
|
+
return pair;
|
|
166
|
+
}
|
|
167
|
+
return pair.substring(0, split + 1) + keepPrefix(pair.substring(split + 1));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// req.originalUrl is relative, so this splits the string rather than parsing a URL. A
|
|
171
|
+
// regex over the whole string backtracks for as long as the url, and the url comes from
|
|
172
|
+
// the caller. Splitting on "?" as well as "&" reaches a nested url in a parameter value.
|
|
173
|
+
export function redactOriginalURL(maybeURL?: string): string {
|
|
174
|
+
if (!_.isString(maybeURL) || maybeURL === "") {
|
|
175
|
+
return "";
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const queryStart = maybeURL.indexOf("?");
|
|
179
|
+
if (queryStart < 0) {
|
|
180
|
+
return maybeURL;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const path = maybeURL.substring(0, queryStart + 1);
|
|
184
|
+
const parts = maybeURL.substring(queryStart + 1).split(/([?&])/);
|
|
185
|
+
|
|
186
|
+
return path + parts.map((part, index) => (index % 2 === 0 ? redactParamPair(part) : part)).join("");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Key names are compared lowercased with "-" and "_" removed, so authToken, auth_token,
|
|
190
|
+
// AUTH-TOKEN and x-api-key all match without listing each spelling. The names come from an
|
|
191
|
+
// audit of massive-tyrion and tabletcommand-backend-models (request bodies, model fields,
|
|
192
|
+
// outgoing request configs).
|
|
193
|
+
export function normalizeKey(key: string): string {
|
|
194
|
+
return key.toLowerCase().replace(/[-_]/g, "");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Values under these keys go entirely: a prefix of a password, private key, SSN or door
|
|
198
|
+
// code gives away too much of it.
|
|
199
|
+
const fullKeyParts = [
|
|
200
|
+
"password",
|
|
201
|
+
"passwd",
|
|
202
|
+
"passphrase",
|
|
203
|
+
"secret",
|
|
204
|
+
"credential",
|
|
205
|
+
"privatekey",
|
|
206
|
+
"signingkey",
|
|
207
|
+
"encryptionkey",
|
|
208
|
+
];
|
|
209
|
+
const fullKeys = new Set([
|
|
210
|
+
"pass", // user password hash
|
|
211
|
+
"pwd",
|
|
212
|
+
"authpass",
|
|
213
|
+
"salt",
|
|
214
|
+
"ssn",
|
|
215
|
+
"dateofbirth",
|
|
216
|
+
"licensenumber",
|
|
217
|
+
"doorcode",
|
|
218
|
+
]);
|
|
219
|
+
|
|
220
|
+
// Values under these keys keep a 7-character prefix, enough to tell two keys apart.
|
|
221
|
+
const prefixKeyParts = [
|
|
222
|
+
"apikey", // apiKey, departmentApiKey, watchDutyApiKey, x-api-key, apiKeyReadOnly, ...
|
|
223
|
+
];
|
|
224
|
+
const prefixKeys = new Set([
|
|
225
|
+
"signupkey",
|
|
226
|
+
"partnerkey",
|
|
227
|
+
"xpartnerkey",
|
|
228
|
+
"cadkey",
|
|
229
|
+
"vendorkey",
|
|
230
|
+
"licensekey",
|
|
231
|
+
"licensecode",
|
|
232
|
+
"mark43key",
|
|
233
|
+
"authkey",
|
|
234
|
+
"accesscode",
|
|
235
|
+
"joincode",
|
|
236
|
+
"jwt",
|
|
237
|
+
"authheader",
|
|
238
|
+
"sessioncookie",
|
|
239
|
+
"readonlysessioncookie",
|
|
240
|
+
"authtokenplaintext",
|
|
241
|
+
]);
|
|
242
|
+
|
|
243
|
+
// Pagination cursors end in "token" but carry no credential.
|
|
244
|
+
const notSecretTokens = ["pagetoken", "paginationtoken"];
|
|
245
|
+
|
|
246
|
+
export type SecretKind = "full" | "prefix" | "authorization" | "cookie" | "set-cookie";
|
|
247
|
+
|
|
248
|
+
export function secretKind(key: string): SecretKind | null {
|
|
249
|
+
const name = normalizeKey(key);
|
|
250
|
+
if (name === "authorization") {
|
|
251
|
+
return "authorization";
|
|
252
|
+
}
|
|
253
|
+
if (name === "cookie" || name === "cookies") {
|
|
254
|
+
return "cookie";
|
|
255
|
+
}
|
|
256
|
+
if (name === "setcookie") {
|
|
257
|
+
return "set-cookie";
|
|
258
|
+
}
|
|
259
|
+
if (fullKeys.has(name) || fullKeyParts.some((part) => name.includes(part))) {
|
|
260
|
+
return "full";
|
|
261
|
+
}
|
|
262
|
+
if (prefixKeys.has(name) || prefixKeyParts.some((part) => name.includes(part))) {
|
|
263
|
+
return "prefix";
|
|
264
|
+
}
|
|
265
|
+
// accessToken, refresh_token, idToken, sessionToken, x-tc-auth-token, deviceToken, ...
|
|
266
|
+
if (name.endsWith("token") && !notSecretTokens.some((part) => name.endsWith(part))) {
|
|
267
|
+
return "prefix";
|
|
268
|
+
}
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function redactString(kind: SecretKind, value: string): string {
|
|
273
|
+
switch (kind) {
|
|
274
|
+
case "full":
|
|
275
|
+
return redacted;
|
|
276
|
+
case "authorization":
|
|
277
|
+
return redactAuthorization(value);
|
|
278
|
+
case "cookie":
|
|
279
|
+
return redactCookie(value);
|
|
280
|
+
case "set-cookie":
|
|
281
|
+
return redactSetCookie(value);
|
|
282
|
+
default:
|
|
283
|
+
return keepPrefix(value);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// Booleans, numbers and null under a secret name are flags and settings (hasAuthSecret,
|
|
288
|
+
// minPasswordLength), not secrets, so they stay readable.
|
|
289
|
+
export function redactSecretValue(kind: SecretKind, value: unknown): unknown {
|
|
290
|
+
if (typeof value === "string") {
|
|
291
|
+
return redactString(kind, value);
|
|
292
|
+
}
|
|
293
|
+
if (Array.isArray(value)) {
|
|
294
|
+
return value.map((entry: unknown) => (typeof entry === "string" ? redactString(kind, entry) : redacted));
|
|
295
|
+
}
|
|
296
|
+
if (value === null || typeof value === "boolean" || typeof value === "number") {
|
|
297
|
+
return value;
|
|
298
|
+
}
|
|
299
|
+
return redacted;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Deeper than this the walk stops and leaves the value as it is. It bounds the cost of a
|
|
303
|
+
// large or cyclic object; real log payloads sit well inside it.
|
|
304
|
+
export const MAX_REDACT_DEPTH = 10;
|
|
305
|
+
|
|
306
|
+
function isPlainObject(value: object): value is Record<string, unknown> {
|
|
307
|
+
const proto: unknown = Object.getPrototypeOf(value);
|
|
308
|
+
return proto === Object.prototype || proto === null;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function hasToJSON(value: object): value is { toJSON: () => unknown } {
|
|
312
|
+
return typeof (value as { toJSON?: unknown }).toJSON === "function";
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function walk(value: unknown, depth: number): unknown {
|
|
316
|
+
if (value === null || typeof value !== "object" || depth > MAX_REDACT_DEPTH) {
|
|
317
|
+
return value;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Mongoose documents, axios errors and the like serialize through toJSON, so redact
|
|
321
|
+
// what will actually be written. Other class instances (raw req/res, Error) are left
|
|
322
|
+
// to their serializers.
|
|
323
|
+
let source: unknown = value;
|
|
324
|
+
if (!Array.isArray(value) && !isPlainObject(value)) {
|
|
325
|
+
if (!hasToJSON(value) || value instanceof Date) {
|
|
326
|
+
return value;
|
|
327
|
+
}
|
|
328
|
+
source = value.toJSON();
|
|
329
|
+
if (source === null || typeof source !== "object") {
|
|
330
|
+
return source;
|
|
331
|
+
}
|
|
332
|
+
if (!Array.isArray(source) && !isPlainObject(source)) {
|
|
333
|
+
return source;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (Array.isArray(source)) {
|
|
338
|
+
let copy: unknown[] | null = null;
|
|
339
|
+
source.forEach((entry: unknown, index: number) => {
|
|
340
|
+
const next = walk(entry, depth + 1);
|
|
341
|
+
if (next !== entry) {
|
|
342
|
+
copy = copy || source.slice();
|
|
343
|
+
copy[index] = next;
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
return copy || source;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Copy on write: the caller's object is never changed, and an object with nothing to
|
|
350
|
+
// redact is returned as it is.
|
|
351
|
+
const record = source as Record<string, unknown>;
|
|
352
|
+
let copy: Record<string, unknown> | null = null;
|
|
353
|
+
for (const key of Object.keys(record)) {
|
|
354
|
+
const entry = record[key];
|
|
355
|
+
const kind = secretKind(key);
|
|
356
|
+
const next = kind ? redactSecretValue(kind, entry) : walk(entry, depth + 1);
|
|
357
|
+
if (next !== entry) {
|
|
358
|
+
copy = copy || { ...record };
|
|
359
|
+
copy[key] = next;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return copy || source;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Redacts every secret-named key in a log object, bindings or serialized value.
|
|
366
|
+
export function redactSecrets<T>(value: T): T {
|
|
367
|
+
return walk(value, 0) as T;
|
|
368
|
+
}
|