From f2422d65c2b41c7f93d3633fe3d9f11cf7d8c8cc Mon Sep 17 00:00:00 2001 From: "Antoine R. Dumont (@ardumont)" <antoine.romain.dumont@gmail.com> Date: Thu, 21 Jun 2018 11:50:40 +0200 Subject: [PATCH] identifiers: Validate that inputs are correct Related T1104 --- swh/model/identifiers.py | 13 ++++++++++++- swh/model/tests/test_identifiers.py | 13 +++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/swh/model/identifiers.py b/swh/model/identifiers.py index e59a973e..6a15c9be 100644 --- a/swh/model/identifiers.py +++ b/swh/model/identifiers.py @@ -609,6 +609,12 @@ def persistent_identifier(type, object, version=1): identifier version (int): persistent identifier version (default to 1) + Raises: + ValidationError (class) in case of: + + invalid type + invalid hash object + Returns: Persistent identifier as string. @@ -635,11 +641,16 @@ def persistent_identifier(type, object, version=1): 'key_id': 'sha1_git' }, } - o = _map[type] + o = _map.get(type) + if not o: + raise ValidationError('Wrong input: Supported types are %s' % ( + list(_map.keys()))) + if isinstance(object, dict): # internal swh representation resolution _hash = object[o['key_id']] else: # client passed direct identifier (bytes/str) _hash = object + validate_sha1(_hash) # can raise if invalid hash _hash = hash_to_hex(_hash) return 'swh:%s:%s:%s' % (version, o['short_name'], _hash) diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py index ed36e1b2..7b0720e9 100644 --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -11,6 +11,7 @@ from nose.tools import istest from swh.model import hashutil, identifiers +from swh.model.exceptions import ValidationError from swh.model.identifiers import SNAPSHOT, RELEASE, REVISION, DIRECTORY from swh.model.identifiers import CONTENT @@ -816,6 +817,18 @@ class SnapshotIdentifier(unittest.TestCase): self.assertEquals(actual_value, expected_persistent_id) + def test_persistent_identifier_wrong_input(self): + _snapshot_id = 'notahash4bc0bf3d81436bf980b46e98bd338453' + _snapshot = {'id': _snapshot_id} + + for _type, _hash, _error in [ + (SNAPSHOT, _snapshot_id, 'Unexpected characters'), + (SNAPSHOT, _snapshot, 'Unexpected characters'), + ('foo', '', 'Wrong input: Supported types are'), + ]: + with self.assertRaisesRegex(ValidationError, _error): + identifiers.persistent_identifier(_type, _hash) + def test_parse_persistent_identifier(self): for pid, _type, _version, _hash in [ ('swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2', 'cnt', -- GitLab