From 17ee8447f12332a56f1630eb9410281e52261bd8 Mon Sep 17 00:00:00 2001 From: JEAN REGIS <190766850+Jean-Regis-M@users.noreply.github.com> Date: Sun, 22 Nov 2026 11:33:47 +0300 Subject: [PATCH] fix: do not load SPDX tag-value .spdx files as JSON #2363 get_default_package_type() returned "spdx" for any ".spdx" file, and get_spdx_document_from_file() then parsed it with json.loads(). ".spdx" is the usual extension of the SPDX tag-value format, so a tag-value file made load_sbom fail with a raw JSONDecodeError, even when the input also contained valid SBOMs. - Only consider a ".spdx" file as SPDX when it contains an SPDX JSON document. ".spdx.json" and ".spdx.yml" are unchanged. - Add a project warning for a tag-value ".spdx" file, so the other SBOMs are still loaded. - Parse the document inside the try block of get_spdx_document_from_file, so invalid JSON/YAML raises the "SPDX document ... is not valid" error. Signed-off-by: JEAN REGIS <190766850+Jean-Regis-M@users.noreply.github.com> --- scanpipe/pipes/resolve.py | 35 ++++++++++++++++++++----- scanpipe/pipes/spdx.py | 4 ++- scanpipe/tests/data/spdx/tag-value.spdx | 11 ++++++++ scanpipe/tests/pipes/test_resolve.py | 25 ++++++++++++++++++ scanpipe/tests/pipes/test_spdx.py | 10 +++++++ scanpipe/tests/test_pipelines.py | 22 ++++++++++++++++ 6 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 scanpipe/tests/data/spdx/tag-value.spdx diff --git a/scanpipe/pipes/resolve.py b/scanpipe/pipes/resolve.py index 4292647fdf..af46725589 100644 --- a/scanpipe/pipes/resolve.py +++ b/scanpipe/pipes/resolve.py @@ -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): + """ + 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) + + 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" if input_location.endswith(("bom.json", ".cdx.json", "bom.xml", ".cdx.xml")): diff --git a/scanpipe/pipes/spdx.py b/scanpipe/pipes/spdx.py index 85c4a4054f..095fb96729 100644 --- a/scanpipe/pipes/spdx.py +++ b/scanpipe/pipes/spdx.py @@ -705,7 +705,9 @@ def is_spdx_document(input_location): with suppress(Exception): data = {} - if location_lower.endswith(".json"): + # ".spdx" is also used for SPDX JSON documents. A tag-value document + # fails to parse as JSON and is not considered a SPDX document here. + if location_lower.endswith((".json", ".spdx")): data = json.loads(Path(input_location).read_text()) elif location_lower.endswith((".yml", ".yaml")): data = saneyaml.load(Path(input_location).read_text()) diff --git a/scanpipe/tests/data/spdx/tag-value.spdx b/scanpipe/tests/data/spdx/tag-value.spdx new file mode 100644 index 0000000000..49f2527ccf --- /dev/null +++ b/scanpipe/tests/data/spdx/tag-value.spdx @@ -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 diff --git a/scanpipe/tests/pipes/test_resolve.py b/scanpipe/tests/pipes/test_resolve.py index 51d1a33eb0..944d942f36 100644 --- a/scanpipe/tests/pipes/test_resolve.py +++ b/scanpipe/tests/pipes/test_resolve.py @@ -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)) + 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) diff --git a/scanpipe/tests/pipes/test_spdx.py b/scanpipe/tests/pipes/test_spdx.py index abc8ae814f..0e6466ab25 100644 --- a/scanpipe/tests/pipes/test_spdx.py +++ b/scanpipe/tests/pipes/test_spdx.py @@ -445,6 +445,16 @@ def test_spdx_is_spdx_document(self): yaml_list.write_text("- item1\n- item2\n") self.assertFalse(spdx.is_spdx_document(yaml_list)) + # SPDX JSON document saved with the ".spdx" extension + spdx_json = tmp_path / "document.spdx" + spdx_json.write_text('{"SPDXID": "SPDXRef-DOCUMENT"}') + self.assertTrue(spdx.is_spdx_document(spdx_json)) + + # SPDX tag-value document + tag_value = tmp_path / "tag-value.spdx" + tag_value.write_text("SPDXVersion: SPDX-2.3\nSPDXID: SPDXRef-DOCUMENT\n") + self.assertFalse(spdx.is_spdx_document(tag_value)) + # JSON and YAML without SPDXID no_spdx_json = tmp_path / "no_spdx.json" no_spdx_json.write_text('{"name": "test"}') diff --git a/scanpipe/tests/test_pipelines.py b/scanpipe/tests/test_pipelines.py index 1170ecab6b..d752303ae5 100644 --- a/scanpipe/tests/test_pipelines.py +++ b/scanpipe/tests/test_pipelines.py @@ -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]) + def test_scanpipe_load_sbom_pipeline_cyclonedx_integration(self): pipeline_name = "load_sbom" project1 = make_project()