Skip to content
Open
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
35 changes: 29 additions & 6 deletions scanpipe/pipes/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

get_manifest_resources is also used by the resolve_dependencies pipeline, which does not load SBOMs. A .spdx file 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?

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)

Expand Down Expand Up @@ -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}')
Expand Down Expand Up @@ -490,6 +498,16 @@ def resolve_spdx_dependencies(input_location):
]


def is_spdx_tag_value_file(input_location):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is SPDX-specific, spdx.py next to is_spdx_document would be a better place, no?

"""
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This returns True for any .spdx file that is not SPDX JSON: empty file, malformed JSON, YAML. The warning then tells the user it is a tag-value document. What about checking the content for the SPDXVersion: tag instead?



def get_default_package_type(input_location):
"""
Return the package type associated with the provided `input_location`.
Expand All @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Now that is_spdx_document handles .spdx, this dedicated branch is not needed. Adding ".spdx" to the generic check below should be enough, and it also covers .SPDX, consistent with #2361:

    if input_location.lower().endswith((".json", ".xml", ".yml", ".yaml", ".spdx")):
Suggested change
# ".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")):
Expand Down
4 changes: 3 additions & 1 deletion scanpipe/pipes/spdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
11 changes: 11 additions & 0 deletions scanpipe/tests/data/spdx/tag-value.spdx
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
25 changes: 25 additions & 0 deletions scanpipe/tests/pipes/test_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing the case of a SPDX JSON document saved as .spdx: the not spdx.is_spdx_document() path returning False is not tested.


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
10 changes: 10 additions & 0 deletions scanpipe/tests/pipes/test_spdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}')
Expand Down
22 changes: 22 additions & 0 deletions scanpipe/tests/test_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's assert the full values:

Suggested change
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])
message = project1.projectmessages.get(severity="warning")
expected_description = (
"SPDX tag-value documents are not supported, "
"please provide the SPDX document in JSON or YAML format."
)
self.assertEqual(expected_description, message.description)
self.assertEqual({"resource_path": "tag-value.spdx"}, message.details)


def test_scanpipe_load_sbom_pipeline_cyclonedx_integration(self):
pipeline_name = "load_sbom"
project1 = make_project()
Expand Down
Loading