From 7779c573e9c71076af55563e60841d19d5c741e2 Mon Sep 17 00:00:00 2001 From: "Antoine R. Dumont (@ardumont)" <antoine.romain.dumont@gmail.com> Date: Wed, 20 Jun 2018 12:11:07 +0200 Subject: [PATCH] identifiers: Also validate the hash is correct Related T1104 --- swh/model/identifiers.py | 8 ++++++++ swh/model/tests/test_identifiers.py | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/swh/model/identifiers.py b/swh/model/identifiers.py index 934ace85..f8a87a13 100644 --- a/swh/model/identifiers.py +++ b/swh/model/identifiers.py @@ -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: diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py index 0cab5003..b191676a 100644 --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -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): -- GitLab