Skip to content
Snippets Groups Projects
Verified Commit 7779c573 authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

identifiers: Also validate the hash is correct

Related T1104
parent 0d3a0514
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,8 @@ import binascii
import datetime
from functools import lru_cache
from .exceptions import ValidationError
from .fields.hashes import validate_sha1
from .hashutil import hash_data, hash_git_data, DEFAULT_ALGORITHMS
from .hashutil import hash_to_hex
......@@ -696,6 +698,12 @@ def parse_persistent_identifier(persistent_id):
raise SWHMalformedIdentifierException(
'Wrong format: Identifier should be present')
try:
validate_sha1(_id)
except ValidationError:
raise SWHMalformedIdentifierException(
'Wrong format: Identifier should be a valid hash')
persistent_id_metadata = {}
for part in persistent_id_parts:
try:
......
......@@ -867,7 +867,11 @@ class SnapshotIdentifier(unittest.TestCase):
', '.join(PERSISTENT_IDENTIFIER_TYPES))),
('swh:1:dir:0b6959356d30f1a4e9b7f6bca59b9a336464c03d;invalid;'
'malformed',
'Contextual data is badly formatted, form key=val expected')
'Contextual data is badly formatted, form key=val expected'),
('swh:1:snp:gh6959356d30f1a4e9b7f6bca59b9a336464c03d',
'Wrong format: Identifier should be a valid hash'),
('swh:1:snp:foo',
'Wrong format: Identifier should be a valid hash')
]:
with self.assertRaisesRegex(
SWHMalformedIdentifierException, _error):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment