|
| 1 | +// SPDX-FileCopyrightText: Provenant contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Build-time canonicalization of `LicenseRef-scancode-*` SPDX keys. |
| 5 | +//! |
| 6 | +//! Upstream ScanCode enforces an (arbitrary) "spdx_license_key must be 50 |
| 7 | +//! characters or less" lint in `licensedcode/models.py`. For licenses in the |
| 8 | +//! ScanCode `LicenseRef-scancode-<key>` namespace this forced dozens of keys to |
| 9 | +//! be squashed or truncated (for example |
| 10 | +//! `LicenseRef-scancode-openssl-exception-lgpl3.0plus` instead of the canonical |
| 11 | +//! `LicenseRef-scancode-openssl-exception-lgpl-3.0-plus`), see ScanCode PR |
| 12 | +//! aboutcode-org/scancode-toolkit#5221. |
| 13 | +//! |
| 14 | +//! For the `LicenseRef-scancode-` namespace the SPDX key is by definition the |
| 15 | +//! license `key` with the namespace prefix, so any deviation is a distortion, |
| 16 | +//! not a semantic choice. Provenant applies no length limit, so this pass |
| 17 | +//! restores the canonical form at index-build time and keeps the previous value |
| 18 | +//! in `other_spdx_license_keys` for backward compatibility. |
| 19 | +//! |
| 20 | +//! A small set of licenses carry a typo in the license `key` itself while their |
| 21 | +//! `spdx_license_key` is already correct. There, mirroring the key would |
| 22 | +//! *regress* the correct SPDX key, so those keys are exempted from this pass. |
| 23 | +//! The license key is left exactly as upstream has it (ScanCode parity), which |
| 24 | +//! is harmless because renaming a license key is a foreign-identity change with |
| 25 | +//! no backward-compatible alias mechanism, and the SPDX key users consume is |
| 26 | +//! already right. |
| 27 | +
|
| 28 | +use crate::license_detection::models::LoadedLicense; |
| 29 | + |
| 30 | +/// The ScanCode SPDX LicenseRef namespace prefix. |
| 31 | +const SCANCODE_LICENSEREF_PREFIX: &str = "LicenseRef-scancode-"; |
| 32 | + |
| 33 | +/// License keys exempted from canonicalization because the upstream typo is in |
| 34 | +/// the license `key`, not the SPDX key. Mirroring the key would regress an |
| 35 | +/// already-correct `spdx_license_key`, so the entry is left untouched. |
| 36 | +/// |
| 37 | +/// Each entry must be justified. |
| 38 | +const SPDX_CANONICALIZATION_EXEMPT_KEYS: &[&str] = &[ |
| 39 | + // "TCG" = Trusted Computing Group (see the license owner/holders). The key |
| 40 | + // misspells it as "tgc" while spdx_license_key already uses the correct |
| 41 | + // "tcg" (LicenseRef-scancode-tcg-spec-license-v2). Canonicalizing would |
| 42 | + // rewrite that correct SPDX key into the typo, so leave it as upstream has |
| 43 | + // it; the license key stays ScanCode-compatible and the SPDX key stays right. |
| 44 | + "tgc-spec-license-v2", |
| 45 | +]; |
| 46 | + |
| 47 | +/// A single SPDX key that was canonicalized to mirror the license `key`. |
| 48 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 49 | +pub struct SpdxKeyCanonicalization { |
| 50 | + pub license_key: String, |
| 51 | + pub previous_spdx_license_key: String, |
| 52 | + pub canonical_spdx_license_key: String, |
| 53 | +} |
| 54 | + |
| 55 | +/// Summary of the changes applied by [`canonicalize_license_spdx_keys`]. |
| 56 | +#[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 57 | +pub struct SpdxKeyCanonicalizationReport { |
| 58 | + pub canonicalized_spdx_keys: Vec<SpdxKeyCanonicalization>, |
| 59 | +} |
| 60 | + |
| 61 | +impl SpdxKeyCanonicalizationReport { |
| 62 | + pub fn is_empty(&self) -> bool { |
| 63 | + self.canonicalized_spdx_keys.is_empty() |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// Canonicalize `LicenseRef-scancode-*` SPDX keys in place so the suffix mirrors |
| 68 | +/// each license `key`. |
| 69 | +/// |
| 70 | +/// This is a build-time curation step: it runs when the embedded license index |
| 71 | +/// artifact is generated, so the corrected values are baked into the artifact |
| 72 | +/// and flow through the SPDX mapping, license references, and SPDX output. |
| 73 | +pub fn canonicalize_license_spdx_keys( |
| 74 | + licenses: &mut [LoadedLicense], |
| 75 | +) -> SpdxKeyCanonicalizationReport { |
| 76 | + let mut report = SpdxKeyCanonicalizationReport::default(); |
| 77 | + |
| 78 | + for license in licenses.iter_mut() { |
| 79 | + if SPDX_CANONICALIZATION_EXEMPT_KEYS.contains(&license.key.as_str()) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + let Some(spdx) = license.spdx_license_key.as_deref() else { |
| 84 | + continue; |
| 85 | + }; |
| 86 | + if !spdx.starts_with(SCANCODE_LICENSEREF_PREFIX) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + let canonical = format!("{SCANCODE_LICENSEREF_PREFIX}{}", license.key); |
| 91 | + if spdx == canonical { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + let previous = spdx.to_string(); |
| 96 | + |
| 97 | + // Drop the canonical form from the alias list to avoid duplicating the |
| 98 | + // new primary, and preserve the previous primary as a backward-compatible |
| 99 | + // alias. |
| 100 | + license |
| 101 | + .other_spdx_license_keys |
| 102 | + .retain(|k| k != &canonical && k != &previous); |
| 103 | + license.other_spdx_license_keys.push(previous.clone()); |
| 104 | + |
| 105 | + license.spdx_license_key = Some(canonical.clone()); |
| 106 | + report |
| 107 | + .canonicalized_spdx_keys |
| 108 | + .push(SpdxKeyCanonicalization { |
| 109 | + license_key: license.key.clone(), |
| 110 | + previous_spdx_license_key: previous, |
| 111 | + canonical_spdx_license_key: canonical, |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + report |
| 116 | +} |
| 117 | + |
| 118 | +#[cfg(test)] |
| 119 | +mod tests { |
| 120 | + use super::*; |
| 121 | + |
| 122 | + fn license(key: &str, spdx: Option<&str>, other: &[&str]) -> LoadedLicense { |
| 123 | + LoadedLicense { |
| 124 | + key: key.to_string(), |
| 125 | + spdx_license_key: spdx.map(str::to_string), |
| 126 | + other_spdx_license_keys: other.iter().map(|s| s.to_string()).collect(), |
| 127 | + ..Default::default() |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn promotes_canonical_form_and_demotes_squashed_primary() { |
| 133 | + // The openssl-exception-lgpl-3.0-plus case from ScanCode PR #5221. |
| 134 | + let mut licenses = vec![license( |
| 135 | + "openssl-exception-lgpl-3.0-plus", |
| 136 | + Some("LicenseRef-scancode-openssl-exception-lgpl3.0plus"), |
| 137 | + &["LicenseRef-scancode-openssl-exception-lgpl-3.0-plus"], |
| 138 | + )]; |
| 139 | + |
| 140 | + let report = canonicalize_license_spdx_keys(&mut licenses); |
| 141 | + |
| 142 | + assert_eq!( |
| 143 | + licenses[0].spdx_license_key.as_deref(), |
| 144 | + Some("LicenseRef-scancode-openssl-exception-lgpl-3.0-plus") |
| 145 | + ); |
| 146 | + assert_eq!( |
| 147 | + licenses[0].other_spdx_license_keys, |
| 148 | + vec!["LicenseRef-scancode-openssl-exception-lgpl3.0plus".to_string()] |
| 149 | + ); |
| 150 | + assert_eq!(report.canonicalized_spdx_keys.len(), 1); |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn adds_previous_primary_when_canonical_not_already_present() { |
| 155 | + // Truncation case (gradle-enterprise-sla-2022-11-08): canonical is not in |
| 156 | + // the alias list yet. |
| 157 | + let mut licenses = vec![license( |
| 158 | + "gradle-enterprise-sla-2022-11-08", |
| 159 | + Some("LicenseRef-scancode-gradle-enterprise-sla-2022-11-"), |
| 160 | + &[], |
| 161 | + )]; |
| 162 | + |
| 163 | + canonicalize_license_spdx_keys(&mut licenses); |
| 164 | + |
| 165 | + assert_eq!( |
| 166 | + licenses[0].spdx_license_key.as_deref(), |
| 167 | + Some("LicenseRef-scancode-gradle-enterprise-sla-2022-11-08") |
| 168 | + ); |
| 169 | + assert_eq!( |
| 170 | + licenses[0].other_spdx_license_keys, |
| 171 | + vec!["LicenseRef-scancode-gradle-enterprise-sla-2022-11-".to_string()] |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + #[test] |
| 176 | + fn exempts_key_typo_so_correct_spdx_key_is_not_regressed() { |
| 177 | + // tgc-spec-license-v2: the key is the typo, the SPDX key is already |
| 178 | + // correct. The pass must leave both fields untouched (no rename, no SPDX |
| 179 | + // regression). |
| 180 | + let mut licenses = vec![license( |
| 181 | + "tgc-spec-license-v2", |
| 182 | + Some("LicenseRef-scancode-tcg-spec-license-v2"), |
| 183 | + &[], |
| 184 | + )]; |
| 185 | + |
| 186 | + let report = canonicalize_license_spdx_keys(&mut licenses); |
| 187 | + |
| 188 | + assert_eq!(licenses[0].key, "tgc-spec-license-v2"); |
| 189 | + assert_eq!( |
| 190 | + licenses[0].spdx_license_key.as_deref(), |
| 191 | + Some("LicenseRef-scancode-tcg-spec-license-v2"), |
| 192 | + "correct SPDX key must be preserved, not regressed to the typo" |
| 193 | + ); |
| 194 | + assert!(licenses[0].other_spdx_license_keys.is_empty()); |
| 195 | + assert!(report.is_empty()); |
| 196 | + } |
| 197 | + |
| 198 | + #[test] |
| 199 | + fn leaves_canonical_and_real_spdx_keys_untouched() { |
| 200 | + let mut licenses = vec![ |
| 201 | + license("mit", Some("MIT"), &[]), |
| 202 | + license( |
| 203 | + "some-ref", |
| 204 | + Some("LicenseRef-scancode-some-ref"), |
| 205 | + &["LicenseRef-scancode-old-alias"], |
| 206 | + ), |
| 207 | + license("no-spdx", None, &[]), |
| 208 | + ]; |
| 209 | + |
| 210 | + let report = canonicalize_license_spdx_keys(&mut licenses); |
| 211 | + |
| 212 | + assert!(report.is_empty()); |
| 213 | + assert_eq!(licenses[0].spdx_license_key.as_deref(), Some("MIT")); |
| 214 | + assert_eq!( |
| 215 | + licenses[1].other_spdx_license_keys, |
| 216 | + vec!["LicenseRef-scancode-old-alias".to_string()] |
| 217 | + ); |
| 218 | + } |
| 219 | +} |
0 commit comments