@bymax-one/nest-logger 1.4.12 → 1.4.14

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,147 @@ heading here.
11
11
 
12
12
  ## [Unreleased]
13
13
 
14
+ ## [1.4.14] - 2026-11-09
15
+
16
+ ### Added
17
+
18
+ - **A host can stop the tenant id from being whatever the client sent.** The bundled
19
+ `RequestIdMiddleware` copies the `x-tenant-id` header into `tenantId` on every entry of the
20
+ request, so any client could attribute its requests to another tenant. The default is unchanged.
21
+ `http.tenantIdHeader` now also accepts `false`, which reads no header, and the new
22
+ `http.tenantIdResolver: (request) => string | undefined` takes the tenant from what the host
23
+ trusts. When a resolver is given it is the only source: the header is not read, whether the
24
+ resolver answers or not. Its answer passes the same 256-character rule as the header, and a
25
+ resolver that throws gives no `tenantId`.
26
+ #### Apply to a derived backend
27
+ A backend that authenticates its tenants sets `tenantIdHeader: false` or supplies
28
+ `tenantIdResolver`; otherwise nothing changes.
29
+
30
+ ### Fixed
31
+
32
+ - **A log call no longer throws for an `Error` that holds an accessor with no setter, under
33
+ `redactStrategy: 'paths'`.** A non-enumerable getter-only property on an `Error` (a
34
+ `defineProperty` with `get` and no `set`) met a path that censors it, and the assignment of the
35
+ censor threw out of the log call. The copy of the `Error` the walk makes now discards the write.
36
+ The secret was never written; the call used to fail.
37
+ **Apply to a derived backend:** nothing to do.
38
+ - **A `tenantIdResolver` written as `async` no longer ends the process.** Its answer is a promise,
39
+ which is no tenant id, and one that later rejected was an unhandled rejection. The answer is
40
+ still ignored, and its rejection is now absorbed. Resolve the tenant synchronously, or set it
41
+ later with `LogContextService.set('tenantId', ...)`.
42
+ **Apply to a derived backend:** a resolver that is `async` gave no tenant id before and gives
43
+ none now; read the tenant synchronously, or set it with `LogContextService.set`.
44
+ - **The 415 the body parser raises no longer repeats what the client sent.** `HttpExceptionFilter`
45
+ answered an exposed client error with the parser's own message, and for an unsupported
46
+ `Content-Encoding` or charset that message carries the header value inside quotes. The value
47
+ reached the response and the `HTTP_EXCEPTION_HANDLED` entry. Both now say
48
+ `unsupported content encoding` or `unsupported charset`. The status and every other message are
49
+ unchanged.
50
+ **Apply to a derived backend:** nothing to do.
51
+ - **The query string of a request no longer reaches a logged exception message.** The `url` fields
52
+ of the access log, the interceptor and the filter always had the query stripped, but Nest words
53
+ its not-found message as `Cannot GET /path?code=...&token=...`, with the whole URL, and
54
+ `HttpExceptionFilter` wrote that message as the `msg` of `HTTP_EXCEPTION_HANDLED`. Any request to
55
+ a route that does not exist, such as an OAuth callback or a reset link on a mistyped path,
56
+ put its `code`, `state` or `token` in the log, where name-based redaction cannot censor free
57
+ text. The request's own query string (`originalUrl` and `url`, as a mounted request differs) is
58
+ now cut out of the message by exact substring, in the filter's `msg` and in the `errorMessage` of
59
+ the interceptor and of the access log. The response is unchanged. A secret in the path itself
60
+ (`;jsessionid=...`, a token segment, a percent-encoded `%3F`) is part of the path and stays.
61
+ **Apply to a derived backend:** nothing to do.
62
+ - **The library's entry for a body the parser refused no longer quotes the body.** The parser
63
+ words the message of a JSON syntax error with a piece of the body, or a partial value when one is
64
+ left unquoted, and a `verify` function's message is the application's own text.
65
+ `HttpExceptionFilter` wrote that message as the `msg` of `HTTP_EXCEPTION_HANDLED`, where
66
+ name-based redaction cannot censor it. For `entity.parse.failed`, `entity.verify.failed` and the
67
+ two unsupported 415 types the entry now carries a fixed message and the parser `type` as
68
+ `bodyParserType`; the interceptor's `errorMessage` does the same. Any other exposed client error
69
+ is logged with its own message, as before. Nest rebuilds the parser's `SyntaxError` (malformed
70
+ JSON) as a `BadRequestException` before any filter runs, so on the Express adapter
71
+ `BymaxLoggerModule` mounts a small error layer after the body parser that keeps the parser's
72
+ `type` for the filter; another adapter, or a parser mounted after `app.init()`, keeps the old
73
+ message. The response body is unchanged, and stays the application's concern.
74
+ **Apply to a derived backend:** an alert or query that matched the old `msg` text of these
75
+ entries matches `bodyParserType` now.
76
+ - **A log call no longer throws under `redactStrategy: 'paths'` on JSON-shaped metadata.** A
77
+ `token` or `password` inside an object held under `cookie` or `authorization` made `@pinojs/redact` throw `TypeError: Cannot assign to read only property`, which the
78
+ call surfaced to the application. That module assigns its censor into the object it is given, in
79
+ strict mode, and the copies the name walk builds had non-writable properties. They are
80
+ writable now, and so is the clone of an `Error`, which could also throw on 1.4.13 when a consumer
81
+ path with a wildcard aimed at its `stack`, `message` or `cause` (`a.*.err.stack`) came together
82
+ with a second path (a lone path did not throw). The secret is still censored, and the output of
83
+ every other input is the same. The default `'names'` strategy was never affected.
84
+ **Apply to a derived backend:** nothing to do.
85
+ - **No `toJSON` hook the walk did not see survives the redaction, in the default mode.**
86
+ `JSON.stringify` reads `toJSON` after the redaction, with `[[Get]]`, so a hook the walk had not
87
+ followed ran afterwards and could write a value under a censored name in clear. Shapes that did
88
+ that: a function-valued Proxy whose `get` trap answers a hook that its `has` and descriptor traps
89
+ hide, a `toJSON` accessor on an `Error` or on any object that answers no hook the first time and a
90
+ hook afterwards, which the property loop or the clone of an `Error` copied along with every other
91
+ property, and a function whose prototype is such a Proxy. A callable whose answer can change (a
92
+ Proxy, a function with a `toJSON` accessor, or one with a Proxy on its prototype chain) is now
93
+ dropped as a function with no hook is, a `toJSON` that reads as a function where the stringifier
94
+ would honour it is not copied, and the clone of an `Error` ends with an own, non-enumerable
95
+ `toJSON` set to `undefined`, which the stringifier reads before any hook on the prototype chain.
96
+ The rule is about the output object, not about what the walk read earlier, which the original
97
+ can change before the output is built.
98
+ Ordinary input is unchanged, including a `toJSON` key that is not a function. These shapes need an
99
+ object built by code that hides its hook, not a value read from data. The one visible change is
100
+ that `Object.getOwnPropertyNames` of a logged `Error` clone lists `toJSON`; `Object.keys`, `for...in`
101
+ and the JSON output are the same.
102
+ **Apply to a derived backend:** nothing to do.
103
+
104
+ ### Internal
105
+
106
+ - **`as const` tables are now mutated entry by entry in a development check.** Stryker neither
107
+ removes a property or an element from a constant table nor changes a number in it, so an entry
108
+ could disappear with every test green. `scripts/mutate-const-tables.mjs` does it, against a test
109
+ command you give it, and `CLAUDE.md` has the command. It is a development tool and not a CI
110
+ step. Its first run found 20 entries of the default redaction set that no test pinned, from
111
+ `passwordHash` to `req.headers["x-auth-token"]`; a spec now pins every name and path that was
112
+ shipped, and censors each name through the default redactor. Nothing in the package changes.
113
+ - **The `minimatch` denial-of-service advisory is closed in the dev tree.** The vulnerable range
114
+ (>= 7.0.0 < 11.0.1) was reachable through the TypeScript ESLint parser, Stryker and Jest coverage. The
115
+ 10.x copies resolve to 11 and `test-exclude` moves to 8, which uses a `glob` that accepts it.
116
+ Development dependencies only: the published package has no dependencies and its `dist/` does
117
+ not change.
118
+
119
+ ## [1.4.13] - 2026-11-08
120
+
121
+ ### Fixed
122
+
123
+ - **A client error the body parser raises keeps its status.** An error with the `http-errors` shape,
124
+ a `status` or `statusCode` from 400 to 499 and `expose: true`, such as a body over the limit
125
+ (413) or an unsupported content encoding (415), was answered by `HttpExceptionFilter` as a 500
126
+ `{ statusCode, message }`. That filter is registered by `forRoot` with `http.isEnabled` and
127
+ `http.shouldCaptureExceptions` (default `true`), or by hand; `forRootAsync` does not register it, and an application that uses it without the filter was
128
+ answered by Nest's own handler, with the right status, before and after. It is now answered
129
+ with its own status and message, `{ statusCode: 413, message: 'request entity too large' }`, and
130
+ logged as `HTTP_EXCEPTION_HANDLED` at warn level. The same error thrown inside a route is no
131
+ longer logged as `HTTP_REQUEST_SERVER_ERROR` but as `HTTP_REQUEST_CLIENT_ERROR`. An error
132
+ without `expose: true`, or with a status outside 400 to 499, stays a 500 with the fixed body.
133
+ **Apply to a derived backend:** nothing to do; a client that read the 500 for these cases now
134
+ reads the 413 or 415 the parser meant.
135
+ - **The README says which filter answers a parser error, and for whom.** `forRoot` with
136
+ `http.isEnabled` and `http.shouldCaptureExceptions` (default `true`) registers the library's own
137
+ exception filter as an `APP_FILTER` provider, so the
138
+ import order of the modules decides between `APP_FILTER` providers, and `app.useGlobalFilters(...)`
139
+ always answers ahead of them. `forRootAsync` registers no filter, so none of that applies to it
140
+ unless the filter is registered by hand. The 1.4.12 note on this was written without that scope.
141
+ Pinned by `use-request-context-filters.e2e-spec.ts`.
142
+
143
+ ### Internal
144
+
145
+ - **A source file over 800 lines now fails CI unless it is on a frozen baseline.**
146
+ `scripts/check-file-length.mjs` (`pnpm check:file-length`) runs in the `verify` job and, in a
147
+ pull request, also compares the baseline with the base branch's, so the baseline cannot widen. It
148
+ starts with the three files that are already over the limit. Nothing in the package changes.
149
+ - **The redaction walk is split into functions of at most 50 lines.** `walkObject` was 174 lines;
150
+ it is now a short function that delegates to smaller ones, with the rationale kept as their
151
+ documentation. No behaviour changes: 48000 comparisons of the old and the new walk over
152
+ generated records (six configurations, as record root and not), and the existing suite, give
153
+ identical output.
154
+
14
155
  ## [1.4.12] - 2026-11-08
