Skip to content

feat: improved sbom filename extension handling - #4919

Closed
Saksham-Sirohi wants to merge 3 commits into
ossf:mainfrom
Saksham-Sirohi:improved-sbom-filename-extension-handling
Closed

Saksham-Sirohi wants to merge 3 commits into
ossf:mainfrom
Saksham-Sirohi:improved-sbom-filename-extension-handling

Conversation

@Saksham-Sirohi

Copy link
Copy Markdown
Contributor

Summary

This PR fixes SBOM parsing test failures caused by invalid CycloneDX file extensions. It enforces strict extension validation for CycloneDX files and improves error handling while ensuring backward compatibility.


Changes Introduced

  • Extension Validation:
    • CycloneDX SBOMs now require .json or .xml extensions. Invalid extensions trigger explicit error logging.
  • Test Fixes:
    • Updated test cases to use valid SBOM files from the test/sbom directory.
    • Added checks for error logging and empty results when invalid extensions are detected.
  • Code Refactoring:
    • Simplified extension validation logic in parse.py.

Checklist

  • ✅ Code adheres to project standards.
  • ✅ Tests pass (including updated SBOM test cases).

Steps to Test

pytest test/test_sbom.py -v

Verified

  • Tests for invalid CycloneDX extensions log errors and return empty results.
  • Valid CycloneDX files (JSON/XML) parse successfully.

Related Issues
Fixes #4836

@Saksham-Sirohi
Saksham-Sirohi force-pushed the improved-sbom-filename-extension-handling branch from 4389a69 to 780892a Compare March 13, 2025 09:43
@Saksham-Sirohi

Copy link
Copy Markdown
Contributor Author

hi @terriko required changes for URL sanitation has been made please take a look

@terriko

terriko commented Apr 14, 2025

Copy link
Copy Markdown
Collaborator

Ugh, looking at this again, I realize that we're going to have issues because splitting on . will cause problems when we have things like \. in the filename. Which I realize is unlikely but sloppy filename parsing in a security tool obviously is a bad look. I don't have the time necessary to help you get this into PR shape so I'm going to close it, but if you want to look at again please consider using the built in stuff from pathlib to make this work.

@terriko terriko closed this Apr 14, 2025
vineoy added a commit to vineoy/cve-bin-tool that referenced this pull request Oct 23, 2026
lib4sbom's parser selects the file format from the filename extension
and rejects files with an unknown (or absent) extension without even
looking at their content, so an SBOM file without an extension could
crash cve-bin-tool with a confusing SBOMParserException traceback or
fail auto-detection silently.

- SBOMParse now falls back to identifying the file format from the
  file contents (lib4sbom parse_string) when the extension is not
  recognised, with clear warnings stating the detected format
- non-SBOM content and files without components now produce clear
  error messages instead of a raw parser exception or KeyErrors
- sbom_detection() identifies CycloneDX, SWID and SPDX (json/yaml/tag)
  data from file contents, and falls back on 'sbom.spdx.*' style names;
  extension handling is done with pathlib instead of substrings
- add_extension_if_not() rewritten with pathlib (per code-review
  guidance on PR ossf#4919), keeping multi-dot names and its extension
  behaviour intact while supporting SBOM extensions (spdx, yaml)
- SPDX SBOM generation now appends the proper extension for the used
  format (tag -> .spdx, json -> .json, yaml -> .yaml)
- documented that SBOM files without matching extensions are
  identified from contents

Fixes ossf#4836

Signed-off-by: vinayak <singhvinayak288@gmail.com>
vineoy added a commit to vineoy/cve-bin-tool that referenced this pull request Oct 27, 2026
lib4sbom's parser selects the file format from the filename extension
and rejects files with an unknown (or absent) extension without even
looking at their content, so an SBOM file without a recognised
extension could crash cve-bin-tool with a raw SBOMParserException
traceback (issue ossf#4836) or fail auto-detection silently.

- SBOMParse now catches SBOMParserException and reports a clear error
  message instead of a traceback
- an SBOM without any software components now produces a clear error
  message instead of a KeyError traceback
- sbom_detection() identifies CycloneDX, SWID and SPDX (json/yaml/tag)
  data from file contents, falling back on 'sbom.spdx.*' style names;
  extension handling is done with pathlib instead of substrings
- add_extension_if_not() rewritten with pathlib (per code-review
  guidance on PR ossf#4919), keeping multi-dot names and its extension
  behaviour intact while supporting SBOM extensions (spdx, yaml)
- SPDX SBOM generation now appends the proper extension for the used
  format (tag -> .spdx, json -> .json, yaml -> .yaml)
- documented that SBOM files which do not follow the filename
  conventions produce an error message instead of a traceback

Content-based parsing of SBOM files whose filename extension is not
recognised is being implemented upstream in lib4sbom
(anthonyharrison/lib4sbom#102); once that is released, cve-bin-tool
will be able to read SBOM files regardless of their filename
extension.

Signed-off-by: vinayak <singhvinayak288@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: improved sbom filename extension handling

3 participants