@lenne.tech/nest-server 11.46.6 → 11.46.7
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/.claude/rules/package-management.md +1 -1
- package/.claude/rules/testing.md +4 -4
- package/FRAMEWORK-API.md +1 -1
- package/dist/core/modules/better-auth/better-auth-cookie-prefix.helper.d.ts +2 -0
- package/dist/core/modules/better-auth/better-auth-cookie-prefix.helper.js +7 -0
- package/dist/core/modules/better-auth/better-auth-cookie-prefix.helper.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.middleware.js +4 -2
- package/dist/core/modules/better-auth/core-better-auth.middleware.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/migration-guides/11.46.6-to-11.46.7.md +70 -0
- package/package.json +1 -1
- package/src/core/modules/better-auth/better-auth-cookie-prefix.helper.ts +17 -0
- package/src/core/modules/better-auth/core-better-auth.middleware.ts +9 -4
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Migration Guide: 11.46.6 → 11.46.7
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
| Category | Details |
|
|
6
|
+
|---|---|
|
|
7
|
+
| **Breaking Changes** | None |
|
|
8
|
+
| **New Features** | None. `resolveJwtCookieName()` and `DEFAULT_JWT_COOKIE_NAME` name the JWT cookie |
|
|
9
|
+
| **Bugfixes** | With `COOKIE_PREFIX`, the API reads a JWT-mode frontend's JWT cookie under `<prefix>-jwt-token`, the name `@lenne.tech/nuxt-extensions` writes (§1) |
|
|
10
|
+
| **Security** | None |
|
|
11
|
+
| **Migration Effort** | Package update. Only projects that set `COOKIE_PREFIX` and run in JWT mode (`cookies: false`) notice a difference |
|
|
12
|
+
|
|
13
|
+
## Quick Migration
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @lenne.tech/nest-server@11.46.7
|
|
17
|
+
pnpm run update # nest-server-starter based projects
|
|
18
|
+
pnpm run build
|
|
19
|
+
pnpm test
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Vendor mode**: run `/lt-dev:backend:update-nest-server-core` as usual.
|
|
23
|
+
|
|
24
|
+
## 1. Bugfix: the JWT cookie follows `COOKIE_PREFIX`
|
|
25
|
+
|
|
26
|
+
**No action needed.** Keep `COOKIE_PREFIX` (API) and `NUXT_PUBLIC_COOKIE_PREFIX` (app) equal, as before.
|
|
27
|
+
|
|
28
|
+
In JWT mode the frontend keeps the Better-Auth JWT in a cookie as well as sending it as a bearer token. A request that
|
|
29
|
+
carries the cookie but no `Authorization` header is authenticated by `CoreBetterAuthMiddleware` from that cookie: a
|
|
30
|
+
server-side rendered page that forwards the browser's cookies, and a passkey request from the app.
|
|
31
|
+
|
|
32
|
+
`@lenne.tech/nuxt-extensions` names that cookie `<NUXT_PUBLIC_COOKIE_PREFIX>-jwt-token` when a prefix is set. Up to
|
|
33
|
+
11.46.6 the API read `lt-jwt-token` only. With a prefix such a request was anonymous, and Better-Auth's plugin routes
|
|
34
|
+
(passkey registration, two-factor) answered `401`.
|
|
35
|
+
|
|
36
|
+
| `COOKIE_PREFIX` | JWT cookie read since 11.46.7 | Before |
|
|
37
|
+
|---|---|---|
|
|
38
|
+
| not set | `lt-jwt-token` | `lt-jwt-token` |
|
|
39
|
+
| `acme` | `acme-jwt-token` | `lt-jwt-token` |
|
|
40
|
+
|
|
41
|
+
The prefix is sanitised by the same rule as on the frontend (`[A-Za-z0-9._-]`). The base path does not enter the name,
|
|
42
|
+
unlike the session cookie (`<prefix>.session_token`, else `iam.session_token`). There is no fallback to `lt-jwt-token`
|
|
43
|
+
while `COOKIE_PREFIX` is set: the prefix keeps the apps on one host apart, and the session cookie does not fall back
|
|
44
|
+
either.
|
|
45
|
+
|
|
46
|
+
## 2. Dependencies
|
|
47
|
+
|
|
48
|
+
A pnpm override lifts `i18next` to 26.4.4 under `@compodoc/compodoc` (GHSA-6jcc-5g8w-32mx, GHSA-5jr9-983r-c8x4). It is dev tooling of this repository (`pnpm run docs`) and does not reach consumers; nothing to do in a project.
|
|
49
|
+
|
|
50
|
+
## Compatibility Notes
|
|
51
|
+
|
|
52
|
+
| Pattern | Status |
|
|
53
|
+
|---|---|
|
|
54
|
+
| No `COOKIE_PREFIX` | Unchanged |
|
|
55
|
+
| `COOKIE_PREFIX` with cookies (the default `cookies: true`) | Unchanged; the session cookie already followed the prefix |
|
|
56
|
+
| `COOKIE_PREFIX` with `cookies: false` and a matching `NUXT_PUBLIC_COOKIE_PREFIX` | Fixed: the JWT cookie is read |
|
|
57
|
+
| A frontend that sets `COOKIE_PREFIX` on the API but writes `lt-jwt-token` | No longer authenticated through the cookie. Set the same prefix on both sides |
|
|
58
|
+
|
|
59
|
+
## Troubleshooting
|
|
60
|
+
|
|
61
|
+
| Symptom | Cause | Fix |
|
|
62
|
+
|---|---|---|
|
|
63
|
+
| Passkey or 2FA routes answer `401` in JWT mode with a cookie prefix | 11.46.6 or older read only `lt-jwt-token` | Update to 11.46.7 |
|
|
64
|
+
| Still `401` after the update | The API and the app use different prefixes | Set `COOKIE_PREFIX` and `NUXT_PUBLIC_COOKIE_PREFIX` to the same value |
|
|
65
|
+
|
|
66
|
+
## Module Documentation
|
|
67
|
+
|
|
68
|
+
- `src/core/modules/better-auth/better-auth-cookie-prefix.helper.ts`: `resolveJwtCookieName()`, `resolveBetterAuthCookiePrefix()`
|
|
69
|
+
- Same fix on the current line: 12.11.2
|
|
70
|
+
- `migration-guides/11.26.1-to-11.26.2.md`, §1: `COOKIE_PREFIX` and the frontend pairing
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lenne.tech/nest-server",
|
|
3
|
-
"version": "11.46.
|
|
3
|
+
"version": "11.46.7",
|
|
4
4
|
"description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"node",
|
|
@@ -58,6 +58,23 @@ export function resolveBetterAuthSessionCookieName(basePath: string, env: NodeJS
|
|
|
58
58
|
return `${resolveBetterAuthCookiePrefix(basePath, env)}.session_token`;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
/** The name of the JWT cookie without `COOKIE_PREFIX`, the one `@lenne.tech/nuxt-extensions` writes by default. */
|
|
62
|
+
export const DEFAULT_JWT_COOKIE_NAME = 'lt-jwt-token';
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The cookie a JWT-mode frontend keeps the Better-Auth JWT in (11.46.7): `<COOKIE_PREFIX>-jwt-token` when `COOKIE_PREFIX`
|
|
66
|
+
* is set, otherwise `lt-jwt-token`. `@lenne.tech/nuxt-extensions` names it the same way from `NUXT_PUBLIC_COOKIE_PREFIX`,
|
|
67
|
+
* sanitised by the same rule. Unlike the session cookie, the basePath never enters the name.
|
|
68
|
+
*
|
|
69
|
+
* There is no fallback to `lt-jwt-token` while `COOKIE_PREFIX` is set: the prefix isolates the apps on one host, and
|
|
70
|
+
* the session cookie does not fall back either. Before 11.46.7 the API read `lt-jwt-token` only, so with a prefix
|
|
71
|
+
* every request without an `Authorization` header (SSR with forwarded cookies, a passkey request) was anonymous.
|
|
72
|
+
*/
|
|
73
|
+
export function resolveJwtCookieName(env: NodeJS.ProcessEnv = process.env): string {
|
|
74
|
+
const explicit = sanitizeCookiePrefix(typeof env.COOKIE_PREFIX === 'string' ? env.COOKIE_PREFIX : '');
|
|
75
|
+
return explicit ? `${explicit}-jwt-token` : DEFAULT_JWT_COOKIE_NAME;
|
|
76
|
+
}
|
|
77
|
+
|
|
61
78
|
/**
|
|
62
79
|
* Detect a Better-Auth cookie-prefix drift between the value the NestJS layer
|
|
63
80
|
* resolves (via {@link resolveBetterAuthCookiePrefix}) and a programmatic
|
|
@@ -2,6 +2,7 @@ import { Injectable, Logger, NestMiddleware } from '@nestjs/common';
|
|
|
2
2
|
import { NextFunction, Request, Response } from 'express';
|
|
3
3
|
|
|
4
4
|
import { hasApiTokenCredential } from '../api-token/core-api-token.helpers';
|
|
5
|
+
import { resolveJwtCookieName } from './better-auth-cookie-prefix.helper';
|
|
5
6
|
import { isLegacyJwt } from './core-better-auth-token.helper';
|
|
6
7
|
import { BetterAuthSessionUser, CoreBetterAuthUserMapper, MappedUser } from './core-better-auth-user.mapper';
|
|
7
8
|
import { convertExpressHeaders, extractSessionToken } from './core-better-auth-web.helper';
|
|
@@ -142,14 +143,18 @@ export class CoreBetterAuthMiddleware implements NestMiddleware {
|
|
|
142
143
|
|
|
143
144
|
// Strategy 2: Try JWT from cookie (for JWT mode when frontend stores JWT as cookie)
|
|
144
145
|
// In JWT mode (cookies=false), the frontend may store the JWT in a cookie
|
|
145
|
-
// (
|
|
146
|
+
// (`lt-jwt-token`, `<COOKIE_PREFIX>-jwt-token` with a prefix) instead of sending it as an Authorization header.
|
|
146
147
|
// This bridges the gap between cookie-based frontend storage and JWT verification.
|
|
147
148
|
if (!req.user && !req.headers.authorization && this.betterAuthService.isJwtEnabled()) {
|
|
148
149
|
try {
|
|
149
|
-
// Try parsed cookies first, then raw header (cookie-parser may not have run yet)
|
|
150
|
-
|
|
150
|
+
// Try parsed cookies first, then raw header (cookie-parser may not have run yet). The name follows
|
|
151
|
+
// COOKIE_PREFIX like the frontend's (`<prefix>-jwt-token`, else `lt-jwt-token`; 11.46.7).
|
|
152
|
+
const jwtCookieName = resolveJwtCookieName();
|
|
153
|
+
let jwtFromCookie = req.cookies?.[jwtCookieName];
|
|
151
154
|
if (!jwtFromCookie && req.headers.cookie) {
|
|
152
|
-
const match = req.headers.cookie.match(
|
|
155
|
+
const match = req.headers.cookie.match(
|
|
156
|
+
new RegExp(`(?:^|;\\s*)${jwtCookieName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}=([^;]+)`),
|
|
157
|
+
);
|
|
153
158
|
if (match) {
|
|
154
159
|
jwtFromCookie = decodeURIComponent(match[1]);
|
|
155
160
|
}
|