Skip to content

Commit e056312

Browse files
committed
refactor: extract restoreHash() method
1 parent 06f4ceb commit e056312

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

system/Security/Security.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,21 @@ public function sanitizeFilename(string $str, bool $relativePath = false): strin
502502
return stripslashes($str);
503503
}
504504

505+
/**
506+
* Restore hash from Session or Cookie
507+
*/
508+
private function restoreHash(): void
509+
{
510+
if ($this->isCSRFCookie()) {
511+
if ($this->isHashInCookie()) {
512+
$this->hash = $this->hashInCookie;
513+
}
514+
} elseif ($this->session->has($this->tokenName)) {
515+
// Session based CSRF protection
516+
$this->hash = $this->session->get($this->tokenName);
517+
}
518+
}
519+
505520
/**
506521
* Generates the CSRF Hash.
507522
*/
@@ -511,13 +526,10 @@ protected function generateHash(): string
511526
// We don't necessarily want to regenerate it with
512527
// each page load since a page could contain embedded
513528
// sub-pages causing this feature to fail
514-
if ($this->isCSRFCookie()) {
515-
if ($this->isHashInCookie()) {
516-
return $this->hash = $this->hashInCookie;
517-
}
518-
} elseif ($this->session->has($this->tokenName)) {
519-
// Session based CSRF protection
520-
return $this->hash = $this->session->get($this->tokenName);
529+
$this->restoreHash();
530+
531+
if ($this->hash !== null) {
532+
return $this->hash;
521533
}
522534

523535
$this->hash = bin2hex(random_bytes(static::CSRF_HASH_BYTES));

0 commit comments

Comments
 (0)