Skip to content

Commit 904f8c1

Browse files
authored
[Strapi 5] Breaking change for default input validation (#2096)
* Update controllers documentation * Create breaking change entry
1 parent 8cb3fa8 commit 904f8c1

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

docusaurus/docs/dev-docs/backend-customization/controllers.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,17 @@ To see a possible advanced usage for custom controllers, read the [services and
225225
It's strongly recommended you sanitize (v4.8.0+) and/or validate (v4.13.0+) your incoming request query utilizing the new `sanitizeQuery` and `validateQuery` functions to prevent the leaking of private data.
226226
:::
227227

228+
Sanitization means that the object is “cleaned” and returned.
229+
230+
Validation means an assertion is made that the data is already clean and throws an error if something is found that shouldn't be there.
231+
232+
In Strapi 5, both query parameters and input data (i.e., create and update body data) are validated. Any create and update data requests with the following invalid input will throw a `400 Bad Request` error:
233+
234+
- relations the user do not have permission to create
235+
- unrecognized values that are not present on a schema
236+
- non-writable fields and internal timestamps like `createdAt` and `createdBy` fields
237+
- the `id` field (other than for connecting relations) which attempts to set or update the `id` of an object
238+
228239
#### Sanitization when utilizing controller factories
229240

230241
Within the Strapi factories the following functions are exposed that can be used for sanitization and validation:

docusaurus/docs/dev-docs/migration/v4-to-v5/breaking-changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ This page lists all the breaking changes introduced in Strapi 5.
3939
<!-- * [Components and dynamic zones do not return an `id` with REST API requests](/dev-docs/migration/v4-to-v5/breaking-changes/components-and-dynamic-zones-do-not-return-id) not implemented yet -->
4040
* [The GraphQL API has been updated](/dev-docs/migration/v4-to-v5/breaking-changes/graphql-api-updated)
4141
* [The Entity Service API is deprecated and replaced by the Document Service API](/dev-docs/migration/v4-to-v5/breaking-changes/entity-service-deprecated)
42+
* [REST API input is validated by default in controllers](/dev-docs/migration/v4-to-v5/breaking-changes/default-input-validation)
4243

4344
## Database
4445

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: REST API input is validated by default in controllers
3+
description: In Strapi 5, REST API input is validated by default in controllers, instead of accepting invalid data and sanitizing it silently.
4+
sidebar_label: Default input validation
5+
displayed_sidebar: devDocsMigrationV5Sidebar
6+
tags:
7+
- breaking changes
8+
- controllers
9+
- validation
10+
---
11+
12+
import Intro from '/docs/snippets/breaking-change-page-intro.md'
13+
import MigrationIntro from '/docs/snippets/breaking-change-page-migration-intro.md'
14+
import YesPlugins from '/docs/snippets/breaking-change-affecting-plugins.md'
15+
import NoCodemods from '/docs/snippets/breaking-change-not-handled-by-codemod.md'
16+
17+
# REST API input is validated by default in controllers
18+
19+
Sanitization means that the object is “cleaned” and returned.
20+
21+
Validation means an assertion is made that the data is already clean and throws an error if something is found that shouldn't be there.
22+
23+
Strapi methods exist both for [sanitization and validation in controllers](/dev-docs/backend-customization/controllers#sanitization-and-validation-in-controllers) and they can target input body data, query parameters, and output (only for sanitization).
24+
25+
In Strapi 5, REST API input is validated by default in controllers, instead of accepting invalid data and sanitizing it silently.
26+
27+
<Intro />
28+
29+
<YesPlugins />
30+
<NoCodemods />
31+
32+
## Breaking change description
33+
34+
<SideBySideContainer>
35+
36+
<SideBySideColumn>
37+
38+
**In Strapi v4**
39+
40+
In v4, query parameters are validated, but input data (create and update body data) is only sanitized.
41+
42+
</SideBySideColumn>
43+
44+
<SideBySideColumn>
45+
46+
**In Strapi 5**
47+
48+
In v5, both query parameters and input data are validated.
49+
50+
</SideBySideColumn>
51+
52+
</SideBySideContainer>
53+
54+
## Migration
55+
56+
<MigrationIntro />
57+
58+
### Notes
59+
60+
* A `400 Bad Request` error will be thrown if the request has invalid values such as in in the following cases:
61+
62+
- relations the user do not have permission to create
63+
- unrecognized values that are not present on a schema
64+
- attempt to writing non-writable fields and internal timestamps like `createdAt` and `createdBy` fields
65+
- usage of the `id` field (other than for connecting relations) to set or update the `id` of an object
66+
67+
### Manual procedure
68+
69+
Users should ensure that parameters and input data are valid to avoid `400` errors being thrown. Additional information can be found in the [sanitization and validation in controllers](/dev-docs/backend-customization/controllers#sanitization-and-validation-in-controllers) documentation.

docusaurus/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,7 @@ const sidebars = {
11581158
// 'dev-docs/migration/v4-to-v5/breaking-changes/components-and-dynamic-zones-do-not-return-id', // not implemented yet
11591159
'dev-docs/migration/v4-to-v5/breaking-changes/graphql-api-updated',
11601160
'dev-docs/migration/v4-to-v5/breaking-changes/entity-service-deprecated',
1161+
'dev-docs/migration/v4-to-v5/breaking-changes/default-input-validation',
11611162
]
11621163
},
11631164
{

0 commit comments

Comments
 (0)