Closed (fixed)
Project:
Drupal core
Version:
8.3.x-dev
Component:
rest.module
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
10 Nov 2016 at 17:47 UTC
Updated:
25 Apr 2017 at 15:45 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
wim leersUpon further inspection, this is because
RequestHandler::handle()must validate the request body's format… because\Drupal\rest\Plugin\ResourceBase::routes()does this:This is blatantly incorrect. Rather than respecting a particular REST resource's configuration, we're just saying
If this would be setting
_content_type_formatto the actually allowed formats, then we wouldn't even need to throwUnsupportedMediaTypeHttpExceptioninRequestHandler::handle()because we wouldn't need to validate the request body's format, since the routing system would then handle that for us!So I started fixing this, and turns out
\Drupal\rest\Plugin\ResourceBase::routes()has no access to the REST resource configuration… hence it must make this very bad assumption. But, rather than setting this incorrect value in\Drupal\rest\Plugin\ResourceBase::routes(), we can alternatively just set the correct value in\Drupal\rest\Routing\ResourceRoutes::getRoutesForResourceConfig().Then we can just remove the
throw new UnsupportedMediaTypeHttpException. Which allowed me to remove this super scary sounding code comment fromRequestHandler:That was committed as part of #1850734: Make serialization formats configurable.
Attached patch includes the fix. I tested with #2737719: EntityResource: Provide comprehensive test coverage: for every entity type, every format, every method applied, no failures (and hence no BC break). So this does not need to block #2737719. This will still need separate test coverage, because it has security implications (well, really, it's a tightening of security).
Comment #3
wim leersBetter title, to reflect what I discovered in #2.
Comment #4
dawehnerComment #6
wim leersComment #7
wim leers#2 no longer applies.
This is a straight reroll of the patch, minus the changes made to
RequestHandler. That hunk I omitted completely for now: it's about simplifyingRequestHandler, it's not necessary to fix this bug itself.Comment #8
wim leers#7 should fail.
It should fail because it fixes the too-late validation of the request body's format. And hence the existing assertions in
EntityResourceTestBasenow fail.This updates those assertions… and as you can see, there's now actually helpful error messages in there!
Comment #9
wim leersAnd that validation that was happening too late (i.e. in
RequestHandler::handle()rather than in the routing system) can now simply be deleted! Let's do that :)Comment #11
dawehnerThis is simply a peace of beauty!
Comment #12
wim leersAwww, thanks! :D Best review message in a very long time! :D
Comment #14
dawehnerHere is a try to reroll.
Comment #16
jofitzRe-roll against 8.3.x.
Comment #18
wim leers#14 failed because it's a 8.4.x patch that was being tested against 8.3.x. #16 then rerolled the patch in #14 so it'd apply to 8.3.x.
(#2827084: EntityNormalizer::denormalize should not throw UnexpectedValueException, but \Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException, so we get a 422 response instead of 400 changed the body of
RequestHandler::handle(), and was only committed to 8.4.x.)#14 is correct, but needed to be tested against 8.4.x Queued that test. #16 should be ignored.
We need either:
As this test shows, it fixes an error response. So arguably, it's a bugfix, so it should be committed ot 8.3.x too.
Comment #19
jofitzSo (assuming test passes) #14 is a patch for 8.4.x & #16 is a patch for 8.3.x (so should not be ignored).
EDIT: #16 has now failed testing and so should be ignored.
Comment #21
wim leers#16 was never a patch for 8.3.x. It applies to 8.3.x, but it brings with it changes intentionally only committed to 8.4.x. That's where #16 goes wrong. Sorry for not making that more clear!
@Jo Fitzgerald: please know that this is not criticism of you, it's just tricky to do this kind of patch reroll, for patches that have parts that have changed between minor releases. I also sometimes struggle with this. I'm very grateful for the reroll you attempted :)
Comment #22
wim leersAnd so here are then the 8.3.x and 8.4.x patches. Then it's clear for core committers what the state of this issue is.
The 8.4.x one is identical to @dawehner's reroll in #14. The 8.3.x one I rebased.
Back to RTBC per #11.
Comment #24
wim leersRandom JS test fail for the 8.3.x patch. And 8.4.x has apparently already changed, so will need to rebase that.
Comment #25
wim leersEh, #22's 8.4.x patch was zero bytes. That was my bad.
The random fail… I can't do anything about.
Better luck this time?
Comment #27
wim leersHAH! A coding standards fail! :D :D <3
And less nice: a fail in
Drupal\editor\Tests\EditorSecurityTest.Comment #28
alexpott4 files changed, 31 insertions, 51 deletions- and better error messages as a result.Is there enough test coverage of the changes here? It's hard to work out exactly how much change is here. It looks mostly simplification and no functional change. Hard to be sure.
How come this is a negative assertion? Also, I think in_array() should always have strict set to TRUE. There are others (https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) - perhaps not relevant - but this seems strange.
Comment #29
wim leers#28:
And because that removes an if-else-test, it's removing one indentation level for all code inside the if-case.
We can remove that if-test-or-else-throw-a-415-HTTP-exception, because the validation of the request body's format (i.e.
Content-Typerequest header) is now happening in the routing system instead of in the controller! In other words: this makes the REST module use the routing system the way it's meant to be used. Instead of duplicating functionality/responsibilities from the routing system in the controller, which is what it's been doing until now.That's the only thing that it changes in this class!
Check
\Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::testPost()+\Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::testPatch():POSTandPATCHreceive request bodies, and those tests assert 415 responses in the appropriate case. If those tests still pass, you can be certain that it's still working correctly.PUTis something none of the REST Resource plugins in Drupal core support, but contrib/custom modules might use that. And they'd needContent Typerequest header validation too.I think this is a tiny detail in the grand scheme of things, but I'm happy to change it to
if (in_array($method, ['POST', 'PATCH', 'PUT'], TRUE)) {if you prefer that.Comment #30
wim leersDone.
Comment #31
alexpottThanks @Wim Leers.
Committed a8fec34 and pushed to 8.4.x. Thanks!
Going to set to patch to be ported for 8.3.x - will discuss with other committers once 8.3.0 is released. Imo this is a bug because of the fix to the error messages.
Comment #33
wim leersExactly!
IOW: these changes prove that the bug is fixed. No error message before, helpful error message after!
Comment #34
alexpottDiscussed with @catch and we agreed to commit this to 8.3.x
Committed 943e2e7 and pushed to 8.3.x. Thanks!