15
156
 
16
157
  ### Added
@@ -2547,7 +2688,9 @@ published `dist/` is identical — no runtime behaviour changes for consumers.
2547
2688
  - Professional CI suite: `ci.yml`, `bench.yml`, `codeql.yml`, `scorecard.yml`,
2548
2689
  `release.yml`, Dependabot, and issue templates
2549
2690
 
2550
- [Unreleased]: https://github.com/bymaxone/nest-logger/compare/v1.4.12...HEAD
2691
+ [Unreleased]: https://github.com/bymaxone/nest-logger/compare/v1.4.14...HEAD
2692
+ [1.4.14]: https://github.com/bymaxone/nest-logger/compare/v1.4.13...v1.4.14
2693
+ [1.4.13]: https://github.com/bymaxone/nest-logger/compare/v1.4.12...v1.4.13
2551
2694
  [1.4.12]: https://github.com/bymaxone/nest-logger/compare/v1.4.11...v1.4.12
2552
2695
  [1.4.11]: https://github.com/bymaxone/nest-logger/compare/v1.4.10...v1.4.11
2553
2696
  [1.4.10]: https://github.com/bymaxone/nest-logger/compare/v1.4.9...v1.4.10
package/README.md CHANGED
@@ -373,7 +373,9 @@ in a guard, and at that point there is no principal yet. The acting user is on t
373
373
  entry, where the guard has populated it. Both entries carry the same `requestId`, so joining on
