8000 Connection parameters are cached hashed by fullbl · Pull Request #3031 · doctrine/dbal · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Connection parameters are cached hashed #3031

8000
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

Merged
merged 1 commit into from
Mar 29, 2018
Merged

Connection parameters are cached hashed #3031

merged 1 commit into from
Mar 29, 2018

Conversation

fullbl
Copy link
Contributor
@fullbl fullbl commented Feb 22, 2018

As per doctrine/orm#7086 (I didn't know it was related to dbal and not doctrine2), parameters should be hashed because they can have passwords inside.

@@ -99,10 +101,14 @@ public function getCacheKey()
*/
public function generateCacheKeys($query, $params, $types, array $connectionParams = [])
{
$hashedParams = array_map(function ($param) {
return crypt($param, md5($param));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach assumes that all $params are scalar which may be true for now but not necessarily true in the future. Why not just hash the serialized representation once instead of hashing parameters individually?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a security expert but md5($param) is not random (the salt should be random). Should something like random_bytes() be used instead, or can we just get away with something like md5() or sha1() for this purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First one seems legit, since I don't know where is it really called I didn't want to change too much stuff. If it's ok I'll change it.
Salt is not random because with same strings we want same outputs!

8000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No crypto needed here: this should just be a plain hash of all parameters. sha256 would be more than sufficient.

$realCacheKey = 'query=' . $query .
'&params=' . serialize($params) .
'&types=' . serialize($types) .
'&connectionParams=' . serialize($connectionParams);
'&connectionParams=' . serialize($hashedParams);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Serializing the hash is not needed anymore, while hashing the serialized parameters would be simpler

@@ -99,10 +101,14 @@ public function getCacheKey()
*/
public function generateCacheKeys($query, $params, $types, array $connectionParams = [])
{
$hashedParams = array_map(function ($param) {
return crypt($param, md5($param));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No crypto needed here: this should just be a plain hash of all parameters. sha256 would be more than sufficient.

@@ -5,65 +5,61 @@
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\ParameterType;
use Doctrine\Tests\DbalTestCase;
use function parse_str;
use function unserialize;

class QueryCacheProfileTest extends DbalTestCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new test method is needed, in which we verify that none of the connection parameters are to be found in the key.

Copy link
Contributor Author
@fullbl fullbl Feb 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it, it's called testConnectionParamsShouldBeHashed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Didn't see it due to diff noise :)

@@ -20,18 +20,19 @@
namespace Doctrine\DBAL\Cache;

use Doctrine\Common\Cache\Cache;
use function serialize;
use function sha1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lines 103, 104 and 109!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, I just saw the diff, thanks 👍

@@ -102,7 +102,7 @@ public function generateCacheKeys($query, $params, $types, array $connectionPara
$realCacheKey = 'query=' . $query .
'&params=' . serialize($params) .
'&types=' . serialize($types) .
'&connectionParams=' . serialize($connectionParams);
'&connectionParams=' . hash('sha256', serialize($connectionParams));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

private $types = [ParameterType::INTEGER];

/** @var array<string> */
private $connectionParams = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alignment went a bit bonkers here. Also, the type is string[], as most tooling doesn't work with array<foo>


$connectionParams = array(
/** @var array<string> */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string[]

$query = 'SELECT * FROM foo WHERE bar = ?';
$params = [666];
$types = [ParameterType::INTEGER];
/** @var array<int> */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int[]


class QueryCacheProfileTest extends DbalTestCase
{
const LIFETIME = 3600;
const CACHE_KEY = 'user_specified_cache_key';
public const LIFETIME = 3600;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be private

const LIFETIME = 3600;
const CACHE_KEY = 'user_specified_cache_key';
public const LIFETIME = 3600;
public const CACHE_KEY = 'user_specified_cache_key';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private

@@ -77,67 +73,64 @@ public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven()

public function testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferentConnections()
{
$query = 'SELECT * FROM foo WHERE bar = ?';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some in the diffs in the test seem completely wrong/unrelated. Also, as I mentioned before, a new method verifying that the connection parameters cannot be found in the key is needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I tried to make it more dry!

self::assertArrayHasKey('connectionParams', $params);

foreach ($this->connectionParams as $param) {
self::assertEquals(false, strpos($param, $params['connectionParams']));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there something like assertStringNotContains that you can use here?

.gitignore Outdated
@@ -8,3 +8,4 @@ vendor/
composer.lock
/phpunit.xml
/.phpcs-cache
.idea
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in your global .gitignore, and not in the project

Ocramius
Ocramius previously approved these changes Feb 26, 2018
@Ocramius
Copy link
Member

Some CS issues remain, but I'll not hassle you with them :)

@@ -20,18 +20,19 @@
namespace Doctrine\DBAL\Cache;

use Doctrine\Common\Cache\Cache;
use function hash;
use function serialize;
use function sha1;
Copy link
Contributor
@Majkl578 Majkl578 Mar 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is now redundant (will fail build imho). Forget it, it's used on another place.

@Ocramius
Copy link
Member

🚢

@Ocramius Ocramius merged commit 684122c into doctrine:master Mar 29, 2018
@Ocramius
Copy link
Member

Thanks @fullbl!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0