@nestjs/observe 0.3.25 → 0.3.27
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/README.md +55 -2
- package/dist/agent/detached-observe-worker.js +50 -6
- package/dist/agent/observe-agent.shared-buffer.d.ts +44 -0
- package/dist/agent/observe-agent.shared-buffer.js +201 -19
- package/dist/agent/observe-agent.worker.d.ts +22 -2
- package/dist/agent/observe-agent.worker.js +43 -7
- package/dist/agent/telemetry-wire-contract.js +10 -4
- package/dist/cli/agent-notes.js +16 -4
- package/dist/cli/link.js +51 -4
- package/dist/cli/observe-cli.js +1 -19
- package/dist/cli/open-url.d.ts +27 -0
- package/dist/cli/open-url.js +63 -0
- package/dist/cli/package-manager.d.ts +6 -0
- package/dist/cli/package-manager.js +60 -3
- package/dist/cli/project-files.d.ts +62 -1
- package/dist/cli/project-files.js +229 -7
- package/dist/cli/project-setup.d.ts +6 -0
- package/dist/cli/project-setup.js +14 -1
- package/dist/cli/wire-application.js +6 -0
- package/dist/custom-metrics/counter.d.ts +13 -2
- package/dist/custom-metrics/counter.js +33 -16
- package/dist/custom-metrics/finite-value.util.d.ts +14 -0
- package/dist/custom-metrics/finite-value.util.js +33 -1
- package/dist/custom-metrics/gauge.d.ts +2 -0
- package/dist/custom-metrics/gauge.js +41 -17
- package/dist/custom-metrics/label-values.util.d.ts +37 -0
- package/dist/custom-metrics/label-values.util.js +51 -0
- package/dist/custom-metrics/summary.d.ts +23 -0
- package/dist/custom-metrics/summary.js +90 -12
- package/dist/instrument/create-instance-decorator.instrument.js +504 -116
- package/dist/instrument/raw-receiver.d.ts +6 -0
- package/dist/instrument/raw-receiver.js +32 -0
- package/dist/interfaces/observe-options.interface.d.ts +17 -6
- package/dist/objectives/objectives.registry.js +12 -0
- package/dist/observe.module.js +119 -3
- package/dist/otlp/otlp-encoders.js +22 -5
- package/dist/otlp/otlp-span.mapper.d.ts +2 -0
- package/dist/otlp/otlp-span.mapper.js +26 -4
- package/dist/otlp/otlp-trace-exporter.service.d.ts +63 -9
- package/dist/otlp/otlp-trace-exporter.service.js +295 -43
- package/dist/outgoing/http.integration.d.ts +8 -4
- package/dist/outgoing/http.integration.js +49 -17
- package/dist/outgoing/llm/llm-fetch.integration.js +62 -1
- package/dist/outgoing/llm/llm-providers.d.ts +16 -4
- package/dist/outgoing/llm/llm-providers.js +72 -22
- package/dist/outgoing/llm/llm-usage.d.ts +11 -6
- package/dist/outgoing/llm/llm-usage.js +72 -25
- package/dist/outgoing/mongodb.integration.d.ts +3 -1
- package/dist/outgoing/mongodb.integration.js +93 -18
- package/dist/outgoing/mssql.integration.js +25 -6
- package/dist/outgoing/mysql2.integration.js +17 -1
- package/dist/outgoing/outgoing-observe-agent.service.js +20 -4
- package/dist/outgoing/outgoing-span.recorder.js +17 -3
- package/dist/outgoing/pg.integration.d.ts +3 -2
- package/dist/outgoing/pg.integration.js +39 -11
- package/dist/outgoing/sql-statement.util.js +139 -21
- package/dist/outgoing/undici.integration.d.ts +13 -0
- package/dist/outgoing/undici.integration.js +68 -0
- package/dist/protocols/bull-observe-agent.service.js +23 -12
- package/dist/protocols/deferred-trace-completion.d.ts +17 -3
- package/dist/protocols/deferred-trace-completion.js +76 -9
- package/dist/protocols/graphql-observe-agent.service.d.ts +97 -6
- package/dist/protocols/graphql-observe-agent.service.js +354 -19
- package/dist/protocols/graphql-operation-parser.js +111 -26
- package/dist/protocols/http-observe-agent.service.d.ts +50 -9
- package/dist/protocols/http-observe-agent.service.js +220 -59
- package/dist/protocols/job-trace-runner.d.ts +42 -3
- package/dist/protocols/job-trace-runner.js +197 -36
- package/dist/protocols/queue-observe-agent.service.js +44 -20
- package/dist/protocols/rpc-observe-agent.service.d.ts +111 -8
- package/dist/protocols/rpc-observe-agent.service.js +332 -56
- package/dist/protocols/ws-observe-agent.service.d.ts +55 -5
- package/dist/protocols/ws-observe-agent.service.js +227 -21
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/logger-patcher.service.js +16 -7
- package/dist/services/operation-trace.registry.d.ts +58 -1
- package/dist/services/operation-trace.registry.js +576 -63
- package/dist/services/stdout-forwarder.service.d.ts +69 -0
- package/dist/services/stdout-forwarder.service.js +308 -51
- package/dist/services/tracer.service.d.ts +39 -11
- package/dist/services/tracer.service.js +145 -36
- package/dist/utils/capture-request.util.d.ts +3 -1
- package/dist/utils/capture-request.util.js +128 -12
- package/dist/utils/context-accessor.util.d.ts +16 -0
- package/dist/utils/context-accessor.util.js +28 -0
- package/dist/utils/default-trace-id-generator.util.js +5 -7
- package/dist/utils/error-stack.util.d.ts +13 -0
- package/dist/utils/error-stack.util.js +166 -0
- package/dist/utils/fit-to-length.util.js +33 -10
- package/dist/utils/log-line.parser.d.ts +22 -0
- package/dist/utils/log-line.parser.js +20 -7
- package/dist/utils/log-redactor.d.ts +14 -0
- package/dist/utils/log-redactor.js +690 -65
- package/dist/utils/operation-state.util.d.ts +39 -0
- package/dist/utils/operation-state.util.js +50 -0
- package/dist/utils/redact-url-query.d.ts +37 -4
- package/dist/utils/redact-url-query.js +197 -32
- package/dist/utils/sensitive-keys.d.ts +4 -1
- package/dist/utils/sensitive-keys.js +48 -10
- package/dist/utils/shared-hook-slot.util.d.ts +48 -0
- package/dist/utils/shared-hook-slot.util.js +103 -0
- package/dist/utils/source-context.util.d.ts +14 -0
- package/dist/utils/source-context.util.js +142 -43
- package/dist/utils/source-map-resolver.util.js +33 -6
- package/dist/utils/store-copy.util.d.ts +10 -0
- package/dist/utils/store-copy.util.js +43 -0
- package/dist/utils/trace-context.util.js +21 -6
- package/package.json +2 -2
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the operation an async store was opened for is still running, for
|
|
3
|
+
* whatever hands the store's trace id on.
|
|
4
|
+
*
|
|
5
|
+
* A store outlives its operation: Node hands it to every timer, socket and
|
|
6
|
+
* listener created while the operation ran, and every span runs in a copy of
|
|
7
|
+
* it. Nothing said the operation was over, so a flush or retry interval a
|
|
8
|
+
* request armed lazily went on labelling everything it did - its outgoing
|
|
9
|
+
* calls' `x-request-id`, its log lines, `currentTraceId()` - with that
|
|
10
|
+
* request's id for the life of the process, and every service it called
|
|
11
|
+
* merged unrelated work into one old trace.
|
|
12
|
+
*
|
|
13
|
+
* Read with a grace period rather than cut at the end: work an operation
|
|
14
|
+
* leaves running when it ends - an email sent after the response, a late log
|
|
15
|
+
* line - still belongs to it, and keeps its id for `OPERATION_GRACE_MS`. Only
|
|
16
|
+
* what runs on past that loses it.
|
|
17
|
+
*/
|
|
18
|
+
/** Where an operation's state sits in its store. */
|
|
19
|
+
export declare const OPERATION_STATE_KEY = "#operation";
|
|
20
|
+
/** How long after its operation ended a store still hands its trace id on. */
|
|
21
|
+
export declare const OPERATION_GRACE_MS = 10000;
|
|
22
|
+
/**
|
|
23
|
+
* One per operation, shared by reference: the copies of the store that spans
|
|
24
|
+
* run in hold the same object, so ending it reaches them all.
|
|
25
|
+
*/
|
|
26
|
+
export interface OperationState {
|
|
27
|
+
/** `performance.now()` when the operation ended; absent while it runs. */
|
|
28
|
+
endedAt?: number;
|
|
29
|
+
}
|
|
30
|
+
/** Marks `store` as opened for an operation now starting. */
|
|
31
|
+
export declare function openOperation(store: Map<any, any>): OperationState;
|
|
32
|
+
/** Marks an operation as ended. Only the first end counts. */
|
|
33
|
+
export declare function endOperation(state: OperationState | undefined): void;
|
|
34
|
+
/**
|
|
35
|
+
* The trace id `store` holds under `traceIdKey`, while its operation runs and
|
|
36
|
+
* for the grace period after - undefined past that, as outside any operation.
|
|
37
|
+
* A store opened before operations were marked counts as running.
|
|
38
|
+
*/
|
|
39
|
+
export declare function liveTraceId(store: Map<any, any> | undefined, traceIdKey: string): string | undefined;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the operation an async store was opened for is still running, for
|
|
3
|
+
* whatever hands the store's trace id on.
|
|
4
|
+
*
|
|
5
|
+
* A store outlives its operation: Node hands it to every timer, socket and
|
|
6
|
+
* listener created while the operation ran, and every span runs in a copy of
|
|
7
|
+
* it. Nothing said the operation was over, so a flush or retry interval a
|
|
8
|
+
* request armed lazily went on labelling everything it did - its outgoing
|
|
9
|
+
* calls' `x-request-id`, its log lines, `currentTraceId()` - with that
|
|
10
|
+
* request's id for the life of the process, and every service it called
|
|
11
|
+
* merged unrelated work into one old trace.
|
|
12
|
+
*
|
|
13
|
+
* Read with a grace period rather than cut at the end: work an operation
|
|
14
|
+
* leaves running when it ends - an email sent after the response, a late log
|
|
15
|
+
* line - still belongs to it, and keeps its id for `OPERATION_GRACE_MS`. Only
|
|
16
|
+
* what runs on past that loses it.
|
|
17
|
+
*/
|
|
18
|
+
/** Where an operation's state sits in its store. */
|
|
19
|
+
export const OPERATION_STATE_KEY = "#operation";
|
|
20
|
+
/** How long after its operation ended a store still hands its trace id on. */
|
|
21
|
+
export const OPERATION_GRACE_MS = 10_000;
|
|
22
|
+
/** Marks `store` as opened for an operation now starting. */
|
|
23
|
+
export function openOperation(store) {
|
|
24
|
+
const state = {};
|
|
25
|
+
store.set(OPERATION_STATE_KEY, state);
|
|
26
|
+
return state;
|
|
27
|
+
}
|
|
28
|
+
/** Marks an operation as ended. Only the first end counts. */
|
|
29
|
+
export function endOperation(state) {
|
|
30
|
+
if (state !== undefined && state.endedAt === undefined) {
|
|
31
|
+
state.endedAt = performance.now();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The trace id `store` holds under `traceIdKey`, while its operation runs and
|
|
36
|
+
* for the grace period after - undefined past that, as outside any operation.
|
|
37
|
+
* A store opened before operations were marked counts as running.
|
|
38
|
+
*/
|
|
39
|
+
export function liveTraceId(store, traceIdKey) {
|
|
40
|
+
const traceId = store?.get(traceIdKey);
|
|
41
|
+
if (typeof traceId !== "string") {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const endedAt = store.get(OPERATION_STATE_KEY)?.endedAt;
|
|
45
|
+
if (endedAt !== undefined &&
|
|
46
|
+
performance.now() - endedAt > OPERATION_GRACE_MS) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
return traceId;
|
|
50
|
+
}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { SensitiveKeyMatcher } from "./sensitive-keys.js";
|
|
2
|
+
/**
|
|
3
|
+
* Whether a key is one of those only a URL makes sensitive. For a caller
|
|
4
|
+
* reading a URL somewhere this module does not - a query string quoted in
|
|
5
|
+
* an error message - which tests the shared list itself.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isUrlOnlySensitiveKey(key: string): boolean;
|
|
2
8
|
export declare const REDACTED = "[REDACTED]";
|
|
3
9
|
/**
|
|
4
10
|
* Masks the values of sensitive query parameters in a request URL.
|
|
@@ -13,9 +19,17 @@ export declare const REDACTED = "[REDACTED]";
|
|
|
13
19
|
* Keys are kept and only values replaced: knowing a request carried a `token`
|
|
14
20
|
* is useful when reading a trace, and knowing which token is not.
|
|
15
21
|
*
|
|
16
|
-
* Values are not
|
|
17
|
-
*
|
|
18
|
-
*
|
|
22
|
+
* Values are not hunted for token-shaped strings - a regex doing that mangles
|
|
23
|
+
* ordinary path segments and ids, and a redactor that mangles real data is one
|
|
24
|
+
* somebody switches off. But a value that is itself a URL (`returnTo`, `next`)
|
|
25
|
+
* or a JSON document carries keys of its own, and those are held to the same
|
|
26
|
+
* rules. So are the parameters a path carries behind `;` and a fragment
|
|
27
|
+
* behind `#`: neither is a query string to URLSearchParams, but a server that
|
|
28
|
+
* reads `;jsessionid=` and an OAuth callback's `#access_token=` both are.
|
|
29
|
+
*
|
|
30
|
+
* Masking is done in place, one parameter at a time. A parameter with nothing
|
|
31
|
+
* to hide keeps the spelling it arrived with even beside a masked one, so a
|
|
32
|
+
* `queryParamsObfuscateRegex` written for what requests carry still finds it.
|
|
19
33
|
*/
|
|
20
34
|
export declare function redactUrlQuery(url: string,
|
|
21
35
|
/**
|
|
@@ -25,4 +39,23 @@ export declare function redactUrlQuery(url: string,
|
|
|
25
39
|
*/
|
|
26
40
|
redactor?: {
|
|
27
41
|
isSensitiveKey: SensitiveKeyMatcher;
|
|
28
|
-
} | null
|
|
42
|
+
} | null,
|
|
43
|
+
/**
|
|
44
|
+
* `http.queryParamsObfuscateRegex`, already made global by
|
|
45
|
+
* `toGlobalRegExp`. Applied here, after the key rules, so every URL the
|
|
46
|
+
* agent records - the request's, a captured `referer`, an outgoing call's -
|
|
47
|
+
* is held to it, not only the one caller that used to apply it.
|
|
48
|
+
*/
|
|
49
|
+
obfuscate?: RegExp): string;
|
|
50
|
+
/**
|
|
51
|
+
* A copy of a RegExp with the global flag. `replaceAll` refuses a non-global
|
|
52
|
+
* pattern outright - inside a request hook, where that would answer every
|
|
53
|
+
* traced request with a 500.
|
|
54
|
+
*
|
|
55
|
+
* Copied even when the flag is there already. A global pattern carries
|
|
56
|
+
* `lastIndex`, which every `replace` and `replaceAll` sets back to 0, and the
|
|
57
|
+
* object is the application's: an `exec()` loop it runs over the same
|
|
58
|
+
* pattern, recording something per match, would start again from the first
|
|
59
|
+
* match each time and never end.
|
|
60
|
+
*/
|
|
61
|
+
export declare function toGlobalRegExp<T extends RegExp | undefined>(pattern: T): T;
|
|
@@ -8,7 +8,31 @@ import { isSensitiveKeyByDefault, normaliseKey, } from "./sensitive-keys.js";
|
|
|
8
8
|
* masking it would cost more than it protects.
|
|
9
9
|
*/
|
|
10
10
|
const URL_ONLY_SENSITIVE_KEYS = new Set(["code", "state", "sig", "key"]);
|
|
11
|
+
/**
|
|
12
|
+
* Whether a key is one of those only a URL makes sensitive. For a caller
|
|
13
|
+
* reading a URL somewhere this module does not - a query string quoted in
|
|
14
|
+
* an error message - which tests the shared list itself.
|
|
15
|
+
*/
|
|
16
|
+
export function isUrlOnlySensitiveKey(key) {
|
|
17
|
+
return URL_ONLY_SENSITIVE_KEYS.has(normaliseKey(key));
|
|
18
|
+
}
|
|
11
19
|
export const REDACTED = "[REDACTED]";
|
|
20
|
+
/** `REDACTED` as a URL carries it. */
|
|
21
|
+
const MASKED_VALUE = encodeURIComponent(REDACTED);
|
|
22
|
+
/**
|
|
23
|
+
* How many URLs deep a value is read for keys of its own. The client chooses
|
|
24
|
+
* the depth (`?next=` holding a URL whose `next=` holds another), and each
|
|
25
|
+
* level is another parse, so past this a value that still holds one is masked
|
|
26
|
+
* whole.
|
|
27
|
+
*/
|
|
28
|
+
const MAX_NESTED_URLS = 4;
|
|
29
|
+
/**
|
|
30
|
+
* Longest value read as JSON (tRPC's `input`, a `filter`). Parsing is linear,
|
|
31
|
+
* but this runs on the request path, and a value this long is no longer a
|
|
32
|
+
* filter anybody reads in a trace.
|
|
33
|
+
*/
|
|
34
|
+
const MAX_JSON_VALUE_LENGTH = 16 * 1024;
|
|
35
|
+
const MAX_JSON_DEPTH = 32;
|
|
12
36
|
/**
|
|
13
37
|
* Masks the values of sensitive query parameters in a request URL.
|
|
14
38
|
*
|
|
@@ -22,9 +46,17 @@ export const REDACTED = "[REDACTED]";
|
|
|
22
46
|
* Keys are kept and only values replaced: knowing a request carried a `token`
|
|
23
47
|
* is useful when reading a trace, and knowing which token is not.
|
|
24
48
|
*
|
|
25
|
-
* Values are not
|
|
26
|
-
*
|
|
27
|
-
*
|
|
49
|
+
* Values are not hunted for token-shaped strings - a regex doing that mangles
|
|
50
|
+
* ordinary path segments and ids, and a redactor that mangles real data is one
|
|
51
|
+
* somebody switches off. But a value that is itself a URL (`returnTo`, `next`)
|
|
52
|
+
* or a JSON document carries keys of its own, and those are held to the same
|
|
53
|
+
* rules. So are the parameters a path carries behind `;` and a fragment
|
|
54
|
+
* behind `#`: neither is a query string to URLSearchParams, but a server that
|
|
55
|
+
* reads `;jsessionid=` and an OAuth callback's `#access_token=` both are.
|
|
56
|
+
*
|
|
57
|
+
* Masking is done in place, one parameter at a time. A parameter with nothing
|
|
58
|
+
* to hide keeps the spelling it arrived with even beside a masked one, so a
|
|
59
|
+
* `queryParamsObfuscateRegex` written for what requests carry still finds it.
|
|
28
60
|
*/
|
|
29
61
|
export function redactUrlQuery(url,
|
|
30
62
|
/**
|
|
@@ -32,37 +64,170 @@ export function redactUrlQuery(url,
|
|
|
32
64
|
* do to a log attribute. Without one the built-in rules still apply: masking
|
|
33
65
|
* a URL is not something `redaction.enabled: false` switches off.
|
|
34
66
|
*/
|
|
35
|
-
redactor
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
67
|
+
redactor,
|
|
68
|
+
/**
|
|
69
|
+
* `http.queryParamsObfuscateRegex`, already made global by
|
|
70
|
+
* `toGlobalRegExp`. Applied here, after the key rules, so every URL the
|
|
71
|
+
* agent records - the request's, a captured `referer`, an outgoing call's -
|
|
72
|
+
* is held to it, not only the one caller that used to apply it.
|
|
73
|
+
*/
|
|
74
|
+
obfuscate) {
|
|
75
|
+
const isSharedSensitive = redactor
|
|
76
|
+
? (key) => redactor.isSensitiveKey(key)
|
|
77
|
+
: isSensitiveKeyByDefault;
|
|
78
|
+
const isSensitive = (key) => isUrlOnlySensitiveKey(key) || isSharedSensitive(key);
|
|
79
|
+
const redacted = redactUrl(url, isSensitive, isSharedSensitive, 0);
|
|
80
|
+
return obfuscate ? redacted.replaceAll(obfuscate, REDACTED) : redacted;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* A copy of a RegExp with the global flag. `replaceAll` refuses a non-global
|
|
84
|
+
* pattern outright - inside a request hook, where that would answer every
|
|
85
|
+
* traced request with a 500.
|
|
86
|
+
*
|
|
87
|
+
* Copied even when the flag is there already. A global pattern carries
|
|
88
|
+
* `lastIndex`, which every `replace` and `replaceAll` sets back to 0, and the
|
|
89
|
+
* object is the application's: an `exec()` loop it runs over the same
|
|
90
|
+
* pattern, recording something per match, would start again from the first
|
|
91
|
+
* match each time and never end.
|
|
92
|
+
*/
|
|
93
|
+
export function toGlobalRegExp(pattern) {
|
|
94
|
+
if (!pattern) {
|
|
95
|
+
return pattern;
|
|
96
|
+
}
|
|
97
|
+
return new RegExp(pattern.source, pattern.global ? pattern.flags : pattern.flags + "g");
|
|
98
|
+
}
|
|
99
|
+
function redactUrl(url, isSensitive, isSharedSensitive, depth) {
|
|
100
|
+
const queryAt = url.indexOf("?");
|
|
101
|
+
const hashAt = url.indexOf("#");
|
|
102
|
+
const pathEnd = Math.min(queryAt === -1 ? url.length : queryAt, hashAt === -1 ? url.length : hashAt);
|
|
103
|
+
const redactParamsOf = (text, separators) => redactParams(text, separators, isSensitive, isSharedSensitive, depth);
|
|
104
|
+
// Behind the first `;` of the path: Fastify's `useSemicolonDelimiter` reads
|
|
105
|
+
// it as the query, a servlet container as `;name=value` pairs.
|
|
106
|
+
const path = url.slice(0, pathEnd);
|
|
107
|
+
const pathParamsAt = path.indexOf(";");
|
|
108
|
+
let output = pathParamsAt === -1
|
|
109
|
+
? path
|
|
110
|
+
: path.slice(0, pathParamsAt + 1) +
|
|
111
|
+
redactParamsOf(path.slice(pathParamsAt + 1), ";&");
|
|
112
|
+
if (pathEnd === queryAt) {
|
|
113
|
+
const queryEnd = hashAt > queryAt ? hashAt : url.length;
|
|
114
|
+
output += "?" + redactParamsOf(url.slice(queryAt + 1, queryEnd), "&");
|
|
115
|
+
}
|
|
116
|
+
const fragmentAt = hashAt !== -1 && hashAt >= pathEnd ? hashAt : -1;
|
|
117
|
+
if (fragmentAt !== -1) {
|
|
118
|
+
output += "#" + redactParamsOf(url.slice(fragmentAt + 1), "&");
|
|
119
|
+
}
|
|
120
|
+
// Compared rather than tracked: what masked nothing comes back as the very
|
|
121
|
+
// string that arrived, not a rebuilt copy of it.
|
|
122
|
+
return output === url ? url : output;
|
|
123
|
+
}
|
|
124
|
+
function redactParams(text, separators, isSensitive, isSharedSensitive, depth) {
|
|
125
|
+
let output = "";
|
|
126
|
+
let start = 0;
|
|
127
|
+
for (let i = 0; i <= text.length; i++) {
|
|
128
|
+
if (i < text.length && !separators.includes(text[i])) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
output += redactParam(text.slice(start, i), isSensitive, isSharedSensitive, depth);
|
|
132
|
+
if (i < text.length) {
|
|
133
|
+
output += text[i];
|
|
134
|
+
}
|
|
135
|
+
start = i + 1;
|
|
136
|
+
}
|
|
137
|
+
return output;
|
|
138
|
+
}
|
|
139
|
+
function redactParam(param, isSensitive, isSharedSensitive, depth) {
|
|
140
|
+
const equalsAt = param.indexOf("=");
|
|
141
|
+
// `?token` alone, or an anchor named after a credential (`#api-key`):
|
|
142
|
+
// nothing is carried, so there is nothing to mask.
|
|
143
|
+
if (equalsAt === -1 || equalsAt === param.length - 1) {
|
|
144
|
+
return param;
|
|
145
|
+
}
|
|
146
|
+
const rawKey = param.slice(0, equalsAt);
|
|
147
|
+
const rawValue = param.slice(equalsAt + 1);
|
|
148
|
+
const key = decodeComponent(rawKey);
|
|
149
|
+
// A path segment ending in `?x` rather than `?x=` puts the name into the
|
|
150
|
+
// key: `/items/1?x?key=abc` is `key=abc` to whatever reads the inner URL.
|
|
151
|
+
const innerKeyAt = key.lastIndexOf("?");
|
|
152
|
+
if (isSensitive(key) ||
|
|
153
|
+
(innerKeyAt !== -1 && isSensitive(key.slice(innerKeyAt + 1)))) {
|
|
154
|
+
return `${rawKey}=${MASKED_VALUE}`;
|
|
155
|
+
}
|
|
156
|
+
const value = decodeComponent(rawValue);
|
|
157
|
+
const redacted = redactValue(value, isSensitive, isSharedSensitive, depth);
|
|
158
|
+
if (redacted === value) {
|
|
159
|
+
return param;
|
|
160
|
+
}
|
|
161
|
+
// Written back the way it came: still raw if it arrived raw, encoded once
|
|
162
|
+
// if it arrived encoded.
|
|
163
|
+
return `${rawKey}=${value === rawValue ? redacted : encodeURIComponent(redacted)}`;
|
|
164
|
+
}
|
|
165
|
+
function redactValue(value, isSensitive, isSharedSensitive, depth) {
|
|
166
|
+
const first = value.trimStart()[0];
|
|
167
|
+
if ((first === "{" || first === "[") &&
|
|
168
|
+
value.length <= MAX_JSON_VALUE_LENGTH) {
|
|
169
|
+
const redacted = redactJson(value, isSharedSensitive);
|
|
170
|
+
if (redacted !== undefined) {
|
|
171
|
+
return redacted;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (!/[?#;]/.test(value)) {
|
|
175
|
+
return value;
|
|
176
|
+
}
|
|
177
|
+
if (depth + 1 >= MAX_NESTED_URLS) {
|
|
178
|
+
return REDACTED;
|
|
179
|
+
}
|
|
180
|
+
return redactUrl(value, isSensitive, isSharedSensitive, depth + 1);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* The value with its sensitive members masked, the value itself when nothing
|
|
184
|
+
* was, or `undefined` when it is not JSON after all.
|
|
185
|
+
*/
|
|
186
|
+
function redactJson(value, isSensitive) {
|
|
187
|
+
let parsed;
|
|
188
|
+
try {
|
|
189
|
+
parsed = JSON.parse(value);
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
return undefined;
|
|
39
193
|
}
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const hashAt = rest.indexOf("#");
|
|
45
|
-
const query = hashAt === -1 ? rest : rest.slice(0, hashAt);
|
|
46
|
-
const fragment = hashAt === -1 ? "" : rest.slice(hashAt);
|
|
47
|
-
// Parsed and re-serialised rather than pattern-replaced: the parser is what
|
|
48
|
-
// knows that `?a=1&token=x` and `?a=1;token=x` differ, and that a value may
|
|
49
|
-
// contain an escaped `&`.
|
|
50
|
-
const params = new URLSearchParams(query);
|
|
51
|
-
let redactedAny = false;
|
|
52
|
-
// The spread is load-bearing: `params.set` below mutates the same object the
|
|
53
|
-
// iterator walks, so the keys have to be materialised before the loop starts.
|
|
54
|
-
// oxlint-disable-next-line unicorn/no-useless-spread
|
|
55
|
-
for (const key of [...params.keys()]) {
|
|
56
|
-
if (URL_ONLY_SENSITIVE_KEYS.has(normaliseKey(key)) ||
|
|
57
|
-
(redactor ? redactor.isSensitiveKey(key) : isSensitiveKeyByDefault(key))) {
|
|
58
|
-
params.set(key, REDACTED);
|
|
59
|
-
redactedAny = true;
|
|
194
|
+
let maskedAny = false;
|
|
195
|
+
const walk = (node, depth) => {
|
|
196
|
+
if (node === null || typeof node !== "object") {
|
|
197
|
+
return node;
|
|
60
198
|
}
|
|
199
|
+
if (depth > MAX_JSON_DEPTH) {
|
|
200
|
+
maskedAny = true;
|
|
201
|
+
return REDACTED;
|
|
202
|
+
}
|
|
203
|
+
if (Array.isArray(node)) {
|
|
204
|
+
return node.map((item) => walk(item, depth + 1));
|
|
205
|
+
}
|
|
206
|
+
const out = {};
|
|
207
|
+
for (const [key, item] of Object.entries(node)) {
|
|
208
|
+
if (isSensitive(key)) {
|
|
209
|
+
maskedAny = true;
|
|
210
|
+
out[key] = REDACTED;
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
out[key] = walk(item, depth + 1);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return out;
|
|
217
|
+
};
|
|
218
|
+
const redacted = walk(parsed, 0);
|
|
219
|
+
return maskedAny ? JSON.stringify(redacted) : value;
|
|
220
|
+
}
|
|
221
|
+
/** What a server reads for a key or value, or the raw text if it cannot. */
|
|
222
|
+
function decodeComponent(raw) {
|
|
223
|
+
if (!raw.includes("%") && !raw.includes("+")) {
|
|
224
|
+
return raw;
|
|
225
|
+
}
|
|
226
|
+
try {
|
|
227
|
+
return decodeURIComponent(raw.replaceAll("+", " "));
|
|
61
228
|
}
|
|
62
|
-
|
|
63
|
-
//
|
|
64
|
-
|
|
65
|
-
return url;
|
|
229
|
+
catch {
|
|
230
|
+
// A malformed escape: no server reads it as anything else either.
|
|
231
|
+
return raw;
|
|
66
232
|
}
|
|
67
|
-
return `${path}?${params.toString()}${fragment}`;
|
|
68
233
|
}
|
|
@@ -4,7 +4,10 @@
|
|
|
4
4
|
* Two lists that drift apart mean one of the paths quietly stops protecting
|
|
5
5
|
* something the other still does.
|
|
6
6
|
*/
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Case-insensitive, ignoring the separators a name is written with: `api_key`,
|
|
9
|
+
* `apiKey`, `API-KEY`, `api.key`, `Api Key` and `api[key]` are one key.
|
|
10
|
+
*/
|
|
8
11
|
export declare function normaliseKey(key: string): string;
|
|
9
12
|
export type SensitiveKeyMatcher = (key: string) => boolean;
|
|
10
13
|
/**
|
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
* something the other still does.
|
|
6
6
|
*/
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Words too short or too generic to be matched inside a longer name - `auth`
|
|
9
|
+
* would take `author` with it - but a credential when they are the whole key
|
|
10
|
+
* or its last word: `pwd`, `db_pwd`, `token_auth`, `x-auth`.
|
|
10
11
|
*/
|
|
11
|
-
const
|
|
12
|
+
const SHORT_WORDS = new Set(["pwd", "auth"]);
|
|
12
13
|
/**
|
|
13
14
|
* What a credential's name ends in. Real applications do not call the
|
|
14
15
|
* parameter `token`; they call it `resetToken`, `client_secret`,
|
|
@@ -48,29 +49,66 @@ const SENSITIVE_WORDS = new Set([
|
|
|
48
49
|
"credential",
|
|
49
50
|
"credentials",
|
|
50
51
|
]);
|
|
51
|
-
/**
|
|
52
|
+
/**
|
|
53
|
+
* Nouns specific enough to mark a key even run together with other words,
|
|
54
|
+
* where no case or separator shows the word: `passwordconfirm`,
|
|
55
|
+
* `PASSWORDHASH`. Not `secret`, which `secretary` contains.
|
|
56
|
+
*/
|
|
57
|
+
const SENSITIVE_STEMS = ["password", "passwd", "credential"];
|
|
58
|
+
/**
|
|
59
|
+
* Case-insensitive, ignoring the separators a name is written with: `api_key`,
|
|
60
|
+
* `apiKey`, `API-KEY`, `api.key`, `Api Key` and `api[key]` are one key.
|
|
61
|
+
*/
|
|
52
62
|
export function normaliseKey(key) {
|
|
53
|
-
return key.toLowerCase().replace(/[-_]/g, "");
|
|
63
|
+
return key.toLowerCase().replace(/[-_. [\]]/g, "");
|
|
54
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* `SMTPPasswordNew` is `smtp password new`: a case change ends a word, and so
|
|
67
|
+
* does the last capital of an acronym followed by a lower-case letter.
|
|
68
|
+
*/
|
|
55
69
|
function wordsOf(key) {
|
|
56
70
|
return key
|
|
57
71
|
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
72
|
+
.replace(/([A-Z])([A-Z][a-z])/g, "$1 $2")
|
|
58
73
|
.toLowerCase()
|
|
59
|
-
.split(/[^a-z0-9]+/)
|
|
74
|
+
.split(/[^a-z0-9]+/)
|
|
75
|
+
.filter((word) => word.length > 0);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Walked by hand rather than with `/\d+$/`, which restarts at every digit of a
|
|
79
|
+
* run that does not reach the end - quadratic, on a query-string key the
|
|
80
|
+
* client chose.
|
|
81
|
+
*/
|
|
82
|
+
function withoutTrailingDigits(key) {
|
|
83
|
+
let end = key.length;
|
|
84
|
+
while (end > 0) {
|
|
85
|
+
const code = key.charCodeAt(end - 1);
|
|
86
|
+
if (code < 48 || code > 57) {
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
end -= 1;
|
|
90
|
+
}
|
|
91
|
+
return key.slice(0, end);
|
|
60
92
|
}
|
|
61
93
|
/**
|
|
62
94
|
* Builds the matcher, with `extraKeys` - the deployment's own `redaction.keys`
|
|
63
95
|
* - matched exactly (after normalising) on top of the built-in rules.
|
|
64
96
|
*/
|
|
65
97
|
export function createSensitiveKeyMatcher(extraKeys = []) {
|
|
66
|
-
const exact = new Set([...
|
|
98
|
+
const exact = new Set([...SHORT_WORDS, ...extraKeys].map(normaliseKey));
|
|
67
99
|
return (key) => {
|
|
68
100
|
const normalised = normaliseKey(key);
|
|
69
101
|
// `password2`, `token_1`: a numbered credential is still one.
|
|
70
|
-
const stem = normalised
|
|
71
|
-
|
|
102
|
+
const stem = withoutTrailingDigits(normalised);
|
|
103
|
+
if (exact.has(normalised) ||
|
|
104
|
+
exact.has(stem) ||
|
|
72
105
|
SENSITIVE_SUFFIXES.some((suffix) => stem.endsWith(suffix)) ||
|
|
73
|
-
|
|
106
|
+
SENSITIVE_STEMS.some((word) => normalised.includes(word))) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
const words = wordsOf(key);
|
|
110
|
+
const last = words.length > 0 ? withoutTrailingDigits(words.at(-1)) : "";
|
|
111
|
+
return (SHORT_WORDS.has(last) || words.some((word) => SENSITIVE_WORDS.has(word)));
|
|
74
112
|
};
|
|
75
113
|
}
|
|
76
114
|
export const isSensitiveKeyByDefault = createSensitiveKeyMatcher();
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shares a hook slot of Nest's with the application.
|
|
3
|
+
*
|
|
4
|
+
* Nest keeps several hooks one to a slot, behind a public setter: the GraphQL
|
|
5
|
+
* host's request hooks, an HTTP adapter's request hooks, a microservice
|
|
6
|
+
* server's processing hooks, a client's dispatch hook. The slot is the
|
|
7
|
+
* application's to use as much as the agent's, and whichever of them called
|
|
8
|
+
* the setter last was the only one left. Here the setter on the instance is
|
|
9
|
+
* shadowed by one that records the application's hook, and what Nest is
|
|
10
|
+
* given, through the setter it had, is one function running both: the
|
|
11
|
+
* application's hook and the agent's part, as `compose` puts them together.
|
|
12
|
+
* The agent sets its part with `setAgentHook` and never through the setter,
|
|
13
|
+
* so either may come first.
|
|
14
|
+
*
|
|
15
|
+
* What is kept lives on the instance under a registered symbol, so a second
|
|
16
|
+
* agent - another application built on the same adapter, another copy of
|
|
17
|
+
* this package - replaces the agent's part instead of taking the first
|
|
18
|
+
* agent's pair for the application's hook.
|
|
19
|
+
*/
|
|
20
|
+
export type Hook = (this: unknown, ...args: any[]) => unknown;
|
|
21
|
+
/** What the slot holds: the application's hook, if any, and the agent's part. */
|
|
22
|
+
export type ComposeHooks<H extends Hook> = (application: H | undefined, agent: H) => H;
|
|
23
|
+
export interface SharedSlotOptions {
|
|
24
|
+
/**
|
|
25
|
+
* The slot's public getter, where Nest has one. What it returns when the
|
|
26
|
+
* slot is first shared is a hook set before: the application's.
|
|
27
|
+
*/
|
|
28
|
+
getter?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Points the getter at the application's own hook, for a slot Nest reads
|
|
31
|
+
* through something else: an application chaining "the previous hook" is
|
|
32
|
+
* handed its own, not the pair that runs the agent's part as well.
|
|
33
|
+
*/
|
|
34
|
+
repointGetter?: boolean;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Takes over `target[setter]`, once per instance; `false` when the instance
|
|
38
|
+
* has no such setter or cannot be written to. A hook set through the setter
|
|
39
|
+
* from then on is the application's.
|
|
40
|
+
*/
|
|
41
|
+
export declare function shareHookSlot(target: object, setter: string, options?: SharedSlotOptions): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Sets the agent's part of the slot, sharing it first if need be. On an
|
|
44
|
+
* instance that cannot be shared, the agent's part is set through the setter
|
|
45
|
+
* as it is - in place of whatever was there, as before slots were shared.
|
|
46
|
+
* Never throws: the callers run while Nest builds the application.
|
|
47
|
+
*/
|
|
48
|
+
export declare function setAgentHook<H extends Hook>(target: object, setter: string, agent: H, compose: ComposeHooks<H>, options?: SharedSlotOptions): void;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shares a hook slot of Nest's with the application.
|
|
3
|
+
*
|
|
4
|
+
* Nest keeps several hooks one to a slot, behind a public setter: the GraphQL
|
|
5
|
+
* host's request hooks, an HTTP adapter's request hooks, a microservice
|
|
6
|
+
* server's processing hooks, a client's dispatch hook. The slot is the
|
|
7
|
+
* application's to use as much as the agent's, and whichever of them called
|
|
8
|
+
* the setter last was the only one left. Here the setter on the instance is
|
|
9
|
+
* shadowed by one that records the application's hook, and what Nest is
|
|
10
|
+
* given, through the setter it had, is one function running both: the
|
|
11
|
+
* application's hook and the agent's part, as `compose` puts them together.
|
|
12
|
+
* The agent sets its part with `setAgentHook` and never through the setter,
|
|
13
|
+
* so either may come first.
|
|
14
|
+
*
|
|
15
|
+
* What is kept lives on the instance under a registered symbol, so a second
|
|
16
|
+
* agent - another application built on the same adapter, another copy of
|
|
17
|
+
* this package - replaces the agent's part instead of taking the first
|
|
18
|
+
* agent's pair for the application's hook.
|
|
19
|
+
*/
|
|
20
|
+
const SHARED_HOOK_SLOTS = Symbol.for("nestjs.observe.sharedHookSlots");
|
|
21
|
+
/**
|
|
22
|
+
* Takes over `target[setter]`, once per instance; `false` when the instance
|
|
23
|
+
* has no such setter or cannot be written to. A hook set through the setter
|
|
24
|
+
* from then on is the application's.
|
|
25
|
+
*/
|
|
26
|
+
export function shareHookSlot(target, setter, options = {}) {
|
|
27
|
+
return findOrShare(target, setter, options) !== undefined;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Sets the agent's part of the slot, sharing it first if need be. On an
|
|
31
|
+
* instance that cannot be shared, the agent's part is set through the setter
|
|
32
|
+
* as it is - in place of whatever was there, as before slots were shared.
|
|
33
|
+
* Never throws: the callers run while Nest builds the application.
|
|
34
|
+
*/
|
|
35
|
+
export function setAgentHook(target, setter, agent, compose, options = {}) {
|
|
36
|
+
const slot = findOrShare(target, setter, options);
|
|
37
|
+
try {
|
|
38
|
+
if (!slot) {
|
|
39
|
+
target[setter](compose(undefined, agent));
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
slot.agent = agent;
|
|
43
|
+
slot.compose = compose;
|
|
44
|
+
fill(slot);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// A setter that throws leaves that slot to whoever had it.
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function findOrShare(target, setter, { getter, repointGetter }) {
|
|
51
|
+
try {
|
|
52
|
+
const instance = target;
|
|
53
|
+
const slots = Object.prototype.hasOwnProperty.call(instance, SHARED_HOOK_SLOTS)
|
|
54
|
+
? instance[SHARED_HOOK_SLOTS]
|
|
55
|
+
: undefined;
|
|
56
|
+
if (slots?.[setter]) {
|
|
57
|
+
return slots[setter];
|
|
58
|
+
}
|
|
59
|
+
const original = instance[setter];
|
|
60
|
+
if (typeof original !== "function") {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
const read = getter ? instance[getter] : undefined;
|
|
64
|
+
const slot = {
|
|
65
|
+
application: typeof read === "function" ? read.call(target) : undefined,
|
|
66
|
+
agent: undefined,
|
|
67
|
+
compose: undefined,
|
|
68
|
+
install: (hook) => original.call(target, hook),
|
|
69
|
+
};
|
|
70
|
+
defineHidden(target, SHARED_HOOK_SLOTS, Object.assign(Object.create(null), slots, { [setter]: slot }));
|
|
71
|
+
// Called on the instance it was taken from, never on `this`: the setter
|
|
72
|
+
// may be reached through a proxy of the instance, ours or Nest's.
|
|
73
|
+
defineHidden(target, setter, function (hook) {
|
|
74
|
+
slot.application = hook;
|
|
75
|
+
fill(slot);
|
|
76
|
+
});
|
|
77
|
+
if (getter && repointGetter && typeof read === "function") {
|
|
78
|
+
defineHidden(target, getter, () => slot.application);
|
|
79
|
+
}
|
|
80
|
+
return slot;
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
// Frozen, or a proxy that refuses the definition.
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** Hands Nest the slot's current pair, or the application's hook alone. */
|
|
88
|
+
function fill(slot) {
|
|
89
|
+
const application = typeof slot.application === "function"
|
|
90
|
+
? slot.application
|
|
91
|
+
: undefined;
|
|
92
|
+
slot.install(slot.agent && slot.compose
|
|
93
|
+
? slot.compose(application, slot.agent)
|
|
94
|
+
: slot.application);
|
|
95
|
+
}
|
|
96
|
+
function defineHidden(target, key, value) {
|
|
97
|
+
Object.defineProperty(target, key, {
|
|
98
|
+
value,
|
|
99
|
+
configurable: true,
|
|
100
|
+
enumerable: false,
|
|
101
|
+
writable: true,
|
|
102
|
+
});
|
|
103
|
+
}
|