374
374
  that gives the whole request.
375
375
 
376
- URLs are automatically normalized — `/users/550e8400-e29b-41d4-a716-446655440000` becomes `/users/:id` so Loki/Grafana cardinality stays bounded. The query string is stripped from every logged URL, because a magic-link token or reset code in a query parameter is a secret no key-name redaction can scrub out of a string value.
376
+ For the parser errors listed here the request body's text stays out of the library's own entry. For a body that does not parse (`entity.parse.failed`), a body the `verify` function of the parser refuses (`entity.verify.failed`) and an unsupported content encoding or charset, `HTTP_EXCEPTION_HANDLED` carries a fixed message and the parser `type` in a `bodyParserType` field, because the parser's message can quote part of the body and no key-name redaction can censor a string. The response body is the application's concern: for a body that does not parse or that `verify` refuses, the parser's own message still reaches the client unless the application answers the error itself. A `verify` function that gives its error a `type` of its own is outside this list. The malformed-JSON case needs the Express adapter and a body parser mounted before `app.init()`, as Nest's own default one is: the module mounts a small error layer after that parser, because Nest rebuilds the parser's error as a `BadRequestException` that no longer has its `type`. With another adapter, or a parser you mount after `app.init()`, that entry keeps the parser's message.
377
+
378
+ URLs are automatically normalized — `/users/550e8400-e29b-41d4-a716-446655440000` becomes `/users/:id` so Loki/Grafana cardinality stays bounded. The query string is stripped from every logged URL, because a magic-link token or reset code in a query parameter is a secret no key-name redaction can scrub out of a string value. The same cut applies to the message of a logged exception: Nest words its not-found message as `Cannot GET /path?code=...`, so the request's own query string is removed from `HTTP_EXCEPTION_HANDLED`'s `msg` and from `errorMessage`. The response still carries what Nest wrote, which is the application's concern. Only a literal `?` starts a query: a secret placed in the path itself (`;jsessionid=...`, a token segment, a percent-encoded `%3F`) is part of the path and stays in `url`.
377
379
 
