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

Fix options handling bug in Hash validator preventing the use of non-default algorithms #399

Merged
merged 1 commit into from
Nov 26, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3377,6 +3377,7 @@
<code><![CDATA[getHash]]></code>
<code><![CDATA[getHash]]></code>
<code><![CDATA[getHash]]></code>
<code><![CDATA[getOptions]]></code>
<code><![CDATA[setHash]]></code>
</DeprecatedMethod>
<MixedArgument>
Expand All @@ -3393,6 +3394,7 @@
<code><![CDATA[basicBehaviorDataProvider]]></code>
<code><![CDATA[hashProvider]]></code>
<code><![CDATA[invalidHashTypes]]></code>
<code><![CDATA[optionsOrderProvider]]></code>
</PossiblyUnusedMethod>
</file>
<file src="test/File/ImageSizeTest.php">
Expand Down
6 changes: 6 additions & 0 deletions src/File/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public function __construct($options = null)
$options['algorithm'] = func_get_arg(1);
}

// The combination of parent and local logic requires us to have the "algorithm" key before the "hash" key
// in the array, or else the default algorithm will be used instead of the passed one.
if (isset($options['algorithm'])) {
$options = ['algorithm' => $options['algorithm']] + $options;
}

parent::__construct($options);
}

Expand Down
50 changes: 50 additions & 0 deletions test/File/HashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace LaminasTest\Validator\File;

use Generator;
use Laminas\Validator\Exception\InvalidArgumentException;
use Laminas\Validator\File\Hash;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -15,6 +16,7 @@
use function basename;
use function current;
use function is_array;
use function key;
use function sprintf;

use const UPLOAD_ERR_NO_FILE;
Expand Down Expand Up @@ -313,4 +315,52 @@ public function testHashMustMatchWithTheAlgorithm(): void

self::assertFalse($validator->isValid(__DIR__ . '/_files/crc32-int.pdf'));
}

/**
* Provides options that should result in the same result irrespective of key order
*
* In order to act as a regression test for issue 398, this needs to include at least
* one couple of permutations that does not use the default algorithm (crc32).
*
* @return Generator<non-empty-string, array{
* options: array<string, mixed>,
* expectedHash: array<string, mixed>,
* }>
*/
public static function optionsOrderProvider(): Generator
{
$algos = ['crc32', 'md5', 'sha1'];
$hashes = [
'crc32' => ['6507f172bceb9ed0cc59246d41569c4d' => 'crc32'],
'md5' => ['6507f172bceb9ed0cc59246d41569c4d' => 'md5'],
'sha1' => ['6507f172bceb9ed0cc59246d41569c4d' => 'sha1'],
];

foreach ($algos as $algo) {
$hash = key($hashes[$algo]);
yield $algo . ' algorithm first' => [
'options' => [
'algorithm' => $algo,
'hash' => $hash,
],
'expectedHash' => $hashes[$algo],
];
yield $algo . ' hash first' => [
'options' => [
'hash' => $hash,
'algorithm' => $algo,
],
'expectedHash' => $hashes[$algo],
];
}
}

#[DataProvider('optionsOrderProvider')]
public function testOptionsKeyOrderIsIrrelevant(array $options, array $expectedHash): void
{
$validator = new Hash($options);
$resultOptions = $validator->getOptions();
self::assertArrayHasKey('hash', $resultOptions);
self::assertSame($expectedHash, $resultOptions['hash']);
}
}