Skip to content

Commit 3db3a13

Browse files
committed
collect checksum failures in a list
1 parent 4e488d3 commit 3db3a13

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/Verifier.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class Verifier
2626
*/
2727
protected $now;
2828

29+
/**
30+
* List of verification failures.
31+
*
32+
* @var array
33+
*/
34+
protected $failures = [];
35+
2936
/**
3037
* Verifier constructor.
3138
*
@@ -168,8 +175,17 @@ public function verifyMessage($signed_message) {
168175
*/
169176
public function verifyChecksumList($signed_checksum_list, $working_directory)
170177
{
171-
$checksum_list_raw = $this->verifyMessage($signed_checksum_list);
172-
return $this->verifyTrustedChecksumList($checksum_list_raw, $working_directory);
178+
try {
179+
$checksum_list_raw = $this->verifyMessage($signed_checksum_list);
180+
}
181+
catch (VerifierException $exception) {
182+
$this->failures[] = $exception->getMessage();
183+
}
184+
$count = $this->verifyTrustedChecksumList($checksum_list_raw, $working_directory);
185+
if ($this->failures) {
186+
throw new VerifierException('Checksum list could not be verified.', $this->failures);
187+
}
188+
return $count;
173189
}
174190

175191
protected function verifyTrustedChecksumList($checksum_list_raw, $working_directory) {
@@ -183,15 +199,18 @@ protected function verifyTrustedChecksumList($checksum_list_raw, $working_direct
183199
{
184200
$actual_hash = @hash_file(strtolower($file_checksum->algorithm), $working_directory . DIRECTORY_SEPARATOR . $file_checksum->filename);
185201
if ($actual_hash === false) {
186-
throw new VerifierException("File \"$file_checksum->filename\" in the checksum list could not be read.");
202+
$this->failures[] = sprintf('File "%s" in the checksum list could not be read.', $file_checksum->filename);
203+
continue;
187204
}
188205
if (empty($actual_hash) || strlen($actual_hash) < 64) {
189-
throw new VerifierException("Failure computing hash for file \"$file_checksum->filename\" in the checksum list.");
206+
$this->failures[] = sprintf('Failure computing hash for file "%s" in the checksum list.', $file_checksum->filename);
207+
continue;
190208
}
191209

192210
if (strcmp($actual_hash, $file_checksum->hex_hash) !== 0)
193211
{
194-
throw new VerifierException("File \"$file_checksum->filename\" does not pass checksum verification.");
212+
$this->failures[] = sprintf('File "%s" does not pass checksum verification.', $file_checksum->filename);
213+
continue;
195214
}
196215

197216
$verified_count++;
@@ -237,12 +256,14 @@ protected function parseChecksumList($checksum_list_raw, $list_is_trusted)
237256
}
238257

239258
if (substr($line, 0, 1) === '\\') {
240-
throw new VerifierException('Filenames with problematic characters are not yet supported.');
259+
$this->failures[] = sprintf('Filenames "%s" with problematic characters are not yet supported.', $line);
260+
continue;
241261
}
242262

243263
$algo = substr($line, 0, strpos($line, ' '));
244264
if (empty($this->HASH_ALGO_BASE64_LENGTHS[$algo])) {
245-
throw new VerifierException("Algorithm \"$algo\" is unsupported for checksum verification.");
265+
$this->failures[] = sprintf('Algorithm "%s" is unsupported for checksum verification.', $algo);
266+
continue;
246267
}
247268

248269
$filename_start = strpos($line, '(') + 1;

src/VerifierException.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,19 @@
22

33
namespace DrupalAssociation\Signify;
44

5-
class VerifierException extends \RuntimeException { }
5+
class VerifierException extends \RuntimeException {
6+
7+
protected $failures;
8+
9+
public function __construct($message = '', array $failures = [], $code = 0, Throwable $previous = NULL)
10+
{
11+
parent::__construct($message, $code, $previous);
12+
$this->failures = $verificationFailures;
13+
}
14+
15+
public function getVerificationFailures()
16+
{
17+
return $this->failures;
18+
}
19+
20+
}

0 commit comments

Comments
 (0)