378
380
  > [!IMPORTANT]
379
381
  > **Why middleware and not an interceptor.** NestJS runs middleware → guards → interceptors →
@@ -451,10 +453,15 @@ To open only the correlation scope ahead of the parser, without the access log,
451
453
  `BymaxLoggerModule.useRequestContext(app)` at the same point. A request the parser refuses (truncated JSON,
452
454
  a content encoding it does not support, a body over the limit) then has its `requestId` and the
453
455
  `x-request-id` response header, so an exception filter that reads the id from `LogContextService`
454
- puts the same value in its envelope. Register that filter with `app.useGlobalFilters(...)` after `NestFactory.create()`: with
455
- `http.isEnabled` (and `http.shouldCaptureExceptions`, which defaults to `true`) the library registers its own exception filter as an `APP_FILTER` provider, and an
456
- `APP_FILTER` provider of yours was answered by it in the measurement, with or without an injected
457
- dependency, while a filter registered with `useGlobalFilters` answered instead. It can be combined with `applyAccessLog(app)` or
456
+ puts the same value in its envelope. Register that filter with `app.useGlobalFilters(...)` after `NestFactory.create()`, which answers
457
+ ahead of every `APP_FILTER` provider. `forRoot` with `http.isEnabled` (and
458
+ `http.shouldCaptureExceptions`, which defaults to `true`) registers the library's own exception
459
+ filter as an `APP_FILTER` provider; `forRootAsync` never does, so there is nothing for yours to
460
+ compete with unless you registered `HttpExceptionFilter` yourself. Where the library's filter is
461
+ registered, the order the modules are imported decides between `APP_FILTER` providers: one in your
462
+ root module, or in a module imported before `BymaxLoggerModule`, was answered by the library's
463
+ filter in the measurement, with or without an injected dependency, while one in a module imported
464
+ after it answered itself. It can be combined with `applyAccessLog(app)` or
458
465
  `applyRequestIdMiddleware(consumer)`: a second mount reuses the scope.
459
466
 
460
467
  `applyRequestIdMiddleware(consumer)` remains supported and unchanged; it is the right choice when
