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

CSRF and PDO improvements #7938

Merged
merged 4 commits into from
Jan 1, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Controller/Component/CsrfComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function startup(Event $event)
if ($request->is('get') && $cookieData === null) {
$this->_setCookie($request, $response);
}
if (!$request->is(['head', 'get', 'options'])) {
if ($request->is(['put', 'post', 'delete', 'patch']) || !empty($request->data)) {
$this->_validateToken($request);
unset($request->data[$this->_config['field']]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Driver/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function connect()
$config['flags'] += [
PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];

if (!empty($config['ssl_key']) && !empty($config['ssl_cert'])) {
Expand Down
1 change: 1 addition & 0 deletions src/Database/Driver/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function connect()
$config = $this->_config;
$config['flags'] += [
PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
if (empty($config['unix_socket'])) {
Expand Down
1 change: 1 addition & 0 deletions src/Database/Driver/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function connect()
$config = $this->_config;
$config['flags'] += [
PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];

Expand Down
1 change: 1 addition & 0 deletions src/Database/Driver/Sqlserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function connect()
$config = $this->_config;
$config['flags'] += [
PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];

Expand Down
10 changes: 7 additions & 3 deletions tests/TestCase/Controller/Component/CsrfComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ public function testSettingCookie()
/**
* Data provider for HTTP method tests.
*
* HEAD and GET do not populate $_POST or request->data.
*
* @return void
*/
public static function safeHttpMethodProvider()
{
return [
['GET'], ['OPTIONS'], ['HEAD']
['GET', 'HEAD']
];
}

Expand Down Expand Up @@ -116,14 +118,14 @@ public function testSafeMethodNoCsrfRequired($method)
}

/**
* Data provider for HTTP method tests.
* Data provider for HTTP methods that can contain request bodies.
*
* @return void
*/
public static function httpMethodProvider()
{
return [
['PATCH'], ['PUT'], ['POST'], ['DELETE'], ['PURGE'], ['INVALIDMETHOD']
['OPTIONS'], ['PATCH'], ['PUT'], ['POST'], ['DELETE'], ['PURGE'], ['INVALIDMETHOD']
];
}

Expand All @@ -142,6 +144,7 @@ public function testValidTokenInHeader($method)
'REQUEST_METHOD' => $method,
'HTTP_X_CSRF_TOKEN' => 'testing123',
],
'post' => ['a' => 'b'],
'cookies' => ['csrfToken' => 'testing123']
]);
$controller->response = new Response();
Expand All @@ -167,6 +170,7 @@ public function testInvalidTokenInHeader($method)
'REQUEST_METHOD' => $method,
'HTTP_X_CSRF_TOKEN' => 'nope',
],
'post' => ['a' => 'b'],
'cookies' => ['csrfToken' => 'testing123']
]);
$controller->response = new Response();
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase/Database/Driver/PostgresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function testConnectionConfigDefault()

$expected['flags'] += [
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];

Expand Down Expand Up @@ -107,6 +108,7 @@ public function testConnectionConfigCustom()
$expected = $config;
$expected['flags'] += [
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];

Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase/Database/Driver/SqliteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function testConnectionConfigDefault()

$expected['flags'] += [
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$driver->expects($this->once())->method('_connect')
Expand Down Expand Up @@ -80,6 +81,7 @@ public function testConnectionConfigCustom()
$expected += ['username' => null, 'password' => null];
$expected['flags'] += [
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];

Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Database/Driver/SqlserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function testConnectionConfigCustom()
$expected = $config;
$expected['flags'] += [
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::SQLSRV_ATTR_ENCODING => 'a-language'
];
Expand Down