@lenne.tech/nest-server 12.6.12 → 12.6.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/.claude/rules/configurable-features.md +1 -1
  2. package/.claude/rules/package-management.md +7 -7
  3. package/.claude/rules/role-system.md +10 -0
  4. package/.claude/rules/testing.md +4 -4
  5. package/FRAMEWORK-API.md +1 -1
  6. package/dist/core/common/decorators/rest-user.decorator.js +4 -0
  7. package/dist/core/common/decorators/rest-user.decorator.js.map +1 -1
  8. package/dist/core/common/filters/http-exception-log.filter.js +4 -0
  9. package/dist/core/common/filters/http-exception-log.filter.js.map +1 -1
  10. package/dist/core/common/helpers/context.helper.js +9 -0
  11. package/dist/core/common/helpers/context.helper.js.map +1 -1
  12. package/dist/core/common/helpers/decorator.helper.js +4 -0
  13. package/dist/core/common/helpers/decorator.helper.js.map +1 -1
  14. package/dist/core/common/helpers/execution-context-request.helper.d.ts +9 -1
  15. package/dist/core/common/helpers/execution-context-request.helper.js +29 -0
  16. package/dist/core/common/helpers/execution-context-request.helper.js.map +1 -1
  17. package/dist/core/common/helpers/graphql-upload.helper.d.ts +10 -0
  18. package/dist/core/common/helpers/graphql-upload.helper.js +237 -0
  19. package/dist/core/common/helpers/graphql-upload.helper.js.map +1 -0
  20. package/dist/core/common/helpers/redact-secrets.helper.js +7 -2
  21. package/dist/core/common/helpers/redact-secrets.helper.js.map +1 -1
  22. package/dist/core/common/interceptors/translate-response.interceptor.js +4 -0
  23. package/dist/core/common/interceptors/translate-response.interceptor.js.map +1 -1
  24. package/dist/core/common/plugins/complexity.plugin.js +73 -9
  25. package/dist/core/common/plugins/complexity.plugin.js.map +1 -1
  26. package/dist/core/modules/auth/guards/auth.guard.js +10 -2
  27. package/dist/core/modules/auth/guards/auth.guard.js.map +1 -1
  28. package/dist/core/modules/auth/guards/legacy-auth-rate-limit.guard.js +4 -1
  29. package/dist/core/modules/auth/guards/legacy-auth-rate-limit.guard.js.map +1 -1
  30. package/dist/core/modules/auth/guards/roles.guard.js +11 -1
  31. package/dist/core/modules/auth/guards/roles.guard.js.map +1 -1
  32. package/dist/core/modules/better-auth/better-auth-roles.guard.js +15 -1
  33. package/dist/core/modules/better-auth/better-auth-roles.guard.js.map +1 -1
  34. package/dist/core/modules/tenant/core-tenant.guard.js +9 -1
  35. package/dist/core/modules/tenant/core-tenant.guard.js.map +1 -1
  36. package/dist/core.module.js +2 -1
  37. package/dist/core.module.js.map +1 -1
  38. package/dist/index.d.ts +1 -0
  39. package/dist/index.js +1 -0
  40. package/dist/index.js.map +1 -1
  41. package/dist/types/graphql-upload.d.ts +34 -2
  42. package/docs/REQUEST-LIFECYCLE.md +33 -9
  43. package/docs/security-overrides.md +3 -2
  44. package/migration-guides/12.6.12-to-12.6.13.md +162 -0
  45. package/migration-guides/12.6.13-to-12.6.14.md +143 -0
  46. package/package.json +10 -3
  47. package/src/core/common/decorators/rest-user.decorator.ts +6 -0
  48. package/src/core/common/filters/http-exception-log.filter.ts +9 -0
  49. package/src/core/common/helpers/context.helper.ts +14 -0
  50. package/src/core/common/helpers/decorator.helper.ts +5 -0
  51. package/src/core/common/helpers/execution-context-request.helper.ts +78 -1
  52. package/src/core/common/helpers/graphql-upload.helper.ts +520 -0
  53. package/src/core/common/helpers/redact-secrets.helper.ts +22 -3
  54. package/src/core/common/interceptors/translate-response.interceptor.ts +8 -0
  55. package/src/core/common/plugins/complexity.plugin.ts +150 -9
  56. package/src/core/modules/auth/guards/auth.guard.ts +19 -2
  57. package/src/core/modules/auth/guards/legacy-auth-rate-limit.guard.ts +9 -2
  58. package/src/core/modules/auth/guards/roles.guard.ts +23 -1
  59. package/src/core/modules/better-auth/better-auth-roles.guard.ts +29 -1
  60. package/src/core/modules/tenant/core-tenant.guard.ts +13 -1
  61. package/src/core.module.ts +5 -1
  62. package/src/index.ts +1 -0
  63. package/src/types/graphql-upload.d.ts +34 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenne.tech/nest-server",
3
- "version": "12.6.12",
3
+ "version": "12.6.14",
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.3",
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.5",
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);