Skip to content

fix(contrib): raise UnknownHashTypeException instead of AttributeError/IndexError from from_composite_str() - #1091

Open
biggdawg320 wants to merge 1 commit into
CycloneDX:mainfrom
biggdawg320:jobscout/1075-hashtypefactory-from-composite-str-raise
Open

biggdawg320 wants to merge 1 commit into
CycloneDX:mainfrom
biggdawg320:jobscout/1075-hashtypefactory-from-composite-str-raise

Conversation

@biggdawg320

Copy link
Copy Markdown

Description

HashTypeFactory.from_composite_str() is documented to raise UnknownHashTypeException when the hash type cannot be determined, but it instead raised uncaught AttributeError for unsupported sha*/sha3*/blake2* prefixes (e.g. PyPI's blake2b_256 wheel digest key or PEP 691's sha224), and IndexError when the composite string was missing the : separator (e.g. 'md5'). This could allow the error to escape except UnknownHashTypeException handlers in downstream consumers such as cyclonedx-python's requirements.py, pipenv.py, and poetry.py.

This change makes from_composite_str() consistently raise UnknownHashTypeException for all unrecognized inputs, matching its docstring and the existing behavior of from_hashlib_alg().

Resolves or fixes issue: #1075

Testing

Added tests/test_contrib/test_hash_factories.py covering:

  • an unsupported blake2 suffix (blake2b_256:...)
  • an unsupported sha suffix (sha224:...)
  • a composite string missing the : separator (md5)

Verified these tests fail with AttributeError/IndexError against the original code and pass after the fix, and that the full tests/test_contrib suite still passes.

AI Tool Disclosure

Affirmation

Fixes #1075


This change was drafted with AI assistance (Claude) and verified against the project's own test suite before submission.

…fixes

HashTypeFactory.from_composite_str() raised uncaught AttributeError for
unsupported sha*/sha3*/blake2* prefixes (e.g. 'sha224', 'blake2b_256')
and IndexError when the composite string was missing the ':' separator
(e.g. 'md5'), instead of the documented UnknownHashTypeException.

Guard the getattr() lookups with a default of None and check the split
result length before raising the documented exception, matching the
behavior of from_hashlib_alg().

Co-Authored-By: Claude <noreply@anthropic.com>
@biggdawg320
biggdawg320 requested a review from a team as a code owner December 2, 2026 21:12
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HashTypeFactory.from_composite_str() raises AttributeError / IndexError instead of UnknownHashTypeException

1 participant