Skip to content

Commit 6d31421

Browse files
committed
BUG: Fix SBOM JSON escaping, Python3 guard, and SPDX metadata issues
Address all findings from greptile code review: P1: Escape all user-supplied fields (version, download location, license, copyright) through _itk_sbom_json_escape() before JSON interpolation — both in itk_generate_sbom() and itk_sbom_register_package(). Previously only description was escaped. P1: Add Python3_EXECUTABLE guard in ITKSBOMValidation.cmake to skip SBOM validation tests when Python3 is not available. P2: Use foreach(... IN LISTS ...) instead of unquoted variable expansion to prevent re-splitting on embedded semicolons. P2: Fix PNG SPDX license identifier from "Libpng-2.0" to "Libpng" (the valid SPDX license list entry for libpng 1.6.x). P2: Expose licenseListVersion as ITK_SBOM_SPDX_LICENSE_LIST_VERSION cache variable (default "3.25") instead of hardcoding "3.22".
1 parent 6359e81 commit 6d31421

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

CMake/ITKSBOMGeneration.cmake

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ if(NOT ITK_GENERATE_SBOM)
2626
return()
2727
endif()
2828

29+
set(
30+
ITK_SBOM_SPDX_LICENSE_LIST_VERSION
31+
"3.25"
32+
CACHE STRING
33+
"SPDX license list version recorded in the generated SBOM"
34+
)
35+
2936
#-----------------------------------------------------------------------------
3037
# Allow remote modules to register SBOM package metadata.
3138
#
@@ -90,24 +97,40 @@ function(itk_sbom_register_package)
9097
# Sanitize the name for use as SPDX ID (only alphanumeric and -)
9198
string(REGEX REPLACE "[^A-Za-z0-9-]" "-" _spdx_id "${_pkg_NAME}")
9299

100+
# Escape all user-supplied fields for JSON safety
101+
_itk_sbom_json_escape("${_pkg_NAME}" _pkg_NAME_escaped)
102+
_itk_sbom_json_escape("${_pkg_VERSION}" _pkg_VERSION_escaped)
103+
_itk_sbom_json_escape("${_pkg_DOWNLOAD_LOCATION}" _pkg_DOWNLOAD_escaped)
104+
_itk_sbom_json_escape("${_pkg_SUPPLIER}" _pkg_SUPPLIER_escaped)
105+
_itk_sbom_json_escape("${_pkg_SPDX_LICENSE}" _pkg_LICENSE_escaped)
106+
_itk_sbom_json_escape("${_pkg_COPYRIGHT}" _pkg_COPYRIGHT_escaped)
107+
93108
set(_entry "")
94109
string(APPEND _entry " {\n")
95110
string(APPEND _entry " \"SPDXID\": \"SPDXRef-${_spdx_id}\",\n")
96-
string(APPEND _entry " \"name\": \"${_pkg_NAME}\",\n")
97-
string(APPEND _entry " \"versionInfo\": \"${_pkg_VERSION}\",\n")
111+
string(APPEND _entry " \"name\": \"${_pkg_NAME_escaped}\",\n")
112+
string(APPEND _entry " \"versionInfo\": \"${_pkg_VERSION_escaped}\",\n")
113+
string(
114+
APPEND
115+
_entry
116+
" \"downloadLocation\": \"${_pkg_DOWNLOAD_escaped}\",\n"
117+
)
118+
string(APPEND _entry " \"supplier\": \"${_pkg_SUPPLIER_escaped}\",\n")
119+
string(
120+
APPEND
121+
_entry
122+
" \"licenseConcluded\": \"${_pkg_LICENSE_escaped}\",\n"
123+
)
98124
string(
99125
APPEND
100126
_entry
101-
" \"downloadLocation\": \"${_pkg_DOWNLOAD_LOCATION}\",\n"
127+
" \"licenseDeclared\": \"${_pkg_LICENSE_escaped}\",\n"
102128
)
103-
string(APPEND _entry " \"supplier\": \"${_pkg_SUPPLIER}\",\n")
104129
string(
105130
APPEND
106131
_entry
107-
" \"licenseConcluded\": \"${_pkg_SPDX_LICENSE}\",\n"
132+
" \"copyrightText\": \"${_pkg_COPYRIGHT_escaped}\",\n"
108133
)
109-
string(APPEND _entry " \"licenseDeclared\": \"${_pkg_SPDX_LICENSE}\",\n")
110-
string(APPEND _entry " \"copyrightText\": \"${_pkg_COPYRIGHT}\",\n")
111134
string(APPEND _entry " \"filesAnalyzed\": false\n")
112135
string(APPEND _entry " }")
113136

