JSON:API: request documents with no `data`, no `type` or a wrong `type` are accepted with 201
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start in src/JsonApi/Serializer, where JsonApi\Serializer\ItemNormalizerTrait::denormalize() reads $data['data']['attributes'] ?? [] and $data['data']['relationships'] ?? [] without checking the document shape. Reproduce with the request bodies in the table (for example "Y", {"title":"A"} and a mismatched type) against a POST /novels operation. Done means malformed documents return 400 Bad Request, and a POST whose type does not match the endpoint returns 409 Conflict, with the existing JSON:API tests still passing.
由索引模型根据 Issue 内容生成。
描述
API Platform version(s) affected
4.4.4
Description
The JSON:API denormalizer does not check the structure of the request document. A body that is not a valid JSON:API document is accepted, and the resource is created with its default values.
The spec requires:
- the top level of a request document is an object with a
datamember (document structure), datais a resource object with atypemember, andattributesis an object (resource objects),- "A server MUST return
409 Conflictwhen processing aPOSTrequest in which the resource object'stypeis not among the type(s) that constitute the collection represented by the endpoint" (creating resources).
JsonApi\Serializer\ItemNormalizerTrait::denormalize() reads $data['data']['attributes'] ?? [] and $data['data']['relationships'] ?? [], and it does not check the other members.
How to reproduce
#[ApiResource(operations: [
new Get(provider: [self::class, 'provide']),
new Post(processor: [self::class, 'process']),
])]
final class Novel
{
public int $id = 1;
public string $title = 'Dune';
public static function provide(): self { return new self(); }
public static function process(self $data, Operation $operation): self { return $data; }
}
Each of these POST /novels bodies answers 201 Created:
| Body | Problem |
|---|---|
"Y" |
not an object |
42 |
not an object |
{"title":"A"} |
no data member |
{"data":"x"} |
data is not a resource object |
{"data":{"type":"Novel","attributes":["x"]}} |
attributes is not an object |
{"data":{"attributes":{"title":"A"}}} |
no type member |
{"data":{"type":"Author","attributes":{"title":"A"}}} |
type does not match the endpoint |
Response for "Y":
{"data":{"id":"/novels/1","type":"Novel","attributes":{"_id":1,"title":"Dune"}}}
A validation constraint on a required property hides the problem in some cases, but a resource with optional properties or defaults is created from any body.
Expected:
- A body that is not a JSON:API document, or has no
dataobject, notypeor a non-objectattributes/relationships:400 Bad Request. - A
typethat does not match the resource onPOST:409 Conflict.
Additional Context
Checked on api-platform/core v4.4.4, Symfony 7.4, PHP 8.5, in an empty Symfony app. The related code in src/JsonApi/Serializer is the same on main, except for the error status cast (#8366).
The app has only FrameworkBundle, TwigBundle and ApiPlatformBundle, with this config:
$c->extension('api_platform', [
'mapping' => ['paths' => ['%kernel.project_dir%/src/ApiResource']],
'formats' => ['jsonapi' => ['application/vnd.api+json']],
'error_formats' => ['jsonapi' => ['application/vnd.api+json'], 'jsonproblem' => ['application/problem+json']],
'doctrine' => ['enabled' => false],
]);
All requests send Accept: application/vnd.api+json and, when they have a body, Content-Type: application/vnd.api+json. The responses are from the prod environment.
- 主要语言
- PHP
- 星标
- 2.6k
- 派生
- 988
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 108
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
api-platform/core 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
api-platform/core#8744 ·
维护者通常 1 天内回复
-
JSON:API: resource schemas (`*.jsonapi`) have `properties` but no `type: object`可能已有人在做 @ousamabenyounes 于 5 天前认领。 未关闭
难度 2/5 1 小时以内 新手友好度 80/100
api-platform/core#8701 ·
维护者通常 1 天内回复
-
[Json-Schema] Nested resources lost externalDoc and description data可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 3/5 1-2 天 新手友好度 68/100
api-platform/core#8721 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 50/100
api-platform/core#8704 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 8/100
api-platform/core#8697 ·
维护者通常 1 天内回复
查看 api-platform/core 的全部 Issue
相似的 Issue
-
needs-triage Technical TODO
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 64/100
Piwigo/piwigo-docker#19 · 1 个 reaction ·
-
[Store][SurrealDb] add(), query() and remove() fail with 403 on a fresh store instance可能已有人在做 @lazerg 今天认领。 未关闭Bug Status: Needs Review Store
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 63/100
-
sync-en
难度 1/5 1 小时以内 新手友好度 86/100
维护者通常 1 天内回复