Skip to content

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

In progress

Description

@trackoor

HashTypeFactory.from_composite_str() documents Raises: UnknownHashTypeException if the type of hash cannot be determined, and raises UnknownHashTypeException on unknown prefixes such as unknown:.... However, it raises uncaught AttributeError on unsupported sha*/sha3*/blake2* prefixes (for example PyPI's blake2b_256 wheel digest key or PEP 691 sha224) and IndexError when : is missing after a known prefix, escaping except UnknownHashTypeException handlers (such as those in cyclonedx-python's requirements.py, pipenv.py, and poetry.py).

Reproduction

Using digests of six-1.17.0-py2.py3-none-any.whl from PyPI:

pip install cyclonedx-python-lib==11.12.0
python3 - <<'PY'
from cyclonedx.contrib.hash.factories import HashTypeFactory

factory = HashTypeFactory()
for value in [
    'unknown:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274',
    'blake2b_256:b7ce149a00dd41f10bc29e5921b496af8b574d8413afcd5e30dfa0ed46c2cc5e',
    'sha224:724673dee32fdfee47614cc790aefe049b461959e37cb8469c6937ff',
    'md5',
]:
    try:
        factory.from_composite_str(value)
    except Exception as exc:
        prefix = value.split(':')[0]
        print(f'{prefix} -> {type(exc).__name__}: {exc}')
PY

Actual output

unknown -> UnknownHashTypeException: Unable to determine hash type from 'unknown:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274'
blake2b_256 -> AttributeError: BLAKE2B__256
sha224 -> AttributeError: SHA_224
md5 -> IndexError: list index out of range

Expected output

All four inputs raise UnknownHashTypeException, matching HashTypeFactory.from_hashlib_alg() and the Raises docstring on from_composite_str().

Root cause

In cyclonedx/contrib/hash/factories.py, HashTypeFactory.from_composite_str() indexes parts[1] without checking that : was present and calls getattr(HashAlgorithm, ...) on sha*, sha3*, and blake2* prefixes without guarding missing HashAlgorithm members.

Versions

  • cyclonedx-python-lib: 11.12.0 and main (3d62bf672340e5a0a3994b028e316033915ef53c)
  • Python: 3.11.2 on Linux 6.1.0-56-cloud-amd64 x86_64

Found with a differential-testing tool; the report was drafted with AI assistance and reviewed and edited by me.

Credit: https://ucsbplse.github.io/

Activity

  1. jkowalleck commented on Nov 25, 2026

    @jkowalleck
    Member

    Debatable, but let's treat this as a bug.

    a fix would not be a breaking change, as it is in the "contrib" area of the library.

    this one is ready to be worked on.

    CycloneDX Python library is a community effort - feel free to contribute to fix.

  2. added
    bug
    help wanted
    good first issue
    contrib
    on Nov 25, 2026
  3. changed the title [-]HashTypeFactory.from_composite_str() raises AttributeError / IndexError instead of UnknownHashTypeException[/-] [+]`HashTypeFactory.from_composite_str()` raises AttributeError / IndexError instead of UnknownHashTypeException[/+] on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcontribgood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions