Skip to content

Commit

Permalink
fix #4067 Cookie(csrfToken)にsecure属性が付かない問題を解決 (#4068)
Browse files Browse the repository at this point in the history
Co-authored-by: kato <[email protected]>
Co-authored-by: ryuring <[email protected]>
  • Loading branch information
3 people authored Dec 6, 2024
1 parent 60acdbb commit e95647a
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public function bootstrap(): void
*/
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
$csrfProtectionMiddlewareOptions = ['httponly' => true];
//リクエストがhttpsならcsrfTokenにもsecureヘッダを付与
$sessionConfig = (array) Configure::read('Session');
if (!empty($sessionConfig['ini']['session.cookie_secure']) || (int) ini_get('session.cookie_secure') === 1) {
$csrfProtectionMiddlewareOptions['secure'] = true;
}
$middlewareQueue
// Catch any exceptions in the lower layers,
// and make an error page/response
Expand All @@ -87,9 +93,7 @@ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue

// Cross Site Request Forgery (CSRF) Protection Middleware
// https://book.cakephp.org/5/en/security/csrf.html#cross-site-request-forgery-csrf-middleware
->add(new CsrfProtectionMiddleware([
'httponly' => true,
]));
->add(new CsrfProtectionMiddleware($csrfProtectionMiddlewareOptions));

return $middlewareQueue;
}
Expand Down

0 comments on commit e95647a

Please sign in to comment.