@bymax-one/nest-auth 1.0.11 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +381 -1
- package/README.md +269 -107
- package/dist/client/index.cjs +41 -4
- package/dist/client/index.mjs +41 -4
- package/dist/nextjs/index.cjs +152 -51
- package/dist/nextjs/index.d.cts +149 -55
- package/dist/nextjs/index.d.ts +149 -55
- package/dist/nextjs/index.mjs +152 -51
- package/dist/react/index.cjs +77 -89
- package/dist/react/index.mjs +77 -89
- package/dist/server/index.cjs +5951 -1655
- package/dist/server/index.d.cts +2818 -188
- package/dist/server/index.d.ts +2818 -188
- package/dist/server/index.mjs +5933 -1644
- package/dist/shared/index.cjs +60 -9
- package/dist/shared/index.d.cts +126 -18
- package/dist/shared/index.d.ts +126 -18
- package/dist/shared/index.mjs +60 -9
- package/package.json +122 -43
package/dist/nextjs/index.d.cts
CHANGED
|
@@ -156,11 +156,11 @@ declare function buildSilentRefreshUrl(request: RequestWithUrl, redirectTo?: str
|
|
|
156
156
|
* its structured attributes so dedup logic (and any future cookie
|
|
157
157
|
* inspection) can work against a typed object instead of raw strings.
|
|
158
158
|
*
|
|
159
|
-
* {@link getSetCookieHeaders}
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
159
|
+
* {@link getSetCookieHeaders} extracts the list of raw Set-Cookie strings
|
|
160
|
+
* from either `Headers.getSetCookie()` or, for a header-like object that
|
|
161
|
+
* does not implement it, `get('set-cookie')` — which concatenates cookies
|
|
162
|
+
* with commas, and is only safe when the values do not themselves contain
|
|
163
|
+
* unquoted commas.
|
|
164
164
|
*
|
|
165
165
|
* Edge-Runtime-safe: pure string manipulation, no Node-only APIs.
|
|
166
166
|
*
|
|
@@ -177,7 +177,7 @@ declare function buildSilentRefreshUrl(request: RequestWithUrl, redirectTo?: str
|
|
|
177
177
|
* *most-recent-writer-wins* invariant users rely on. If that
|
|
178
178
|
* upstream behaviour ever changes, widen the key.
|
|
179
179
|
* - `parseSetCookieHeader` intentionally does NOT trim the cookie
|
|
180
|
-
* *value* — RFC 6265 §5.2
|
|
180
|
+
* *value* — RFC 6265 §5.2, item 2 says the value is everything
|
|
181
181
|
* after the first `=` up to the first `;`. Attribute *values*
|
|
182
182
|
* (e.g., `Path=/x `) are trimmed because the spec explicitly
|
|
183
183
|
* permits surrounding OWS there.
|
|
@@ -240,11 +240,11 @@ interface HeadersLike {
|
|
|
240
240
|
* with attribute semantics (`SameSite`, `Domain`) are lowercased to
|
|
241
241
|
* keep the dedup key stable. The `name` and `value` of the cookie
|
|
242
242
|
* itself are preserved as-is — including any internal whitespace, per
|
|
243
|
-
* RFC 6265 §5.2
|
|
243
|
+
* RFC 6265 §5.2, item 2.
|
|
244
244
|
*
|
|
245
245
|
* Rejects inputs containing CR or LF characters (header smuggling
|
|
246
246
|
* defence): the returned record has an empty `name`, which the dedup
|
|
247
|
-
*
|
|
247
|
+
* filter discards.
|
|
248
248
|
*
|
|
249
249
|
* Malformed input (no `=` in the first segment) similarly returns an
|
|
250
250
|
* empty parsed record.
|
|
@@ -274,13 +274,14 @@ declare function dedupeSetCookieHeaders(cookies: readonly string[]): string[];
|
|
|
274
274
|
* Return the array of raw `Set-Cookie` header strings from a
|
|
275
275
|
* {@link HeadersLike}.
|
|
276
276
|
*
|
|
277
|
-
* Prefers
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
277
|
+
* Prefers `Headers.getSetCookie()`, which correctly separates multiple
|
|
278
|
+
* Set-Cookie headers. Every runtime this library supports has it — the
|
|
279
|
+
* fallback is not for old Node but for the header-like objects consumers
|
|
280
|
+
* legitimately pass instead of a real `Headers`: test doubles, framework
|
|
281
|
+
* wrappers, and hand-rolled shims, all of which `HeadersLike` admits by
|
|
282
|
+
* typing the method optional. For those, the single comma-joined value from
|
|
283
|
+
* `get('set-cookie')` is split with an awareness that commas inside `Expires`
|
|
284
|
+
* values (`Expires=Wed, 09 Jun 2021 10:18:14 GMT`) are NOT separators.
|
|
284
285
|
*
|
|
285
286
|
* @param headers - Any object with `get()` and optionally `getSetCookie()`.
|
|
286
287
|
* @returns An array of raw `Set-Cookie` header strings (possibly empty).
|
|
@@ -324,6 +325,7 @@ declare function getSetCookieHeaders(headers: HeadersLike): string[];
|
|
|
324
325
|
*
|
|
325
326
|
* Pinning the algorithm at verification time eliminates both vectors.
|
|
326
327
|
*/
|
|
328
|
+
|
|
327
329
|
/**
|
|
328
330
|
* Header of a JWT as carried in the first base64url segment.
|
|
329
331
|
*
|
|
@@ -353,14 +355,50 @@ interface DecodedToken {
|
|
|
353
355
|
/**
|
|
354
356
|
* `true` when the payload parses AND `exp` is in the future.
|
|
355
357
|
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
* only when `verifyJwtToken` was invoked with a non-null secret.
|
|
358
|
+
* Expiry ONLY. It says nothing about the signature:
|
|
359
|
+
* {@link decodeJwtToken} sets it without checking one at all. It is
|
|
360
|
+
* one half of the question an authorisation decision is asking —
|
|
361
|
+
* see {@link DecodedToken.signatureVerified} for the other half and
|
|
362
|
+
* for the check that uses both.
|
|
362
363
|
*/
|
|
363
364
|
readonly isValid: boolean;
|
|
365
|
+
/**
|
|
366
|
+
* `true` only when the signature was actually checked against a
|
|
367
|
+
* non-empty secret — i.e. only past {@link verifyJwtToken}'s HMAC
|
|
368
|
+
* check. `false` on every {@link decodeJwtToken} result, including
|
|
369
|
+
* the ones that carry `isValid: true` and a fully populated payload,
|
|
370
|
+
* and on every `verifyJwtToken` failure (which returns an empty
|
|
371
|
+
* token, so `isValid` is `false` there as well — `verifyJwtToken`
|
|
372
|
+
* fails closed on a missing or empty secret rather than falling back
|
|
373
|
+
* to a decode).
|
|
374
|
+
*
|
|
375
|
+
* The two flags are INDEPENDENT, and neither is sufficient alone:
|
|
376
|
+
*
|
|
377
|
+
* - `isValid` without `signatureVerified` is a token that has not
|
|
378
|
+
* expired and that nobody checked the signature of — including one
|
|
379
|
+
* an attacker minted with `alg: none` and an arbitrary `sub`,
|
|
380
|
+
* `role` and `tenantId`. This is the trap the flag exists for: the
|
|
381
|
+
* two branches are otherwise indistinguishable at runtime, and
|
|
382
|
+
* `if (decoded.isValid && decoded.role === 'ADMIN')` is the natural
|
|
383
|
+
* reading of a function called `verifyJwtToken`.
|
|
384
|
+
* - `signatureVerified` without `isValid` is a **genuinely signed
|
|
385
|
+
* token that has expired**, or one carrying no `exp` at all. The
|
|
386
|
+
* signature is checked before the expiry is read, so this
|
|
387
|
+
* combination is reachable on the ordinary path — a session that
|
|
388
|
+
* simply ran out.
|
|
389
|
+
*
|
|
390
|
+
* So an authorisation decision reads BOTH:
|
|
391
|
+
*
|
|
392
|
+
* ```ts
|
|
393
|
+
* const decoded = await verifyJwtToken(token, secret)
|
|
394
|
+
* if (decoded.isValid && decoded.signatureVerified && decoded.role === 'ADMIN') {
|
|
395
|
+
* ```
|
|
396
|
+
*
|
|
397
|
+
* The proxy's own `TokenState.signatureVerified` is this conjunction
|
|
398
|
+
* already (`decoded.signatureVerified && decoded.isValid && …`); a
|
|
399
|
+
* caller using these helpers directly has to form it here.
|
|
400
|
+
*/
|
|
401
|
+
readonly signatureVerified: boolean;
|
|
364
402
|
/** Raw header object, or `undefined` if the header failed to parse. */
|
|
365
403
|
readonly header: JwtHeader | undefined;
|
|
366
404
|
/**
|
|
@@ -410,24 +448,33 @@ declare function decodeJwtToken(token: string): DecodedToken;
|
|
|
410
448
|
* 6. Verify the signature over `<header>.<payload>` (ASCII bytes).
|
|
411
449
|
* 7. Validate `exp` is in the future.
|
|
412
450
|
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
451
|
+
* **A missing secret fails closed.** `undefined`, `null` and the
|
|
452
|
+
* empty string all return `emptyDecoded()` rather than falling back
|
|
453
|
+
* to a decode. This function used to fall back to
|
|
454
|
+
* {@link decodeJwtToken}, and the two branches returned the same
|
|
455
|
+
* shape with the same `isValid: true` — so a caller writing
|
|
456
|
+
* `const d = await verifyJwtToken(t, secret)` and then
|
|
457
|
+
* `if (d.isValid && d.role === 'ADMIN')`, the natural
|
|
458
|
+
* reading of the name, admitted a token an attacker minted with
|
|
459
|
+
* `alg: none` the moment the secret went missing. An unset
|
|
460
|
+
* environment variable was enough to arrange that. A function that
|
|
461
|
+
* cannot verify must refuse, not quietly answer a weaker question;
|
|
462
|
+
* {@link decodeJwtToken} remains the explicit, correctly-named entry
|
|
463
|
+
* point for the non-authoritative read.
|
|
464
|
+
*
|
|
465
|
+
* The empty string is refused for a second reason of its own: an
|
|
466
|
+
* empty HMAC key is technically valid to the Web Crypto API and
|
|
467
|
+
* would verify a token signed with the same empty key — a common
|
|
468
|
+
* misconfiguration (`JWT_SECRET=""` in an environment file) that
|
|
469
|
+
* must not silently downgrade to a no-op.
|
|
425
470
|
*
|
|
426
471
|
* @param token - JWS compact serialisation.
|
|
427
|
-
* @param secret - HS256 shared secret
|
|
428
|
-
*
|
|
472
|
+
* @param secret - HS256 shared secret. A missing or empty value
|
|
473
|
+
* makes every token invalid.
|
|
429
474
|
* @returns A {@link DecodedToken} where `isValid` reflects BOTH the
|
|
430
|
-
* signature verification AND the expiry check
|
|
475
|
+
* signature verification AND the expiry check, and
|
|
476
|
+
* `signatureVerified` is `true` only when a signature was
|
|
477
|
+
* actually checked.
|
|
431
478
|
*/
|
|
432
479
|
declare function verifyJwtToken(token: string, secret?: string | null): Promise<DecodedToken>;
|
|
433
480
|
/**
|
|
@@ -711,9 +758,14 @@ declare function createAuthProxy(config: AuthProxyConfig): AuthProxyInstance;
|
|
|
711
758
|
* - **Rate limiting.** The handler makes one upstream `POST` per
|
|
712
759
|
* inbound `GET`. Apply route-level rate limiting in the consumer
|
|
713
760
|
* app to prevent DoS amplification.
|
|
714
|
-
* - **Host-header trust.**
|
|
715
|
-
* `
|
|
716
|
-
*
|
|
761
|
+
* - **Host-header trust.** No longer an obligation for this
|
|
762
|
+
* handler's redirects: the `Location` it emits is relative, so
|
|
763
|
+
* the browser resolves it against the URL it actually requested
|
|
764
|
+
* and a forged `Host` has no destination to choose. The request
|
|
765
|
+
* origin is still derived from `Host`, but only to REJECT a
|
|
766
|
+
* cross-origin `?redirect=` — a comparison, not a destination,
|
|
767
|
+
* where the worst a forged value buys is admitting a path that
|
|
768
|
+
* lands on the real app anyway.
|
|
717
769
|
* - **`apiBase`.** Always points to your OWN NestJS backend — all
|
|
718
770
|
* incoming cookies are forwarded there.
|
|
719
771
|
*
|
|
@@ -751,7 +803,8 @@ interface SilentRefreshHandlerConfig {
|
|
|
751
803
|
* match the upstream NestJS auth module's configuration. On
|
|
752
804
|
* failure the handler clears:
|
|
753
805
|
* - `access` at path `/`
|
|
754
|
-
* - `refresh` at `refreshCookiePath` (default `/api/auth
|
|
806
|
+
* - `refresh` at `refreshCookiePath` (default `/api/auth`; see the field's note — it
|
|
807
|
+
* must match the scope the upstream module planted the cookie with)
|
|
755
808
|
* - `hasSession` at path `/`
|
|
756
809
|
*/
|
|
757
810
|
readonly cookieNames: {
|
|
@@ -760,11 +813,28 @@ interface SilentRefreshHandlerConfig {
|
|
|
760
813
|
readonly hasSession: string;
|
|
761
814
|
};
|
|
762
815
|
/**
|
|
763
|
-
* Path attribute for the refresh
|
|
764
|
-
*
|
|
765
|
-
*
|
|
816
|
+
* Path attribute for the refresh-cookie clear. Defaults to `/api/auth`.
|
|
817
|
+
*
|
|
818
|
+
* **This is not the server's default**, which is `/auth`. It is the value the *proxy*
|
|
819
|
+
* topology needs: the browser addresses the Next.js route, so the cookie must be scoped to
|
|
820
|
+
* the Next.js path, which means the upstream module has to be configured with
|
|
821
|
+
* `cookies.refreshCookiePath: '/api/auth'` to plant it there in the first place — the proxy
|
|
822
|
+
* forwards `Set-Cookie` verbatim and never rewrites `Path`. A deployment that leaves the
|
|
823
|
+
* server on `/auth` must set this to `/auth` too: a browser matches a deletion on name,
|
|
824
|
+
* domain and path, so a mismatch means the clear silently does nothing and the refresh
|
|
825
|
+
* cookie outlives the logout (the session itself is revoked server-side either way).
|
|
766
826
|
*/
|
|
767
827
|
readonly refreshCookiePath?: string;
|
|
828
|
+
/**
|
|
829
|
+
* The cookie `Domain` the NestJS server planted the session with, when
|
|
830
|
+
* `cookies.resolveDomains` is configured there. Leave unset for the
|
|
831
|
+
* host-only default.
|
|
832
|
+
*
|
|
833
|
+
* A browser matches a deletion on **name, domain AND path** (RFC 6265 §5.3),
|
|
834
|
+
* so a clear that omits `Domain` cannot remove a cookie that carries one — it
|
|
835
|
+
* plants a new host-only cookie and the originals survive.
|
|
836
|
+
*/
|
|
837
|
+
readonly cookieDomain?: string;
|
|
768
838
|
}
|
|
769
839
|
/**
|
|
770
840
|
* Type of the Next.js App Router GET handler returned by the
|
|
@@ -791,11 +861,13 @@ type SilentRefreshHandler = (request: NextRequest) => Promise<NextResponse>;
|
|
|
791
861
|
* DoS amplification against the upstream service.
|
|
792
862
|
*
|
|
793
863
|
* @remarks
|
|
794
|
-
* HOST-HEADER TRUST —
|
|
795
|
-
* `Host`
|
|
796
|
-
*
|
|
797
|
-
*
|
|
798
|
-
*
|
|
864
|
+
* HOST-HEADER TRUST — the handler emits a RELATIVE `Location`, so a
|
|
865
|
+
* forged `Host` cannot choose where the browser goes: the browser
|
|
866
|
+
* resolves the header against the URL it actually requested. The
|
|
867
|
+
* request origin is still read, but only to REJECT a cross-origin
|
|
868
|
+
* `?redirect=`, which is a comparison rather than a destination —
|
|
869
|
+
* the worst a forged `Host` buys there is admitting a path that then
|
|
870
|
+
* lands on the real app anyway. See `redirectToPath`.
|
|
799
871
|
*
|
|
800
872
|
* @throws {Error} When `loginPath` is not a same-origin pathname.
|
|
801
873
|
*/
|
|
@@ -957,12 +1029,13 @@ declare const CLIENT_REFRESH_ROUTE: "/api/auth/client-refresh";
|
|
|
957
1029
|
* manages its own navigation.
|
|
958
1030
|
*
|
|
959
1031
|
* @remarks
|
|
960
|
-
* HOST-HEADER TRUST — in `'redirect'` mode the destination
|
|
961
|
-
*
|
|
962
|
-
*
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
*
|
|
1032
|
+
* HOST-HEADER TRUST — in `'redirect'` mode the destination is emitted
|
|
1033
|
+
* as a RELATIVE `Location` and the response never reads
|
|
1034
|
+
* `request.nextUrl.origin`, so the browser resolves it against the
|
|
1035
|
+
* URL it actually requested. A forged `Host` has nothing to influence
|
|
1036
|
+
* here, and this handler needs no vetting of that header to be safe.
|
|
1037
|
+
* That is a property of this response only — it says nothing about
|
|
1038
|
+
* the rest of an application, where `Host` may still reach a URL.
|
|
966
1039
|
*
|
|
967
1040
|
* Edge-Runtime-safe.
|
|
968
1041
|
*/
|
|
@@ -978,8 +1051,29 @@ interface LogoutCookieConfig {
|
|
|
978
1051
|
readonly refresh: string;
|
|
979
1052
|
readonly hasSession: string;
|
|
980
1053
|
};
|
|
981
|
-
/**
|
|
1054
|
+
/**
|
|
1055
|
+
* Path attribute for the refresh-cookie clear. Defaults to `/api/auth`.
|
|
1056
|
+
*
|
|
1057
|
+
* **This is not the server's default**, which is `/auth`. It is the value the *proxy*
|
|
1058
|
+
* topology needs: the browser addresses the Next.js route, so the cookie must be scoped to
|
|
1059
|
+
* the Next.js path, which means the upstream module has to be configured with
|
|
1060
|
+
* `cookies.refreshCookiePath: '/api/auth'` to plant it there in the first place — the proxy
|
|
1061
|
+
* forwards `Set-Cookie` verbatim and never rewrites `Path`. A deployment that leaves the
|
|
1062
|
+
* server on `/auth` must set this to `/auth` too: a browser matches a deletion on name,
|
|
1063
|
+
* domain and path, so a mismatch means the clear silently does nothing and the refresh
|
|
1064
|
+
* cookie outlives the logout (the session itself is revoked server-side either way).
|
|
1065
|
+
*/
|
|
982
1066
|
readonly refreshCookiePath?: string;
|
|
1067
|
+
/**
|
|
1068
|
+
* The cookie `Domain` the NestJS server planted the session with, when
|
|
1069
|
+
* `cookies.resolveDomains` is configured there. Leave unset for the
|
|
1070
|
+
* host-only default.
|
|
1071
|
+
*
|
|
1072
|
+
* A browser matches a deletion on **name, domain AND path** (RFC 6265 §5.3),
|
|
1073
|
+
* so a clear that omits `Domain` cannot remove a cookie that carries one — it
|
|
1074
|
+
* plants a new host-only cookie and the originals survive.
|
|
1075
|
+
*/
|
|
1076
|
+
readonly cookieDomain?: string;
|
|
983
1077
|
}
|
|
984
1078
|
/**
|
|
985
1079
|
* Logout response mode = `'redirect'`. `loginPath` is required and
|
package/dist/nextjs/index.d.ts
CHANGED
|
@@ -156,11 +156,11 @@ declare function buildSilentRefreshUrl(request: RequestWithUrl, redirectTo?: str
|
|
|
156
156
|
* its structured attributes so dedup logic (and any future cookie
|
|
157
157
|
* inspection) can work against a typed object instead of raw strings.
|
|
158
158
|
*
|
|
159
|
-
* {@link getSetCookieHeaders}
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
159
|
+
* {@link getSetCookieHeaders} extracts the list of raw Set-Cookie strings
|
|
160
|
+
* from either `Headers.getSetCookie()` or, for a header-like object that
|
|
161
|
+
* does not implement it, `get('set-cookie')` — which concatenates cookies
|
|
162
|
+
* with commas, and is only safe when the values do not themselves contain
|
|
163
|
+
* unquoted commas.
|
|
164
164
|
*
|
|
165
165
|
* Edge-Runtime-safe: pure string manipulation, no Node-only APIs.
|
|
166
166
|
*
|
|
@@ -177,7 +177,7 @@ declare function buildSilentRefreshUrl(request: RequestWithUrl, redirectTo?: str
|
|
|
177
177
|
* *most-recent-writer-wins* invariant users rely on. If that
|
|
178
178
|
* upstream behaviour ever changes, widen the key.
|
|
179
179
|
* - `parseSetCookieHeader` intentionally does NOT trim the cookie
|
|
180
|
-
* *value* — RFC 6265 §5.2
|
|
180
|
+
* *value* — RFC 6265 §5.2, item 2 says the value is everything
|
|
181
181
|
* after the first `=` up to the first `;`. Attribute *values*
|
|
182
182
|
* (e.g., `Path=/x `) are trimmed because the spec explicitly
|
|
183
183
|
* permits surrounding OWS there.
|
|
@@ -240,11 +240,11 @@ interface HeadersLike {
|
|
|
240
240
|
* with attribute semantics (`SameSite`, `Domain`) are lowercased to
|
|
241
241
|
* keep the dedup key stable. The `name` and `value` of the cookie
|
|
242
242
|
* itself are preserved as-is — including any internal whitespace, per
|
|
243
|
-
* RFC 6265 §5.2
|
|
243
|
+
* RFC 6265 §5.2, item 2.
|
|
244
244
|
*
|
|
245
245
|
* Rejects inputs containing CR or LF characters (header smuggling
|
|
246
246
|
* defence): the returned record has an empty `name`, which the dedup
|
|
247
|
-
*
|
|
247
|
+
* filter discards.
|
|
248
248
|
*
|
|
249
249
|
* Malformed input (no `=` in the first segment) similarly returns an
|
|
250
250
|
* empty parsed record.
|
|
@@ -274,13 +274,14 @@ declare function dedupeSetCookieHeaders(cookies: readonly string[]): string[];
|
|
|
274
274
|
* Return the array of raw `Set-Cookie` header strings from a
|
|
275
275
|
* {@link HeadersLike}.
|
|
276
276
|
*
|
|
277
|
-
* Prefers
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
277
|
+
* Prefers `Headers.getSetCookie()`, which correctly separates multiple
|
|
278
|
+
* Set-Cookie headers. Every runtime this library supports has it — the
|
|
279
|
+
* fallback is not for old Node but for the header-like objects consumers
|
|
280
|
+
* legitimately pass instead of a real `Headers`: test doubles, framework
|
|
281
|
+
* wrappers, and hand-rolled shims, all of which `HeadersLike` admits by
|
|
282
|
+
* typing the method optional. For those, the single comma-joined value from
|
|
283
|
+
* `get('set-cookie')` is split with an awareness that commas inside `Expires`
|
|
284
|
+
* values (`Expires=Wed, 09 Jun 2021 10:18:14 GMT`) are NOT separators.
|
|
284
285
|
*
|
|
285
286
|
* @param headers - Any object with `get()` and optionally `getSetCookie()`.
|
|
286
287
|
* @returns An array of raw `Set-Cookie` header strings (possibly empty).
|
|
@@ -324,6 +325,7 @@ declare function getSetCookieHeaders(headers: HeadersLike): string[];
|
|
|
324
325
|
*
|
|
325
326
|
* Pinning the algorithm at verification time eliminates both vectors.
|
|
326
327
|
*/
|
|
328
|
+
|
|
327
329
|
/**
|
|
328
330
|
* Header of a JWT as carried in the first base64url segment.
|
|
329
331
|
*
|
|
@@ -353,14 +355,50 @@ interface DecodedToken {
|
|
|
353
355
|
/**
|
|
354
356
|
* `true` when the payload parses AND `exp` is in the future.
|
|
355
357
|
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
* only when `verifyJwtToken` was invoked with a non-null secret.
|
|
358
|
+
* Expiry ONLY. It says nothing about the signature:
|
|
359
|
+
* {@link decodeJwtToken} sets it without checking one at all. It is
|
|
360
|
+
* one half of the question an authorisation decision is asking —
|
|
361
|
+
* see {@link DecodedToken.signatureVerified} for the other half and
|
|
362
|
+
* for the check that uses both.
|
|
362
363
|
*/
|
|
363
364
|
readonly isValid: boolean;
|
|
365
|
+
/**
|
|
366
|
+
* `true` only when the signature was actually checked against a
|
|
367
|
+
* non-empty secret — i.e. only past {@link verifyJwtToken}'s HMAC
|
|
368
|
+
* check. `false` on every {@link decodeJwtToken} result, including
|
|
369
|
+
* the ones that carry `isValid: true` and a fully populated payload,
|
|
370
|
+
* and on every `verifyJwtToken` failure (which returns an empty
|
|
371
|
+
* token, so `isValid` is `false` there as well — `verifyJwtToken`
|
|
372
|
+
* fails closed on a missing or empty secret rather than falling back
|
|
373
|
+
* to a decode).
|
|
374
|
+
*
|
|
375
|
+
* The two flags are INDEPENDENT, and neither is sufficient alone:
|
|
376
|
+
*
|
|
377
|
+
* - `isValid` without `signatureVerified` is a token that has not
|
|
378
|
+
* expired and that nobody checked the signature of — including one
|
|
379
|
+
* an attacker minted with `alg: none` and an arbitrary `sub`,
|
|
380
|
+
* `role` and `tenantId`. This is the trap the flag exists for: the
|
|
381
|
+
* two branches are otherwise indistinguishable at runtime, and
|
|
382
|
+
* `if (decoded.isValid && decoded.role === 'ADMIN')` is the natural
|
|
383
|
+
* reading of a function called `verifyJwtToken`.
|
|
384
|
+
* - `signatureVerified` without `isValid` is a **genuinely signed
|
|
385
|
+
* token that has expired**, or one carrying no `exp` at all. The
|
|
386
|
+
* signature is checked before the expiry is read, so this
|
|
387
|
+
* combination is reachable on the ordinary path — a session that
|
|
388
|
+
* simply ran out.
|
|
389
|
+
*
|
|
390
|
+
* So an authorisation decision reads BOTH:
|
|
391
|
+
*
|
|
392
|
+
* ```ts
|
|
393
|
+
* const decoded = await verifyJwtToken(token, secret)
|
|
394
|
+
* if (decoded.isValid && decoded.signatureVerified && decoded.role === 'ADMIN') {
|
|
395
|
+
* ```
|
|
396
|
+
*
|
|
397
|
+
* The proxy's own `TokenState.signatureVerified` is this conjunction
|
|
398
|
+
* already (`decoded.signatureVerified && decoded.isValid && …`); a
|
|
399
|
+
* caller using these helpers directly has to form it here.
|
|
400
|
+
*/
|
|
401
|
+
readonly signatureVerified: boolean;
|
|
364
402
|
/** Raw header object, or `undefined` if the header failed to parse. */
|
|
365
403
|
readonly header: JwtHeader | undefined;
|
|
366
404
|
/**
|
|
@@ -410,24 +448,33 @@ declare function decodeJwtToken(token: string): DecodedToken;
|
|
|
410
448
|
* 6. Verify the signature over `<header>.<payload>` (ASCII bytes).
|
|
411
449
|
* 7. Validate `exp` is in the future.
|
|
412
450
|
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
451
|
+
* **A missing secret fails closed.** `undefined`, `null` and the
|
|
452
|
+
* empty string all return `emptyDecoded()` rather than falling back
|
|
453
|
+
* to a decode. This function used to fall back to
|
|
454
|
+
* {@link decodeJwtToken}, and the two branches returned the same
|
|
455
|
+
* shape with the same `isValid: true` — so a caller writing
|
|
456
|
+
* `const d = await verifyJwtToken(t, secret)` and then
|
|
457
|
+
* `if (d.isValid && d.role === 'ADMIN')`, the natural
|
|
458
|
+
* reading of the name, admitted a token an attacker minted with
|
|
459
|
+
* `alg: none` the moment the secret went missing. An unset
|
|
460
|
+
* environment variable was enough to arrange that. A function that
|
|
461
|
+
* cannot verify must refuse, not quietly answer a weaker question;
|
|
462
|
+
* {@link decodeJwtToken} remains the explicit, correctly-named entry
|
|
463
|
+
* point for the non-authoritative read.
|
|
464
|
+
*
|
|
465
|
+
* The empty string is refused for a second reason of its own: an
|
|
466
|
+
* empty HMAC key is technically valid to the Web Crypto API and
|
|
467
|
+
* would verify a token signed with the same empty key — a common
|
|
468
|
+
* misconfiguration (`JWT_SECRET=""` in an environment file) that
|
|
469
|
+
* must not silently downgrade to a no-op.
|
|
425
470
|
*
|
|
426
471
|
* @param token - JWS compact serialisation.
|
|
427
|
-
* @param secret - HS256 shared secret
|
|
428
|
-
*
|
|
472
|
+
* @param secret - HS256 shared secret. A missing or empty value
|
|
473
|
+
* makes every token invalid.
|
|
429
474
|
* @returns A {@link DecodedToken} where `isValid` reflects BOTH the
|
|
430
|
-
* signature verification AND the expiry check
|
|
475
|
+
* signature verification AND the expiry check, and
|
|
476
|
+
* `signatureVerified` is `true` only when a signature was
|
|
477
|
+
* actually checked.
|
|
431
478
|
*/
|
|
432
479
|
declare function verifyJwtToken(token: string, secret?: string | null): Promise<DecodedToken>;
|
|
433
480
|
/**
|
|
@@ -711,9 +758,14 @@ declare function createAuthProxy(config: AuthProxyConfig): AuthProxyInstance;
|
|
|
711
758
|
* - **Rate limiting.** The handler makes one upstream `POST` per
|
|
712
759
|
* inbound `GET`. Apply route-level rate limiting in the consumer
|
|
713
760
|
* app to prevent DoS amplification.
|
|
714
|
-
* - **Host-header trust.**
|
|
715
|
-
* `
|
|
716
|
-
*
|
|
761
|
+
* - **Host-header trust.** No longer an obligation for this
|
|
762
|
+
* handler's redirects: the `Location` it emits is relative, so
|
|
763
|
+
* the browser resolves it against the URL it actually requested
|
|
764
|
+
* and a forged `Host` has no destination to choose. The request
|
|
765
|
+
* origin is still derived from `Host`, but only to REJECT a
|
|
766
|
+
* cross-origin `?redirect=` — a comparison, not a destination,
|
|
767
|
+
* where the worst a forged value buys is admitting a path that
|
|
768
|
+
* lands on the real app anyway.
|
|
717
769
|
* - **`apiBase`.** Always points to your OWN NestJS backend — all
|
|
718
770
|
* incoming cookies are forwarded there.
|
|
719
771
|
*
|
|
@@ -751,7 +803,8 @@ interface SilentRefreshHandlerConfig {
|
|
|
751
803
|
* match the upstream NestJS auth module's configuration. On
|
|
752
804
|
* failure the handler clears:
|
|
753
805
|
* - `access` at path `/`
|
|
754
|
-
* - `refresh` at `refreshCookiePath` (default `/api/auth
|
|
806
|
+
* - `refresh` at `refreshCookiePath` (default `/api/auth`; see the field's note — it
|
|
807
|
+
* must match the scope the upstream module planted the cookie with)
|
|
755
808
|
* - `hasSession` at path `/`
|
|
756
809
|
*/
|
|
757
810
|
readonly cookieNames: {
|
|
@@ -760,11 +813,28 @@ interface SilentRefreshHandlerConfig {
|
|
|
760
813
|
readonly hasSession: string;
|
|
761
814
|
};
|
|
762
815
|
/**
|
|
763
|
-
* Path attribute for the refresh
|
|
764
|
-
*
|
|
765
|
-
*
|
|
816
|
+
* Path attribute for the refresh-cookie clear. Defaults to `/api/auth`.
|
|
817
|
+
*
|
|
818
|
+
* **This is not the server's default**, which is `/auth`. It is the value the *proxy*
|
|
819
|
+
* topology needs: the browser addresses the Next.js route, so the cookie must be scoped to
|
|
820
|
+
* the Next.js path, which means the upstream module has to be configured with
|
|
821
|
+
* `cookies.refreshCookiePath: '/api/auth'` to plant it there in the first place — the proxy
|
|
822
|
+
* forwards `Set-Cookie` verbatim and never rewrites `Path`. A deployment that leaves the
|
|
823
|
+
* server on `/auth` must set this to `/auth` too: a browser matches a deletion on name,
|
|
824
|
+
* domain and path, so a mismatch means the clear silently does nothing and the refresh
|
|
825
|
+
* cookie outlives the logout (the session itself is revoked server-side either way).
|
|
766
826
|
*/
|
|
767
827
|
readonly refreshCookiePath?: string;
|
|
828
|
+
/**
|
|
829
|
+
* The cookie `Domain` the NestJS server planted the session with, when
|
|
830
|
+
* `cookies.resolveDomains` is configured there. Leave unset for the
|
|
831
|
+
* host-only default.
|
|
832
|
+
*
|
|
833
|
+
* A browser matches a deletion on **name, domain AND path** (RFC 6265 §5.3),
|
|
834
|
+
* so a clear that omits `Domain` cannot remove a cookie that carries one — it
|
|
835
|
+
* plants a new host-only cookie and the originals survive.
|
|
836
|
+
*/
|
|
837
|
+
readonly cookieDomain?: string;
|
|
768
838
|
}
|
|
769
839
|
/**
|
|
770
840
|
* Type of the Next.js App Router GET handler returned by the
|
|
@@ -791,11 +861,13 @@ type SilentRefreshHandler = (request: NextRequest) => Promise<NextResponse>;
|
|
|
791
861
|
* DoS amplification against the upstream service.
|
|
792
862
|
*
|
|
793
863
|
* @remarks
|
|
794
|
-
* HOST-HEADER TRUST —
|
|
795
|
-
* `Host`
|
|
796
|
-
*
|
|
797
|
-
*
|
|
798
|
-
*
|
|
864
|
+
* HOST-HEADER TRUST — the handler emits a RELATIVE `Location`, so a
|
|
865
|
+
* forged `Host` cannot choose where the browser goes: the browser
|
|
866
|
+
* resolves the header against the URL it actually requested. The
|
|
867
|
+
* request origin is still read, but only to REJECT a cross-origin
|
|
868
|
+
* `?redirect=`, which is a comparison rather than a destination —
|
|
869
|
+
* the worst a forged `Host` buys there is admitting a path that then
|
|
870
|
+
* lands on the real app anyway. See `redirectToPath`.
|
|
799
871
|
*
|
|
800
872
|
* @throws {Error} When `loginPath` is not a same-origin pathname.
|
|
801
873
|
*/
|
|
@@ -957,12 +1029,13 @@ declare const CLIENT_REFRESH_ROUTE: "/api/auth/client-refresh";
|
|
|
957
1029
|
* manages its own navigation.
|
|
958
1030
|
*
|
|
959
1031
|
* @remarks
|
|
960
|
-
* HOST-HEADER TRUST — in `'redirect'` mode the destination
|
|
961
|
-
*
|
|
962
|
-
*
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
*
|
|
1032
|
+
* HOST-HEADER TRUST — in `'redirect'` mode the destination is emitted
|
|
1033
|
+
* as a RELATIVE `Location` and the response never reads
|
|
1034
|
+
* `request.nextUrl.origin`, so the browser resolves it against the
|
|
1035
|
+
* URL it actually requested. A forged `Host` has nothing to influence
|
|
1036
|
+
* here, and this handler needs no vetting of that header to be safe.
|
|
1037
|
+
* That is a property of this response only — it says nothing about
|
|
1038
|
+
* the rest of an application, where `Host` may still reach a URL.
|
|
966
1039
|
*
|
|
967
1040
|
* Edge-Runtime-safe.
|
|
968
1041
|
*/
|
|
@@ -978,8 +1051,29 @@ interface LogoutCookieConfig {
|
|
|
978
1051
|
readonly refresh: string;
|
|
979
1052
|
readonly hasSession: string;
|
|
980
1053
|
};
|
|
981
|
-
/**
|
|
1054
|
+
/**
|
|
1055
|
+
* Path attribute for the refresh-cookie clear. Defaults to `/api/auth`.
|
|
1056
|
+
*
|
|
1057
|
+
* **This is not the server's default**, which is `/auth`. It is the value the *proxy*
|
|
1058
|
+
* topology needs: the browser addresses the Next.js route, so the cookie must be scoped to
|
|
1059
|
+
* the Next.js path, which means the upstream module has to be configured with
|
|
1060
|
+
* `cookies.refreshCookiePath: '/api/auth'` to plant it there in the first place — the proxy
|
|
1061
|
+
* forwards `Set-Cookie` verbatim and never rewrites `Path`. A deployment that leaves the
|
|
1062
|
+
* server on `/auth` must set this to `/auth` too: a browser matches a deletion on name,
|
|
1063
|
+
* domain and path, so a mismatch means the clear silently does nothing and the refresh
|
|
1064
|
+
* cookie outlives the logout (the session itself is revoked server-side either way).
|
|
1065
|
+
*/
|
|
982
1066
|
readonly refreshCookiePath?: string;
|
|
1067
|
+
/**
|
|
1068
|
+
* The cookie `Domain` the NestJS server planted the session with, when
|
|
1069
|
+
* `cookies.resolveDomains` is configured there. Leave unset for the
|
|
1070
|
+
* host-only default.
|
|
1071
|
+
*
|
|
1072
|
+
* A browser matches a deletion on **name, domain AND path** (RFC 6265 §5.3),
|
|
1073
|
+
* so a clear that omits `Domain` cannot remove a cookie that carries one — it
|
|
1074
|
+
* plants a new host-only cookie and the originals survive.
|
|
1075
|
+
*/
|
|
1076
|
+
readonly cookieDomain?: string;
|
|
983
1077
|
}
|
|
984
1078
|
/**
|
|
985
1079
|
* Logout response mode = `'redirect'`. `loginPath` is required and
|