-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Security] Add $tokenSource argument to #[IsCsrfTokenValid] to support reading tokens from the query string or headers
#61694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,7 +50,11 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo | |
| continue; | ||
| } | ||
|
|
||
| if (!$this->csrfTokenManager->isTokenValid(new CsrfToken($id, $request->getPayload()->getString($attribute->tokenKey)))) { | ||
| $tokenValue = $this->getTokenValue($request, $attribute->tokenSource, $attribute->tokenKey); | ||
| if ( | ||
| null === $tokenValue | ||
| || !$this->csrfTokenManager->isTokenValid(new CsrfToken($id, $tokenValue)) | ||
| ) { | ||
| throw new InvalidCsrfTokenException('Invalid CSRF token.'); | ||
| } | ||
| } | ||
|
|
@@ -74,4 +78,25 @@ private function getTokenId(string|Expression $id, Request $request, array $argu | |
| 'args' => $arguments, | ||
| ]); | ||
| } | ||
|
|
||
| private function getTokenValue(Request $request, int $tokenSource, string $tokenKey): ?string | ||
| { | ||
| $sources = [ | ||
| IsCsrfTokenValid::SOURCE_PAYLOAD => static fn () => $request->getPayload()->get($tokenKey), | ||
| IsCsrfTokenValid::SOURCE_QUERY => static fn () => $request->query->get($tokenKey), | ||
| IsCsrfTokenValid::SOURCE_HEADER => static fn () => $request->headers->get($tokenKey), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we really expect the header name to be If the name cannot be common, we cannot really support a bitfield IMO.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can yes, that'd be just a custom header. Ppl that want something else can always do it the procedural way I'd say |
||
| ]; | ||
|
|
||
| foreach ($sources as $source => $getter) { | ||
| if (!($tokenSource & $source)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (null !== $token = $getter()) { | ||
| return $token; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.