Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scanpipe/pipes/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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):
Expand Down
13 changes: 7 additions & 6 deletions scanpipe/pipes/spdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since #2360 explicitly asks for case-insensitive extensions, keeping this and making the two callers agree seems like the better of the two options I mentioned. Something like this in scanpipe/pipes/resolve.py:

# get_spdx_document_from_file (~L454)
if input_path.suffix.lower() in (".yml", ".yaml"):

# get_default_package_type (~L510)
if input_location.lower().endswith((".json", ".xml", ".yml", ".yaml")):

A test that calls resolve.get_default_package_type() with an uppercase .JSON SPDX file would then cover the real entry point and not only the helper.

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.

Hi @Jean-Regis-M, thanks for the review and suggestions,

I have updated scanpipe/pipes/resolve.py so that both get_default_package_type() and get_spdx_document_from_file() handle extensions case-insensitively, agreeing with is_spdx_document().

I also added tests in scanpipe/tests/pipes/test_resolve.py covering uppercase .JSON and .YML SPDX files for both detection and parsing, and rebased the branch on latest main to resolve merge conflicts.


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
17 changes: 17 additions & 0 deletions scanpipe/tests/pipes/test_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
67 changes: 67 additions & 0 deletions scanpipe/tests/pipes/test_spdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"))
Loading