From 3ef7b2ac4a8d3c057678dfb991346567c47d66c4 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Fri, 9 Oct 2026 15:41:47 +0200 Subject: [PATCH] fix(sbom): SPDX licences are declared, and copyleft is found by SPDX id SPDX export: the licence goes into licenseDeclared, and licenseConcluded is NOASSERTION. Licences are what the supplier declares; nothing is concluded without a licence review of our own (decided 2026-10-06, BSI TR-03183-2). Copyleft check in the SBOM licence summary: each term of the licence's SPDX form (the shared table from #543) is compared, so "GNU Lesser General Public License v2.1" or "MIT OR GPL-3.0-only" is flagged. A name the table does not know is still flagged when it contains a copyleft id, as before. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p --- compliance-agent/src/api/handlers/sbom.rs | 24 +--------- compliance-core/src/sbom_docs.rs | 20 ++++++--- compliance-core/src/spdx_license.rs | 53 +++++++++++++++++++++++ 3 files changed, 67 insertions(+), 30 deletions(-) diff --git a/compliance-agent/src/api/handlers/sbom.rs b/compliance-agent/src/api/handlers/sbom.rs index 8adb441..8747f49 100644 --- a/compliance-agent/src/api/handlers/sbom.rs +++ b/compliance-agent/src/api/handlers/sbom.rs @@ -16,25 +16,6 @@ pub(crate) const MAX_EXPORT_COMPONENTS: u64 = 100_000; /// Package names listed per license in the summary; `count` stays exact. const MAX_LICENSE_PACKAGES: i64 = 100; -const COPYLEFT_LICENSES: &[&str] = &[ - "GPL-2.0", - "GPL-2.0-only", - "GPL-2.0-or-later", - "GPL-3.0", - "GPL-3.0-only", - "GPL-3.0-or-later", - "AGPL-3.0", - "AGPL-3.0-only", - "AGPL-3.0-or-later", - "LGPL-2.1", - "LGPL-2.1-only", - "LGPL-2.1-or-later", - "LGPL-3.0", - "LGPL-3.0-only", - "LGPL-3.0-or-later", - "MPL-2.0", -]; - #[tracing::instrument(skip_all)] pub async fn sbom_filters( Extension(agent): AgentExt, @@ -278,11 +259,8 @@ pub async fn license_summary( cursor .filter_map(|row| async move { let row: LicenseRow = mongodb::bson::from_document(row.ok()?).ok()?; - let upper = row.license.to_uppercase(); Some(LicenseSummary { - is_copyleft: COPYLEFT_LICENSES - .iter() - .any(|c| upper.contains(&c.to_uppercase())), + is_copyleft: compliance_core::spdx_license::is_copyleft(&row.license), license: row.license, count: u64::try_from(row.count).unwrap_or(0), packages: row.packages, diff --git a/compliance-core/src/sbom_docs.rs b/compliance-core/src/sbom_docs.rs index de77a2f..45a1f1b 100644 --- a/compliance-core/src/sbom_docs.rs +++ b/compliance-core/src/sbom_docs.rs @@ -329,8 +329,10 @@ pub fn spdx(entries: &[SbomEntry], subject: &str) -> serde_json::Value { .iter() .enumerate() .map(|(i, e)| { - // SPDX takes only an expression of listed ids here; another - // license is NOASSERTION, its text kept as a comment. + // The license is what the supplier declares; nothing is + // concluded without a license review of our own. SPDX takes only + // an expression of listed ids; another license is NOASSERTION, + // its text kept as a comment. let raw = e .license .as_deref() @@ -344,7 +346,8 @@ pub fn spdx(entries: &[SbomEntry], subject: &str) -> serde_json::Value { "name": e.name, "versionInfo": e.version, "downloadLocation": "NOASSERTION", - "licenseConcluded": license.as_deref().unwrap_or("NOASSERTION"), + "licenseConcluded": "NOASSERTION", + "licenseDeclared": license.as_deref().unwrap_or("NOASSERTION"), "externalRefs": e.purl.as_ref().map(|p| vec![serde_json::json!({ "referenceCategory": "PACKAGE-MANAGER", "referenceType": "purl", @@ -415,14 +418,17 @@ mod tests { let spdx = spdx(&entries, "t"); let package = |i: usize| &spdx["packages"][i]; - assert_eq!(package(0)["licenseConcluded"], "Apache-2.0"); - assert_eq!(package(1)["licenseConcluded"], "MIT"); - assert_eq!(package(2)["licenseConcluded"], "NOASSERTION"); + for i in 0..4 { + assert_eq!(package(i)["licenseConcluded"], "NOASSERTION"); + } + assert_eq!(package(0)["licenseDeclared"], "Apache-2.0"); + assert_eq!(package(1)["licenseDeclared"], "MIT"); + assert_eq!(package(2)["licenseDeclared"], "NOASSERTION"); assert_eq!( package(2)["licenseComments"], "Declared: Custom Pump License" ); - assert_eq!(package(3)["licenseConcluded"], "MIT OR Apache-2.0"); + assert_eq!(package(3)["licenseDeclared"], "MIT OR Apache-2.0"); assert!(package(3).get("licenseComments").is_none()); } } diff --git a/compliance-core/src/spdx_license.rs b/compliance-core/src/spdx_license.rs index c83d922..0d9da01 100644 --- a/compliance-core/src/spdx_license.rs +++ b/compliance-core/src/spdx_license.rs @@ -88,6 +88,33 @@ pub fn normalise(license: &str) -> Cow<'_, str> { known.map_or(Cow::Borrowed(license), Cow::Borrowed) } +/// The copyleft licences, as SPDX ids without `-only`/`-or-later`. +const COPYLEFT: [&str; 7] = [ + "GPL-2.0", "GPL-3.0", "AGPL-3.0", "LGPL-2.0", "LGPL-2.1", "LGPL-3.0", "MPL-2.0", +]; + +/// Whether `license` is or includes a copyleft licence: any term of its SPDX +/// form ([`normalise`]), so "GNU Lesser General Public License v2.1" and +/// `MIT OR GPL-3.0-only` are. A name this module does not know is copyleft +/// when it contains a copyleft id (`GPL-2.0 with exceptions`). +pub fn is_copyleft(license: &str) -> bool { + let is_copyleft_id = |id: &str| { + let base = id.strip_suffix('+').unwrap_or(id); + let base = base + .strip_suffix("-only") + .or_else(|| base.strip_suffix("-or-later")) + .unwrap_or(base); + COPYLEFT.iter().any(|c| c.eq_ignore_ascii_case(base)) + }; + let spdx = normalise(license); + let by_term = spdx + .split(|c: char| c.is_whitespace() || c == '(' || c == ')') + .filter(|t| !t.is_empty() && !OPERATORS.contains(t)) + .any(is_copyleft_id); + let upper = license.to_ascii_uppercase(); + by_term || COPYLEFT.iter().any(|c| upper.contains(c)) +} + /// Whether `text` joins terms with an SPDX operator (`MIT OR Apache-2.0`), /// as an expression does. pub fn has_operator(text: &str) -> bool { @@ -345,6 +372,32 @@ pub fn spdx_of_name(name: &str) -> Option<&'static str> { mod tests { use super::*; + #[test] + fn copyleft_is_found_in_names_ids_and_expressions() { + for license in [ + "GNU Lesser General Public License v2.1", + "GNU General Public License, version 2", + "LGPL-2.1-or-later", + "gpl-3.0-only", + "GPL-2.0+", + "MIT OR GPL-3.0-only", + "GPL-2.0-only WITH Classpath-exception-2.0", + "Mozilla Public License 2.0", + "AGPL-3.0", + ] { + assert!(is_copyleft(license), "{license}"); + } + for license in [ + "Apache License, Version 2.0", + "MIT", + "BSD-3-Clause", + "Custom Pump License", + "", + ] { + assert!(!is_copyleft(license), "{license}"); + } + } + /// Licence names and URLs as Maven POMs, npm and NuGet give them, and /// their SPDX id. const KNOWN: &[(&str, &str)] = &[ -- 2.54.1