fix(sbom): SPDX licences are declared, and copyleft is found by SPDX id (#545)
This commit was merged in pull request #545.
This commit is contained in:
1 parent
7f2b128efd
commit
6f1172589e
3 files changed
+67
-30
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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)] = &[
|
||||
|
||||
Reference in new issue
Block a user