Skip to content

[Security/Csrf] Trust "Referer" at the same level as "Origin" #59269

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

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,21 @@ public function onKernelResponse(ResponseEvent $event): void
*/
private function isValidOrigin(Request $request): ?bool
{
$source = $request->headers->get('Origin') ?? $request->headers->get('Referer') ?? 'null';
$target = $request->getSchemeAndHttpHost().'/';
$source = 'null';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why using a string containing null here instead of an actual null value ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "null" string is a possible value of the Origin header. This saves checking for one more case.
See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin#description

Copy link
Member

@GromNaN GromNaN Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, here you assume that we cannot receive Origin: null and Referer: http://not-the-same-origin.com/. From the doc it seems unlikely to expose the Referer with "privacy sensitive" origin.

Edit: the function will return false while previously that case returned null. Which is safer for this limit case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's what we should do, isn't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes;


return 'null' === $source ? null : str_starts_with($source.'/', $request->getSchemeAndHttpHost().'/');
foreach (['Origin', 'Referer'] as $header) {
if (!$request->headers->has($header)) {
continue;
}
$source = $request->headers->get($header);

if (str_starts_with($source.'/', $target)) {
return true;
}
}

return 'null' === $source ? null : false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ public function testValidOrigin()
$this->assertSame(1 << 8, $request->attributes->get('csrf-token'));
}

public function testValidRefererInvalidOrigin()
{
$request = new Request();
$request->headers->set('Origin', 'http://localhost:1234');
$request->headers->set('Referer', $request->getSchemeAndHttpHost());
$this->requestStack->push($request);

$token = new CsrfToken('test_token', str_repeat('a', 24));

$this->logger->expects($this->once())->method('debug')->with('CSRF validation accepted using origin info.');
$this->assertTrue($this->csrfTokenManager->isTokenValid($token));
$this->assertSame(1 << 8, $request->attributes->get('csrf-token'));
}

public function testValidOriginAfterDoubleSubmit()
{
$session = $this->createMock(Session::class);
Expand Down