@bymax-one/nest-logger 1.4.9 → 1.4.10

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/CHANGELOG.md CHANGED
@@ -11,6 +11,51 @@ heading here.
11
11
 
12
12
  ## [Unreleased]
13
13
 
14
+ ## [1.4.10] - 2026-11-06
15
+
16
+ ### Security
17
+
18
+ - **A database error is recognised from its SQLSTATE `code` alone.** 1.4.9 also required an own
19
+ `severity` and an own `routine`. The PostgreSQL protocol sends the SQLSTATE `code` and the message
20
+ on every error but `routine` only when the server includes it, and a client library or proxy may
21
+ not expose `severity`, so a database error that arrived without them kept `detail`, `where` and
22
+ `internalQuery`, which can quote the row. An error whose own `code` is a string of five characters
23
+ from `0-9A-Z`, the first not `E`, now loses those three fields; its other fields stay. The first
24
+ character rules out the operating-system codes of the same length (`EPIPE`, `EPERM`), and no class
25
+ of the PostgreSQL error codes starts with `E`. An error with any other `code`, or none, is written
26
+ as before.
27
+
28
+ ### Changed
29
+
30
+ - **One redaction call stops expanding after 200000 containers.** The name redaction walks an object
31
+ once per path that reaches it, so a value in which objects share references (a graph where each
32
+ level holds the next twice) took time and produced output that doubled with each level: a graph 20
33
+ levels deep took 570 ms and wrote 19 MB. A call now counts the containers it visits (an object, an
34
+ array or an error, and also what an object's `toJSON` returns and an error's `cause` and `errors`,
35
+ each of which is walked as a container of its own), and from the 200001st on it writes
36
+ `'[REDACTION_BUDGET_EXCEEDED]'` in place of each container, keeping the primitives beside them, as
37
+ `'[REDACTION_DEPTH_EXCEEDED]'` does for depth. Nothing is passed through unwalked. The added
38
+ `type` and `message` of a nested `Error` also count against the cause-chain budget, as `cause` and
39
+ `errors` do.
40
+ - **A stack is written at most 16384 characters long.** The engine builds the stack header from an
41
+ error's `name` and `message`, and when those are themselves errors it stringifies them, so a
42
+ single `stack` could reach hundreds of megabytes (about 54 MB in one measured line). The stack is
43
+ now cut at 16384 characters, after the dependency frames are dropped and the control characters
44
+ escaped, and ends with `…[truncated]`; a cut never leaves half of a surrogate pair. It applies to
45
+ every stack the logger writes: the root `err`, each `cause` and `errors` link, a nested `Error` in
46
+ the metadata, the semconv `exception.stacktrace`, the `stack` argument of `error(message, stack)`
47
+ (cut, with its frames left as given) and the stack in a destination's shutdown-failure report on
48
+ stderr.
49
+
50
+ #### Apply to a derived backend
51
+
52
+ Nothing to change in code. A stack longer than 16384 characters is cut and ends with `…[truncated]`;
53
+ an ordinary stack is far shorter. A single log call that makes more than 200000 container visits,
54
+ such as an array of 250000 objects in the metadata (an object with a `toJSON` counts twice, once for
55
+ itself and once for what it returns), now has its last containers replaced by the marker; a filter
56
+ or alert that reads the whole array should read the part that is kept, or the array should be
57
+ summarised before it is logged. Ordinary entries are unaffected.
58
+
14
59
  ## [1.4.9] - 2026-11-06
15
60
 
16
61
  ### Security
@@ -2413,7 +2458,8 @@ published `dist/` is identical — no runtime behaviour changes for consumers.
2413
2458
  - Professional CI suite: `ci.yml`, `bench.yml`, `codeql.yml`, `scorecard.yml`,
2414
2459
  `release.yml`, Dependabot, and issue templates
2415
2460
 
