After upgrading jsonapi to 8.x-1.10 in response the security advisory, I am now getting the following message returned when trying to create a file via the api:
{
"errors": [
{
"title": "Internal Server Error",
"status": 500,
"detail": "Field data is unknown.",
"links": {
"info": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1"
},
"code": 0
}
]
}The request body is:
{
"data": {
"type": "file--image",
"attributes": {
"data": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAACgKADAAQAAAABAAACgAAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+
........... and so on for a Base64 encoded image file ....................................
/5dc0faR1PbvTsxNos4wME8AelMPXCgD2qIXIznPWkEvPJot3F1Jc7CSe1NY5wMlsdqb5ifWmq6EfhTSG3Ycfcc0wkbMEnnOKN3PFHXHPfJGKa0FcCMDODjHQU0Nj/ClY5OMcetMOAMY69aaFtqf/2Q==",
"uri": "public://2018-02/green-client/test-image-10a.jpg"
}
}
}
I get the same result when trying to save the image to the "private://" file system.
This exact same request works fine with v8.x-1.7 (when patched with #2883086-22 for uploads to the private file system).
A brief history:
v8.x-1.7 would save a file to the public file system just fine (using the above request). However, attempts to save to the private file system would give the response:
{
"errors": [
{
"title": "Internal Server Error",
"status": 500,
"detail": "The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\\jsonapi\\ResourceResponse.",
"links": {
"info": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1"
},
"code": 0
}
]
}Nevertheless, the patch mentioned previously, solved that problem.
v8.x-1.8 will not save successfully to the public file system. A file object is created and recorded in the DB, and the response data is as expected, but there is no content on disc. Saving to the "private://" file system with 8.x-1.8 results in the 500 error above, but this time the file object is created in the database, and still no content on disc.
v8.x-1.9 gives the 500 error response "Field data is unknown" for both public and private file system saves. This is the same behaviour as 8.x-1.10.
| Comment | File | Size | Author |
|---|---|---|---|
| #29 | 2946746-29.patch | 7.29 KB | wim leers |
| #29 | interdiff.txt | 1 KB | wim leers |
| #26 | 2946746-26.patch | 6.33 KB | wim leers |
| #26 | interdiff.txt | 1.56 KB | wim leers |
| #22 | 2946746-22.patch | 6.28 KB | wim leers |
Comments
Comment #2
gabesulliceHi @jlscott, thanks for the detailed bug report! I LOVE when we get reports as clearcut as yours :) (also, good for you for staying on top of those SAs so quickly!)
Given that you were previously on a patched version of the module and the error only presents itself when using JSON API File, I thing I need to close this issue as a duplicate of #2883086: [PP-1] Port RequestHandler + ResourceResponseSubscriber improvements from REST module to JSON API. I will make a note of this bug report in that issue. It sounds like that patch just needs to be "rerolled" for the latest JSON API version.
If that turns out not to be the case, then I would recommend filing an issue with JSON API File.
Or... if you think I'm totally crazy and this really is a separate, new and specific issue to the main JSON API module, feel free to reopen this with your reasoning :)
Edit: I've also opened an issue with JSON API File here: #2946793: Broken with latest version of JSON API (1.10)
Comment #3
jayelless commentedThanks @gabesullice. I wasn't sure whether to attach the report to #2883086: Port RequestHandler + ResourceResponse(Interface) improvements from REST module to JSON API, or create a new issue, so I appreciate your clarifying this.
Comment #4
jayelless commentedComment #5
chihada commentedI have the same problem
I have tested the following patch: https://www.drupal.org/files/issues/2018-03-20/2883086-54-combined.patch comes on the issue: https://www.drupal.org/project/jsonapi/issues/2883086
but it's not work with 8.x-14 version of jsonapi module
Comment #6
e0ipsoComment #7
chihada commentedI have the same problem
I applied the patch 2883086-54-combined.patch coming from #2883086: Port RequestHandler + ResourceResponse(Interface) improvements from REST module to JSON API but the problem did not solved
I am using Acquia Lightning 3.0.3 release
Comment #8
Moniroaf commentedI have the same problem I've applied the patch
2883086-54-combined.patch
coming from
#2883086: Port RequestHandler + ResourceResponse(Interface) improvements from REST module to JSON API but the problem did not solved
I am using Acquia Lightning 3.0.3 release
Thanks
Comment #9
pwolanin commentedThis is still a bug in jsonapi that happens when data is posted with an attribute that is not a valid entity field
Comment #10
pwolanin commentedHere's a trivial patch that resolves the 500 error at least.
Comment #11
gabesulliceThanks @pwolanin! This is a really good catch.
It looks like this is also possible for PATCH as well.
I'm not sure that we want to simply ignore the field. I need to look at the spec. @e0ipso, do you have thoughts? Maybe this should be an InvalidEntityException?
Comment #12
gabesulliceOkay, took a quick look at the spec. I see nothing about handling extra fields. My gut reaction is that this needs to be a
422 Unprocessable Entityto be in line with entity validation. We can't say the request was a "success" if we weren't able to save certain attributes. I think that would be in line with this part of the spec:Comment #13
pwolanin commentedSeems to be a long,open debate within jsonapi: https://github.com/json-api/json-api/issues/246
Comment #14
e0ipsoYeah, a 422 with a nice error code is the desired outcome.
Comment #15
wim leers422++ — otherwise incorrect requests are treated as perfectly okay. And if that non-existing field is then added in the future, all clients would suddenly break.
Comment #16
sandboxplPossible workaround would be to add computed fields to certain entity type,
but I guess this shouldn't be a part of jsonapi module, but rather a project-specific piece of code,
example for jsonapi_file module:
https://www.drupal.org/project/jsonapi_file/issues/2946793#comment-12566772
Comment #17
jayelless commentedAfter updating the module jsonapi_file with patch #6 from #2946793-Broken with latest version of JSON API (1.10) I am getting returned responses of
This can be solved by making the class \Drupal\jsonapi\Resourceresponse a subclass of \Drupal\Core\Ajax\AjaxResponse instead of being a subclass of Symfony\Component\HttpFoundation\Response. Note that Ajaxresponse is itself a subclass of the symfony Response class.
I have not encountered any problems with this change of class, but someone with more knowledge of the possible side-effects should review this.
Updated patch attached.
Comment #18
wim leers#16:
I don't understand how this would solve the problem? You can't add every unknown field name to a certain entity type?
#17: Thanks for the patch, but that's the wrong solution. The problem lies in the
jsonapi_filemodule, not in thejsonapimodule.Comment #19
sandboxplThat's why I also wrote that "I guess this shouldn't be a part of jsonapi module, but rather a project-specific piece of code". When developer knows which data he expects in source, sorry for confusion, looks like my comments shouldn't arrive here, but rather be sticked to jsonapi_file issues
Comment #20
jayelless commented@Wim Leers. You are correct, in that I have found a solution after making a change in jsonapi_file. The offending metadata causing the error message in #17 was "url.site" so I looked at possible sources of that.
The denormalize function was setting the file->url->value to an empty string to avoid file validation errors. After removing this action, the file still validated without problems, and the metadata problem disappeared.
I have withdrawn the patch I submitted in #17.
Cheers.
Comment #21
wim leersOk, so #17–#20 were off-topic/out-of-scope, they were about https://www.drupal.org/project/jsonapi_file.
Let's get this issue back on track: the patch in #10, discussion in #11–#15.
Turns out JSON API already had something like this in a few places:
\Drupal\jsonapi\Controller\EntityResource::validateReferencedResource()whenGETting relationships\Drupal\jsonapi\Controller\EntityResource::updateEntityField()was already detecting non-existent fields, but it was sending a 400 response.Related: #2942549: Spec Compliance: JSON API allows POSTing relationship fields in 'attributes' rather than in 'relationships'.
Comment #22
wim leersAnd finally: test coverage! The test-only patch is the interdiff.
Comment #26
wim leersWell of course hard-coding
node--camelidswill not work for all other resource types 🤣Comment #27
wim leersComment #29
wim leers\Drupal\Tests\jsonapi\Functional\JsonApiFunctionalTest::testWrite()contained an assertion for one of the cases that was supported (see #21) but that this patch fixed/made consistent.Comment #30
wim leersGiven that:
Especially because it's marked "major" in terms of importance/impact for DX, while it's actually a trivial patch.
Comment #32
wim leersMade @pwolanin the patch author because it's thanks to him that this is being fixed!