@lenne.tech/nest-server 11.27.7 → 11.28.1
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/role-system.md +90 -0
- package/.claude/rules/versioning.md +17 -2
- package/FRAMEWORK-API.md +27 -1
- package/dist/core/common/decorators/restricted.decorator.js +11 -7
- package/dist/core/common/decorators/restricted.decorator.js.map +1 -1
- package/dist/core/common/exceptions/access-denied.exception.d.ts +4 -0
- package/dist/core/common/exceptions/access-denied.exception.js +12 -0
- package/dist/core/common/exceptions/access-denied.exception.js.map +1 -0
- package/dist/core/common/helpers/input.helper.js +4 -2
- package/dist/core/common/helpers/input.helper.js.map +1 -1
- package/dist/core/common/helpers/service.helper.js +4 -3
- package/dist/core/common/helpers/service.helper.js.map +1 -1
- package/dist/core/modules/auth/guards/roles.guard.js +2 -2
- package/dist/core/modules/auth/guards/roles.guard.js.map +1 -1
- package/dist/core/modules/better-auth/better-auth-roles.guard.js +1 -1
- package/dist/core/modules/better-auth/better-auth-roles.guard.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-email-verification.service.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.controller.js +1 -3
- package/dist/core/modules/better-auth/core-better-auth.controller.js.map +1 -1
- package/dist/core/modules/tenant/core-tenant-member.model.js +3 -3
- package/dist/core/modules/tenant/core-tenant-member.model.js.map +1 -1
- package/dist/core/modules/tenant/core-tenant.guard.js +7 -6
- package/dist/core/modules/tenant/core-tenant.guard.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/server/modules/user/user.service.js +1 -1
- package/dist/server/modules/user/user.service.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/docs/REQUEST-LIFECYCLE.md +39 -0
- package/migration-guides/11.27.7-to-11.28.0.md +350 -0
- package/migration-guides/11.28.0-to-11.28.1.md +155 -0
- package/package.json +24 -20
- package/src/core/common/decorators/restricted.decorator.ts +34 -12
- package/src/core/common/exceptions/access-denied.exception.ts +49 -0
- package/src/core/common/helpers/input.helper.ts +9 -4
- package/src/core/common/helpers/service.helper.ts +6 -4
- package/src/core/modules/auth/guards/roles.guard.ts +5 -4
- package/src/core/modules/better-auth/better-auth-roles.guard.ts +4 -2
- package/src/core/modules/better-auth/core-better-auth-email-verification.service.ts +1 -1
- package/src/core/modules/better-auth/core-better-auth.controller.ts +5 -6
- package/src/core/modules/tenant/core-tenant-member.model.ts +8 -3
- package/src/core/modules/tenant/core-tenant.guard.ts +26 -11
- package/src/index.ts +1 -0
- package/src/server/modules/user/user.service.ts +3 -2
|
@@ -949,6 +949,41 @@ The `process()` method in `ModuleService` is the **primary** way to handle CRUD
|
|
|
949
949
|
+---------------------------------------------------------------+
|
|
950
950
|
```
|
|
951
951
|
|
|
952
|
+
### Status Codes: 401 vs 403 (v11.28.0+)
|
|
953
|
+
|
|
954
|
+
One policy across **all five** permission layers — the role guards, the tenant guard, `check()` /
|
|
955
|
+
`checkRights`, `checkRestricted()` (object and field level), and a model's `securityCheck()`:
|
|
956
|
+
|
|
957
|
+
| Situation | Status | Thrown by |
|
|
958
|
+
|-----------|--------|-----------|
|
|
959
|
+
| Requester is **not authenticated** | **401** `ErrorCode.UNAUTHORIZED` | guards, `accessDeniedException(undefined)` |
|
|
960
|
+
| Requester **is authenticated** but lacks a right | **403** `ErrorCode.ACCESS_DENIED` | guards, `accessDeniedException(user)` |
|
|
961
|
+
| Resource is locked via `S_NO_ONE` | **403**, always — even for anonymous requesters | guards, `check()` |
|
|
962
|
+
|
|
963
|
+
`S_NO_ONE` is 403 for everyone because authenticating can never unlock it; a 401 would tell the
|
|
964
|
+
client to retry after logging in, which is a lie.
|
|
965
|
+
|
|
966
|
+
**Why this matters:** SPA auth layers commonly treat 401 as "session expired" and clear the session
|
|
967
|
+
(the `@lenne.tech/nuxt-extensions` auth interceptor patches `$fetch`/`fetch` globally and does exactly
|
|
968
|
+
this). A permission error answered with 401 therefore logs the user out of the whole app. With this
|
|
969
|
+
policy a frontend may treat 401 as "session invalid" — with one exception:
|
|
970
|
+
`ErrorCode.EMAIL_VERIFICATION_REQUIRED` is a legitimate 401 (no session exists yet at sign-in) that
|
|
971
|
+
must **not** trigger a logout. Branch on the ErrorCode, not on the status alone.
|
|
972
|
+
|
|
973
|
+
**Writing new denial code:** use the exported factory rather than hand-rolling the decision. It
|
|
974
|
+
returns the **native** `ForbiddenException` / `UnauthorizedException`, so `instanceof` checks and
|
|
975
|
+
`@Catch(...)` filters in consuming projects keep working:
|
|
976
|
+
|
|
977
|
+
```typescript
|
|
978
|
+
import { accessDeniedException } from '@lenne.tech/nest-server';
|
|
979
|
+
|
|
980
|
+
// In a service, a custom guard, or a model's securityCheck():
|
|
981
|
+
throw accessDeniedException(currentUser);
|
|
982
|
+
```
|
|
983
|
+
|
|
984
|
+
See `src/core/common/exceptions/access-denied.exception.ts` and
|
|
985
|
+
`migration-guides/11.27.7-to-11.28.0.md`.
|
|
986
|
+
|
|
952
987
|
### Depth-Based Optimization (v11.23.0+)
|
|
953
988
|
|
|
954
989
|
When `process()` is called from within another `process()` call (service cascades like A.create → B.create → C.create), steps 4–6 are **conditionally skipped** on inner calls to avoid redundant work:
|
|
@@ -1095,6 +1130,10 @@ Controls who can access a resolver/controller method. Evaluated by the RolesGuar
|
|
|
1095
1130
|
|
|
1096
1131
|
Controls who can see or modify specific properties. Evaluated by `CheckResponseInterceptor` (output) and `checkRights()` (input).
|
|
1097
1132
|
|
|
1133
|
+
On **output** a denied field is silently removed (no exception). On **input** the request is
|
|
1134
|
+
rejected: **403** for an authenticated requester, **401** for an anonymous one, and **403 always**
|
|
1135
|
+
for `S_NO_ONE` — see [Status Codes: 401 vs 403](#status-codes-401-vs-403-v11280) above.
|
|
1136
|
+
|
|
1098
1137
|
```typescript
|
|
1099
1138
|
export class User extends CorePersistenceModel {
|
|
1100
1139
|
// Only admins or the user themselves can see the email
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
# Migration Guide: 11.27.7 → 11.28.0
|
|
2
|
+
|
|
3
|
+
> Coming from 11.27.6 or earlier? Read [11.27.6 → 11.27.7](./11.27.6-to-11.27.7.md) first — it is a
|
|
4
|
+
> separate startup-crash + cookie-security bugfix release with no overlap with the changes here.
|
|
5
|
+
|
|
6
|
+
## Overview
|
|
7
|
+
|
|
8
|
+
| Category | Details |
|
|
9
|
+
|----------|---------|
|
|
10
|
+
| **Breaking Changes** | **(1)** Permission errors for **authenticated** users now return **403 Forbidden** instead of 401 Unauthorized. **(2)** `@Roles(S_NO_ONE)` / `@Restricted(S_NO_ONE)` now return **403 for every requester**, including unauthenticated ones (was 401). **(3)** The **error messages** of these denials changed from raw English strings to the translatable `ErrorCode.ACCESS_DENIED` / `ErrorCode.UNAUTHORIZED`. **(4)** `CoreTenantGuard` now returns **401** (was 403) when a request has no authenticated user. **(5)** `@Restricted(S_SELF)` / `@Restricted(S_CREATOR)` on **input** fields now actually enforce ownership (they were silently inert before) — a field that looked owner-restricted may become writable, so **audit them before upgrading**. |
|
|
11
|
+
| **New Features** | `accessDeniedException(user, message?)` — an exported factory that derives 401 or 403 from the requester's auth state. Use it in your own services, models and guards instead of hand-rolling the decision. |
|
|
12
|
+
| **Bugfixes** | **(a)** Status-code semantics per RFC 9110 across **all five** permission layers (role guards, tenant guard, `check()`, `checkRestricted()`, model `securityCheck()`), which previously contradicted each other. **(b)** `checkRestricted()` decided `S_SELF`/`S_CREATOR` ownership from the request **payload** instead of the persisted object — an authenticated attacker could unlock an owner-restricted input field on someone else's record. Now aligned with `check()`, which always read the persisted object. |
|
|
13
|
+
| **Migration Effort** | ~5–20 minutes. `pnpm update`, then **(a)** update any assertion or handler that matches on **status 401** for an authenticated permission error or on the **exact wording** of a permission-error message, and **(b)** audit every `@Restricted(S_SELF)` / `@Restricted(S_CREATOR)` on an **input** type (Breaking Change 5). |
|
|
14
|
+
|
|
15
|
+
### Why
|
|
16
|
+
|
|
17
|
+
- **RFC 9110:** 401 means "authenticate yourself", 403 means "you are authenticated, but not
|
|
18
|
+
allowed". A permission error answered with 401 tells clients to re-authenticate, which cannot fix
|
|
19
|
+
the problem.
|
|
20
|
+
- **Frontend auto-logout:** SPA auth layers commonly treat 401 as "session expired" and clear the
|
|
21
|
+
session — the `@lenne.tech/nuxt-extensions` auth interceptor patches `$fetch`/`fetch` globally and
|
|
22
|
+
does exactly this. With the old behavior, an authenticated user who merely lacked a right was
|
|
23
|
+
logged out of the whole app.
|
|
24
|
+
- **Debuggability:** 401 logs no longer mix real authentication failures with permission failures.
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Quick Migration
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm add @lenne.tech/nest-server@11.28.0
|
|
32
|
+
pnpm run build && pnpm test
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Then work through the five breaking changes below. If your project never asserts 401 on an
|
|
36
|
+
authenticated request, never matches on permission-error message text, and uses no
|
|
37
|
+
`@Restricted(S_SELF)` / `@Restricted(S_CREATOR)` on an input type, there is nothing to do — but
|
|
38
|
+
**verify that last point** (Breaking Change 5), it is the one with security impact.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Breaking Change 1: 403 instead of 401 for authenticated permission errors
|
|
43
|
+
|
|
44
|
+
| Path | Before | After (authenticated) | After (unauthenticated) |
|
|
45
|
+
|------|--------|-----------------------|-------------------------|
|
|
46
|
+
| `check()` / `checkRights` (`input.helper.ts`) | 401 | **403** | 401 |
|
|
47
|
+
| `checkRestricted()`, object level (`restricted.decorator.ts`) | 401 | **403** | 401 |
|
|
48
|
+
| `checkRestricted()`, field level (`restricted.decorator.ts`) | 401 | **403** | 401 |
|
|
49
|
+
| Roles processing in `prepareInput` (`service.helper.ts`) | 401 | **403** | 401 |
|
|
50
|
+
| `CoreTenantMemberModel.securityCheck()` (`core-tenant-member.model.ts`) | 401 | **403** | 401 |
|
|
51
|
+
|
|
52
|
+
The decision key is `user.id`: a requester whose user object carries an id is authenticated and gets
|
|
53
|
+
403; everyone else gets 401. A **falsy but present** id (`0`, `''`) counts as authenticated — relevant
|
|
54
|
+
if your project uses numeric user ids.
|
|
55
|
+
|
|
56
|
+
**Before:**
|
|
57
|
+
```typescript
|
|
58
|
+
// e2e: an authenticated customer patches an admin-only field
|
|
59
|
+
await testHelper.rest(`/users/${customer.id}`, {
|
|
60
|
+
cookies: customer.token,
|
|
61
|
+
method: 'PATCH',
|
|
62
|
+
payload: { customerNumber: 'B-HACKED' },
|
|
63
|
+
statusCode: 401, // ← old: permission error surfaced as 401
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**After:**
|
|
68
|
+
```typescript
|
|
69
|
+
await testHelper.rest(`/users/${customer.id}`, {
|
|
70
|
+
cookies: customer.token,
|
|
71
|
+
method: 'PATCH',
|
|
72
|
+
payload: { customerNumber: 'B-HACKED' },
|
|
73
|
+
statusCode: 403, // ← new: authenticated, but lacking rights
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Breaking Change 2: `S_NO_ONE` is now 403 for everyone
|
|
78
|
+
|
|
79
|
+
`S_NO_ONE` marks a resource as locked for **all** requesters, administrators included.
|
|
80
|
+
Authenticating can therefore never grant access, which makes 401 ("authenticate and retry") a lie
|
|
81
|
+
even for an anonymous requester. All four paths now agree on **403**:
|
|
82
|
+
|
|
83
|
+
| Path | Before | After |
|
|
84
|
+
|------|--------|-------|
|
|
85
|
+
| `RolesGuard` (`@Roles(S_NO_ONE)`) | 401 always | **403 always** |
|
|
86
|
+
| `BetterAuthRolesGuard` (`@Roles(S_NO_ONE)`) | 401 always | **403 always** |
|
|
87
|
+
| `CoreTenantGuard` (`@Roles(S_NO_ONE)`) | 403 always | 403 always (unchanged) |
|
|
88
|
+
| `check()` (`@Restricted(S_NO_ONE)`, e.g. on `password`) | 401 always | **403 always** |
|
|
89
|
+
|
|
90
|
+
Update any test asserting `statusCode: 401` on a locked endpoint or a `S_NO_ONE`-restricted field.
|
|
91
|
+
|
|
92
|
+
## Breaking Change 3: error messages are now translatable `ErrorCode`s
|
|
93
|
+
|
|
94
|
+
The paths above previously returned raw English strings (`'Missing rights'`, `'No access'`,
|
|
95
|
+
`'The current user has no access rights for <field> of <Class>'`,
|
|
96
|
+
`'Current user not allowed setting roles: <roles>'`). They now return the same `ErrorCode`s the role
|
|
97
|
+
guards have always thrown:
|
|
98
|
+
|
|
99
|
+
| Requester | Message |
|
|
100
|
+
|-----------|---------|
|
|
101
|
+
| authenticated | `ErrorCode.ACCESS_DENIED` → `#LTNS_0101: Access denied - Insufficient permissions` |
|
|
102
|
+
| unauthenticated | `ErrorCode.UNAUTHORIZED` → `#LTNS_0100: Unauthorized - User is not logged in` |
|
|
103
|
+
|
|
104
|
+
Two reasons:
|
|
105
|
+
|
|
106
|
+
1. **They are translatable now.** The frontend error-translation layer keys off the `#LTNS_xxxx:`
|
|
107
|
+
marker. The old raw strings carried no marker, so users saw untranslated English — while the
|
|
108
|
+
*same* 403 coming from a role guard was translated. The framework no longer emits two
|
|
109
|
+
incompatible 403 formats.
|
|
110
|
+
2. **They no longer leak internals.** The old field-level message embedded the Input/Model class name
|
|
111
|
+
and the property name in the HTTP response. That detail is still available — it goes to the debug
|
|
112
|
+
log (`config.debug`) instead of to the client.
|
|
113
|
+
|
|
114
|
+
**Migration:** replace message assertions with `ErrorCode` comparisons.
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
// Before
|
|
118
|
+
expect(res.errors[0].message).toEqual('The current user has no access rights for roles of UserInput');
|
|
119
|
+
|
|
120
|
+
// After
|
|
121
|
+
import { ErrorCode } from '@lenne.tech/nest-server';
|
|
122
|
+
expect(res.errors[0].message).toEqual(ErrorCode.ACCESS_DENIED);
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
If you relied on the class/field detail while debugging, enable `config.debug` and read the server log.
|
|
126
|
+
|
|
127
|
+
## Breaking Change 4: `CoreTenantGuard` returns 401 when unauthenticated
|
|
128
|
+
|
|
129
|
+
`CoreTenantGuard` previously threw `ForbiddenException('Authentication required')` — a **403** for a
|
|
130
|
+
request with **no user at all**. That inverts the policy above: re-authenticating *is* the remedy
|
|
131
|
+
here, so the client must be told. All five sites now throw **401** (`ErrorCode.UNAUTHORIZED`).
|
|
132
|
+
|
|
133
|
+
This only surfaces when `CoreTenantGuard` runs without a role guard ahead of it; in the standard
|
|
134
|
+
chain `RolesGuard` / `BetterAuthRolesGuard` already answered `!user` with 401.
|
|
135
|
+
|
|
136
|
+
## Breaking Change 5: `S_SELF` / `S_CREATOR` on input fields now actually enforce ownership
|
|
137
|
+
|
|
138
|
+
This is the one that needs a real audit before you upgrade.
|
|
139
|
+
|
|
140
|
+
`checkRestricted()` used to decide `S_SELF` / `S_CREATOR` from the **request payload** (`data`) rather
|
|
141
|
+
than from the persisted object. On the input path `data` is the caller-supplied DTO, so an
|
|
142
|
+
authenticated attacker could unlock an owner-restricted field on **someone else's** record just by
|
|
143
|
+
asserting ownership in the body — the service applies the input to the target it was called with, not
|
|
144
|
+
to the ids in the payload:
|
|
145
|
+
|
|
146
|
+
```
|
|
147
|
+
PATCH /users/<victim> { "id": "<attacker>", "createdBy": "<attacker>", "<restricted field>": ... }
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Because `MapAndValidatePipe` strips `id`/`createdBy` from payloads (they are not `@UnifiedField`s),
|
|
151
|
+
the branch usually could not fire, and such fields were **effectively admin-only-or-denied**. The fix
|
|
152
|
+
reads ownership from `serviceOptions.dbObject` (as `check()` always did) — so `S_SELF`/`S_CREATOR` on
|
|
153
|
+
an input field **start working**. A field that *looked* owner-restricted may suddenly become writable.
|
|
154
|
+
|
|
155
|
+
### The audit
|
|
156
|
+
|
|
157
|
+
Search for ownership roles on input types:
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
grep -rn "S_SELF\|S_CREATOR" src/server/**/inputs/
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
For each hit, decide what the field really means:
|
|
164
|
+
|
|
165
|
+
| You want… | Use |
|
|
166
|
+
|-----------|-----|
|
|
167
|
+
| "the user may edit their own record's field" | `S_SELF` — correct, and now enforced |
|
|
168
|
+
| "only an admin" (the common real case for `email`, `status`, `roles`) | `RoleEnum.ADMIN` — remove `S_CREATOR` |
|
|
169
|
+
| "the field is open to any logged-in user" | `S_USER` |
|
|
170
|
+
|
|
171
|
+
**Two traps:**
|
|
172
|
+
|
|
173
|
+
1. **`S_CREATOR` is not "the user themselves".** `createdBy` is stamped by the audit plugin onto
|
|
174
|
+
whoever **created the record**. On a self-signup that is the user; in an **invite / admin-provisioning
|
|
175
|
+
flow it is the inviting admin, permanently**. `@Restricted(S_CREATOR)` on a user input therefore
|
|
176
|
+
grants the *inviter* write access to the invited user's fields — rarely the intent, and dangerous
|
|
177
|
+
on `email` (→ password reset).
|
|
178
|
+
2. **A broader class-level role hides the field-level one.** `@Restricted(S_USER)` on the class
|
|
179
|
+
OR-merges with a field-level `S_SELF` (`mergeRoles` defaults to true), so `S_USER` alone already
|
|
180
|
+
grants the field — it was never owner-gated, and this change does not alter it. Such fields are
|
|
181
|
+
safe but misleading; consider dropping the redundant `S_SELF`.
|
|
182
|
+
|
|
183
|
+
### Not affected
|
|
184
|
+
|
|
185
|
+
- `check()` (`input.helper.ts`) already read from `dbObject` — no behavior change there.
|
|
186
|
+
- **Output** filtering is unchanged: the object being checked *is* the persisted record.
|
|
187
|
+
- `create()` has no `dbObject`, so ownership cannot be established there — `S_SELF`/`S_CREATOR` deny,
|
|
188
|
+
exactly as `check()` already did. (A normal create DTO carries neither `id` nor `createdBy`, so this
|
|
189
|
+
matches the old behavior for honest callers.)
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## What is NOT affected
|
|
194
|
+
|
|
195
|
+
- **All "requires auth" cases** (no token/session) — still 401. Only the `S_NO_ONE` case changed
|
|
196
|
+
(Breaking Change 2), because there authentication can never help.
|
|
197
|
+
- **Token errors** (`Invalid token`, `Token expired`, refresh-token flows) — still 401.
|
|
198
|
+
- **Sign-in with wrong credentials** — still 401.
|
|
199
|
+
- **Email verification at sign-in** (`ErrorCode.EMAIL_VERIFICATION_REQUIRED`) — still **401**, and
|
|
200
|
+
correctly so: the requester has no session yet, so they are not authenticated. Note for frontends:
|
|
201
|
+
this is a 401 that must **not** trigger the auto-logout flow — branch on the ErrorCode, not on the
|
|
202
|
+
status alone.
|
|
203
|
+
- **`instanceof` checks and `@Catch(...)` filters** — see below. This is deliberate.
|
|
204
|
+
|
|
205
|
+
## `instanceof` and exception filters keep working
|
|
206
|
+
|
|
207
|
+
The framework throws the **native** `ForbiddenException` / `UnauthorizedException`, never a custom
|
|
208
|
+
subclass. Existing filters keep firing, and the REST error body (including the `name` field that
|
|
209
|
+
`HttpExceptionLogFilter` emits) is unchanged:
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
@Catch(ForbiddenException) // fires for authenticated permission errors
|
|
213
|
+
@Catch(UnauthorizedException) // fires for unauthenticated ones
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
A `@Catch(UnauthorizedException)` filter that used to catch an authenticated permission error will no
|
|
217
|
+
longer see it — it is a `ForbiddenException` now. That is Breaking Change 1, and the fix is to handle
|
|
218
|
+
`ForbiddenException` as well. No filter silently stops matching because of a changed exception class.
|
|
219
|
+
|
|
220
|
+
### Use the factory in your own code
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
import { accessDeniedException } from '@lenne.tech/nest-server';
|
|
224
|
+
|
|
225
|
+
// In a service, a model's securityCheck(), or a custom guard:
|
|
226
|
+
throw accessDeniedException(currentUser); // 403 if authenticated, else 401
|
|
227
|
+
throw accessDeniedException(currentUser, 'Custom message'); // same decision, custom message
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Prefer this over hand-rolling `user?.id ? new ForbiddenException() : new UnauthorizedException()` —
|
|
231
|
+
it keeps the codebase on one policy and handles falsy-but-present ids correctly.
|
|
232
|
+
|
|
233
|
+
---
|
|
234
|
+
|
|
235
|
+
## Detailed Migration Steps
|
|
236
|
+
|
|
237
|
+
### Step 1: Update the package
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
pnpm add @lenne.tech/nest-server@11.28.0
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Step 2: Find status-code expectations to update
|
|
244
|
+
|
|
245
|
+
The candidates are assertions that send a token/cookie **and** expect 401:
|
|
246
|
+
|
|
247
|
+
```bash
|
|
248
|
+
# Backend tests
|
|
249
|
+
grep -rn "statusCode: 401" tests/
|
|
250
|
+
grep -rn "toEqual(401)\|toBe(401)" tests/
|
|
251
|
+
|
|
252
|
+
# Locked endpoints / fields (S_NO_ONE) — every one of these is 403 now
|
|
253
|
+
grep -rn "S_NO_ONE" src/ tests/
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Step 3: Find message assertions to update
|
|
257
|
+
|
|
258
|
+
```bash
|
|
259
|
+
grep -rn "Missing rights\|No access\|no access rights for\|not allowed setting roles" src/ tests/
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
Replace them with `ErrorCode.ACCESS_DENIED` / `ErrorCode.UNAUTHORIZED`.
|
|
263
|
+
|
|
264
|
+
### Step 4: Find frontend 401 handlers
|
|
265
|
+
|
|
266
|
+
The auto-logout interceptor usually does **not** live in your own `src/`/`app/` — in lt projects it
|
|
267
|
+
ships with `@lenne.tech/nuxt-extensions` (`node_modules/`). Check your own branches too:
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
grep -rn "=== 401\|status === 401\|statusCode === 401\|case 401\|\[401" src/ app/
|
|
271
|
+
grep -rn "onResponseError\|onError" app/plugins/ app/composables/
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
A 403 must show an error message; only a 401 may clear the session. Remember that
|
|
275
|
+
`EMAIL_VERIFICATION_REQUIRED` is a 401 that must **not** log the user out — branch on the ErrorCode.
|
|
276
|
+
|
|
277
|
+
For GraphQL clients the status sits at `errors[0].extensions.originalError.statusCode`, and
|
|
278
|
+
`originalError.error` is `"Forbidden"` instead of `"Unauthorized"` for these cases.
|
|
279
|
+
|
|
280
|
+
### Step 5: Audit `S_SELF` / `S_CREATOR` on input types (security)
|
|
281
|
+
|
|
282
|
+
This is the step with security impact — do not skip it. See Breaking Change 5 for the full reasoning.
|
|
283
|
+
|
|
284
|
+
```bash
|
|
285
|
+
grep -rn "S_SELF\|S_CREATOR" src/server/**/inputs/
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
For each hit, confirm the intent still holds now that the role is actually enforced:
|
|
289
|
+
|
|
290
|
+
- **`S_CREATOR` on a user-editable field** (e.g. `email`, `status`) — almost always wrong: in an
|
|
291
|
+
invite/admin-provisioning flow the *creator* is the inviting admin, not the user. Change to
|
|
292
|
+
`RoleEnum.ADMIN`.
|
|
293
|
+
- **`S_SELF` under a broader class-level role** (`@Restricted(S_USER)` on the class) — never gated
|
|
294
|
+
anything (OR-merge); safe, but drop the redundant role to avoid the next reader's confusion.
|
|
295
|
+
- **`S_SELF` as the only role** — now correctly enforces "owner only". Confirm that is what you want.
|
|
296
|
+
|
|
297
|
+
### Step 6: Verify
|
|
298
|
+
|
|
299
|
+
```bash
|
|
300
|
+
pnpm run build && pnpm test
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
---
|
|
304
|
+
|
|
305
|
+
## Compatibility Notes
|
|
306
|
+
|
|
307
|
+
- Consumers of `@lenne.tech/nuxt-extensions`: version ≥ 1.8.4 additionally hardens the client auth
|
|
308
|
+
interceptor to verify the session before logging out on 401, so even backends still returning 401
|
|
309
|
+
for permission errors no longer cause wrongful logouts. Both changes are independent and
|
|
310
|
+
complementary.
|
|
311
|
+
- Projects with a **vendored core** adopt this change via the regular core sync
|
|
312
|
+
(`/lt-dev:backend:update-nest-server-core`).
|
|
313
|
+
- **Custom models that throw from `securityCheck()`** should switch to `accessDeniedException(user)`.
|
|
314
|
+
Otherwise they keep returning 401 to authenticated users and reintroduce the auto-logout bug in
|
|
315
|
+
your own code. `CoreTenantMemberModel` is the reference implementation.
|
|
316
|
+
|
|
317
|
+
---
|
|
318
|
+
|
|
319
|
+
## Troubleshooting
|
|
320
|
+
|
|
321
|
+
### A test now fails with "expected 401, received 403"
|
|
322
|
+
|
|
323
|
+
The request is authenticated and the user lacks rights — or the resource is `S_NO_ONE`-locked. 403 is
|
|
324
|
+
the new, correct status. Update the assertion (Step 2).
|
|
325
|
+
|
|
326
|
+
### A test now fails on the error message
|
|
327
|
+
|
|
328
|
+
Permission errors return `ErrorCode.ACCESS_DENIED` / `ErrorCode.UNAUTHORIZED` instead of raw English
|
|
329
|
+
(Breaking Change 3). Compare against the `ErrorCode` constant (Step 3).
|
|
330
|
+
|
|
331
|
+
### My frontend no longer logs users out on permission errors
|
|
332
|
+
|
|
333
|
+
That is the intended behavior: a permission error does not invalidate the session. Show an error
|
|
334
|
+
message instead — the new messages carry the `#LTNS_xxxx:` marker, so `useLtErrorTranslation()`
|
|
335
|
+
resolves them. Real session expiry still yields 401 and still triggers the logout flow.
|
|
336
|
+
|
|
337
|
+
### I lost the "no access rights for `<field>` of `<Class>`" detail
|
|
338
|
+
|
|
339
|
+
It is written to the debug log now rather than returned to the client (it exposed internal class and
|
|
340
|
+
property names in the HTTP response). Enable `config.debug` to see it server-side.
|
|
341
|
+
|
|
342
|
+
---
|
|
343
|
+
|
|
344
|
+
## References
|
|
345
|
+
|
|
346
|
+
- [RFC 9110 §15.5.2 (401 Unauthorized) / §15.5.4 (403 Forbidden)](https://www.rfc-editor.org/rfc/rfc9110)
|
|
347
|
+
- `src/core/common/exceptions/access-denied.exception.ts` — the factory and the policy it encodes
|
|
348
|
+
- `src/core/modules/auth/guards/roles.guard.ts` — the 401/403 pattern the service layer now mirrors
|
|
349
|
+
- `.claude/rules/role-system.md` — role system and status-code rules
|
|
350
|
+
- [nest-server-starter](https://github.com/lenneTech/nest-server-starter) — reference implementation
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Migration Guide: 11.28.0 → 11.28.1
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
| Category | Details |
|
|
6
|
+
|----------|---------|
|
|
7
|
+
| **Breaking Changes** | None |
|
|
8
|
+
| **Bugfixes** | Internal only — the `ejs` import in the BetterAuth email-verification service was normalized to the CommonJS style (`import ejs = require('ejs')`), silencing a static-analysis warning. No runtime behavior change. |
|
|
9
|
+
| **New Features** | None |
|
|
10
|
+
| **Maintenance** | Dependency housekeeping: 17 within-major package updates, 4 previously-undeclared runtime dependencies now declared explicitly, security overrides pruned 9 → 6. `pnpm audit` remains at **0 vulnerabilities**. |
|
|
11
|
+
| **Migration Effort** | **0 minutes for npm-mode consumers** (`pnpm update`). **~2 minutes for vendor-mode consumers** — ensure 4 runtime dependencies are present (see below). |
|
|
12
|
+
|
|
13
|
+
This is a **pure maintenance patch**. There is no API change, no configuration change, and no
|
|
14
|
+
behavioral change. Every one of the framework's 1381 tests passes unchanged.
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Quick Migration (npm mode)
|
|
19
|
+
|
|
20
|
+
No code changes required.
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
# Update the package
|
|
24
|
+
pnpm add @lenne.tech/nest-server@11.28.1
|
|
25
|
+
|
|
26
|
+
# Verify
|
|
27
|
+
pnpm run build
|
|
28
|
+
pnpm test
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
All transitive dependency changes resolve automatically. Nothing in your project needs to change.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## What Changed
|
|
36
|
+
|
|
37
|
+
### 1. Framework dependency updates (all within NestJS 11)
|
|
38
|
+
|
|
39
|
+
The MAJOR version still mirrors NestJS 11 — no NestJS major boundary was crossed. Notable updates:
|
|
40
|
+
|
|
41
|
+
| Package | 11.28.0 | 11.28.1 | Note |
|
|
42
|
+
|---------|---------|---------|------|
|
|
43
|
+
| `@nestjs/common` / `core` / `platform-express` / `websockets` | 11.1.23 | 11.1.28 | NestJS 11 patch |
|
|
44
|
+
| `mongoose` | 9.6.2 | 9.7.4 | Minor (mongodb stays 7.2.0 — coupled) |
|
|
45
|
+
| `nodemailer` | 8.0.8 | 9.0.3 | **Major bump** — internal email transport (see Compatibility Notes) |
|
|
46
|
+
| `multer` | 2.1.1 | 2.2.0 | Security (unhandled multipart DoS) |
|
|
47
|
+
| `graphql-query-complexity` | 1.1.0 | 1.1.1 | Patch |
|
|
48
|
+
|
|
49
|
+
Dev-only tooling was also refreshed (`vitest`/`@vitest/*` 4.1.7 → 4.1.10, `vite` 8.0.14 → 8.1.4,
|
|
50
|
+
`oxlint` 1.66.0 → 1.74.0, `@swc/core` 1.15.40 → 1.15.43, `tsx` 4.22.3 → 4.23.1, plus `@types/*`).
|
|
51
|
+
Dev-tooling changes have **zero** effect on consuming projects.
|
|
52
|
+
|
|
53
|
+
### 2. Newly declared runtime dependencies (relevant for vendor mode)
|
|
54
|
+
|
|
55
|
+
Four packages that the framework's shipped code **imports directly** were previously only resolved
|
|
56
|
+
**transitively** (phantom dependencies), relying on pnpm hoisting. They are now declared explicitly
|
|
57
|
+
and pinned to the exact versions that were already resolving — **this is a declaration change, not a
|
|
58
|
+
version bump, so nothing new is installed for npm-mode consumers.**
|
|
59
|
+
|
|
60
|
+
| Package | Version | Imported by | Reached transitively before via |
|
|
61
|
+
|---------|---------|-------------|----------------------------------|
|
|
62
|
+
| `cron` | 4.4.0 | `src/core/common/services/core-cron-jobs.service.ts` (`new CronJob`) | `@nestjs/schedule` |
|
|
63
|
+
| `jose` | 6.2.1 | `src/core/modules/better-auth/core-better-auth.service.ts` (`importJWK`/`jwtVerify`) | `better-auth`, `@modelcontextprotocol/sdk` |
|
|
64
|
+
| `ws` | 8.21.0 | `src/test/test.helper.ts` (`require('ws')`) | `@nestjs/graphql`, `@nestjs/apollo` |
|
|
65
|
+
| `graphql-ws` | 6.0.8 | `src/test/test.helper.ts` (`createClient`) | `@nestjs/graphql`, `@nestjs/apollo` |
|
|
66
|
+
|
|
67
|
+
Why this matters: relying on a transitive package means its presence and **version** are controlled
|
|
68
|
+
by someone else's dependency tree. `ws` already resolved to two versions in the tree (`8.21.0` and
|
|
69
|
+
a nested `7.5.11`); an explicit declaration removes the hoisting lottery.
|
|
70
|
+
|
|
71
|
+
### 3. Security override cleanup (9 → 6)
|
|
72
|
+
|
|
73
|
+
Three overrides in `pnpm-workspace.yaml` became genuine no-ops after the direct-dependency updates
|
|
74
|
+
above and were removed (verified with `pnpm audit`, which stays at 0 vulnerabilities):
|
|
75
|
+
|
|
76
|
+
| Removed override | Now resolved by |
|
|
77
|
+
|------------------|-----------------|
|
|
78
|
+
| `nodemailer@<9.0.1` | direct dependency is now `nodemailer@9.0.3` (the old override was silently patching a vulnerable `8.0.8` direct pin) |
|
|
79
|
+
| `multer@<2.2.0` | `@nestjs/platform-express@11.1.28` now pins `multer@2.2.0` exactly |
|
|
80
|
+
| `vite@>=8.0.0 <8.0.16` | all `vite` now resolves to `8.1.4` |
|
|
81
|
+
|
|
82
|
+
Six overrides remain, each proven still load-bearing (removing them reintroduces a vulnerability):
|
|
83
|
+
`ajv`, `picomatch`, `ws`, `uuid`, `@babel/core`, `js-yaml`. Each carries its CVE/GHSA rationale as
|
|
84
|
+
an inline comment.
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Compatibility Notes
|
|
89
|
+
|
|
90
|
+
- **npm-mode consumers:** Nothing to do. `pnpm update @lenne.tech/nest-server` is sufficient.
|
|
91
|
+
- **`nodemailer` 8 → 9 (internal):** `nodemailer` is used internally by the framework's
|
|
92
|
+
`EmailService`/SMTP transport. Projects configure it declaratively through
|
|
93
|
+
`email.smtp` in `config.env.ts`, and those options are unchanged across the 8 → 9 boundary — all
|
|
94
|
+
email tests pass unmodified. If (and only if) your project constructs a **custom `nodemailer`
|
|
95
|
+
transport object directly** and passes it in, review the
|
|
96
|
+
[nodemailer 9 release notes](https://github.com/nodemailer/nodemailer/releases) for the transport
|
|
97
|
+
API. The declarative `email.smtp` path needs no changes.
|
|
98
|
+
- **Projects without BetterAuth / cron / GraphQL subscriptions:** Unaffected — the newly declared
|
|
99
|
+
packages were already in your tree transitively.
|
|
100
|
+
|
|
101
|
+
### Vendor-mode consumers (`src/core/` copied into your project)
|
|
102
|
+
|
|
103
|
+
Vendor-mode projects do **not** install `@lenne.tech/nest-server` as an npm dependency, so they do
|
|
104
|
+
not inherit its `dependencies`. After syncing this release into your vendored `src/core/`, make sure
|
|
105
|
+
these four runtime packages exist in **your** `package.json` (pinned, per the fixed-version rule):
|
|
106
|
+
|
|
107
|
+
```jsonc
|
|
108
|
+
{
|
|
109
|
+
"dependencies": {
|
|
110
|
+
"cron": "4.4.0", // required — imported by core-cron-jobs.service.ts
|
|
111
|
+
"jose": "6.2.1" // required — imported by core-better-auth.service.ts
|
|
112
|
+
// ws / graphql-ws: required only if you use the exported TestHelper (src/test/test.helper.ts)
|
|
113
|
+
// in your own tests — most projects do:
|
|
114
|
+
// "graphql-ws": "6.0.8",
|
|
115
|
+
// "ws": "8.21.0"
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
In practice these are almost always already present transitively (via `@nestjs/schedule`,
|
|
121
|
+
`better-auth`, `@nestjs/graphql`). Declaring them explicitly protects you from a future transitive
|
|
122
|
+
change silently removing them. The `lt-dev:nest-server-core-updater` agent surfaces this during a
|
|
123
|
+
core sync.
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## Troubleshooting
|
|
128
|
+
|
|
129
|
+
### `Cannot find module 'jose'` / `'cron'` after a vendor-mode sync
|
|
130
|
+
|
|
131
|
+
You copied the updated `src/core/` but did not add the runtime dependency. Add the package to your
|
|
132
|
+
project's `package.json` (see the vendor-mode block above) and run `pnpm install`.
|
|
133
|
+
|
|
134
|
+
### `pnpm audit` reports a vulnerability I thought an override covered
|
|
135
|
+
|
|
136
|
+
The `nodemailer`, `multer`, and `vite` overrides were removed because the direct/transitive
|
|
137
|
+
resolutions now land on fixed versions on their own. If your project maintains its **own**
|
|
138
|
+
`pnpm-workspace.yaml` overrides (vendor mode or a monorepo root), re-verify with a with/without
|
|
139
|
+
lockfile diff — do not blindly copy this repo's removals; your tree may still resolve a vulnerable
|
|
140
|
+
version back into range.
|
|
141
|
+
|
|
142
|
+
### Build tool (`nest build`) fails with a permission error after `pnpm install`
|
|
143
|
+
|
|
144
|
+
Unrelated to your code — a known pnpm 11 hoisted-linker store-dedup artifact can drop the executable
|
|
145
|
+
bit on `node_modules/.bin/nest`. This release deliberately keeps `@nestjs/cli` at `11.0.21` to avoid
|
|
146
|
+
re-linking it. If you still hit it, `chmod +x node_modules/@nestjs/cli/bin/nest.js` restores it and
|
|
147
|
+
survives subsequent installs.
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## References
|
|
152
|
+
|
|
153
|
+
- [Package Management Rules](../.claude/rules/package-management.md) — fixed-version policy, override target rules
|
|
154
|
+
- [Migration Guide 11.27.7 → 11.28.0](./11.27.7-to-11.28.0.md) — previous release (the 401/403 policy + S_SELF/S_CREATOR ownership fixes)
|
|
155
|
+
- [nest-server-starter](https://github.com/lenneTech/nest-server-starter) (reference implementation)
|