Repository navigation
fix(contrib): raise UnknownHashTypeException instead of AttributeError/IndexError from from_composite_str() - #1091
Conversation
…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 <[email protected]>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
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.
|
Sorry, will not accept this solution. |
|
re: #1091 (comment) This was not an easy decision. |
Description
HashTypeFactory.from_composite_str()is documented to raiseUnknownHashTypeExceptionwhen the hash type cannot be determined, but it instead raised uncaughtAttributeErrorfor unsupportedsha*/sha3*/blake2*prefixes (e.g. PyPI'sblake2b_256wheel digest key or PEP 691'ssha224), andIndexErrorwhen the composite string was missing the:separator (e.g.'md5'). This could allow the error to escapeexcept UnknownHashTypeExceptionhandlers in downstream consumers such ascyclonedx-python'srequirements.py,pipenv.py, andpoetry.py.This change makes
from_composite_str()consistently raiseUnknownHashTypeExceptionfor all unrecognized inputs, matching its docstring and the existing behavior offrom_hashlib_alg().Resolves or fixes issue: #1075
Testing
Added
tests/test_contrib/test_hash_factories.pycovering:blake2suffix (blake2b_256:...)shasuffix (sha224:...):separator (md5)Verified these tests fail with
AttributeError/IndexErroragainst the original code and pass after the fix, and that the fulltests/test_contribsuite still passes.AI Tool Disclosure
HashTypeFactory.from_composite_str()raises AttributeError / IndexError instead of UnknownHashTypeException #1075 with a failing test, then fixHashTypeFactory.from_composite_str()so unsupported hash prefixes and missing:separators raiseUnknownHashTypeExceptioninstead ofAttributeError/IndexError.Affirmation
Fixes #1075
This change was drafted with AI assistance (Claude) and verified against the project's own test suite before submission.