Repository navigation
fix: do not load SPDX tag-value .spdx files as JSON #2363 #2365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -219,6 +219,15 @@ def get_manifest_resources(project): | |||||||||
| manifest_type = get_default_package_type(input_location=resource.location) | ||||||||||
| if manifest_type: | ||||||||||
| resource.update(status=flag.APPLICATION_PACKAGE) | ||||||||||
| elif is_spdx_tag_value_file(resource.location): | ||||||||||
| project.add_warning( | ||||||||||
| description=( | ||||||||||
| "SPDX tag-value documents are not supported, " | ||||||||||
| "please provide the SPDX document in JSON or YAML format." | ||||||||||
| ), | ||||||||||
| model="get_manifest_resources", | ||||||||||
| object_instance=resource, | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| return project.codebaseresources.filter(status=flag.APPLICATION_PACKAGE) | ||||||||||
|
|
||||||||||
|
|
@@ -451,12 +460,11 @@ def get_spdx_document_from_file(input_location): | |||||||||
| """Return the loaded SPDX document from the `input_location` file.""" | ||||||||||
| input_path = Path(input_location) | ||||||||||
|
|
||||||||||
| if input_path.suffix.lower() in (".yml", ".yaml"): | ||||||||||
| spdx_document = saneyaml.load(input_path.read_text()) | ||||||||||
| else: | ||||||||||
| spdx_document = json.loads(input_path.read_text()) | ||||||||||
|
|
||||||||||
| try: | ||||||||||
| if input_path.suffix.lower() in (".yml", ".yaml"): | ||||||||||
| spdx_document = saneyaml.load(input_path.read_text()) | ||||||||||
| else: | ||||||||||
| spdx_document = json.loads(input_path.read_text()) | ||||||||||
| spdx.validate_document(spdx_document) | ||||||||||
| except Exception as e: | ||||||||||
| raise Exception(f'SPDX document "{input_path.name}" is not valid: {e}') | ||||||||||
|
|
@@ -490,6 +498,16 @@ def resolve_spdx_dependencies(input_location): | |||||||||
| ] | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def is_spdx_tag_value_file(input_location): | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is SPDX-specific, |
||||||||||
| """ | ||||||||||
| Return True if the file at `input_location` has the ".spdx" extension of the | ||||||||||
| SPDX tag-value format but does not contain an SPDX JSON document. | ||||||||||
| """ | ||||||||||
| if not str(input_location).endswith(".spdx"): | ||||||||||
| return False | ||||||||||
| return not spdx.is_spdx_document(input_location) | ||||||||||
|
Comment on lines
+506
to
+508
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns True for any |
||||||||||
|
|
||||||||||
|
|
||||||||||
| def get_default_package_type(input_location): | ||||||||||
| """ | ||||||||||
| Return the package type associated with the provided `input_location`. | ||||||||||
|
|
@@ -501,7 +519,12 @@ def get_default_package_type(input_location): | |||||||||
| if handler.is_datafile(input_location): | ||||||||||
| return handler.default_package_type | ||||||||||
|
|
||||||||||
| if input_location.endswith((".spdx", ".spdx.json", ".spdx.yml")): | ||||||||||
| if input_location.endswith((".spdx.json", ".spdx.yml")): | ||||||||||
| return "spdx" | ||||||||||
|
|
||||||||||
| # ".spdx" is the usual extension of the SPDX tag-value format, which is not | ||||||||||
| # supported: only consider a ".spdx" file that contains an SPDX JSON document. | ||||||||||
| if input_location.endswith(".spdx") and spdx.is_spdx_document(input_location): | ||||||||||
| return "spdx" | ||||||||||
|
|
||||||||||
|
Comment on lines
+525
to
529
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that if input_location.lower().endswith((".json", ".xml", ".yml", ".yaml", ".spdx")):
Suggested change
|
||||||||||
| if input_location.endswith(("bom.json", ".cdx.json", "bom.xml", ".cdx.xml")): | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| SPDXVersion: SPDX-2.3 | ||
| DataLicense: CC0-1.0 | ||
| SPDXID: SPDXRef-DOCUMENT | ||
| DocumentName: demo | ||
| DocumentNamespace: https://example.com/demo | ||
| Creator: Tool: demo | ||
| Created: 2026-01-01T00:00:00Z | ||
|
|
||
| PackageName: demo | ||
| SPDXID: SPDXRef-Package-demo | ||
| PackageDownloadLocation: NOASSERTION |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,17 @@ def test_scanpipe_pipes_resolve_get_default_package_type(self): | |
| input_location = self.manifest_location / "curl-7.70.0.yaml" | ||
| self.assertEqual("spdx", resolve.get_default_package_type(input_location)) | ||
|
|
||
| # ".spdx" is the extension of the SPDX tag-value format, which is not supported | ||
| input_location = self.data / "spdx" / "tag-value.spdx" | ||
| self.assertIsNone(resolve.get_default_package_type(input_location)) | ||
|
|
||
| # an SPDX JSON document stored with the ".spdx" extension is supported | ||
| with tempfile.TemporaryDirectory() as tmp_dir: | ||
| input_location = Path(tmp_dir) / "toml.spdx" | ||
| spdx_json = self.manifest_location / "toml.spdx.json" | ||
| input_location.write_text(spdx_json.read_text()) | ||
| self.assertEqual("spdx", resolve.get_default_package_type(input_location)) | ||
|
|
||
| input_location = self.data / "cyclonedx" / "nested.cdx.json" | ||
| self.assertEqual("cyclonedx", resolve.get_default_package_type(input_location)) | ||
|
|
||
|
|
@@ -306,6 +317,20 @@ def test_scanpipe_pipes_resolve_get_spdx_document_from_file(self): | |
| self.assertEqual("SPDXRef-DOCUMENT", spdx_document["SPDXID"]) | ||
| self.assertEqual("SPDX-2.2", spdx_document["spdxVersion"]) | ||
|
|
||
| def test_scanpipe_pipes_resolve_get_spdx_document_from_file_invalid_json(self): | ||
| with tempfile.TemporaryDirectory() as tmp_dir: | ||
| input_location = Path(tmp_dir) / "invalid.spdx.json" | ||
| input_location.write_text("not json") | ||
| with self.assertRaisesMessage(Exception, "is not valid"): | ||
| resolve.get_spdx_document_from_file(input_location) | ||
|
|
||
| def test_scanpipe_pipes_resolve_is_spdx_tag_value_file(self): | ||
| input_location = self.data / "spdx" / "tag-value.spdx" | ||
| self.assertTrue(resolve.is_spdx_tag_value_file(input_location)) | ||
|
|
||
| input_location = self.manifest_location / "toml.spdx.json" | ||
| self.assertFalse(resolve.is_spdx_tag_value_file(input_location)) | ||
|
Comment on lines
+331
to
+332
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing the case of a SPDX JSON document saved as |
||
|
|
||
| def test_scanpipe_pipes_resolve_spdx_package_to_package_data(self): | ||
| p1 = Project.objects.create(name="Analysis") | ||
| package = pipes.update_or_create_package(p1, package_data1) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2185,6 +2185,28 @@ def test_scanpipe_load_sbom_pipeline_spdx_integration(self): | |||||||||||||||||||||||
| self.assertEqual("MIT", discoveredpackage.extracted_license_statement) | ||||||||||||||||||||||||
| self.assertEqual("mit", discoveredpackage.declared_license_expression) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_scanpipe_load_sbom_pipeline_spdx_tag_value_not_supported(self): | ||||||||||||||||||||||||
| pipeline_name = "load_sbom" | ||||||||||||||||||||||||
| project1 = make_project() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| project1.copy_input_from(self.data / "manifests" / "toml.spdx.json") | ||||||||||||||||||||||||
| project1.copy_input_from(self.data / "spdx" / "tag-value.spdx") | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| run = project1.add_pipeline(pipeline_name) | ||||||||||||||||||||||||
| pipeline = run.make_pipeline_instance() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| exitcode, out = pipeline.execute() | ||||||||||||||||||||||||
| self.assertEqual(0, exitcode, msg=out) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| # the SPDX JSON document is still loaded | ||||||||||||||||||||||||
| self.assertEqual(1, project1.discoveredpackages.count()) | ||||||||||||||||||||||||
| self.assertEqual("toml", project1.discoveredpackages.get().name) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| message = project1.projectmessages.get(severity="warning") | ||||||||||||||||||||||||
| self.assertIn("SPDX tag-value documents are not supported", message.description) | ||||||||||||||||||||||||
| resource_path = message.details["resource_path"] | ||||||||||||||||||||||||
| self.assertEqual("tag-value.spdx", resource_path.split("/")[-1]) | ||||||||||||||||||||||||
|
Comment on lines
+2205
to
+2208
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's assert the full values:
Suggested change
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_scanpipe_load_sbom_pipeline_cyclonedx_integration(self): | ||||||||||||||||||||||||
| pipeline_name = "load_sbom" | ||||||||||||||||||||||||
| project1 = make_project() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_manifest_resourcesis also used by theresolve_dependenciespipeline, which does not load SBOMs. A.spdxfile in that codebase gets this warning, and asking for JSON or YAML does not help there since SPDX is not resolved by that pipeline either. Is it expected?