daemon: Improve error message for wrong hash sizes.

* nix/libutil/hash.cc (parseHash): Show the hash algorithm name and
expected size in the error message.
* tests/derivations.scm ("fixed-output derivation, invalid hash size"):
New test.
This commit is contained in:
Ludovic Courtès 2023-01-09 16:48:01 +01:00
parent 5d24e57a61
commit 407175a1d0
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5
2 changed files with 17 additions and 2 deletions

View file

@ -76,8 +76,11 @@ string printHash(const Hash & hash)
Hash parseHash(HashType ht, const string & s) Hash parseHash(HashType ht, const string & s)
{ {
Hash hash(ht); Hash hash(ht);
if (s.length() != hash.hashSize * 2) if (s.length() != hash.hashSize * 2) {
throw Error(format("invalid hash `%1%'") % s); string algo = gcry_md_algo_name(ht);
throw Error(format("invalid %1% hash '%2%' (%3% bytes but expected %4%)")
% algo % s % (s.length() / 2) % hash.hashSize);
}
for (unsigned int i = 0; i < hash.hashSize; i++) { for (unsigned int i = 0; i < hash.hashSize; i++) {
string s2(s, i * 2, 2); string s2(s, i * 2, 2);
if (!isxdigit(s2[0]) || !isxdigit(s2[1])) if (!isxdigit(s2[0]) || !isxdigit(s2[1]))

View file

@ -396,6 +396,18 @@ (define prefix-len (string-length dir))
(call-with-input-file p get-bytevector-all)) (call-with-input-file p get-bytevector-all))
(bytevector? (query-path-hash %store p))))))) (bytevector? (query-path-hash %store p)))))))
(test-assert "fixed-output derivation, invalid hash size"
(guard (c ((store-protocol-error? c)
(string-contains-ci (store-protocol-error-message c)
"invalid SHA512 hash")))
(derivation %store "download-with-invalid-hash"
"builtin:download" '()
#:env-vars `(("url"
. ,(object->string "http://example.org")))
#:hash-algo 'sha512
#:hash #vu8(1 2 3))
#f))
(test-assert "derivation with a fixed-output input" (test-assert "derivation with a fixed-output input"
;; A derivation D using a fixed-output derivation F doesn't has the same ;; A derivation D using a fixed-output derivation F doesn't has the same
;; output path when passed F or F', as long as F and F' have the same output ;; output path when passed F or F', as long as F and F' have the same output