Skip to content
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

fix: TypeError when $tokenRandomize = true and no token posted #5742

Merged
merged 3 commits into from
Feb 25, 2022
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
5 changes: 3 additions & 2 deletions system/Security/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ public function verify(RequestInterface $request)
return $this;
}

$token = $this->tokenRandomize ? $this->derandomize($this->getPostedToken($request))
: $this->getPostedToken($request);
$postedToken = $this->getPostedToken($request);
$token = ($postedToken !== null && $this->tokenRandomize)
? $this->derandomize($postedToken) : $postedToken;

// Do the tokens match?
if (! isset($token, $this->hash) || ! hash_equals($this->hash, $token)) {
Expand Down
15 changes: 15 additions & 0 deletions tests/system/Security/SecurityCSRFSessionRandomizeTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ public function testHashIsReadFromSession()
);
}

public function testCSRFVerifyPostNoToken()
{
$this->expectException(SecurityException::class);
$this->expectExceptionMessage('The action you requested is not allowed.');

$_SERVER['REQUEST_METHOD'] = 'POST';
unset($_POST['csrf_test_name']);

$request = new IncomingRequest(new MockAppConfig(), new URI('http://badurl.com'), null, new UserAgent());

$security = new Security(new MockAppConfig());

$security->verify($request);
}

public function testCSRFVerifyPostThrowsExceptionOnNoMatch()
{
$this->expectException(SecurityException::class);
Expand Down