2416
- [Unreleased]: https://github.com/bymaxone/nest-logger/compare/v1.4.9...HEAD
2461
+ [Unreleased]: https://github.com/bymaxone/nest-logger/compare/v1.4.10...HEAD
2462
+ [1.4.10]: https://github.com/bymaxone/nest-logger/compare/v1.4.9...v1.4.10
2417
2463
  [1.4.9]: https://github.com/bymaxone/nest-logger/compare/v1.4.8...v1.4.9
2418
2464
  [1.4.8]: https://github.com/bymaxone/nest-logger/compare/v1.4.7...v1.4.8
2419
2465
  [1.4.7]: https://github.com/bymaxone/nest-logger/compare/v1.4.6...v1.4.7
package/README.md CHANGED
@@ -727,7 +727,7 @@ any other serializer output.
727
727
  An error's own enumerable properties (`code`, `statusCode`, domain fields) are carried through, as
728
728
  Pino's standard serializer did, with one exception: the arguments of a failed driver call are not
729
729
  written. The fields `args` and `parameters` are omitted, a `command` object is reduced to its
730
- `name`, and a PostgreSQL error loses `detail`, `where` and `internalQuery`, which quote the row and
730
+ `name`, and an error with a SQLSTATE `code` (five characters from `0-9A-Z`, the first not `E`) loses `detail`, `where` and `internalQuery`, which quote the row and
731
731
  the statement (its other fields stay), because the redaction
732
732
  matches key names and cannot see a value inside a string.
733
733
 
@@ -1248,6 +1248,8 @@ Every `write()` runs inside a try/catch. A destination that throws or rejects pr
1248
1248
 
1249
1249
  ### What redaction covers, and what it cannot
1250
1250
 
1251
+ The walk is bounded: a container nested more than 100 levels deep is written as `[REDACTION_DEPTH_EXCEEDED]`, and a call that visits more than 200000 containers writes `[REDACTION_BUDGET_EXCEEDED]` in place of each further one. Nothing past either bound is passed through unwalked. A stack is written at most 16384 characters long and ends with `…[truncated]` when it was cut.
1252
+
1251
1253
  Redaction here matches **field names**, not values. Under the default `redactStrategy: 'names'` a secret stored under a covered name is censored at any depth — under the legacy `'paths'` strategy the four-level `fast-redact` ceiling applies instead, and a deeper field is not covered. Either way, the same secret **embedded inside a string** under an uncovered name travels intact. Measured:
1252
1254
 
