From 86e707099a44e22e29a2d0f91ea7246733bf7761 Mon Sep 17 00:00:00 2001 From: Vaibhav Lalwani Date: Wed, 25 Nov 2026 16:28:34 +0000 Subject: [PATCH 1/3] fix: raise UnknownHashTypeException for unsupported or malformed composite hashes Closes #1075 Signed-off-by: Vaibhav Lalwani --- cyclonedx/contrib/hash/factories.py | 43 ++++++----------- tests/test_contrib/test_hash_factories.py | 57 +++++++++++++++++++++++ 2 files changed, 72 insertions(+), 28 deletions(-) create mode 100644 tests/test_contrib/test_hash_factories.py diff --git a/cyclonedx/contrib/hash/factories.py b/cyclonedx/contrib/hash/factories.py index c4b477fd..55770657 100644 --- a/cyclonedx/contrib/hash/factories.py +++ b/cyclonedx/contrib/hash/factories.py @@ -19,6 +19,8 @@ __all__ = ['HashTypeFactory'] +from typing import Optional + from ...exception.model import UnknownHashTypeException from ...model import HashAlgorithm, HashType @@ -88,42 +90,27 @@ def from_composite_str(self, composite_hash: str) -> HashType: An instance of `HashType`. """ parts = composite_hash.split(':') + if len(parts) < 2: + raise UnknownHashTypeException(f'Unable to determine hash type from {composite_hash!r}') algorithm_prefix = parts[0].lower() + content = parts[1].lower() + alg: Optional[HashAlgorithm] = None if algorithm_prefix == 'md5': - return HashType( - alg=HashAlgorithm.MD5, - content=parts[1].lower() - ) + alg = HashAlgorithm.MD5 elif algorithm_prefix[0:4] == 'sha3': - return HashType( - alg=getattr(HashAlgorithm, f'SHA3_{algorithm_prefix[5:]}'), - content=parts[1].lower() - ) + alg = HashAlgorithm.__members__.get(f'SHA3_{algorithm_prefix[5:]}') elif algorithm_prefix == 'sha1': - return HashType( - alg=HashAlgorithm.SHA_1, - content=parts[1].lower() - ) + alg = HashAlgorithm.SHA_1 elif algorithm_prefix[0:3] == 'sha': # This is actually SHA2... - return HashType( - alg=getattr(HashAlgorithm, f'SHA_{algorithm_prefix[3:]}'), - content=parts[1].lower() - ) + alg = HashAlgorithm.__members__.get(f'SHA_{algorithm_prefix[3:]}') elif algorithm_prefix[0:7] == 'blake2b': - return HashType( - alg=getattr(HashAlgorithm, f'BLAKE2B_{algorithm_prefix[7:]}'), - content=parts[1].lower() - ) + alg = HashAlgorithm.__members__.get(f'BLAKE2B_{algorithm_prefix[7:]}') elif algorithm_prefix[0:6] == 'blake2': - return HashType( - alg=getattr(HashAlgorithm, f'BLAKE2B_{algorithm_prefix[6:]}'), - content=parts[1].lower() - ) + alg = HashAlgorithm.__members__.get(f'BLAKE2B_{algorithm_prefix[6:]}') elif algorithm_prefix[0:6] == 'blake3': - return HashType( - alg=HashAlgorithm.BLAKE3, - content=parts[1].lower() - ) + alg = HashAlgorithm.BLAKE3 + if alg is not None: + return HashType(alg=alg, content=content) raise UnknownHashTypeException(f'Unable to determine hash type from {composite_hash!r}') diff --git a/tests/test_contrib/test_hash_factories.py b/tests/test_contrib/test_hash_factories.py new file mode 100644 index 00000000..79c72726 --- /dev/null +++ b/tests/test_contrib/test_hash_factories.py @@ -0,0 +1,57 @@ +# This file is part of CycloneDX Python Library +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) OWASP Foundation. All Rights Reserved. + +from unittest import TestCase + +from cyclonedx.contrib.hash.factories import HashTypeFactory +from cyclonedx.exception.model import UnknownHashTypeException +from cyclonedx.model import HashAlgorithm + + +class TestHashTypeFactoryCompositeStr(TestCase): + + def test_known_prefixes(self) -> None: + factory = HashTypeFactory() + for composite, alg in ( + ('md5:ABC', HashAlgorithm.MD5), + ('sha1:ABC', HashAlgorithm.SHA_1), + ('sha256:ABC', HashAlgorithm.SHA_256), + ('sha3-256:ABC', HashAlgorithm.SHA3_256), + ('blake2b256:ABC', HashAlgorithm.BLAKE2B_256), + ('blake3:ABC', HashAlgorithm.BLAKE3), + ): + with self.subTest(composite=composite): + hash_ = factory.from_composite_str(composite) + self.assertIs(alg, hash_.alg) + self.assertEqual('abc', hash_.content) + + def test_unsupported_algorithm_raises_unknown_hash_type(self) -> None: + factory = HashTypeFactory() + for composite in ( + 'unknown:abc', + 'blake2b_256:abc', + 'sha224:abc', + 'sha3_224:abc', + ): + with self.subTest(composite=composite), self.assertRaises(UnknownHashTypeException): + factory.from_composite_str(composite) + + def test_missing_separator_raises_unknown_hash_type(self) -> None: + factory = HashTypeFactory() + for composite in ('md5', 'sha256', 'sha3-256', 'blake3', ''): + with self.subTest(composite=composite), self.assertRaises(UnknownHashTypeException): + factory.from_composite_str(composite) From b1ba6c89051fdafb701c8ac46860becc7b4ad417 Mon Sep 17 00:00:00 2001 From: Vaibhav Lalwani Date: Wed, 25 Nov 2026 16:54:39 +0000 Subject: [PATCH 2/3] fix: keep composite hash fix minimal Signed-off-by: Vaibhav Lalwani --- cyclonedx/contrib/hash/factories.py | 48 +++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/cyclonedx/contrib/hash/factories.py b/cyclonedx/contrib/hash/factories.py index 55770657..4bf965c5 100644 --- a/cyclonedx/contrib/hash/factories.py +++ b/cyclonedx/contrib/hash/factories.py @@ -19,8 +19,6 @@ __all__ = ['HashTypeFactory'] -from typing import Optional - from ...exception.model import UnknownHashTypeException from ...model import HashAlgorithm, HashType @@ -43,6 +41,13 @@ } +def _hash_algorithm(name: str) -> HashAlgorithm: + try: + return HashAlgorithm[name] + except KeyError: + raise UnknownHashTypeException(f'Unable to determine hash alg for {name!r}') from None + + class HashTypeFactory: def from_hashlib_alg(self, hashlib_alg: str, content: str) -> HashType: @@ -94,23 +99,40 @@ def from_composite_str(self, composite_hash: str) -> HashType: raise UnknownHashTypeException(f'Unable to determine hash type from {composite_hash!r}') algorithm_prefix = parts[0].lower() - content = parts[1].lower() - alg: Optional[HashAlgorithm] = None if algorithm_prefix == 'md5': - alg = HashAlgorithm.MD5 + return HashType( + alg=HashAlgorithm.MD5, + content=parts[1].lower() + ) elif algorithm_prefix[0:4] == 'sha3': - alg = HashAlgorithm.__members__.get(f'SHA3_{algorithm_prefix[5:]}') + return HashType( + alg=_hash_algorithm(f'SHA3_{algorithm_prefix[5:]}'), + content=parts[1].lower() + ) elif algorithm_prefix == 'sha1': - alg = HashAlgorithm.SHA_1 + return HashType( + alg=HashAlgorithm.SHA_1, + content=parts[1].lower() + ) elif algorithm_prefix[0:3] == 'sha': # This is actually SHA2... - alg = HashAlgorithm.__members__.get(f'SHA_{algorithm_prefix[3:]}') + return HashType( + alg=_hash_algorithm(f'SHA_{algorithm_prefix[3:]}'), + content=parts[1].lower() + ) elif algorithm_prefix[0:7] == 'blake2b': - alg = HashAlgorithm.__members__.get(f'BLAKE2B_{algorithm_prefix[7:]}') + return HashType( + alg=_hash_algorithm(f'BLAKE2B_{algorithm_prefix[7:]}'), + content=parts[1].lower() + ) elif algorithm_prefix[0:6] == 'blake2': - alg = HashAlgorithm.__members__.get(f'BLAKE2B_{algorithm_prefix[6:]}') + return HashType( + alg=_hash_algorithm(f'BLAKE2B_{algorithm_prefix[6:]}'), + content=parts[1].lower() + ) elif algorithm_prefix[0:6] == 'blake3': - alg = HashAlgorithm.BLAKE3 - if alg is not None: - return HashType(alg=alg, content=content) + return HashType( + alg=HashAlgorithm.BLAKE3, + content=parts[1].lower() + ) raise UnknownHashTypeException(f'Unable to determine hash type from {composite_hash!r}') From b7bfa5db12431f8d57c31e6aa9643e0485fef445 Mon Sep 17 00:00:00 2001 From: vaibhav4046 Date: Tue, 1 Dec 2026 14:33:45 +0000 Subject: [PATCH 3/3] test: use DDT instead of subtests for composite hash cases --- tests/test_contrib/test_hash_factories.py | 70 +++++++++++++---------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/tests/test_contrib/test_hash_factories.py b/tests/test_contrib/test_hash_factories.py index 79c72726..89a3d940 100644 --- a/tests/test_contrib/test_hash_factories.py +++ b/tests/test_contrib/test_hash_factories.py @@ -17,41 +17,49 @@ from unittest import TestCase +from ddt import ddt, named_data, unpack + from cyclonedx.contrib.hash.factories import HashTypeFactory from cyclonedx.exception.model import UnknownHashTypeException from cyclonedx.model import HashAlgorithm +@ddt class TestHashTypeFactoryCompositeStr(TestCase): - def test_known_prefixes(self) -> None: - factory = HashTypeFactory() - for composite, alg in ( - ('md5:ABC', HashAlgorithm.MD5), - ('sha1:ABC', HashAlgorithm.SHA_1), - ('sha256:ABC', HashAlgorithm.SHA_256), - ('sha3-256:ABC', HashAlgorithm.SHA3_256), - ('blake2b256:ABC', HashAlgorithm.BLAKE2B_256), - ('blake3:ABC', HashAlgorithm.BLAKE3), - ): - with self.subTest(composite=composite): - hash_ = factory.from_composite_str(composite) - self.assertIs(alg, hash_.alg) - self.assertEqual('abc', hash_.content) - - def test_unsupported_algorithm_raises_unknown_hash_type(self) -> None: - factory = HashTypeFactory() - for composite in ( - 'unknown:abc', - 'blake2b_256:abc', - 'sha224:abc', - 'sha3_224:abc', - ): - with self.subTest(composite=composite), self.assertRaises(UnknownHashTypeException): - factory.from_composite_str(composite) - - def test_missing_separator_raises_unknown_hash_type(self) -> None: - factory = HashTypeFactory() - for composite in ('md5', 'sha256', 'sha3-256', 'blake3', ''): - with self.subTest(composite=composite), self.assertRaises(UnknownHashTypeException): - factory.from_composite_str(composite) + @named_data( + ('md5', 'md5:ABC', HashAlgorithm.MD5), + ('sha1', 'sha1:ABC', HashAlgorithm.SHA_1), + ('sha256', 'sha256:ABC', HashAlgorithm.SHA_256), + ('sha3-256', 'sha3-256:ABC', HashAlgorithm.SHA3_256), + ('blake2b256', 'blake2b256:ABC', HashAlgorithm.BLAKE2B_256), + ('blake3', 'blake3:ABC', HashAlgorithm.BLAKE3), + ) + @unpack + def test_known_prefixes(self, composite: str, alg: HashAlgorithm) -> None: + hash_ = HashTypeFactory().from_composite_str(composite) + self.assertIs(alg, hash_.alg) + self.assertEqual('abc', hash_.content) + + @named_data( + ('unknown', 'unknown:abc'), + ('blake2b_256', 'blake2b_256:abc'), + ('sha224', 'sha224:abc'), + ('sha3_224', 'sha3_224:abc'), + ) + @unpack + def test_unsupported_algorithm_raises_unknown_hash_type(self, composite: str) -> None: + with self.assertRaises(UnknownHashTypeException): + HashTypeFactory().from_composite_str(composite) + + @named_data( + ('md5', 'md5'), + ('sha256', 'sha256'), + ('sha3-256', 'sha3-256'), + ('blake3', 'blake3'), + ('empty', ''), + ) + @unpack + def test_missing_separator_raises_unknown_hash_type(self, composite: str) -> None: + with self.assertRaises(UnknownHashTypeException): + HashTypeFactory().from_composite_str(composite)