@@ -200,7 +200,9 @@ public function __construct(App $config)
200200 $ this ->request = Services::request ();
201201 $ this ->hashInCookie = $ this ->request ->getCookie ($ this ->cookieName );
202202
203- $ this ->generateHash ();
203+ if ($ this ->hash === null ) {
204+ $ this ->generateHash ();
205+ }
204206 }
205207
206208 private function isCSRFCookie (): bool
@@ -321,7 +323,9 @@ public function verify(RequestInterface $request)
321323 }
322324 }
323325
324- $ this ->generateHash ();
326+ if ($ this ->hash === null ) {
327+ $ this ->generateHash ();
328+ }
325329
326330 log_message ('info ' , 'CSRF token verified. ' );
327331
@@ -503,28 +507,26 @@ public function sanitizeFilename(string $str, bool $relativePath = false): strin
503507 */
504508 protected function generateHash (): string
505509 {
506- if ($ this ->hash === null ) {
507- // If the cookie exists we will use its value.
508- // We don't necessarily want to regenerate it with
509- // each page load since a page could contain embedded
510- // sub-pages causing this feature to fail
511- if ($ this ->isCSRFCookie ()) {
512- if ($ this ->isHashInCookie ()) {
513- return $ this ->hash = $ this ->hashInCookie ;
514- }
515- } elseif ($ this ->session ->has ($ this ->tokenName )) {
516- // Session based CSRF protection
517- return $ this ->hash = $ this ->session ->get ($ this ->tokenName );
510+ // If the cookie exists we will use its value.
511+ // We don't necessarily want to regenerate it with
512+ // each page load since a page could contain embedded
513+ // sub-pages causing this feature to fail
514+ if ($ this ->isCSRFCookie ()) {
515+ if ($ this ->isHashInCookie ()) {
516+ return $ this ->hash = $ this ->hashInCookie ;
518517 }
518+ } elseif ($ this ->session ->has ($ this ->tokenName )) {
519+ // Session based CSRF protection
520+ return $ this ->hash = $ this ->session ->get ($ this ->tokenName );
521+ }
519522
520- $ this ->hash = bin2hex (random_bytes (static ::CSRF_HASH_BYTES ));
523+ $ this ->hash = bin2hex (random_bytes (static ::CSRF_HASH_BYTES ));
521524
522- if ($ this ->isCSRFCookie ()) {
523- $ this ->saveHashInCookie ();
524- } else {
525- // Session based CSRF protection
526- $ this ->saveHashInSession ();
527- }
525+ if ($ this ->isCSRFCookie ()) {
526+ $ this ->saveHashInCookie ();
527+ } else {
528+ // Session based CSRF protection
529+ $ this ->saveHashInSession ();
528530 }
529531
530532 return $ this ->hash ;
0 commit comments