1253
1255
  ```json
@@ -440,12 +440,11 @@ function toSanitizedError(value, seen, depth2, maxDepth) {
440
440
  }
441
441
  var ARGUMENT_FIELDS = /* @__PURE__ */ new Set(["args", "parameters"]);
442
442
  var DATABASE_VALUE_FIELDS = /* @__PURE__ */ new Set(["detail", "where", "internalQuery"]);
443
- var SQLSTATE = /^[0-9A-Z]{5}$/;
443
+ var SQLSTATE = /^[0-9A-DF-Z][0-9A-Z]{4}$/;
444
444
  var OMIT_OWN_FIELD = /* @__PURE__ */ Symbol("omit-own-field");
445
445
  function isDatabaseError(source) {
446
- const own = (key) => Object.getOwnPropertyDescriptor(source, key)?.value;
447
- const code = own("code");
448
- return typeof own("severity") === "string" && typeof own("routine") === "string" && typeof code === "string" && SQLSTATE.test(code);
446
+ const code = Object.getOwnPropertyDescriptor(source, "code")?.value;
447
+ return typeof code === "string" && SQLSTATE.test(code);
449
448
  }
450
449
  function ownFieldForLog(source, key, value) {
451
450
  if (ARGUMENT_FIELDS.has(key.toLowerCase())) return OMIT_OWN_FIELD;
@@ -489,10 +488,24 @@ function readAggregatedErrors(value) {
489
488
  const candidate = value.errors;
490
489
  return Array.isArray(candidate) ? candidate : void 0;
491
490
  }
491
+ var MAX_STACK_LENGTH = 16384;
492
+ var STACK_TRUNCATED = "\u2026[truncated]";
493
+ var MAX_STACK_SCAN = 8 * MAX_STACK_LENGTH;
492
494
  function scrubStack(stack) {
493
- return escapeControlCharacters(
494
- stack.split("\n").filter((line) => !line.includes("node_modules")).join("\n")
495
+ const scrubbed = escapeControlCharacters(
496
+ stack.slice(0, MAX_STACK_SCAN).split("\n").filter((line) => !line.includes("node_modules")).join("\n")
495
497
  );
498
+ return cutAtLimit(scrubbed, stack.length > MAX_STACK_SCAN);
499
+ }
500
+ function cutAtLimit(text, alreadyCut) {
501
+ if (!alreadyCut && text.length <= MAX_STACK_LENGTH) return text;
502
+ const head = text.slice(0, MAX_STACK_LENGTH);
503
+ const last = head.charCodeAt(head.length - 1);
504
+ const whole = last >= 55296 && last <= 56319 ? head.slice(0, -1) : head;
505
+ return whole + STACK_TRUNCATED;
506
+ }
507
+ function cutStack(stack) {
508
+ return cutAtLimit(stack, false);
496
509
  }
497
510
  function isObject(value) {
498
511
  return typeof value === "object" && value !== null;
@@ -608,7 +621,7 @@ exports.PinoLoggerService = class PinoLoggerService {
608
621
  const context = typeof optionalParams[1] === "string" ? optionalParams[1] : this.context;
609
622
  const payload = {};
610
623
  assignIfDefined(payload, "context", context);
611
- assignIfDefined(payload, "stack", stack);
624
+ assignIfDefined(payload, "stack", stack === void 0 ? stack : cutStack(stack));
612
625
  assignIfDefined(payload, "err", findErrorParam(optionalParams));
613
626
  this.pino.error(payload, safeLogMessage(message));
614
627
  }
@@ -1474,6 +1487,8 @@ function destinationToStream(destination, health) {
1474
1487
  // src/server/utils/redact-by-name.util.ts
1475
1488
  var REDACT_MAX_TRAVERSAL_DEPTH = 100;
1476
1489
  var REDACT_DEPTH_EXCEEDED = "[REDACTION_DEPTH_EXCEEDED]";
1490
+ var REDACT_MAX_VISITS = 2e5;
1491
+ var REDACT_BUDGET_EXCEEDED = "[REDACTION_BUDGET_EXCEEDED]";
1477
1492
  var REDACT_CIRCULAR = "[Circular]";
1478
1493
  function resolveToJson(value) {
1479
1494
  let getter;
@@ -1531,6 +1546,9 @@ function walk(value, rules, depth2, ancestors, honorToJson = true, inSerialized
1531
1546
  if (depth2 > REDACT_MAX_TRAVERSAL_DEPTH) {
1532
1547
  return REDACT_DEPTH_EXCEEDED;
1533
1548
  }
1549
+ if (++rules.visits.count > rules.maxVisits) {
1550
+ return REDACT_BUDGET_EXCEEDED;
1551
+ }
1534
1552
  if (ancestors.has(value)) {
1535
1553
  return REDACT_CIRCULAR;
1536
1554
  }
@@ -1543,6 +1561,7 @@ function walkError(error, rules, depth2, ancestors, honorToJson, inSerialized) {
1543
1561
  try {
1544
1562
  return walkObject(error, rules, depth2, ancestors, honorToJson, inSerialized);
1545
1563
  } catch {
1564
+ rules.visits.count--;
1546
1565
  return walk(sanitizeError(error), rules, depth2, ancestors, honorToJson, inSerialized);
1547
1566
  }
1548
1567
  }
@@ -1634,7 +1653,7 @@ function addErrorParts(copy, error, ownKeys, rules, depth2, ancestors) {
1634
1653
  const taken = new Set(ownKeys);
1635
1654
  const level = linkDepths.get(error) ?? 0;
1636
1655
  let incomplete = false;
1637
- const add = (key, covers, read, write = (value) => walk(value, rules, depth2 + 1, ancestors)) => {
1656
+ const add = (key, covers, read, write = (value) => walkChainPart(value, level, rules, depth2, ancestors)) => {
1638
1657
  if (taken.has(key)) return;
1639
1658
  try {
1640
1659
  const written = write(read());
@@ -1653,30 +1672,22 @@ function addErrorParts(copy, error, ownKeys, rules, depth2, ancestors) {
1653
1672
  () => error.stack,
1654
1673
  (stack) => typeof stack === "string" ? scrubStack(stack) : void 0
1655
1674
  );
1656
- add(
1657
- "cause",
1658
- ["cause"],
1659
- () => error.cause,
1660
- (cause) => walkChainPart(cause, level, rules, depth2, ancestors)
1661
- );
1662
- add(
1663
- "errors",
1664
- ["errors"],
1665
- () => capErrors(error.errors),
1666
- (errors) => walkChainPart(errors, level, rules, depth2, ancestors)
1667
- );
1675
+ add("cause", ["cause"], () => error.cause);
1676
+ add("errors", ["errors"], () => capErrors(error.errors));
1668
1677
  if (incomplete && !taken.has(INCOMPLETE_KEY)) defineOwn(copy, INCOMPLETE_KEY, true);
1669
1678
  }
1670
- function createNameRedactor(fieldNames, censor, serializedKeys) {
1679
+ function createNameRedactor(fieldNames, censor, serializedKeys, maxVisits = REDACT_MAX_VISITS) {
1671
1680
  const rules = {
1672
1681
  sensitive: new Set(fieldNames.map((name) => name.toLowerCase())),
1673
1682
  censor,
1674
1683
  serializedKeys: new Set(serializedKeys),
1675
- errorsAsData: serializedKeys !== void 0
1684
+ errorsAsData: serializedKeys !== void 0,
1685
+ maxVisits
1676
1686
  };
1677
1687
  return (value, isRecordRoot = false) => {
1678
1688
  try {
1679
- return walk(value, rules, 0, /* @__PURE__ */ new Set(), !isRecordRoot);
1689
+ const call = { ...rules, visits: { count: 0 } };
1690
+ return walk(value, call, 0, /* @__PURE__ */ new Set(), !isRecordRoot);
1680
1691
  } catch {
1681
1692
  return {
1682
1693
  _redactionFailed: true,
@@ -2440,7 +2451,7 @@ var DestinationRegistry = class {
2440
2451
  let detail = "UnknownError";
2441
2452
  try {
2442
2453
  const stack = isErrorLike(cause) ? cause.stack : void 0;
2443
- detail = stack === void 0 ? toSingleLineMessage(isErrorLike(cause) ? String(cause.message) : String(cause)) : escapeControlCharacters(String(stack));
2454
+ detail = stack === void 0 ? toSingleLineMessage(isErrorLike(cause) ? String(cause.message) : String(cause)) : escapeControlCharacters(cutStack(String(stack)));
2444
2455
  } catch {
2445
2456
  }
2446
2457
  writeStderrSafely(`[DestinationRegistry] Shutdown failed for "${name}": ${detail}
