|
1
|
--- |
|
2
|
sidebar_label: "allow-fields-must-be-global-middleware" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `allow-fields-must-be-global-middleware - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule requires that `allowFields`, `req.allowed`, and `req.disallowed` are only set in a global middleware, not in a method-scoped one. |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
This rule is available since [Medusa v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). |
|
16
|
|
|
17
|
</Note> |
|
18
|
|
|
19
|
## Severity |
|
20
|
|
|
21
|
`error`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset. |
|
22
|
|
|
23
|
## What it Targets |
|
24
|
|
|
25
|
This rule targets the `src/api/middlewares.ts` file. It reports any middleware that uses `allowFields`, writes to `req.allowed`, or assigns `req.disallowed` when the route object includes a `method` or `methods` property. |
|
26
|
|
|
27
|
Medusa validates the request's query parameters before it runs method-scoped middlewares, so setting allowed or disallowed fields in a method-scoped middleware has no effect. |
|
28
|
|
|
29
|
The following code is reported by the rule: |
|
30
|
|
|
31
|
```ts title="src/api/middlewares.ts" |
|
32
|
import { allowFields, defineMiddlewares } from "@medusajs/framework/http" |
|
33
|
|
|
34
|
export default defineMiddlewares({ |
|
35
|
routes: [ |
|
36
|
{ |
|
37
|
matcher: "/store/products", |
|
38
|
method: "GET", |
|
39
|
middlewares: [allowFields("brand")], |
|
40
|
}, |
|
41
|
], |
|
42
|
}) |
|
43
|
``` |
|
44
|
|
|
45
|
Instead, remove the `method` property so the middleware applies to every request that matches the route: |
|
46
|
|
|
47
|
```ts title="src/api/middlewares.ts" |
|
48
|
import { allowFields, defineMiddlewares } from "@medusajs/framework/http" |
|
49
|
|
|
50
|
export default defineMiddlewares({ |
|
51
|
routes: [ |
|
52
|
{ |
|
53
|
matcher: "/store/products", |
|
54
|
middlewares: [allowFields("brand")], |
|
55
|
}, |
|
56
|
], |
|
57
|
}) |
|
58
|
``` |
|
59
|
|
|
60
|
The same applies when writing to `req.allowed` or `req.disallowed` directly: |
|
61
|
|
|
62
|
```ts title="src/api/middlewares.ts" |
|
63
|
// non-compliant — method-scoped |
|
64
|
export default defineMiddlewares({ |
|
65
|
routes: [ |
|
66
|
{ |
|
67
|
matcher: "/store/products", |
|
68
|
method: "GET", |
|
69
|
middlewares: [ |
|
70
|
(req, res, next) => { |
|
71
|
req.allowed.push("brand") |
|
72
|
next() |
|
73
|
}, |
|
74
|
], |
|
75
|
}, |
|
76
|
], |
|
77
|
}) |
|
78
|
|
|
79
|
// compliant — global middleware |
|
80
|
export default defineMiddlewares({ |
|
81
|
routes: [ |
|
82
|
{ |
|
83
|
matcher: "/store/products", |
|
84
|
middlewares: [ |
|
85
|
(req, res, next) => { |
|
86
|
req.allowed.push("brand") |
|
87
|
next() |
|
88
|
}, |
|
89
|
], |
|
90
|
}, |
|
91
|
], |
|
92
|
}) |
|
93
|
``` |
|
94
|
|
|
95
|
## Why it's Important |
|
96
|
|
|
97
|
Medusa runs query-parameter validation before method-scoped middlewares execute. By the time a method-scoped middleware runs, the allowed or disallowed field lists are already applied, so any changes the middleware makes are ignored silently. To actually expand or restrict the retrievable fields, the middleware must be global (no `method` or `methods` property on the route). |
|
98
|
|
|
99
|
Learn more in the [Allowed Fields in API Routes documentation](!docs!/learn/fundamentals/api-routes/allowed-fields). |
|
100
|
|
|
101
|
## Fixable |
|
102
|
|
|
103
|
This rule isn't auto-fixable. |