diff --git a/scanpipe/pipes/resolve.py b/scanpipe/pipes/resolve.py index d238c47fc8..933ece7f40 100644 --- a/scanpipe/pipes/resolve.py +++ b/scanpipe/pipes/resolve.py @@ -451,7 +451,7 @@ def get_spdx_document_from_file(input_location): """Return the loaded SPDX document from the `input_location` file.""" input_path = Path(input_location) - if str(input_path).endswith((".yml", ".yaml")): + 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()) @@ -507,7 +507,7 @@ def get_default_package_type(input_location): if input_location.endswith(("bom.json", ".cdx.json", "bom.xml", ".cdx.xml")): return "cyclonedx" - if input_location.endswith((".json", ".xml", ".yml", ".yaml")): + if input_location.lower().endswith((".json", ".xml", ".yml", ".yaml")): if cyclonedx.is_cyclonedx_bom(input_location): return "cyclonedx" if spdx.is_spdx_document(input_location): diff --git a/scanpipe/pipes/spdx.py b/scanpipe/pipes/spdx.py index f785a8a32a..85c4a4054f 100644 --- a/scanpipe/pipes/spdx.py +++ b/scanpipe/pipes/spdx.py @@ -701,15 +701,16 @@ def validate_document(document, schema=SPDX_SCHEMA_2_3_PATH): def is_spdx_document(input_location): """Return True if the file at `input_location` is a SPDX Document.""" - input_location = str(input_location) - data = {} + location_lower = str(input_location).lower() with suppress(Exception): - if input_location.endswith(".json"): + data = {} + if location_lower.endswith(".json"): data = json.loads(Path(input_location).read_text()) - elif input_location.endswith((".yml", ".yaml")): + elif location_lower.endswith((".yml", ".yaml")): data = saneyaml.load(Path(input_location).read_text()) - if data.get("SPDXID"): - return True + if isinstance(data, dict) and data.get("SPDXID"): + return True + return False diff --git a/scanpipe/tests/pipes/test_resolve.py b/scanpipe/tests/pipes/test_resolve.py index be626f95e9..0f8594ec83 100644 --- a/scanpipe/tests/pipes/test_resolve.py +++ b/scanpipe/tests/pipes/test_resolve.py @@ -76,6 +76,15 @@ def test_scanpipe_pipes_resolve_get_default_package_type(self): input_location = self.data / "cyclonedx" / "laravel-7.12.0" / "bom.1.7.json" self.assertEqual("cyclonedx", resolve.get_default_package_type(input_location)) + with tempfile.TemporaryDirectory() as tmp_dir: + upper_json = Path(tmp_dir) / "spdx_doc.JSON" + upper_json.write_text('{"SPDXID": "SPDXRef-DOCUMENT"}') + self.assertEqual("spdx", resolve.get_default_package_type(upper_json)) + + upper_yml = Path(tmp_dir) / "spdx_doc.YML" + upper_yml.write_text("SPDXID: SPDXRef-DOCUMENT\n") + self.assertEqual("spdx", resolve.get_default_package_type(upper_yml)) + def test_scanpipe_pipes_resolve_set_license_expression(self): extracted_license_statement = {"license": "MIT"} data = resolve.set_license_expression( @@ -289,6 +298,14 @@ 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"]) + with tempfile.TemporaryDirectory() as tmp_dir: + upper_yml = Path(tmp_dir) / "test.YML" + upper_yml.write_text(input_location.read_text()) + spdx_document = resolve.get_spdx_document_from_file(upper_yml) + self.assertIsInstance(spdx_document, dict) + self.assertEqual("SPDXRef-DOCUMENT", spdx_document["SPDXID"]) + self.assertEqual("SPDX-2.2", spdx_document["spdxVersion"]) + 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 bcc940a132..abc8ae814f 100644 --- a/scanpipe/tests/pipes/test_spdx.py +++ b/scanpipe/tests/pipes/test_spdx.py @@ -20,6 +20,7 @@ # ScanCode.io is a free software code scanning tool from nexB Inc. and others. # Visit https://github.com/aboutcode-org/scancode.io for support and download. +import tempfile from pathlib import Path from unittest import TestCase @@ -412,3 +413,69 @@ def test_spdx_validate_document(self): with self.assertRaises(Exception): spdx.validate_document({}, self.schema_2_3) + + def test_spdx_is_spdx_document(self): + document_location = self.data / "spdx" / "example-2.3.1.json" + self.assertTrue(spdx.is_spdx_document(document_location)) + + with tempfile.TemporaryDirectory() as tmp_dir: + tmp_path = Path(tmp_dir) + + # Valid SPDX YAML document + yaml_spdx = tmp_path / "valid.spdx.yaml" + yaml_spdx.write_text("SPDXID: SPDXRef-DOCUMENT\nname: test\n") + self.assertTrue(spdx.is_spdx_document(yaml_spdx)) + + # Case-insensitive extensions + upper_json = tmp_path / "test.JSON" + upper_json.write_text('{"SPDXID": "SPDXRef-DOCUMENT"}') + self.assertTrue(spdx.is_spdx_document(upper_json)) + + upper_yml = tmp_path / "test.YML" + upper_yml.write_text("SPDXID: SPDXRef-DOCUMENT") + self.assertTrue(spdx.is_spdx_document(upper_yml)) + + # Top-level list in JSON + json_list = tmp_path / "list.json" + json_list.write_text('[{"name": "foo"}]') + self.assertFalse(spdx.is_spdx_document(json_list)) + + # Top-level list in YAML + yaml_list = tmp_path / "list.yaml" + yaml_list.write_text("- item1\n- item2\n") + self.assertFalse(spdx.is_spdx_document(yaml_list)) + + # JSON and YAML without SPDXID + no_spdx_json = tmp_path / "no_spdx.json" + no_spdx_json.write_text('{"name": "test"}') + self.assertFalse(spdx.is_spdx_document(no_spdx_json)) + + no_spdx_yaml = tmp_path / "no_spdx.yaml" + no_spdx_yaml.write_text("name: test") + self.assertFalse(spdx.is_spdx_document(no_spdx_yaml)) + + # Primitive types in JSON and YAML + str_json = tmp_path / "string.json" + str_json.write_text('"just a string"') + self.assertFalse(spdx.is_spdx_document(str_json)) + + str_yaml = tmp_path / "string.yaml" + str_yaml.write_text("just a string") + self.assertFalse(spdx.is_spdx_document(str_yaml)) + + int_json = tmp_path / "number.json" + int_json.write_text("42") + self.assertFalse(spdx.is_spdx_document(int_json)) + + # Empty file + empty_file = tmp_path / "empty.json" + empty_file.write_text("") + self.assertFalse(spdx.is_spdx_document(empty_file)) + + # Non-json/yaml file + txt_file = tmp_path / "file.txt" + txt_file.write_text('{"SPDXID": "SPDXRef-DOCUMENT"}') + self.assertFalse(spdx.is_spdx_document(txt_file)) + + # Non-existent file + self.assertFalse(spdx.is_spdx_document(tmp_path / "non_existent.json"))