@lenne.tech/nest-server 12.6.12 → 12.6.13
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 +7 -7
- package/.claude/rules/role-system.md +10 -0
- package/.claude/rules/testing.md +4 -4
- package/FRAMEWORK-API.md +1 -1
- package/dist/core/common/decorators/rest-user.decorator.js +4 -0
- package/dist/core/common/decorators/rest-user.decorator.js.map +1 -1
- package/dist/core/common/filters/http-exception-log.filter.js +4 -0
- package/dist/core/common/filters/http-exception-log.filter.js.map +1 -1
- package/dist/core/common/helpers/context.helper.js +9 -0
- package/dist/core/common/helpers/context.helper.js.map +1 -1
- package/dist/core/common/helpers/decorator.helper.js +4 -0
- package/dist/core/common/helpers/decorator.helper.js.map +1 -1
- package/dist/core/common/helpers/execution-context-request.helper.d.ts +9 -1
- package/dist/core/common/helpers/execution-context-request.helper.js +29 -0
- package/dist/core/common/helpers/execution-context-request.helper.js.map +1 -1
- package/dist/core/common/helpers/graphql-upload.helper.d.ts +10 -0
- package/dist/core/common/helpers/graphql-upload.helper.js +220 -0
- package/dist/core/common/helpers/graphql-upload.helper.js.map +1 -0
- package/dist/core/common/interceptors/translate-response.interceptor.js +4 -0
- package/dist/core/common/interceptors/translate-response.interceptor.js.map +1 -1
- package/dist/core/modules/auth/guards/auth.guard.js +10 -2
- package/dist/core/modules/auth/guards/auth.guard.js.map +1 -1
- package/dist/core/modules/auth/guards/legacy-auth-rate-limit.guard.js +4 -1
- package/dist/core/modules/auth/guards/legacy-auth-rate-limit.guard.js.map +1 -1
- package/dist/core/modules/auth/guards/roles.guard.js +11 -1
- package/dist/core/modules/auth/guards/roles.guard.js.map +1 -1
- package/dist/core/modules/better-auth/better-auth-roles.guard.js +15 -1
- package/dist/core/modules/better-auth/better-auth-roles.guard.js.map +1 -1
- package/dist/core/modules/tenant/core-tenant.guard.js +9 -1
- package/dist/core/modules/tenant/core-tenant.guard.js.map +1 -1
- package/dist/core.module.js +2 -1
- package/dist/core.module.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/types/graphql-upload.d.ts +34 -2
- package/docs/REQUEST-LIFECYCLE.md +30 -8
- package/docs/security-overrides.md +3 -2
- package/migration-guides/12.6.12-to-12.6.13.md +162 -0
- package/package.json +10 -3
- package/src/core/common/decorators/rest-user.decorator.ts +6 -0
- package/src/core/common/filters/http-exception-log.filter.ts +9 -0
- package/src/core/common/helpers/context.helper.ts +14 -0
- package/src/core/common/helpers/decorator.helper.ts +5 -0
- package/src/core/common/helpers/execution-context-request.helper.ts +78 -1
- package/src/core/common/helpers/graphql-upload.helper.ts +482 -0
- package/src/core/common/interceptors/translate-response.interceptor.ts +8 -0
- package/src/core/modules/auth/guards/auth.guard.ts +19 -2
- package/src/core/modules/auth/guards/legacy-auth-rate-limit.guard.ts +9 -2
- package/src/core/modules/auth/guards/roles.guard.ts +23 -1
- package/src/core/modules/better-auth/better-auth-roles.guard.ts +29 -1
- package/src/core/modules/tenant/core-tenant.guard.ts +13 -1
- package/src/core.module.ts +5 -1
- package/src/index.ts +1 -0
- package/src/types/graphql-upload.d.ts +34 -2
|
@@ -217,7 +217,7 @@ JWT-based authentication for existing projects:
|
|
|
217
217
|
| **Subscription re-check** | Since 11.42.3 that context is not trusted for the life of the socket. Every connection a `CoreModule` `onConnect` accepts is registered (`registerWsConnection()`) and checked again — at once on an invalidation (tenant guard, role change, password reset, Better-Auth deleting the session), otherwise before the next operation or event once `graphQl.subscriptionRevalidationMs` (30 s) has passed. The check re-runs the handshake's authentication and the tenant resolution; a connection whose rights changed is closed with 4403 (retried by the graphql-ws client, which then reconnects with its current rights) and the event that triggered the check is dropped. Before, a removed membership, a deactivated tenant, a withdrawn role or a revoked session kept receiving events on an open socket until the client reconnected. See `.claude/rules/role-system.md` → "Open WebSockets" |
|
|
218
218
|
| **Complexity Analysis** | Query cost calculation to prevent DoS attacks |
|
|
219
219
|
| **Enum Registration** | `registerEnum()` helper for GraphQL enum types |
|
|
220
|
-
| **Upload Support** | `graphqlUploadExpress()` for multipart file uploads |
|
|
220
|
+
| **Upload Support** | `graphqlUploadExpress()` for multipart file uploads, with the framework's `processGraphQlUploadRequest` (graphql-upload's parser with the GHSA-5vm7-vc2x-p3w2 and GHSA-qwwm-252j-9whw fixes; `map` bounded to 64 keys per path and 10,000 entries and path keys together) |
|
|
221
221
|
|
|
222
222
|
### Development & Operations
|
|
223
223
|
|
|
@@ -364,6 +364,8 @@ The following diagram shows the exact order of execution from HTTP request to re
|
|
|
364
364
|
| |
|
|
365
365
|
| 3. graphqlUploadExpress() [GraphQL only] |
|
|
366
366
|
| - Handles multipart file uploads |
|
|
367
|
+
| - processGraphQlUploadRequest: bad map path -> 400 |
|
|
368
|
+
| - map: 64 keys/path, 10,000 entries + keys |
|
|
367
369
|
+----------------------------+----------------------------+
|
|
368
370
|
|
|
|
369
371
|
+----------------------------v----------------------------+
|
|
@@ -592,7 +594,7 @@ NestJS middleware runs for every request after Express-level middleware. Registr
|
|
|
592
594
|
// src/core.module.ts
|
|
593
595
|
configure(consumer: MiddlewareConsumer) {
|
|
594
596
|
consumer.apply(RequestContextMiddleware).forRoutes('*');
|
|
595
|
-
consumer.apply(graphqlUploadExpress()).forRoutes('graphql');
|
|
597
|
+
consumer.apply(graphqlUploadExpress({ processRequest: processGraphQlUploadRequest })).forRoutes('graphql');
|
|
596
598
|
}
|
|
597
599
|
```
|
|
598
600
|
|
|
@@ -740,6 +742,24 @@ session cookie riding along can never turn a token request into a session reques
|
|
|
740
742
|
|
|
741
743
|
Only for GraphQL routes. Handles multipart file upload requests according to the [GraphQL multipart request specification](https://github.com/jaydenseric/graphql-multipart-request-spec).
|
|
742
744
|
|
|
745
|
+
It runs before every guard, so whatever it parses comes from an anonymous client. `CoreModule` passes
|
|
746
|
+
the framework's `processGraphQlUploadRequest` (`common/helpers/graphql-upload.helper.ts`) instead of graphql-upload's
|
|
747
|
+
own `processRequest`: graphql-upload 17.1.0's parser with both hunks of the fix in graphql-upload 18.0.2 (which needs
|
|
748
|
+
graphql 17). A `map` path with a `__proto__` key, or one that goes through an already placed upload, is answered with
|
|
749
|
+
400 "Invalid object path" (GHSA-5vm7-vc2x-p3w2 — before, `{"1":["variables.file"],"2":["variables.file.reject"]}`
|
|
750
|
+
without file fields made the parser throw an uncaught `TypeError` and the process exit), and a second file field with
|
|
751
|
+
the name of an already resolved upload is ignored (GHSA-qwwm-252j-9whw — before, its temporary file leaked). A project
|
|
752
|
+
that registers `graphqlUploadExpress()` itself passes `{ processRequest: processGraphQlUploadRequest }` too.
|
|
753
|
+
|
|
754
|
+
It also bounds the `map`: at most `GRAPHQL_UPLOAD_MAX_PATH_KEYS` (64) keys per path, and at most
|
|
755
|
+
`GRAPHQL_UPLOAD_MAX_MAP_KEYS` (10,000) entries and path keys together — each entry counts one, each key of its paths
|
|
756
|
+
one more. A path over a bound is answered with 400 "Invalid object path", an entry over the map bound with 400
|
|
757
|
+
"Invalid ‘map’ multipart field entry key …" before its upload is created. The parser places each upload with
|
|
758
|
+
object-path's `set()`, which costs O(n²) for a path of n keys, and creates an upload per entry that it rejects with an
|
|
759
|
+
error of its own when the file never arrives: before, one anonymous request with an 80 KB path exhausted a 1 GB heap
|
|
760
|
+
and ended the process, 1 MB of paths just under a per-path bound blocked the event loop for up to 640 ms, and 1 MB of
|
|
761
|
+
entries without a path for 410 ms with 230 MB. graphql-upload 18.0.2 has the same flaws.
|
|
762
|
+
|
|
743
763
|
> **NestJS docs:** [Middleware](https://docs.nestjs.com/middleware)
|
|
744
764
|
|
|
745
765
|
---
|
|
@@ -1649,13 +1669,15 @@ export class UserService extends CoreUserService<User> {
|
|
|
1649
1669
|
|
|
1650
1670
|
### Guard Context Detection
|
|
1651
1671
|
|
|
1652
|
-
Guards
|
|
1672
|
+
Guards, the response interceptors and `@CurrentUser()` resolve the request through ONE helper,
|
|
1673
|
+
`resolveGuardRequest()` (`common/helpers/execution-context-request.helper.ts`):
|
|
1653
1674
|
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1675
|
+
| Context type | Request a guard decides on |
|
|
1676
|
+
|--------------|----------------------------|
|
|
1677
|
+
| `http` | `context.switchToHttp().getRequest()` |
|
|
1678
|
+
| `graphql` (HTTP) | `GqlExecutionContext.create(context).getContext().req` |
|
|
1679
|
+
| `graphql` (WebSocket subscription) | the graphql-ws context `CoreModule`'s `onConnect` recorded the user on |
|
|
1680
|
+
| `ai` (`@nestjs/ai` agent function) / `mcp` (`@nestjs/mcp` primitive) | `args[1].request` — the HTTP request the call runs in; never `args[0]`, the model's / MCP client's arguments. Without a request the caller is anonymous (401 on every role-gated method, also in `CoreTenantGuard`); `HttpExceptionLogFilter` hands the exception back instead of writing a response |
|
|
1659
1681
|
|
|
1660
1682
|
> **NestJS docs:** [REST Controllers](https://docs.nestjs.com/controllers), [GraphQL Resolvers](https://docs.nestjs.com/graphql/resolvers)
|
|
1661
1683
|
|
|
@@ -119,8 +119,9 @@ through globparts amplification) and [GHSA-3g3m-2fjr-6pw4](https://github.com/ad
|
|
|
119
119
|
(glob `<= 14.0.1`, moderate) are fixed only in new majors that no consumer can take yet: `@nestjs/cli` 12.0.11
|
|
120
120
|
pins minimatch 10.2.6 exactly, ts-morph 28 (`@ts-morph/common` 0.29), nodemon, `@swc/cli` and glob 13 need the 9.x /
|
|
121
121
|
10.x lines, rimraf 6.1.3 needs glob 13 — all on their newest release. An override across the major is no way out
|
|
122
|
-
(minimatch 11 removed `makeRe()` and `Minimatch#regexp`). nest-server suppresses
|
|
123
|
-
`auditConfig.unusableFixConsumers`, which `check:overrides` re-tests on every run
|
|
122
|
+
(minimatch 11 removed `makeRe()` and `Minimatch#regexp`). nest-server suppresses minimatch in its own repository with
|
|
123
|
+
`auditConfig.unusableFixConsumers`, which `check:overrides` re-tests on every run (glob until 12.6.12; rimraf 6.1.4 moved
|
|
124
|
+
to glob 14 — raise rimraf to 6.1.4 and the glob entry can go). What it found for its own tree:
|
|
124
125
|
the only production path is `@lenne.tech/nest-server > ts-morph > @ts-morph/common > minimatch`, used by the
|
|
125
126
|
Permissions module's scanner with globs the developer writes; every other path is dev tooling. Your project's tree
|
|
126
127
|
may have other paths, so check `pnpm why minimatch --prod` and `pnpm why glob --prod` before you suppress the same
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Migration Guide: 12.6.12 → 12.6.13
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
| Category | Details |
|
|
6
|
+
|---|---|
|
|
7
|
+
| **Breaking Changes** | None |
|
|
8
|
+
| **New Features** | None. New exports for the fixes below: `processGraphQlUploadRequest`, `GRAPHQL_UPLOAD_MAX_PATH_KEYS`, `GRAPHQL_UPLOAD_MAX_MAP_KEYS`, `isAgentCallContext()`, `resolveAgentCallRequest()` |
|
|
9
|
+
| **Bugfixes** | `HttpExceptionLogFilter` threw on `@nestjs/ai` / `@nestjs/mcp` calls, so the model read "Internal server error" for every refusal (§2) |
|
|
10
|
+
| **Security** | GraphQL multipart uploads (§1): graphql-upload GHSA-5vm7-vc2x-p3w2 (high, one anonymous request crashed the process), GHSA-qwwm-252j-9whw (medium, duplicate file fields leaked temporary files), and an unbounded `map` (one anonymous request with a deep path exhausted the heap, one with many entries blocked the event loop and took hundreds of MB). With `@nestjs/ai` or `@nestjs/mcp` installed, the global guards decided agent function and MCP tool calls on the tool ARGUMENTS (§2) |
|
|
11
|
+
| **Migration Effort** | `pnpm run update`. Projects that register `graphqlUploadExpress()` themselves pass the framework's parser (§1); vendor-mode projects add four dependencies (§3) |
|
|
12
|
+
|
|
13
|
+
## Quick Migration
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @lenne.tech/nest-server@12.6.13
|
|
17
|
+
pnpm run update # nest-server-starter based projects
|
|
18
|
+
grep -rn "graphqlUploadExpress(" src/ # own registrations: pass processGraphQlUploadRequest (§1)
|
|
19
|
+
pnpm run build
|
|
20
|
+
pnpm test
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Vendor mode**: run `/lt-dev:backend:update-nest-server-core` as usual, then add the dependencies from §3.
|
|
24
|
+
|
|
25
|
+
## 1. Security: GraphQL multipart uploads
|
|
26
|
+
|
|
27
|
+
**No action needed** unless the project registers `graphqlUploadExpress()` itself (below).
|
|
28
|
+
|
|
29
|
+
`CoreModule` registers graphql-upload's `graphqlUploadExpress()` for `/graphql`, in front of every guard. Up to 12.6.12
|
|
30
|
+
it ran graphql-upload's own `processRequest`, which accepted any object path and any number of entries in the
|
|
31
|
+
multipart `map` field:
|
|
32
|
+
|
|
33
|
+
- **GHSA-5vm7-vc2x-p3w2 (high):** a path into an upload placed by an earlier entry replaced one of its methods. A
|
|
34
|
+
request with `map={"1":["variables.file"],"2":["variables.file.reject"]}` and no file fields made the parser throw
|
|
35
|
+
`TypeError: upload.reject is not a function` outside any request — an uncaught exception, on which the process
|
|
36
|
+
exits (`installProcessDiagnostics()` exits 1, as Node does without it). **One anonymous request took the server
|
|
37
|
+
down.** A path with a `__proto__` key let a client-supplied object pass the `GraphQLUpload` scalar's check.
|
|
38
|
+
- **GHSA-qwwm-252j-9whw (medium):** a second file field with the name of an already resolved upload created another
|
|
39
|
+
temporary file and dropped the reference to the first, which was never deleted — temporary files and file
|
|
40
|
+
descriptors leaked per request.
|
|
41
|
+
- **Unbounded path depth (no advisory; graphql-upload 18.0.2 has the same flaw):** the parser places each upload with
|
|
42
|
+
object-path's `set()`, which recurses once per key and copies the remaining keys each time — O(n²) memory and time
|
|
43
|
+
for a path of n keys, in one synchronous call. Measured on Node 24 with `map={"1":["variables.a.a.a…"]}`: 8,000 keys
|
|
44
|
+
(16 KB) took 310 MB and blocked the event loop for half a second; 40,000 keys (80 KB) exhausted a 1 GB heap and
|
|
45
|
+
ended the process. The default `maxFieldSize` of 1 MB leaves room for 400,000 keys.
|
|
46
|
+
- **Unbounded number of entries (no advisory; graphql-upload 18.0.2 has the same flaw):** every `map` entry becomes an
|
|
47
|
+
upload, and one whose file never arrives is rejected at the end of the request with an error of its own, stack trace
|
|
48
|
+
included. 1 MB of entries without any path (83,000 of them) took 230 MB and blocked the event loop for 410 ms.
|
|
49
|
+
|
|
50
|
+
graphql-upload fixed the two advisories in 18.0.2, which requires graphql 17; this framework runs graphql 16. 12.6.13
|
|
51
|
+
therefore ships `processGraphQlUploadRequest` (`common/helpers/graphql-upload.helper.ts`): graphql-upload 17.1.0's
|
|
52
|
+
parser with both upstream fixes and two bounds of its own, and `CoreModule` passes it to `graphqlUploadExpress()`. It
|
|
53
|
+
answers with 400:
|
|
54
|
+
|
|
55
|
+
- a path into another upload or with a `__proto__` key — "Invalid object path",
|
|
56
|
+
- a path with more than `GRAPHQL_UPLOAD_MAX_PATH_KEYS` (64) keys — "Invalid object path",
|
|
57
|
+
- a `map` with more than `GRAPHQL_UPLOAD_MAX_MAP_KEYS` (10,000) entries and path keys together. Every entry counts one,
|
|
58
|
+
every key of its paths one more, so `{"1":["variables.file"]}` counts 3. The path that goes over the bound is
|
|
59
|
+
answered with "Invalid object path", an entry with "Invalid ‘map’ multipart field entry key …" before its upload is
|
|
60
|
+
created.
|
|
61
|
+
|
|
62
|
+
A duplicate file field is ignored. Each of these requests is now answered within about 40 ms, most of it parsing the
|
|
63
|
+
field's JSON (the 83,000 entries: ~35 ms, +50 MB). A path a client library builds has a handful of keys
|
|
64
|
+
(`variables.file` 2, `variables.input.files.3` 4, batched `0.variables.file` 3); 10,000 is some 2,000 files at
|
|
65
|
+
`variables.input.files.N`. The most expensive request the bounds still accept — 10,000 entries without a path — blocks
|
|
66
|
+
the event loop for about 60 ms with +45 MB. graphql-upload stays at 17.1.0 for its scalar and middleware.
|
|
67
|
+
|
|
68
|
+
**A project that registers `graphqlUploadExpress()` itself** (its own route, its own limits) passes the framework's
|
|
69
|
+
parser — graphql-upload's default is the vulnerable one:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { processGraphQlUploadRequest } from '@lenne.tech/nest-server';
|
|
73
|
+
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
|
|
74
|
+
|
|
75
|
+
consumer
|
|
76
|
+
.apply(graphqlUploadExpress({ maxFiles: 10, maxFileSize: 10_000_000, processRequest: processGraphQlUploadRequest }))
|
|
77
|
+
.forRoutes('graphql');
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Find such registrations with `grep -rn "graphqlUploadExpress(" src/`. A project that calls graphql-upload's
|
|
81
|
+
`processRequest` directly switches to `processGraphQlUploadRequest` (same signature and options).
|
|
82
|
+
|
|
83
|
+
**Behaviour change:** a GraphQL variable literally named `__proto__` that receives a file is refused with 400 (input
|
|
84
|
+
object fields cannot use that name; names starting with `__` are reserved).
|
|
85
|
+
|
|
86
|
+
## 2. Guards on `@nestjs/ai` and `@nestjs/mcp` calls
|
|
87
|
+
|
|
88
|
+
Only relevant to a project that installs `@nestjs/ai` (`@AgentFunction()`) or `@nestjs/mcp` (`@Tool()`, `@Resource()`,
|
|
89
|
+
`@Prompt()`). Both run every global guard, interceptor and filter of the application around their methods, with the
|
|
90
|
+
context types `'ai'` / `'mcp'` and the arguments `[input, call]`. Index 0 is what the model or the MCP client wrote.
|
|
91
|
+
Up to 12.6.12 the framework's guards fell back to `switchToHttp().getRequest()` there — index 0. Tool arguments
|
|
92
|
+
`{ "user": { "roles": ["admin"] } }` were the caller a guard decided on.
|
|
93
|
+
|
|
94
|
+
Since 12.6.13 the framework resolves the request of such a call from the call object (`caller.request`,
|
|
95
|
+
`McpContext.request`):
|
|
96
|
+
|
|
97
|
+
- `@Roles()` on an agent function or MCP tool is decided against the request's user exactly as over HTTP, tenant
|
|
98
|
+
header included — in `RolesGuard`, `BetterAuthRolesGuard`, `CoreTenantGuard`, the auth guard and the legacy
|
|
99
|
+
rate-limit guard.
|
|
100
|
+
- A **request-less** caller — `caller: { user, scopes }` (a queue job), a durable run, MCP over stdio — is
|
|
101
|
+
**anonymous**: public methods run, every other one is refused with 401. To run a role-gated agent function from a job,
|
|
102
|
+
authenticate there and pass a request (`caller: { request }`).
|
|
103
|
+
- `@CurrentUser()` / `@RESTUser()` and the response interceptors (`@Restricted`, secret fields) use the request's user.
|
|
104
|
+
- `HttpExceptionLogFilter` hands the exception back to the package instead of writing an HTTP response, so the model
|
|
105
|
+
or MCP client reads the refusal's message instead of "Internal server error".
|
|
106
|
+
|
|
107
|
+
**Not in 12.6.13** — these parts of the 12.10.x work stay on the 12.10 line: the `MCP_VERIFIED_USER` token,
|
|
108
|
+
`CoreAiMcpOAuthService.createMcpTokenVerifier()` for a `@nestjs/mcp` endpoint, the AI SDK transport
|
|
109
|
+
(`providerType: 'nestjs-ai'`), agent functions as AI tools, and the removal of secret fields from AI tool results.
|
|
110
|
+
Update to 12.10.x for them.
|
|
111
|
+
|
|
112
|
+
**A guard of your own** that reads the request with `switchToHttp().getRequest()` (or `GqlExecutionContext`'s `req`
|
|
113
|
+
with that fallback) has the same flaw on these calls. Resolve the request through the framework:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { resolveGuardRequest } from '@lenne.tech/nest-server';
|
|
117
|
+
import { GqlExecutionContext } from '@nestjs/graphql';
|
|
118
|
+
|
|
119
|
+
canActivate(context: ExecutionContext): boolean {
|
|
120
|
+
// undefined for a request-less agent / MCP call: treat it as anonymous
|
|
121
|
+
const request = resolveGuardRequest(context, (ctx) => GqlExecutionContext.create(ctx).getContext());
|
|
122
|
+
// …
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Nothing to do otherwise, unless the project relied on the old behaviour.
|
|
127
|
+
|
|
128
|
+
## 3. Dependencies
|
|
129
|
+
|
|
130
|
+
New direct `dependencies` for the multipart parser (§1): `busboy` 1.6.0, `fs-capacitor` 8.0.0, `http-errors` 2.0.1 and
|
|
131
|
+
`object-path` 0.11.8 — the versions graphql-upload 17.1.0 already installs, so an npm-mode project gets no new package.
|
|
132
|
+
|
|
133
|
+
A **vendor-mode** project declares them in `dependencies`: the core imports them directly, and a pnpm project does not
|
|
134
|
+
see graphql-upload's own copies. A project that compiles with `noImplicitAny` (part of `strict`) also adds
|
|
135
|
+
`@types/busboy` 1.5.5, `@types/http-errors` 2.0.5 and `@types/object-path` 0.11.4 to `devDependencies`.
|
|
136
|
+
|
|
137
|
+
## Compatibility Notes
|
|
138
|
+
|
|
139
|
+
| Pattern | Status |
|
|
140
|
+
|---|---|
|
|
141
|
+
| Uploads through `CoreModule`'s `/graphql` registration | Protected with the update |
|
|
142
|
+
| `graphqlUploadExpress()` registered by the project | Pass `processRequest: processGraphQlUploadRequest` (§1) |
|
|
143
|
+
| Vendor mode, GraphQL uploads | Add the four dependencies (and the `@types/*` under `noImplicitAny`) from §3 |
|
|
144
|
+
| Projects without `@nestjs/ai` / `@nestjs/mcp` | Unchanged — the guards take the new branch only for the context types `'ai'` / `'mcp'` |
|
|
145
|
+
| A guard of the project's own on agent / MCP calls | Resolve the request with `resolveGuardRequest()` (§2) |
|
|
146
|
+
|
|
147
|
+
## Troubleshooting
|
|
148
|
+
|
|
149
|
+
| Symptom | Cause | Fix |
|
|
150
|
+
|---|---|---|
|
|
151
|
+
| Vendor mode: `Cannot find package 'busboy'` (or `fs-capacitor`, `http-errors`, `object-path`) at start | The core imports them since 12.6.13 | Add them to `dependencies` (§3) |
|
|
152
|
+
| Vendor mode: `TS7016: Could not find a declaration file for module 'busboy'` (or `http-errors`, `object-path`) | `noImplicitAny` without the type packages | Add the `@types/*` packages from §3 |
|
|
153
|
+
| A file upload answers 400 `Invalid object path for the ‘map’ multipart field …` | The `map` names a path into another upload, with a `__proto__` key or with more than 64 keys | Send each file at its own flat variable path (`variables.files.0`, `variables.input.files.3`) |
|
|
154
|
+
| An upload of very many files answers 400 `Invalid object path …` or `Invalid ‘map’ multipart field entry key …` | The `map` has more than 10,000 entries and path keys together | Split the upload into several requests |
|
|
155
|
+
| A role-gated agent function answers 401 from a queue job | Request-less callers are anonymous | Authenticate in the job and pass `caller: { request }` |
|
|
156
|
+
|
|
157
|
+
## Module Documentation
|
|
158
|
+
|
|
159
|
+
- `docs/REQUEST-LIFECYCLE.md` — "3. graphqlUploadExpress" and the guard table row for `ai` / `mcp`
|
|
160
|
+
- `src/core/common/helpers/graphql-upload.helper.ts` — the parser, its fixes and bounds
|
|
161
|
+
- `src/core/common/helpers/execution-context-request.helper.ts` — `resolveGuardRequest()`, `resolveAgentCallRequest()`
|
|
162
|
+
- `.claude/rules/role-system.md` — "Agent function and MCP calls"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lenne.tech/nest-server",
|
|
3
|
-
"version": "12.6.
|
|
3
|
+
"version": "12.6.13",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Server framework in TypeScript on top of NestJS: GraphQL and REST APIs on MongoDB, PostgreSQL, MySQL or MariaDB, with authentication, field-level permissions, multi-tenancy, offline sync and an AI assistant.",
|
|
6
6
|
"keywords": [
|
|
@@ -119,6 +119,7 @@
|
|
|
119
119
|
"@tus/server": "2.4.7",
|
|
120
120
|
"@types/supertest": "7.2.1",
|
|
121
121
|
"bcrypt": "6.0.0",
|
|
122
|
+
"busboy": "1.6.0",
|
|
122
123
|
"class-transformer": "0.5.1",
|
|
123
124
|
"class-validator": "0.15.1",
|
|
124
125
|
"compression": "1.8.2",
|
|
@@ -127,11 +128,13 @@
|
|
|
127
128
|
"dotenv": "18.0.9",
|
|
128
129
|
"ejs": "7.0.2",
|
|
129
130
|
"express": "5.3.0",
|
|
131
|
+
"fs-capacitor": "8.0.0",
|
|
130
132
|
"graphql": "16.14.2",
|
|
131
133
|
"graphql-query-complexity": "2.0.0",
|
|
132
134
|
"graphql-subscriptions": "3.0.0",
|
|
133
135
|
"graphql-upload": "17.1.0",
|
|
134
136
|
"graphql-ws": "6.3.0",
|
|
137
|
+
"http-errors": "2.0.1",
|
|
135
138
|
"jose": "6.2.15",
|
|
136
139
|
"js-sha256": "1.0.0",
|
|
137
140
|
"json-to-graphql-query": "2.3.0",
|
|
@@ -139,6 +142,7 @@
|
|
|
139
142
|
"multer": "2.5.0",
|
|
140
143
|
"node-mailjet": "6.0.11",
|
|
141
144
|
"nodemailer": "10.4.1",
|
|
145
|
+
"object-path": "0.11.8",
|
|
142
146
|
"passport": "0.7.0",
|
|
143
147
|
"passport-jwt": "4.0.1",
|
|
144
148
|
"reflect-metadata": "0.2.2",
|
|
@@ -240,13 +244,16 @@
|
|
|
240
244
|
"@swc/cli": "0.8.2",
|
|
241
245
|
"@swc/core": "1.16.21",
|
|
242
246
|
"@tus/s3-store": "2.1.0",
|
|
247
|
+
"@types/busboy": "1.5.5",
|
|
243
248
|
"@types/compression": "1.8.1",
|
|
244
249
|
"@types/cookie-parser": "1.4.10",
|
|
245
250
|
"@types/ejs": "3.1.5",
|
|
246
251
|
"@types/express": "5.0.6",
|
|
252
|
+
"@types/http-errors": "2.0.5",
|
|
247
253
|
"@types/lodash": "4.17.27",
|
|
248
254
|
"@types/multer": "2.3.0",
|
|
249
255
|
"@types/node": "26.6.6",
|
|
256
|
+
"@types/object-path": "0.11.4",
|
|
250
257
|
"@types/passport": "1.0.17",
|
|
251
258
|
"@types/pg": "8.23.1",
|
|
252
259
|
"@vitest/coverage-v8": "5.1.0",
|
|
@@ -266,14 +273,14 @@
|
|
|
266
273
|
"oxlint-tsgolint": "7.0.2005",
|
|
267
274
|
"pg": "8.23.1",
|
|
268
275
|
"playwright-core": "1.64.1",
|
|
269
|
-
"rimraf": "6.1.
|
|
276
|
+
"rimraf": "6.1.4",
|
|
270
277
|
"smtp-server": "3.21.3",
|
|
271
278
|
"tsx": "4.23.15",
|
|
272
279
|
"tus-js-client": "4.3.1",
|
|
273
280
|
"typeorm": "1.1.2",
|
|
274
281
|
"typescript": "6.0.3",
|
|
275
282
|
"unplugin-swc": "2.0.0",
|
|
276
|
-
"vite": "8.3.
|
|
283
|
+
"vite": "8.3.8",
|
|
277
284
|
"vitest": "5.1.0"
|
|
278
285
|
},
|
|
279
286
|
"main": "dist/index.js",
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
|
|
2
2
|
|
|
3
|
+
import { isAgentCallContext, resolveAgentCallRequest } from '../helpers/execution-context-request.helper.js';
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* User decorator for REST request
|
|
5
7
|
*/
|
|
6
8
|
export const RESTUser = createParamDecorator((data: unknown, ctx: ExecutionContext) => {
|
|
9
|
+
// An agent function / MCP call: the request's user, never one in the call's arguments (nestjs-ai plan §1)
|
|
10
|
+
if (isAgentCallContext(ctx)) {
|
|
11
|
+
return resolveAgentCallRequest(ctx)?.user;
|
|
12
|
+
}
|
|
7
13
|
const request = ctx.switchToHttp().getRequest();
|
|
8
14
|
return request.user;
|
|
9
15
|
});
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
} from '@nestjs/common';
|
|
9
9
|
import { GqlContextType } from '@nestjs/graphql';
|
|
10
10
|
import { Response } from 'express';
|
|
11
|
+
import { isAgentCallContext } from '../helpers/execution-context-request.helper.js';
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Nest's logger rather than `console.debug` (12.0.0): the stack carries the filter as its context, honours the
|
|
@@ -31,6 +32,14 @@ export class HttpExceptionLogFilter implements ExceptionFilter {
|
|
|
31
32
|
return exception;
|
|
32
33
|
}
|
|
33
34
|
|
|
35
|
+
// An agent function call (`@nestjs/ai`, `'ai'`) or an MCP call (`@nestjs/mcp`, `'mcp'`): the second argument is
|
|
36
|
+
// the CALL, not a response (nestjs-ai plan §1). Returning nothing hands the exception back to the package, which
|
|
37
|
+
// tells the model or MCP client its message. Writing to `switchToHttp()` there threw a TypeError instead, and the
|
|
38
|
+
// model read "Internal server error" for every 401 / 403 / 404. Every other transport is unchanged.
|
|
39
|
+
if (isAgentCallContext(host)) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
34
43
|
// REST
|
|
35
44
|
const ctx = host.switchToHttp();
|
|
36
45
|
const res = ctx.getResponse<Response>();
|
|
@@ -2,6 +2,8 @@ import { ExecutionContext } from '@nestjs/common';
|
|
|
2
2
|
import { GqlExecutionContext } from '@nestjs/graphql';
|
|
3
3
|
import { Request as RequestType } from 'express';
|
|
4
4
|
|
|
5
|
+
import { isAgentCallContext, resolveAgentCallRequest } from './execution-context-request.helper.js';
|
|
6
|
+
|
|
5
7
|
/**
|
|
6
8
|
* Helper for context processing
|
|
7
9
|
* @deprecated use functions directly
|
|
@@ -33,6 +35,18 @@ export function getContextData(context: ExecutionContext): {
|
|
|
33
35
|
return { args: null, currentUser: null, request: null };
|
|
34
36
|
}
|
|
35
37
|
|
|
38
|
+
// An agent function / MCP call (nestjs-ai plan §1): the user is the one of the request the call runs in — never a
|
|
39
|
+
// `user` in the call's arguments, which the model or the MCP client wrote. A request-less call has no user, so the
|
|
40
|
+
// interceptors filter its result for an anonymous caller (the strictest view).
|
|
41
|
+
if (isAgentCallContext(context)) {
|
|
42
|
+
const agentRequest = resolveAgentCallRequest(context);
|
|
43
|
+
return {
|
|
44
|
+
args: context.getArgByIndex(0),
|
|
45
|
+
currentUser: agentRequest?.user,
|
|
46
|
+
request: (agentRequest ?? null) as RequestType,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
36
50
|
// Init data
|
|
37
51
|
let user: { [key: string]: any };
|
|
38
52
|
let rawContext: any = null;
|
|
@@ -3,12 +3,17 @@ import { GqlContextType, GqlExecutionContext } from '@nestjs/graphql';
|
|
|
3
3
|
import type { PopulateOptions } from 'mongoose';
|
|
4
4
|
|
|
5
5
|
import { getPopulateOptions } from './db.helper.js';
|
|
6
|
+
import { isAgentCallContext, resolveAgentCallRequest } from './execution-context-request.helper.js';
|
|
6
7
|
import { removePropertiesDeep } from './input.helper.js';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Decorator function to get current user for Controller (http context) and Resolver (graphql context)
|
|
10
11
|
*/
|
|
11
12
|
export function currentUserDec(data, ctx: ExecutionContext) {
|
|
13
|
+
// An agent function / MCP call: the request's user, never one in the call's arguments (nestjs-ai plan §1)
|
|
14
|
+
if (isAgentCallContext(ctx)) {
|
|
15
|
+
return resolveAgentCallRequest(ctx)?.user;
|
|
16
|
+
}
|
|
12
17
|
if (ctx.getType<GqlContextType>() === 'graphql') {
|
|
13
18
|
const gqlContext = GqlExecutionContext.create(ctx);
|
|
14
19
|
return gqlContext.getContext().req.user;
|
|
@@ -28,7 +28,78 @@
|
|
|
28
28
|
* `src/core/common/**` and from `src/core/modules/**` alike without adding an import edge between
|
|
29
29
|
* them. See `.claude/rules/architecture.md` → "DI Token Placement (SWC-Safe)".
|
|
30
30
|
*/
|
|
31
|
-
import type { ExecutionContext } from '@nestjs/common';
|
|
31
|
+
import type { ArgumentsHost, ExecutionContext } from '@nestjs/common';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The execution context types of an agent function call (`@nestjs/ai`, `'ai'`) and of an MCP primitive call
|
|
35
|
+
* (`@nestjs/mcp`, `'mcp'`). nestjs-ai plan §1.
|
|
36
|
+
*
|
|
37
|
+
* Both packages wrap a method with Nest's `ExternalContextCreator`, so EVERY global guard, interceptor and filter of
|
|
38
|
+
* the application runs around it — ours included, in any project that installs either package, whether or not it uses
|
|
39
|
+
* the framework's own AI module. Their arguments are `[input, call]`:
|
|
40
|
+
*
|
|
41
|
+
* | Index | `'ai'` (`AgentFunctionContext`) | `'mcp'` (`McpContext`) | Trust |
|
|
42
|
+
* |-------|---------------------------------|------------------------|-------|
|
|
43
|
+
* | 0 | the tool arguments the MODEL wrote | the tool arguments the MCP CLIENT sent | none — attacker-controlled |
|
|
44
|
+
* | 1 | built by `@nestjs/ai` from the call's `caller` (`request`, `user`, `scopes`) | built by `@nestjs/mcp` (`request`, `authInfo`) | server code |
|
|
45
|
+
*
|
|
46
|
+
* Until this fix every guard fell back to `switchToHttp().getRequest()` on such a context — which is index 0, the
|
|
47
|
+
* MODEL's arguments. A tool call `{ "user": { "roles": ["admin"] } }` was then the request a guard decided on. The
|
|
48
|
+
* resolvers below are duck-typed against that argument layout on purpose: `src/core/common/` imports neither package,
|
|
49
|
+
* so a project without them loads nothing new.
|
|
50
|
+
*/
|
|
51
|
+
export const AGENT_CALL_CONTEXT_TYPES: ReadonlySet<string> = new Set(['ai', 'mcp']);
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The second argument of an agent or MCP call, as far as the framework reads it. Everything else on it belongs to the
|
|
55
|
+
* package that built it.
|
|
56
|
+
*/
|
|
57
|
+
export interface AgentCallLike {
|
|
58
|
+
/** The agent function's / tool's name. */
|
|
59
|
+
name?: string;
|
|
60
|
+
/** The HTTP platform's request the call runs in; `undefined` for a queue job, a durable run, stdio. */
|
|
61
|
+
request?: GuardRequestLike;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Whether `context` is an agent function call (`'ai'`) or an MCP primitive call (`'mcp'`). */
|
|
65
|
+
export function isAgentCallContext(context: ArgumentsHost | undefined): boolean {
|
|
66
|
+
let type: unknown;
|
|
67
|
+
try {
|
|
68
|
+
type = context?.getType?.();
|
|
69
|
+
} catch {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
return typeof type === 'string' && AGENT_CALL_CONTEXT_TYPES.has(type);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The call object (`args[1]`) of an agent or MCP call; `undefined` for any other context. */
|
|
76
|
+
export function agentCallOf(context: ArgumentsHost | undefined): AgentCallLike | undefined {
|
|
77
|
+
if (!isAgentCallContext(context)) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
const call = context.getArgByIndex(1);
|
|
81
|
+
return call && typeof call === 'object' ? (call as AgentCallLike) : undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The request an agent or MCP call runs in — never its arguments.
|
|
86
|
+
*
|
|
87
|
+
* `undefined` for a REQUEST-LESS caller: `@nestjs/ai`'s `caller: { user, scopes }` (a queue job, a durable run) or MCP
|
|
88
|
+
* over stdio. The framework treats such a caller as ANONYMOUS : its
|
|
89
|
+
* `caller.user` is data the calling code put there — a job payload read back from Redis, a workflow's stored input —
|
|
90
|
+
* that nothing authenticated, and the tenant scope of the data layer (`RequestContext`) has no request to come from.
|
|
91
|
+
* So a role-gated agent function or tool refuses it with 401; one open to `S_EVERYONE` runs. A project that runs a
|
|
92
|
+
* role-gated agent function from a job authenticates first and passes the request (`caller: { request }`).
|
|
93
|
+
*
|
|
94
|
+
*/
|
|
95
|
+
export function resolveAgentCallRequest(context: ArgumentsHost | undefined): GuardRequestLike | undefined {
|
|
96
|
+
const call = agentCallOf(context);
|
|
97
|
+
const request = call?.request;
|
|
98
|
+
if (!request || typeof request !== 'object') {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
return request;
|
|
102
|
+
}
|
|
32
103
|
|
|
33
104
|
/**
|
|
34
105
|
* The shape a guard reads and writes. It is deliberately loose: on HTTP this IS the Express request,
|
|
@@ -61,6 +132,8 @@ function isSubscriptionContext(value: any): boolean {
|
|
|
61
132
|
* Resolve the object a guard should decide on.
|
|
62
133
|
*
|
|
63
134
|
* Order matters:
|
|
135
|
+
* 0. An agent function or MCP call (`'ai'` / `'mcp'`): the request the call runs in, or `undefined` — never the
|
|
136
|
+
* call's arguments, which the HTTP fallback below would return (see {@link resolveAgentCallRequest}).
|
|
64
137
|
* 1. `ctx.getContext().req` — HTTP GraphQL, and the shape every existing consumer expects.
|
|
65
138
|
* 2. The graphql-ws subscription context itself, when it looks like one (see above).
|
|
66
139
|
* 3. `switchToHttp().getRequest()` — REST controllers. On a GraphQL context this returns the
|
|
@@ -73,6 +146,10 @@ export function resolveGuardRequest(
|
|
|
73
146
|
context: ExecutionContext,
|
|
74
147
|
gqlContextOf: (context: ExecutionContext) => any,
|
|
75
148
|
): GuardRequestLike | undefined {
|
|
149
|
+
if (isAgentCallContext(context)) {
|
|
150
|
+
return resolveAgentCallRequest(context);
|
|
151
|
+
}
|
|
152
|
+
|
|
76
153
|
let gqlContext: any;
|
|
77
154
|
try {
|
|
78
155
|
gqlContext = gqlContextOf(context);
|