Problem/Motivation
CsrfRequestHeaderAccessCheck::applies() checks the _method requirement to see which methods are supported by the route.
But this requirement is deprecated, the modern version to allow methods is with the methods property: https://www.drupal.org/docs/drupal-apis/routing-system/structure-of-rout...
Steps to reproduce
Create a route like this:
test:
path: '/test_csrf_foo'
methods: [ 'GET' ]
defaults:
_controller: '\Drupal\test\Controller\TestController::test'
_title: 'Hello World'
requirements:
_permission: 'access content'
_csrf_request_header_token: 'TRUE'
applies() returns TRUE even though the route only supports GET requests.
Proposed resolution
Check for the methods property instead.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Comments
Comment #2
sourav_paulChecking...
Comment #4
sourav_paulVerified this locally by following the reproduction steps, and I can confirm the issue.
I also checked the route docs here:
https://www.drupal.org/docs/drupal-apis/routing-system/structure-of-rout...
methods is the modern way to define allowed HTTP methods on a route, while _method is the older legacy/deprecated style.
Local result:
when I used a route with _csrf_request_header_token: 'TRUE' and methods: ['GET'], CsrfRequestHeaderAccessCheck::applies() still returned TRUE. Since this is a read-only route, it should not apply the CSRF request header check.
So the fix should be to use the route methods first, and keep _method only as a fallback for older routes if needed.
[Used AI to re-verify the fix]
Comment #5
sourav_paulComment #6
smustgrave commentedDo we have test coverage for this?
Comment #7
longwaveRediscovered this issue when looking at #3613245: Validate route requirements
I think that we can convert this access check to a static checker, it doesn't need to be dynamic at all.
Comment #8
longwaveIf we add
applies_to: _csrf_request_header_tokento the service definition then we can drop theapplies()method entirely.Comment #9
smustgrave commentedNice! Everything looks good to me. Not sure how else to review.
Comment #11
catchThis looks great, nice to be able to replace all that code with a one-liner! Committed/pushed to main and 11.x, thanks!