@nestjs/observe 0.3.26 → 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 +38 -2
- package/dist/agent/observe-agent.shared-buffer.js +5 -0
- package/dist/agent/telemetry-wire-contract.js +3 -3
- package/dist/cli/link.js +24 -2
- package/dist/cli/package-manager.d.ts +6 -0
- package/dist/cli/package-manager.js +60 -3
- package/dist/cli/project-files.d.ts +25 -5
- package/dist/cli/project-files.js +98 -15
- package/dist/custom-metrics/summary.js +17 -10
- package/dist/instrument/create-instance-decorator.instrument.js +364 -38
- 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 +14 -5
- package/dist/otlp/otlp-span.mapper.d.ts +2 -0
- package/dist/otlp/otlp-span.mapper.js +19 -1
- package/dist/otlp/otlp-trace-exporter.service.d.ts +63 -9
- package/dist/otlp/otlp-trace-exporter.service.js +232 -38
- package/dist/outgoing/http.integration.js +36 -11
- package/dist/outgoing/mongodb.integration.d.ts +3 -1
- package/dist/outgoing/mongodb.integration.js +93 -18
- package/dist/outgoing/mssql.integration.js +9 -4
- package/dist/outgoing/outgoing-observe-agent.service.js +5 -1
- package/dist/outgoing/sql-statement.util.js +36 -10
- package/dist/protocols/bull-observe-agent.service.js +21 -0
- package/dist/protocols/deferred-trace-completion.d.ts +8 -2
- package/dist/protocols/deferred-trace-completion.js +40 -3
- package/dist/protocols/graphql-observe-agent.service.d.ts +62 -2
- package/dist/protocols/graphql-observe-agent.service.js +188 -14
- package/dist/protocols/graphql-operation-parser.js +45 -14
- package/dist/protocols/http-observe-agent.service.d.ts +14 -7
- package/dist/protocols/http-observe-agent.service.js +57 -14
- package/dist/protocols/job-trace-runner.d.ts +7 -0
- package/dist/protocols/job-trace-runner.js +72 -8
- package/dist/protocols/rpc-observe-agent.service.d.ts +61 -0
- package/dist/protocols/rpc-observe-agent.service.js +146 -10
- package/dist/protocols/ws-observe-agent.service.js +1 -1
- package/dist/services/logger-patcher.service.js +13 -3
- package/dist/services/operation-trace.registry.d.ts +13 -0
- package/dist/services/operation-trace.registry.js +195 -65
- package/dist/services/stdout-forwarder.service.js +22 -2
- package/dist/services/tracer.service.d.ts +19 -14
- package/dist/services/tracer.service.js +57 -34
- package/dist/utils/capture-request.util.js +36 -8
- 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/log-line.parser.d.ts +8 -0
- package/dist/utils/log-line.parser.js +13 -4
- package/dist/utils/log-redactor.js +184 -10
- package/dist/utils/redact-url-query.d.ts +16 -5
- package/dist/utils/redact-url-query.js +20 -7
- package/dist/utils/source-context.util.d.ts +6 -0
- package/dist/utils/source-context.util.js +68 -33
- package/dist/utils/source-map-resolver.util.js +25 -4
- package/dist/utils/store-copy.util.d.ts +10 -0
- package/dist/utils/store-copy.util.js +43 -0
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { stripAnsi } from "./log-line.parser.js";
|
|
2
|
+
import { isUrlOnlySensitiveKey, toGlobalRegExp } from "./redact-url-query.js";
|
|
2
3
|
import { createSensitiveKeyMatcher, } from "./sensitive-keys.js";
|
|
3
4
|
/**
|
|
4
5
|
* Strips secrets out of log lines before they leave the process.
|
|
@@ -145,15 +146,23 @@ function isBase64Char(code) {
|
|
|
145
146
|
* name, as util.inspect writes one - otherwise it is an arrow function. Must
|
|
146
147
|
* not start inside a name,
|
|
147
148
|
* so each name is tried once, from its first character; a bracket is not part
|
|
148
|
-
* of the name's own run, so `[token=x]` is a name after a bracket.
|
|
149
|
+
* of the name's own run, so `[token=x]` is a name after a bracket. The `--`
|
|
150
|
+
* of a command's option is not part of it either: `--password=x`.
|
|
149
151
|
*/
|
|
150
|
-
const KEY_CANDIDATE = /(?<![\w$.-])((?:\\?["'])?)([A-Za-z_$][\w$.-]*(?:\[[\w$.-]*\])*)((?:\\?["'])?)[ \t]*(:=|=>|:|=(?!=))[ \t]*/g;
|
|
152
|
+
const KEY_CANDIDATE = /(?:(?<![\w$.-])|(?<=(?<![\w$.-])--))((?:\\?["'])?)([A-Za-z_$][\w$.-]*(?:\[[\w$.-]*\])*)((?:\\?["'])?)[ \t]*(:=|=>|:|=(?!=))[ \t]*/g;
|
|
151
153
|
/**
|
|
152
154
|
* What every name the shared matcher calls sensitive contains, written
|
|
153
155
|
* without separators. Tested first so the matcher - which splits a name into
|
|
154
156
|
* words - runs only for the few names that could be one.
|
|
155
157
|
*/
|
|
156
158
|
const KEY_STEMS = /pass|pwd|secret|token|auth|cred|key|sig|jwt|cookie|session/i;
|
|
159
|
+
/**
|
|
160
|
+
* A parameter of a query string or fragment quoted in the text, named as only
|
|
161
|
+
* a URL makes sensitive - `?key=`, `&sig=`, `?code=...&state=...` (see
|
|
162
|
+
* `isUrlOnlySensitiveKey`). Tested first for the same reason as `KEY_STEMS`,
|
|
163
|
+
* which `code` and `state` are not in.
|
|
164
|
+
*/
|
|
165
|
+
const URL_ONLY_KEY_HINT = /[?&#](?:code|state|sig|key)=/i;
|
|
157
166
|
/**
|
|
158
167
|
* Names whose value is a whole header: a scheme and a credential
|
|
159
168
|
* (`Authorization: ApiKey sk_...`), or every cookie of a `Cookie` line.
|
|
@@ -181,13 +190,20 @@ const SCHEME_WORD = /^(?:bearer|basic|token)$/i;
|
|
|
181
190
|
*
|
|
182
191
|
* The name is held to the same matcher as an attribute's key - the
|
|
183
192
|
* deployment's `redaction.keys` included - so free text and structured data
|
|
184
|
-
* cannot drift apart.
|
|
193
|
+
* cannot drift apart. A name that is a parameter of a quoted URL - written
|
|
194
|
+
* straight after its `?`, `&` or `#`, and straight before its `=` - is held
|
|
195
|
+
* to what a recorded URL is, too: `?key=`, `&sig=`, `?code=` and `&state=`
|
|
196
|
+
* are masked there and nowhere else, so `code=42 state=open` in prose stays.
|
|
185
197
|
*
|
|
186
198
|
* `literalValuesOnly` is the variant for source code, where the text either
|
|
187
199
|
* side of a `:` or `=` is mostly identifiers: `const token = await sign(user)`
|
|
188
200
|
* assigns no secret, and masking `await` would shred every frame that mentions
|
|
189
201
|
* a credential by name. In source a secret is a literal, so only a quoted
|
|
190
|
-
* value is masked there.
|
|
202
|
+
* value is masked there - in code. Inside a string, a template literal or a
|
|
203
|
+
* comment the text is text again, and a pair there is read as a message's
|
|
204
|
+
* is: `"...?base=USD&token=9f8e..."`, a connection string's `Password=x;`,
|
|
205
|
+
* `--password=x` in a shell command, a commented-out `DB_PASSWORD=x`. See
|
|
206
|
+
* `sourceTextReader` for how a frame is read for them.
|
|
191
207
|
*/
|
|
192
208
|
function keyValueRule(isSensitiveKey, checkEveryKey, literalValuesOnly) {
|
|
193
209
|
const isSensitive = (key) => {
|
|
@@ -197,16 +213,21 @@ function keyValueRule(isSensitiveKey, checkEveryKey, literalValuesOnly) {
|
|
|
197
213
|
return {
|
|
198
214
|
scan: (text, mask) => {
|
|
199
215
|
// Most lines name no credential at all; they are passed over whole.
|
|
200
|
-
if (!checkEveryKey &&
|
|
216
|
+
if (!checkEveryKey &&
|
|
217
|
+
!KEY_STEMS.test(text) &&
|
|
218
|
+
!URL_ONLY_KEY_HINT.test(text)) {
|
|
201
219
|
return text;
|
|
202
220
|
}
|
|
203
221
|
const candidate = new RegExp(KEY_CANDIDATE.source, "g");
|
|
222
|
+
const textAt = literalValuesOnly ? sourceTextReader(text) : undefined;
|
|
204
223
|
let output = "";
|
|
205
224
|
let copiedTo = 0;
|
|
206
225
|
let match;
|
|
207
226
|
while ((match = candidate.exec(text))) {
|
|
208
227
|
const key = match[2];
|
|
209
|
-
if ((match[4] === "=>" && !match[3]) ||
|
|
228
|
+
if ((match[4] === "=>" && !match[3]) ||
|
|
229
|
+
!(isSensitive(key) ||
|
|
230
|
+
(isUrlOnlySensitiveKey(key) && isQueryParameter(text, match)))) {
|
|
210
231
|
continue;
|
|
211
232
|
}
|
|
212
233
|
const valueStart = match.index + match[0].length;
|
|
@@ -220,6 +241,29 @@ function keyValueRule(isSensitiveKey, checkEveryKey, literalValuesOnly) {
|
|
|
220
241
|
continue;
|
|
221
242
|
}
|
|
222
243
|
const header = HEADER_KEY.test(key.toLowerCase().replace(/[-_]/g, ""));
|
|
244
|
+
// In source, where the name sits: in code, or in text.
|
|
245
|
+
const within = textAt?.(match.index + match[1].length);
|
|
246
|
+
// A quoted value - `\"x\"` included - is read as in code: a quote
|
|
247
|
+
// there may also be where the string ends (`"a&token=" + t`), but
|
|
248
|
+
// a frame that starts inside a template literal reads its closing
|
|
249
|
+
// backtick as an opening one, and a secret quoted after it would be
|
|
250
|
+
// missed.
|
|
251
|
+
const first = text[valueStart];
|
|
252
|
+
if (within !== undefined &&
|
|
253
|
+
within !== "code" &&
|
|
254
|
+
first !== '"' &&
|
|
255
|
+
first !== "'" &&
|
|
256
|
+
first !== "`" &&
|
|
257
|
+
!(first === "\\" && /["']/.test(text[valueStart + 1] ?? ""))) {
|
|
258
|
+
const end = readBareInText(text, valueStart, header);
|
|
259
|
+
if (end !== -1) {
|
|
260
|
+
output += text.slice(copiedTo, valueStart);
|
|
261
|
+
output += mask(text.slice(valueStart, end));
|
|
262
|
+
copiedTo = end;
|
|
263
|
+
candidate.lastIndex = end;
|
|
264
|
+
}
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
223
267
|
const valueEnd = readValue(text, valueStart, literalValuesOnly, header);
|
|
224
268
|
if (valueEnd === -1) {
|
|
225
269
|
continue;
|
|
@@ -233,6 +277,113 @@ function keyValueRule(isSensitiveKey, checkEveryKey, literalValuesOnly) {
|
|
|
233
277
|
},
|
|
234
278
|
};
|
|
235
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* Reads a frame of source once, front to back, and tells what each position
|
|
282
|
+
* asked about sits in. Positions must be asked about in order - as the scan
|
|
283
|
+
* finds its names - so however many are asked, every character is read once.
|
|
284
|
+
*
|
|
285
|
+
* A comment is recognised before a quote, so `// don't` opens no string. A
|
|
286
|
+
* `'` or `"` string ends at its line, as JavaScript's do, so a quote it
|
|
287
|
+
* misreads - one in a regular expression, an apostrophe in JSX - is text for
|
|
288
|
+
* the rest of that line only. A template literal's `${...}` is code again,
|
|
289
|
+
* through the brace that closes it. A frame cut out of the middle of a file
|
|
290
|
+
* may start inside a template literal or a comment. That start is read as
|
|
291
|
+
* code, as every frame was before; a template literal's closing backtick
|
|
292
|
+
* then opens one, and the code up to the next backtick is read as text,
|
|
293
|
+
* where a bare value after a credential's name is masked although it is
|
|
294
|
+
* code.
|
|
295
|
+
*/
|
|
296
|
+
function sourceTextReader(text) {
|
|
297
|
+
let span = "code";
|
|
298
|
+
let blockComment = false;
|
|
299
|
+
// One count of open braces per `${` the code is inside.
|
|
300
|
+
const expressions = [];
|
|
301
|
+
let read = 0;
|
|
302
|
+
return (at) => {
|
|
303
|
+
while (read < at) {
|
|
304
|
+
const char = text[read];
|
|
305
|
+
const next = text[read + 1];
|
|
306
|
+
if (span === "code") {
|
|
307
|
+
if (char === "/" && (next === "/" || next === "*")) {
|
|
308
|
+
span = "comment";
|
|
309
|
+
blockComment = next === "*";
|
|
310
|
+
read += 2;
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
314
|
+
span = char;
|
|
315
|
+
}
|
|
316
|
+
else if (char === "{" && expressions.length > 0) {
|
|
317
|
+
expressions[expressions.length - 1]++;
|
|
318
|
+
}
|
|
319
|
+
else if (char === "}" && expressions.length > 0) {
|
|
320
|
+
if (expressions[expressions.length - 1] === 0) {
|
|
321
|
+
expressions.pop();
|
|
322
|
+
span = "`";
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
expressions[expressions.length - 1]--;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
else if (span === "comment") {
|
|
330
|
+
if (blockComment ? char === "*" && next === "/" : char === "\n") {
|
|
331
|
+
span = "code";
|
|
332
|
+
read += blockComment ? 2 : 1;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
else if (char === "\\") {
|
|
337
|
+
read += 2;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
else if (char === span || (char === "\n" && span !== "`")) {
|
|
341
|
+
span = "code";
|
|
342
|
+
}
|
|
343
|
+
else if (span === "`" && char === "$" && next === "{") {
|
|
344
|
+
span = "code";
|
|
345
|
+
expressions.push(0);
|
|
346
|
+
read += 2;
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
read += 1;
|
|
350
|
+
}
|
|
351
|
+
return span;
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Where a value without quotes ends inside a string or comment of source, or
|
|
356
|
+
* -1 when there is none: where a message's would, and also at a backtick, a
|
|
357
|
+
* `${`, or the `*\/` closing a comment - the text's own end.
|
|
358
|
+
*/
|
|
359
|
+
function readBareInText(text, at, header) {
|
|
360
|
+
const stop = header ? HEADER_VALUE_END : BARE_VALUE_END;
|
|
361
|
+
let end = at;
|
|
362
|
+
while (end < text.length) {
|
|
363
|
+
const char = text[end];
|
|
364
|
+
if (stop.test(char) ||
|
|
365
|
+
char === "\n" ||
|
|
366
|
+
char === "`" ||
|
|
367
|
+
(char === "$" && text[end + 1] === "{") ||
|
|
368
|
+
(char === "*" && text[end + 1] === "/")) {
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
end++;
|
|
372
|
+
}
|
|
373
|
+
// Nothing, or the next pair's key.
|
|
374
|
+
return end === at || text[end - 1] === ":" ? -1 : end;
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Whether a `KEY_CANDIDATE` match is a parameter of a quoted URL: right
|
|
378
|
+
* after its `?`, `&` or `#`, right before a bare `=`. `a&&state=1` is code.
|
|
379
|
+
*/
|
|
380
|
+
function isQueryParameter(text, match) {
|
|
381
|
+
const before = text[match.index - 1];
|
|
382
|
+
return ((before === "?" ||
|
|
383
|
+
before === "#" ||
|
|
384
|
+
(before === "&" && text[match.index - 2] !== "&")) &&
|
|
385
|
+
match[0] === `${match[2]}=`);
|
|
386
|
+
}
|
|
236
387
|
/** Where the value starting at `at` ends, or -1 when there is none to mask. */
|
|
237
388
|
function readValue(text, at, literalValuesOnly, header) {
|
|
238
389
|
const first = text[at];
|
|
@@ -406,6 +557,27 @@ function buildDefaultRules(isSensitiveKey, checkEveryKey, literalValuesOnly = fa
|
|
|
406
557
|
// Card numbers before any key: a key's bare value stops at a space, so
|
|
407
558
|
// `token: 4000 0566 5566 5556` would lose only its first group.
|
|
408
559
|
{ scan: scanCardNumbers },
|
|
560
|
+
// The password in a URL: `postgres://app:hunter2@db/orders`, a git remote
|
|
561
|
+
// `https://deploy:ghp_...@host/repo.git` that a failed `execFile` quotes.
|
|
562
|
+
// No key stands in front of it, so the key=value rule never sees it. The
|
|
563
|
+
// scheme, the user and everything from the `@` on are kept.
|
|
564
|
+
//
|
|
565
|
+
// The password runs to the last `@` before a `/`, as a URL parser reads
|
|
566
|
+
// it, so one with an `@`, `#` or `?` it never escaped - the URL that does
|
|
567
|
+
// not parse, and is quoted in the error for it - is masked whole. It
|
|
568
|
+
// stops at a `/`, a quote or a space, which keeps `host:4873/@scope/pkg`
|
|
569
|
+
// and `"http://host:3000","email":"a@b.c"` as they were, and a port
|
|
570
|
+
// followed by a query (`host:3000?email=a@b.c`) is a port. Anchored on
|
|
571
|
+
// `://`, and a password stops at the `/` of the next one, so a line is
|
|
572
|
+
// read a bounded number of times however many it holds.
|
|
573
|
+
//
|
|
574
|
+
// In source a `${...}` in that place is an expression, not a secret.
|
|
575
|
+
{
|
|
576
|
+
pattern: new RegExp(String.raw `(:\/\/[^\s:@/?#"'\x60]*:)(?!\d{1,5}[?#,])` +
|
|
577
|
+
(literalValuesOnly ? String.raw `(?!\$\{)` : "") +
|
|
578
|
+
String.raw `[^\s/"'\x60]+(?=@)`, "g"),
|
|
579
|
+
replace: (replacement, prefix) => `${prefix}${replacement}`,
|
|
580
|
+
},
|
|
409
581
|
// Authorization schemes: `Bearer eyJ...`, `Basic dXNlcjpwYXNz`.
|
|
410
582
|
//
|
|
411
583
|
// Must precede the key=value rule. That rule's value stops at whitespace, so
|
|
@@ -574,10 +746,12 @@ export class LogRedactor {
|
|
|
574
746
|
: buildDefaultRules(this.matchesSensitiveKey, checkEveryKey);
|
|
575
747
|
const extra = (options.patterns ?? []).map((pattern) => ({
|
|
576
748
|
// Rebuilt with the global flag: a caller-supplied pattern without /g would
|
|
577
|
-
// silently redact only the first occurrence in a line.
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
749
|
+
// silently redact only the first occurrence in a line. And rebuilt when
|
|
750
|
+
// it has one too: every `replace` sets a global pattern's `lastIndex`
|
|
751
|
+
// back to 0, and the object is the application's - an `exec()` loop
|
|
752
|
+
// over it that logs a line per match started again from the first one
|
|
753
|
+
// on every line, and never ended.
|
|
754
|
+
pattern: toGlobalRegExp(pattern),
|
|
581
755
|
replace: (replacement) => replacement,
|
|
582
756
|
}));
|
|
583
757
|
this.rules = [...defaults, ...extra];
|
|
@@ -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.
|
|
@@ -42,9 +48,14 @@ redactor?: {
|
|
|
42
48
|
*/
|
|
43
49
|
obfuscate?: RegExp): string;
|
|
44
50
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
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.
|
|
49
60
|
*/
|
|
50
|
-
export declare function toGlobalRegExp
|
|
61
|
+
export declare function toGlobalRegExp<T extends RegExp | undefined>(pattern: T): T;
|
|
@@ -8,6 +8,14 @@ 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]";
|
|
12
20
|
/** `REDACTED` as a URL carries it. */
|
|
13
21
|
const MASKED_VALUE = encodeURIComponent(REDACTED);
|
|
@@ -67,21 +75,26 @@ obfuscate) {
|
|
|
67
75
|
const isSharedSensitive = redactor
|
|
68
76
|
? (key) => redactor.isSensitiveKey(key)
|
|
69
77
|
: isSensitiveKeyByDefault;
|
|
70
|
-
const isSensitive = (key) =>
|
|
78
|
+
const isSensitive = (key) => isUrlOnlySensitiveKey(key) || isSharedSensitive(key);
|
|
71
79
|
const redacted = redactUrl(url, isSensitive, isSharedSensitive, 0);
|
|
72
80
|
return obfuscate ? redacted.replaceAll(obfuscate, REDACTED) : redacted;
|
|
73
81
|
}
|
|
74
82
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
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.
|
|
79
92
|
*/
|
|
80
93
|
export function toGlobalRegExp(pattern) {
|
|
81
|
-
if (!pattern
|
|
94
|
+
if (!pattern) {
|
|
82
95
|
return pattern;
|
|
83
96
|
}
|
|
84
|
-
return new RegExp(pattern.source, pattern.flags + "g");
|
|
97
|
+
return new RegExp(pattern.source, pattern.global ? pattern.flags : pattern.flags + "g");
|
|
85
98
|
}
|
|
86
99
|
function redactUrl(url, isSensitive, isSharedSensitive, depth) {
|
|
87
100
|
const queryAt = url.indexOf("?");
|
|
@@ -39,6 +39,12 @@ export declare function collectCodeFrames(stack: string | undefined, options?: {
|
|
|
39
39
|
* a stack that came off an error.
|
|
40
40
|
*/
|
|
41
41
|
message?: string;
|
|
42
|
+
/**
|
|
43
|
+
* The error's `name`, and its `code` when it has one that is text: with
|
|
44
|
+
* the message, the header the stack opens with. `Error` when absent.
|
|
45
|
+
*/
|
|
46
|
+
name?: string;
|
|
47
|
+
code?: string;
|
|
42
48
|
/**
|
|
43
49
|
* Redacts the source before a frame is cut from it - see
|
|
44
50
|
* `redactedSource`.
|
|
@@ -96,43 +96,31 @@ function isReadableSourcePath(file, root = resolve(process.cwd())) {
|
|
|
96
96
|
* `"\n at f (/app/dist/config.js:1:1)"` - a request parameter interpolated
|
|
97
97
|
* into a validation or driver error is enough - reads as a frame, and sits
|
|
98
98
|
* above the real ones. So the boundary is taken from the error itself: the
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
99
|
+
* stack has to open with exactly the header its name, code and message make,
|
|
100
|
+
* and only the unbroken run of frame lines directly after it counts. That run
|
|
101
|
+
* also ends before anything appended later (`Caused by: ...`), which is one
|
|
102
|
+
* more message.
|
|
103
103
|
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
104
|
+
* The whole header, not the message wherever it ends the first line: a first
|
|
105
|
+
* line written from some other message can end with this one, and the lines
|
|
106
|
+
* after it are then that message's - a `GraphQLError` carries the stack of
|
|
107
|
+
* the error it wraps, whose message may quote the request and end with the
|
|
108
|
+
* wrapper's own.
|
|
109
|
+
*
|
|
110
|
+
* Fails closed. An error whose message was reassigned after its stack was
|
|
111
|
+
* written, or that took over another error's stack, no longer matches it and
|
|
112
|
+
* gets no code frames, which costs a nicety; guessing the boundary instead is
|
|
113
|
+
* what the paragraphs above are about.
|
|
107
114
|
*
|
|
108
115
|
* With `message` undefined the whole string is treated as frames - only for a
|
|
109
116
|
* stack the caller wrote itself.
|
|
110
117
|
*/
|
|
111
|
-
function frameLines(stack, message) {
|
|
118
|
+
function frameLines(stack, message, name = "Error", code) {
|
|
112
119
|
if (message === undefined) {
|
|
113
120
|
return stack.split("\n", MAX_FRAME_LINES);
|
|
114
121
|
}
|
|
115
|
-
|
|
116
|
-
if (
|
|
117
|
-
headerEnd = stack.indexOf("\n");
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
// A match that starts on the first line and runs to the end of a line
|
|
121
|
-
// has one possible place: the message's first line has to end where the
|
|
122
|
-
// stack's first line does. That steps over a match inside the name -
|
|
123
|
-
// `UnauthorizedException: Unauthorized` - and it is computed rather than
|
|
124
|
-
// searched for. Neither string is bounded here, nor certain to be what
|
|
125
|
-
// V8 wrote - a driver can copy both off a server's reply - and visiting
|
|
126
|
-
// every match on a first line that repeats the message was quadratic.
|
|
127
|
-
const stackLineEnd = stack.indexOf("\n");
|
|
128
|
-
const messageLineEnd = message.indexOf("\n");
|
|
129
|
-
const at = stackLineEnd - (messageLineEnd === -1 ? message.length : messageLineEnd);
|
|
130
|
-
headerEnd =
|
|
131
|
-
at >= 0 && at < stackLineEnd && stack.startsWith(message, at)
|
|
132
|
-
? at + message.length
|
|
133
|
-
: -1;
|
|
134
|
-
}
|
|
135
|
-
if (headerEnd === -1 || stack[headerEnd] !== "\n") {
|
|
122
|
+
const headerEnd = headerEndOf(stack, message, name, code);
|
|
123
|
+
if (headerEnd === -1) {
|
|
136
124
|
return [];
|
|
137
125
|
}
|
|
138
126
|
const lines = [];
|
|
@@ -144,6 +132,42 @@ function frameLines(stack, message) {
|
|
|
144
132
|
}
|
|
145
133
|
return lines;
|
|
146
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Where the header a stack opens with ends, when it is the one V8 writes for
|
|
137
|
+
* an error of this name, code and message - at the line break after it - or
|
|
138
|
+
* -1. V8 writes what `Error.prototype.toString` returns: `name: message`, or
|
|
139
|
+
* either alone when the other is empty. Node's own errors put their code in
|
|
140
|
+
* it too, `TypeError [ERR_INVALID_ARG_TYPE]: ...`, and `source-map-support`
|
|
141
|
+
* writes `name: ` for an empty message as well.
|
|
142
|
+
*
|
|
143
|
+
* Compared in place rather than built and compared: neither the message nor
|
|
144
|
+
* the stack is bounded here.
|
|
145
|
+
*/
|
|
146
|
+
function headerEndOf(stack, message, name, code) {
|
|
147
|
+
const endsAt = (at) => (stack[at] === "\n" ? at : -1);
|
|
148
|
+
if (message === "" && stack.startsWith(name)) {
|
|
149
|
+
const end = endsAt(name.length);
|
|
150
|
+
if (end !== -1) {
|
|
151
|
+
return end;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (name === "" && stack.startsWith(message)) {
|
|
155
|
+
const end = endsAt(message.length);
|
|
156
|
+
if (end !== -1) {
|
|
157
|
+
return end;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const prefixes = code === undefined ? [`${name}: `] : [`${name}: `, `${name} [${code}]: `];
|
|
161
|
+
for (const prefix of prefixes) {
|
|
162
|
+
if (stack.startsWith(prefix) && stack.startsWith(message, prefix.length)) {
|
|
163
|
+
const end = endsAt(prefix.length + message.length);
|
|
164
|
+
if (end !== -1) {
|
|
165
|
+
return end;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return -1;
|
|
170
|
+
}
|
|
147
171
|
/**
|
|
148
172
|
* Reads one V8 frame line, in either shape:
|
|
149
173
|
* at fn (/abs/path/file.ts:42:31)
|
|
@@ -180,11 +204,22 @@ function parseFrameLine(raw) {
|
|
|
180
204
|
return undefined;
|
|
181
205
|
}
|
|
182
206
|
return {
|
|
183
|
-
file: location.slice(0, lineAt),
|
|
207
|
+
file: ownCopy(location.slice(0, lineAt)),
|
|
184
208
|
line: Number(line),
|
|
185
209
|
column: Number(column),
|
|
186
210
|
};
|
|
187
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* Its own copy of a frame's path. V8 keeps a substring as a slice of the
|
|
214
|
+
* string it was cut from, and the path is cut from the stack, which repeats
|
|
215
|
+
* a message as long as whoever threw made it: a code frame's `file`, kept on
|
|
216
|
+
* an error payload until the flush, held the whole stack alive for a path of
|
|
217
|
+
* a few dozen characters. UTF-16 both ways, so a path with a lone surrogate
|
|
218
|
+
* is copied as it is.
|
|
219
|
+
*/
|
|
220
|
+
function ownCopy(text) {
|
|
221
|
+
return Buffer.from(text, "utf16le").toString("utf16le");
|
|
222
|
+
}
|
|
188
223
|
/**
|
|
189
224
|
* A frame's file as a path. V8 names an ES module's file by its URL -
|
|
190
225
|
* `at run (file:///app/dist/main.js:3:9)` - which is the same file as the
|
|
@@ -202,9 +237,9 @@ function toFilePath(file) {
|
|
|
202
237
|
return undefined;
|
|
203
238
|
}
|
|
204
239
|
}
|
|
205
|
-
function parseFrames(stack,
|
|
240
|
+
function parseFrames(stack, header) {
|
|
206
241
|
const frames = [];
|
|
207
|
-
for (const raw of frameLines(stack, message)) {
|
|
242
|
+
for (const raw of frameLines(stack, header.message, header.name, header.code)) {
|
|
208
243
|
const parsed = parseFrameLine(raw);
|
|
209
244
|
if (!parsed) {
|
|
210
245
|
continue;
|
|
@@ -308,7 +343,7 @@ export function collectCodeFrames(stack, options = {}) {
|
|
|
308
343
|
}
|
|
309
344
|
const maxFrames = options.maxFrames ?? 5;
|
|
310
345
|
try {
|
|
311
|
-
const frames = parseFrames(stack, options
|
|
346
|
+
const frames = parseFrames(stack, options).slice(0, maxFrames);
|
|
312
347
|
const codeFrames = [];
|
|
313
348
|
for (const frame of frames) {
|
|
314
349
|
let file = frame.file;
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
* eagerly costs one module parse and keeps this file loadable either way.
|
|
9
9
|
*/
|
|
10
10
|
import { originalPositionFor, sourceContentFor, TraceMap, } from "@jridgewell/trace-mapping";
|
|
11
|
-
import { readFileSync } from "node:fs";
|
|
12
|
-
import { dirname, resolve } from "node:path";
|
|
11
|
+
import { readFileSync, realpathSync } from "node:fs";
|
|
12
|
+
import { dirname, resolve, sep } from "node:path";
|
|
13
13
|
const SOURCE_MAPPING_URL_PATTERN = /\/\/[#@]\s*sourceMappingURL=(\S+)\s*$/gm;
|
|
14
14
|
const INLINE_MAP_PREFIX = "data:application/json";
|
|
15
15
|
/**
|
|
@@ -20,10 +20,31 @@ const INLINE_MAP_PREFIX = "data:application/json";
|
|
|
20
20
|
const MAX_CACHED_MAPS = 200;
|
|
21
21
|
/** Parsed maps by compiled-file path; null marks "looked, found none". */
|
|
22
22
|
const mapCache = new Map();
|
|
23
|
+
/**
|
|
24
|
+
* Reads a file that is inside the working directory once links are resolved.
|
|
25
|
+
*
|
|
26
|
+
* Neither file read here is named by this package: the compiled file comes
|
|
27
|
+
* off a stack frame that was checked only as written, and its map is
|
|
28
|
+
* wherever a comment in that file says. What a map embeds ships as the code
|
|
29
|
+
* frame, so both are held to the gate `readSource` holds a source file to -
|
|
30
|
+
* the resolved path under the resolved root. Without it a link under the
|
|
31
|
+
* root, or a `sourceMappingURL` leading out of it, shipped source the
|
|
32
|
+
* application does not own.
|
|
33
|
+
*
|
|
34
|
+
* Throws for a path that leads anywhere else, which the caller reads as "no
|
|
35
|
+
* map".
|
|
36
|
+
*/
|
|
37
|
+
function readFileUnderRoot(path) {
|
|
38
|
+
const real = realpathSync(path);
|
|
39
|
+
if (!real.startsWith(realpathSync(process.cwd()) + sep)) {
|
|
40
|
+
throw new Error(`${path} is outside the working directory`);
|
|
41
|
+
}
|
|
42
|
+
return readFileSync(real, "utf8");
|
|
43
|
+
}
|
|
23
44
|
function readMapFor(file) {
|
|
24
45
|
let compiled;
|
|
25
46
|
try {
|
|
26
|
-
compiled =
|
|
47
|
+
compiled = readFileUnderRoot(file);
|
|
27
48
|
}
|
|
28
49
|
catch {
|
|
29
50
|
return null;
|
|
@@ -50,7 +71,7 @@ function readMapFor(file) {
|
|
|
50
71
|
else {
|
|
51
72
|
const mapPath = resolve(dirname(file), url);
|
|
52
73
|
mapDir = dirname(mapPath);
|
|
53
|
-
rawMap =
|
|
74
|
+
rawMap = readFileUnderRoot(mapPath);
|
|
54
75
|
}
|
|
55
76
|
}
|
|
56
77
|
catch {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** A copy of `store` for a span, with `entries` over what it copies. */
|
|
2
|
+
export declare function copyStore<K, V>(store: Map<K, V>, ...entries: Array<[K, V]>): Map<K, V>;
|
|
3
|
+
/** Sets `key` in `store` and in every store it was copied from. */
|
|
4
|
+
export declare function setThroughCopies(store: Map<any, unknown>, key: unknown, value: unknown): void;
|
|
5
|
+
/**
|
|
6
|
+
* `key` as `store` holds it, or else as the nearest store it was copied from
|
|
7
|
+
* holds it: what a span running beside the one that set it - copied before
|
|
8
|
+
* it was set - still finds in the operation's own store.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getThroughCopies(store: Map<any, unknown>, key: unknown): unknown;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A span runs in a copy of its caller's store, so that the copy can name the
|
|
3
|
+
* span as the caller of whatever it calls. An attribute written to the copy
|
|
4
|
+
* alone was gone once the span returned - one a guard, a pipe or a service
|
|
5
|
+
* set never reached the handler - while an operation that is not recorded
|
|
6
|
+
* makes no copies and kept it.
|
|
7
|
+
*
|
|
8
|
+
* So each copy remembers the store it was made from, here rather than in the
|
|
9
|
+
* store: the store is the application's to read and write as well, and an
|
|
10
|
+
* entry of the agent's own would show in every `entries()` it walks. An
|
|
11
|
+
* attribute is written through the copy to every store it came from, so it
|
|
12
|
+
* is there once the span returns, for `getAttribute` and for a plain
|
|
13
|
+
* `store.get` alike.
|
|
14
|
+
*/
|
|
15
|
+
const copiedFrom = new WeakMap();
|
|
16
|
+
/** A copy of `store` for a span, with `entries` over what it copies. */
|
|
17
|
+
export function copyStore(store, ...entries) {
|
|
18
|
+
const copy = new Map(store);
|
|
19
|
+
for (const [key, value] of entries) {
|
|
20
|
+
copy.set(key, value);
|
|
21
|
+
}
|
|
22
|
+
copiedFrom.set(copy, store);
|
|
23
|
+
return copy;
|
|
24
|
+
}
|
|
25
|
+
/** Sets `key` in `store` and in every store it was copied from. */
|
|
26
|
+
export function setThroughCopies(store, key, value) {
|
|
27
|
+
for (let current = store; current; current = copiedFrom.get(current)) {
|
|
28
|
+
current.set(key, value);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* `key` as `store` holds it, or else as the nearest store it was copied from
|
|
33
|
+
* holds it: what a span running beside the one that set it - copied before
|
|
34
|
+
* it was set - still finds in the operation's own store.
|
|
35
|
+
*/
|
|
36
|
+
export function getThroughCopies(store, key) {
|
|
37
|
+
for (let current = store; current; current = copiedFrom.get(current)) {
|
|
38
|
+
if (current.has(key)) {
|
|
39
|
+
return current.get(key);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
package/package.json
CHANGED