8000 bug #46309 [Security] Fix division by zero (tvlooy) · r-martins/symfony@5584221 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5584221

Browse files
committed
bug symfony#46309 [Security] Fix division by zero (tvlooy)
This PR was merged into the 5.4 branch. Discussion ---------- [Security] Fix division by zero | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | Given: CSRF token abc.def.ghi was returned When: I change the value of this token in my browser to abc..ghi Then: the key becomes '' and the xor that is called in denormalize results in a division by zero and http 500 Commits ------- 5028662 Fix division by zero
2 parents c493607 + 5028662 commit 5584221

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Symfony/Component/Security/Csrf/CsrfTokenManager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ private function derandomize(string $value): string
134134
return $value;
135135
}
136136
$key = base64_decode(strtr($parts[1], '-_', '+/'));
137+
if ('' === $key || false === $key) {
138+
return $value;
139+
}
137140
$value = base64_decode(strtr($parts[2], '-_', '+/'));
138141

139142
return $this->xor($value, $key);

src/Symfony/Component/Security/Csrf/Tests/CsrfTokenManagerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,26 @@ public function testNonExistingTokenIsNotValid($namespace, $manager, $storage)
193193
$this->assertFalse($manager->isTokenValid(new CsrfToken('token_id', 'FOOBAR')));
194194
}
195195

196+
public function testTokenShouldNotTriggerDivisionByZero()
197+
{
198+
[$generator, $storage] = $this->getGeneratorAndStorage();
199+
$manager = new CsrfTokenManager($generator, $storage);
200+
201+
// Scenario: the token that was returned is abc.def.ghi, and gets modified in the browser to abc..ghi
202+
203+
$storage->expects($this->once())
204+
->method('hasToken')
205+
->with('https-token_id')
206+
->willReturn(true);
207+
208+
$storage->expects($this->once())
209+
->method('getToken')
210+
->with('https-token_id')
211+
->willReturn('def');
212+
213+
$this->assertFalse($manager->isTokenValid(new CsrfToken('token_id', 'abc..ghi')));
214+
}
215+
196216
/**
197217
* @dataProvider getManagerGeneratorAndStorage
198218
*/

0 commit comments

Comments
 (0)
0