Skip to content

Fix CycloneDX vulnerability-component linking (#980)#981

Merged
woodruffw merged 2 commits intopypa:mainfrom
AryanBagade:fix-cyclonedx-bomref-980
Dec 1, 2025
Merged

Fix CycloneDX vulnerability-component linking (#980)#981
woodruffw merged 2 commits intopypa:mainfrom
AryanBagade:fix-cyclonedx-bomref-980

Conversation

@AryanBagade
Copy link
Copy Markdown
Contributor

Add affects field to vulnerabilities to link them to their components. Previously bom-refs didn't match, making it impossible to trace vulnerabilities to affected packages.

Fixes #980

Changes

  • Added affects field to Vulnerability objects using BomTarget
  • Each vulnerability now references its component via BomTarget(ref=c.bom_ref)
  • Updated CHANGELOG.md with fix entry

Add affects field to vulnerabilities to link them to their components. Previously bom-refs didn't match, making it impossible to trace vulnerabilities to affected packages.

Fixes pypa#980
Comment on lines +44 to +45
# BomTarget expects str in type hints, but accepts BomRef at runtime
affects=[BomTarget(ref=c.bom_ref)], # type: ignore[arg-type]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love the idea of ignoring the type hints here, even if it incidentally works -- that strongly suggests undocumented API behavior that could change at any time.

Is there a reason we can't use c.bom_ref.value instead? From the API docs, that seems to produce a str that should correspond to the right value to pass into a BomTarget.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is, the approach depends on whether bom_ref is manually set vs auto-generated.
When components have auto-generated bom_refs (which is the case in pip-audit), c.bom_ref.value returns None
The .value approach works when bom_ref is manually set as a string ( like in CycloneDX/cyclonedx-python-lib#547 )

Since pip-audit uses auto-generated bom_refs (no manual bom_ref= assignment), we need to pass the BomRef object itself rather than its .value property. The type mismatch is a quirk of the cyclonedx-python-lib API - the type hint says str, but the implementation handles BomRef objects for auto-generated refs.

The # type: ignore is unfortunate but necessary to work around the library's type annotation not matching its runtime behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also noticed that the Vulnerability class itself accepts bom_ref: str | BomRef (as shown in the docs), suggesting the library has a pattern of accepting both types even when type hints indicate otherwise. This supports using the BomRef object directly for auto-generated refs.
( https://cyclonedx-python-library.readthedocs.io/en/latest/autoapi/cyclonedx/model/vulnerability/index.html )

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's kind of not ideal -- do you think we could get those type hints fixed upstream? My experience with this CycloneDX dependency is that there have been significant breaking changes over time, so I'm hesitant to merge something that works unless it's actually encoded in the types.

As a stop-gap, I'd be okay with merging this if we also add a "backstop" test, i.e. add a test for the CycloneDX APIs themselves to ensure they don't deviate between releases. That way at least the tests will fail on dependency updates, which will give us advance warning if they change things 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm good idea will write the test

Copy link
Copy Markdown
Member

@woodruffw woodruffw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @AryanBagade! One question, otherwise this looks great to me. I appreciate you sending a patch for this!

@woodruffw woodruffw added the enhancement New feature or request label Nov 28, 2025
Copy link
Copy Markdown
Member

@woodruffw woodruffw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @AryanBagade!

@woodruffw woodruffw merged commit d191a22 into pypa:main Dec 1, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: cyclonedx-json vulnerabilities are not linked to components (different bomrefs)

2 participants