@@ -174,7 +197,11 @@ function(itk_generate_sbom)
174197
string(APPEND _json " \"Tool: CMake-${CMAKE_VERSION}\",\n")
175198
string(APPEND _json " \"Organization: NumFOCUS\"\n")
176199
string(APPEND _json " ],\n")
177-
string(APPEND _json " \"licenseListVersion\": \"3.22\"\n")
200+
string(
201+
APPEND
202+
_json
203+
" \"licenseListVersion\": \"${ITK_SBOM_SPDX_LICENSE_LIST_VERSION}\"\n"
204+
)
178205
string(APPEND _json " },\n")
179206

180207
# --- packages array ---
@@ -238,7 +265,12 @@ function(itk_generate_sbom)
238265
set(_pkg_copyright "NOASSERTION")
239266
endif()
240267

241-
# Get description from module declaration and escape for JSON
268+
# Escape all user-supplied fields for JSON safety
269+
_itk_sbom_json_escape("${_pkg_version}" _pkg_version)
270+
_itk_sbom_json_escape("${_pkg_download}" _pkg_download)
271+
_itk_sbom_json_escape("${_pkg_license}" _pkg_license)
272+
_itk_sbom_json_escape("${_pkg_copyright}" _pkg_copyright)
273+
242274
set(_pkg_description "${ITK_MODULE_${_mod}_DESCRIPTION}")
243275
if(_pkg_description)
244276
_itk_sbom_json_escape("${_pkg_description}" _pkg_description)
@@ -314,7 +346,7 @@ function(itk_generate_sbom)
314346

315347
# Append extra packages registered by remote modules
316348
get_property(_extra_packages GLOBAL PROPERTY ITK_SBOM_EXTRA_PACKAGES)
317-
foreach(_extra_pkg ${_extra_packages})
349+
foreach(_extra_pkg IN LISTS _extra_packages)
318350
string(APPEND _json ",\n${_extra_pkg}")
319351
endforeach()
320352

@@ -342,7 +374,7 @@ function(itk_generate_sbom)
342374

343375
# Extra packages registered by remote modules
344376
get_property(_extra_spdx_ids GLOBAL PROPERTY ITK_SBOM_EXTRA_SPDX_IDS)
345-
foreach(_spdx_id ${_extra_spdx_ids})
377+
foreach(_spdx_id IN LISTS _extra_spdx_ids)
346378
string(APPEND _json ",\n")
347379
string(APPEND _json " {\n")
348380
string(APPEND _json " \"spdxElementId\": \"SPDXRef-ITK\",\n")

CMake/ITKSBOMValidation.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ if(NOT EXISTS "${_sbom_file}")
1818
return()
1919
endif()
2020

21+
if(NOT Python3_EXECUTABLE)
22+
message(WARNING "Python3 not found; skipping SBOM validation tests.")
23+
return()
24+
endif()
25+
2126
add_test(
2227
NAME ITKSBOMValidation
2328
COMMAND

Modules/ThirdParty/PNG/itk-module.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ itk_module(
1111
ITKZLIB
1212
DESCRIPTION "${DOCUMENTATION}"
1313
SPDX_LICENSE
14-
"Libpng-2.0"
14+
"Libpng"
1515
SPDX_VERSION
1616
"1.6.54"
1717
SPDX_DOWNLOAD_LOCATION

0 commit comments

Comments
 (0)