|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Disallowed Fields in API Routes`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you'll learn about disallowed fields in API routes, how to apply them to custom API routes, and how to apply them to Medusa's API routes. |
|
8
|
|
|
9
|
<Note> |
|
10
|
|
|
11
|
The `disallowed` query configuration is available since Medusa [v2.18.0](https://github.com/medusajs/medusa/releases/tag/v2.18.0). |
|
12
|
|
|
13
|
</Note> |
|
14
|
|
|
15
|
## What are Disallowed Fields? |
|
16
|
|
|
17
|
An API route can block specific field path segments as a hard security boundary. Medusa removes these fields from the query before it executes, regardless of what the client requests. |
|
18
|
|
|
19
|
Use disallowed fields to guard internal or sensitive relations on a route, such as stock locations, price sets, or a custom link. This prevents a client from pivoting from a public resource into private data. |
|
20
|
|
|
21
|
For example, if a custom API route blocks the `campaign` segment, a request to `GET /store/custom?fields=promotions.campaign.budget` returns the data without the `campaign` relation, even if the client explicitly requests it. |
|
22
|
|
|
23
|
<Note title="Important"> |
|
24
|
|
|
25
|
Medusa's own API routes don't set disallowed fields. They restrict what a client can retrieve through their [allowed fields](../allowed-fields/page.mdx) instead. A field that isn't in a route's `allowed` array never reaches the query, so the allowed list is the boundary that protects Medusa's routes. |
|
26
|
|
|
27
|
Before [Medusa v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0), Medusa's store routes applied a disallowed list that blocked any field path segment ending in `_link` (matched by `/_link$/`). This prevented clients from pivoting through custom link models on routes such as `/store/carts`, `/store/orders`, and `/store/customers/me`. Starting from v2.21.0, Medusa replaced this approach with strict allowed lists on all store routes, so custom link fields must now be explicitly added to a route's allowed list using [middleware](../allowed-fields/page.mdx#how-to-override-allowed-fields-of-medusas-api-routes) to be accessible. |
|
28
|
|
|
29
|
</Note> |
|
30
|
|
|
31
|
Disallowed fields are different than [allowed fields](../allowed-fields/page.mdx): |
|
32
|
|
|
33
|
- Allowed fields are an opt-in list of the fields a route can retrieve. Medusa applies them to its API routes to keep them performant and secure. |
|
34
|
- Disallowed fields are a deny list that Medusa always enforces, even if the field is in the allowed list, and even if the client requests it explicitly. Since v2.21.0, Medusa doesn't apply them to its own API routes, so you set them on custom routes that need them. |
|
35
|
|
|
36
|
--- |
|
37
|
|
|
38
|
## How to Disallow Fields in Custom API Routes? |
|
39
|
|
|
40
|
To block field path segments in a custom API route, pass a `disallowed` array to the `validateAndTransformQuery` configuration: |
|
41
|
|
|
42
|
export const disallowHighlights = [ |
|
43
|
["18", "disallowed", "The field path segments to always strip from the query."], |
|
44
|
] |
|
45
|
|
|
46
|
```ts title="src/api/middlewares.ts" highlights={disallowHighlights} |
|
47
|
import { |
|
48
|
validateAndTransformQuery, |
|
49
|
defineMiddlewares, |
|
50
|
} from "@medusajs/framework/http" |
|
51
|
import { createFindParams } from "@medusajs/medusa/api/utils/validators" |
|
52
|
|
|
53
|
export default defineMiddlewares({ |
|
54
|
routes: [ |
|
55
|
{ |
|
56
|
matcher: "/store/custom", |
|
57
|
method: "GET", |
|
58
|
middlewares: [ |
|
59
|
validateAndTransformQuery( |
|
60
|
createFindParams(), |
|
61
|
{ |
|
62
|
defaults: ["id", "title"], |
|
63
|
isList: true, |
|
64
|
disallowed: ["orders", "customer"], |
|
65
|
} |
|
66
|
), |
|
67
|
], |
|
68
|
}, |
|
69
|
], |
|
70
|
}) |
|
71
|
``` |
|
72
|
|
|
73
|
Medusa strips any field path that contains `orders` or `customer` as a segment from the query before it executes. |
|
74
|
|
|
75
|
Each entry of the `disallowed` array is either: |
|
76
|
|
|
77
|
- A string, which Medusa matches against a whole field path segment. So, `orders` also blocks `orders.customer.email`. |
|
78
|
- A regular expression, which Medusa tests against each segment, such as `/_link$/`, and against the full dotted path. This allows you to block a relation at a specific position. For example, `/\.orders(?:\.|$)/` blocks `orders` everywhere but at the root. |
|
79
|
|
|
80
|
<Note title="Warning" type="warning"> |
|
81
|
|
|
82
|
A disallowed array isn't a substitute for an `allowed` array. A deny list only blocks the segments you thought of, whereas an allowed list blocks everything you didn't list, including relations that you add to the data model later. Set `allowed` on your custom API routes, and use `disallowed` to block a segment that the allowed list would otherwise let through. |
|
83
|
|
|
84
|
</Note> |
|
85
|
|
|
86
|
--- |
|
87
|
|
|
88
|
## How to Disallow Fields in Medusa's API Routes? |
|
89
|
|
|
90
|
<Note> |
|
91
|
|
|
92
|
Setting disallowed fields on a request is available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). |
|
93
|
|
|
94
|
</Note> |
|
95
|
|
|
96
|
Medusa's API routes don't set disallowed fields. If you [add a field to a route's allowed list](../allowed-fields/page.mdx#how-to-override-allowed-fields-of-medusas-api-routes) and want to block a segment under it, apply a [global middleware](../middlewares/page.mdx) that sets the `disallowed` property of the request object. |
|
97
|
|
|
98
|
For example, to block the `payment_collection` segment on the [List Products Store API Route](!api!/store/products/list-products), create the file `src/api/middlewares.ts` with the following content: |
|
99
|
|
|
100
|
export const highlights = [ |
|
101
|
["9", "req.disallowed", "Block these segments for this request."], |
|
102
|
] |
|
103
|
|
|
104
|
```ts title="src/api/middlewares.ts" highlights={highlights} |
|
105
|
import { defineMiddlewares } from "@medusajs/framework/http" |
|
106
|
|
|
107
|
export default defineMiddlewares({ |
|
108
|
routes: [ |
|
109
|
{ |
|
110
|
matcher: "/store/products", |
|
111
|
middlewares: [ |
|
112
|
(req, res, next) => { |
|
113
|
req.disallowed = ["payment_collection"] |
|
114
|
next() |
|
115
|
}, |
|
116
|
], |
|
117
|
}, |
|
118
|
], |
|
119
|
}) |
|
120
|
``` |
|
121
|
|
|
122
|
Medusa strips any field path that contains `payment_collection` as a segment, even if the path is in the route's allowed list. |
|
123
|
|
|
124
|
The property replaces the route's configured disallowed fields rather than adding to them. Medusa's API routes configure none, so the array you set is the full deny list for the request. On a custom API route that configures `disallowed`, include the segments you still want to block: |
|
125
|
|
|
126
|
```ts title="src/api/middlewares.ts" |
|
127
|
req.disallowed = [ |
|
128
|
"orders", |
|
129
|
"customer", |
|
130
|
"payment_collection", |
|
131
|
] |
|
132
|
``` |
|
133
|
|
|
134
|
<Note title="Important"> |
|
135
|
|
|
136
|
This approach only works using a global middleware, since Medusa applies the disallowed fields in a route middleware. It also only applies to the request that the middleware handles, so it never leaks into other requests. |
|
137
|
|
|
138
|
</Note> |