@@ -437,12 +437,11 @@ function toSanitizedError(value, seen, depth2, maxDepth) {
437
437
  }
438
438
  var ARGUMENT_FIELDS = /* @__PURE__ */ new Set(["args", "parameters"]);
439
439
  var DATABASE_VALUE_FIELDS = /* @__PURE__ */ new Set(["detail", "where", "internalQuery"]);
440
- var SQLSTATE = /^[0-9A-Z]{5}$/;
440
+ var SQLSTATE = /^[0-9A-DF-Z][0-9A-Z]{4}$/;
441
441
  var OMIT_OWN_FIELD = /* @__PURE__ */ Symbol("omit-own-field");
442
442
  function isDatabaseError(source) {
443
- const own = (key) => Object.getOwnPropertyDescriptor(source, key)?.value;
444
- const code = own("code");
445
- return typeof own("severity") === "string" && typeof own("routine") === "string" && typeof code === "string" && SQLSTATE.test(code);
443
+ const code = Object.getOwnPropertyDescriptor(source, "code")?.value;
444
+ return typeof code === "string" && SQLSTATE.test(code);
446
445
  }
447
446
  function ownFieldForLog(source, key, value) {
448
447
  if (ARGUMENT_FIELDS.has(key.toLowerCase())) return OMIT_OWN_FIELD;
@@ -486,10 +485,24 @@ function readAggregatedErrors(value) {
486
485
  const candidate = value.errors;
487
486
  return Array.isArray(candidate) ? candidate : void 0;
488
487
  }
488
+ var MAX_STACK_LENGTH = 16384;
489
+ var STACK_TRUNCATED = "\u2026[truncated]";
490
+ var MAX_STACK_SCAN = 8 * MAX_STACK_LENGTH;
489
491
  function scrubStack(stack) {
490
- return escapeControlCharacters(
491
- stack.split("\n").filter((line) => !line.includes("node_modules")).join("\n")
492
+ const scrubbed = escapeControlCharacters(
493
+ stack.slice(0, MAX_STACK_SCAN).split("\n").filter((line) => !line.includes("node_modules")).join("\n")
492
494
  );
495
+ return cutAtLimit(scrubbed, stack.length > MAX_STACK_SCAN);
496
+ }
497
+ function cutAtLimit(text, alreadyCut) {
498
+ if (!alreadyCut && text.length <= MAX_STACK_LENGTH) return text;
499
+ const head = text.slice(0, MAX_STACK_LENGTH);
500
+ const last = head.charCodeAt(head.length - 1);
501
+ const whole = last >= 55296 && last <= 56319 ? head.slice(0, -1) : head;
502
+ return whole + STACK_TRUNCATED;
503
+ }
504
+ function cutStack(stack) {
505
+ return cutAtLimit(stack, false);
493
506
  }
494
507
  function isObject(value) {
495
508
  return typeof value === "object" && value !== null;
@@ -605,7 +618,7 @@ var PinoLoggerService = class {
605
618
  const context = typeof optionalParams[1] === "string" ? optionalParams[1] : this.context;
606
619
  const payload = {};
607
620
  assignIfDefined(payload, "context", context);
608
- assignIfDefined(payload, "stack", stack);
621
+ assignIfDefined(payload, "stack", stack === void 0 ? stack : cutStack(stack));
609
622
  assignIfDefined(payload, "err", findErrorParam(optionalParams));
610
623
  this.pino.error(payload, safeLogMessage(message));
611
624
  }
@@ -1471,6 +1484,8 @@ function destinationToStream(destination, health) {
1471
1484
  // src/server/utils/redact-by-name.util.ts
1472
1485
  var REDACT_MAX_TRAVERSAL_DEPTH = 100;
1473
1486
  var REDACT_DEPTH_EXCEEDED = "[REDACTION_DEPTH_EXCEEDED]";
1487
+ var REDACT_MAX_VISITS = 2e5;
1488
+ var REDACT_BUDGET_EXCEEDED = "[REDACTION_BUDGET_EXCEEDED]";
1474
1489
  var REDACT_CIRCULAR = "[Circular]";
1475
1490
  function resolveToJson(value) {
1476
1491
  let getter;
@@ -1528,6 +1543,9 @@ function walk(value, rules, depth2, ancestors, honorToJson = true, inSerialized
1528
1543
  if (depth2 > REDACT_MAX_TRAVERSAL_DEPTH) {
1529
1544
  return REDACT_DEPTH_EXCEEDED;
1530
1545
  }
1546
+ if (++rules.visits.count > rules.maxVisits) {
1547
+ return REDACT_BUDGET_EXCEEDED;
1548
+ }
1531
1549
  if (ancestors.has(value)) {
1532
1550
  return REDACT_CIRCULAR;
1533
1551
  }
@@ -1540,6 +1558,7 @@ function walkError(error, rules, depth2, ancestors, honorToJson, inSerialized) {
1540
1558
  try {
1541
1559
  return walkObject(error, rules, depth2, ancestors, honorToJson, inSerialized);
1542
1560
  } catch {
1561
+ rules.visits.count--;
1543
1562
  return walk(sanitizeError(error), rules, depth2, ancestors, honorToJson, inSerialized);
1544
1563
  }
1545
1564
  }
@@ -1631,7 +1650,7 @@ function addErrorParts(copy, error, ownKeys, rules, depth2, ancestors) {
1631
1650
  const taken = new Set(ownKeys);
1632
1651
  const level = linkDepths.get(error) ?? 0;
1633
1652
  let incomplete = false;
1634
- const add = (key, covers, read, write = (value) => walk(value, rules, depth2 + 1, ancestors)) => {
1653
+ const add = (key, covers, read, write = (value) => walkChainPart(value, level, rules, depth2, ancestors)) => {
1635
1654
  if (taken.has(key)) return;
1636
1655
  try {
1637
1656
  const written = write(read());
@@ -1650,30 +1669,22 @@ function addErrorParts(copy, error, ownKeys, rules, depth2, ancestors) {
1650
1669
  () => error.stack,
1651
1670
  (stack) => typeof stack === "string" ? scrubStack(stack) : void 0
1652
1671
  );
1653
- add(
1654
- "cause",
1655
- ["cause"],
1656
- () => error.cause,
1657
- (cause) => walkChainPart(cause, level, rules, depth2, ancestors)
1658
- );
1659
- add(
1660
- "errors",
1661
- ["errors"],
1662
- () => capErrors(error.errors),
1663
- (errors) => walkChainPart(errors, level, rules, depth2, ancestors)
1664
- );
1672
+ add("cause", ["cause"], () => error.cause);
1673
+ add("errors", ["errors"], () => capErrors(error.errors));
1665
1674
  if (incomplete && !taken.has(INCOMPLETE_KEY)) defineOwn(copy, INCOMPLETE_KEY, true);
1666
1675
  }
1667
- function createNameRedactor(fieldNames, censor, serializedKeys) {
1676
+ function createNameRedactor(fieldNames, censor, serializedKeys, maxVisits = REDACT_MAX_VISITS) {
1668
1677
  const rules = {
1669
1678
  sensitive: new Set(fieldNames.map((name) => name.toLowerCase())),
1670
1679
  censor,
1671
1680
  serializedKeys: new Set(serializedKeys),
1672
- errorsAsData: serializedKeys !== void 0
1681
+ errorsAsData: serializedKeys !== void 0,
1682
+ maxVisits
1673
1683
  };
1674
1684
  return (value, isRecordRoot = false) => {
1675
1685
  try {
1676
- return walk(value, rules, 0, /* @__PURE__ */ new Set(), !isRecordRoot);
1686
+ const call = { ...rules, visits: { count: 0 } };
1687
+ return walk(value, call, 0, /* @__PURE__ */ new Set(), !isRecordRoot);
1677
1688
  } catch {
1678
1689
  return {
1679
1690
  _redactionFailed: true,
@@ -2437,7 +2448,7 @@ var DestinationRegistry = class {
2437
2448
  let detail = "UnknownError";
2438
2449
  try {
2439
2450
  const stack = isErrorLike(cause) ? cause.stack : void 0;
2440
- detail = stack === void 0 ? toSingleLineMessage(isErrorLike(cause) ? String(cause.message) : String(cause)) : escapeControlCharacters(String(stack));
2451
+ detail = stack === void 0 ? toSingleLineMessage(isErrorLike(cause) ? String(cause.message) : String(cause)) : escapeControlCharacters(cutStack(String(stack)));
2441
2452
  } catch {
2442
2453
  }
2443
2454
  writeStderrSafely(`[DestinationRegistry] Shutdown failed for "${name}": ${detail}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bymax-one/nest-logger",
3
- "version": "1.4.9",
3
+ "version": "1.4.10",
4
4
  "description": "Structured JSON logging for NestJS based on Pino 10, with optional OpenTelemetry correlation.",
5
5
  "author": "Bymax One <support@bymax.one>",
6
6
  "license": "MIT",