Skip to content

Commit

Permalink
Add option to make CSRF Token HttpOnly.
Browse files Browse the repository at this point in the history
This option lets users opt-in to http only cookies for CSRF tokens, this
is useful when you have no client-side scripting that needs a CSRF
token.

Refs #7727
  • Loading branch information
markstory committed Nov 26, 2015
1 parent 238b053 commit f7f5e21
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Controller/Component/CsrfComponent.php
Expand Up @@ -46,6 +46,7 @@ class CsrfComponent extends Component
* - cookieName = The name of the cookie to send.
* - expiry = How long the CSRF token should last. Defaults to browser session.
* - secure = Whether or not the cookie will be set with the Secure flag. Defaults to false.
* - httpOnly = Whether or not the cookie will be set with the HttpOnly flag. Defaults to false.
* - field = The form field to check. Changing this will also require configuring
* FormHelper.
*
Expand All @@ -55,6 +56,7 @@ class CsrfComponent extends Component
'cookieName' => 'csrfToken',
'expiry' => 0,
'secure' => false,
'httpOnly' => false,
'field' => '_csrfToken',
];

Expand Down Expand Up @@ -132,6 +134,7 @@ protected function _setCookie(Request $request, Response $response)
'expire' => $expiry->format('U'),
'path' => $request->webroot,
'secure' => $this->_config['secure'],
'httpOnly' => $this->_config['httpOnly'],
]);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/TestCase/Controller/Component/CsrfComponentTest.php
Expand Up @@ -265,7 +265,8 @@ public function testConfigurationCookieCreate()
$component = new CsrfComponent($this->registry, [
'cookieName' => 'token',
'expiry' => '+1 hour',
'secure' => true
'secure' => true,
'httpOnly' => true
]);

$event = new Event('Controller.startup', $controller);
Expand All @@ -278,6 +279,7 @@ public function testConfigurationCookieCreate()
$this->assertWithinRange((new Time('+1 hour'))->format('U'), $cookie['expire'], 1, 'session duration.');
$this->assertEquals('/dir/', $cookie['path'], 'session path.');
$this->assertTrue($cookie['secure'], 'cookie security flag missing');
$this->assertTrue($cookie['httpOnly'], 'cookie httpOnly flag missing');
}

/**
Expand Down

0 comments on commit f7f5e21

Please sign in to comment.