@@ -800,6 +807,11 @@ The bundled `RequestIdMiddleware` does not trust the headers it reads. A request
800
807
  | `x-request-id` | a fresh UUID, or none when `http.shouldGenerateRequestId` is `false` (an enclosing scope's `requestId`, if any, is then inherited) |
801
808
  | the tenant header (`http.tenantIdHeader`) | no `tenantId` from the header; a tenant is never minted |
802
809
 
810
+ **A `tenantId` read from the header is what the client asserted.** Any client can send any tenant in `x-tenant-id`, and every entry of its requests will carry it. The 256-character rule above keeps the value safe to write; it does not make it true. Do not use a header-derived `tenantId` to decide whose data an entry is about unless something in front of the application sets or overwrites the header. A host that authenticates its tenants has two options, and both leave the default unchanged for everyone else:
811
+
812
+ - `http: { tenantIdHeader: false }` reads no header, so the middleware takes no `tenantId` from the request.
813
+ - `http: { tenantIdResolver: (req) => ... }` takes the tenant from what the host trusts, for example a value an earlier middleware put on the request after verifying a token. When a resolver is given it is the only source: the header is not read even if the resolver answers nothing, so a client cannot fall back to it. The answer passes the same 256-character rule, and a resolver that throws gives no `tenantId`. The middleware runs before guards, so a tenant known only after authentication is set later with `LogContextService.set('tenantId', ...)`.
814
+
803
815
  An accepted id is echoed back on the response `x-request-id` header and written to every entry the request produces, which is why the set is narrow. The example above reads the header raw to stay short; use `RequestIdMiddleware` through `applyAccessLog(app)` or `applyRequestIdMiddleware(consumer)` unless you apply the same rule yourself.
804
816
 
805
817
  ---
@@ -836,13 +848,14 @@ Full options reference for `BymaxLoggerModule.forRoot(options)`:
836
848
 
837
849
  ### `http` options
838
850
 
839
- | Option | Type | Default | Description |
840
- | ------------------------------ | ---------- | ------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
841
- | `http.isEnabled` | `boolean` | `false` | Register `HttpLoggingInterceptor` (and, on `forRoot`, `HttpExceptionFilter`) globally |
842
- | `http.shouldCaptureExceptions` | `boolean` | `true` | Capture unhandled HTTP exceptions and emit `HTTP_EXCEPTION_UNHANDLED`. `forRoot` only: see the note under [Async configuration](#3-async-configuration-with-configservice) |
843
- | `http.shouldGenerateRequestId` | `boolean` | `true` | Generate a `requestId` when the inbound request header is absent |
844
- | `http.excludePaths` | `RegExp[]` | `[/^\/health$/, /^\/health\/live$/, /^\/health\/ready$/, /^\/metrics$/]` | Paths that bypass HTTP logging. Use anchored, linear-time regexes (ReDoS-safe) |
845
- | `http.tenantIdHeader` | `string` | `'x-tenant-id'` | Request header carrying the tenant identifier |
851
+ | Option | Type | Default | Description |
852
+ | ------------------------------ | ------------------------------ | ------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
853
+ | `http.isEnabled` | `boolean` | `false` | Register `HttpLoggingInterceptor` (and, on `forRoot`, `HttpExceptionFilter`) globally |
854
+ | `http.shouldCaptureExceptions` | `boolean` | `true` | Capture unhandled HTTP exceptions and emit `HTTP_EXCEPTION_UNHANDLED`. `forRoot` only: see the note under [Async configuration](#3-async-configuration-with-configservice) |
855
+ | `http.shouldGenerateRequestId` | `boolean` | `true` | Generate a `requestId` when the inbound request header is absent |
856
+ | `http.excludePaths` | `RegExp[]` | `[/^\/health$/, /^\/health\/live$/, /^\/health\/ready$/, /^\/metrics$/]` | Paths that bypass HTTP logging. Use anchored, linear-time regexes (ReDoS-safe) |
857
+ | `http.tenantIdHeader` | `string \| false` | `'x-tenant-id'` | Request header carrying the tenant identifier, or `false` to read none. A value from the header is what the client asserted: see [Inbound `x-request-id` and tenant header](#inbound-x-request-id-and-tenant-header) |
858
+ | `http.tenantIdResolver` | `(req) => string \| undefined` | none | Resolves the tenant id on the server side. When given it is the only source and the header is not read |
846
859
 
847
860
  ### `otel` options
848
861