From 1529c6fada437feea9af3f0a811e8bd87320be45 Mon Sep 17 00:00:00 2001 From: Jonathan Yoder Date: Thu, 23 Jul 2026 08:31:19 -0400 Subject: [PATCH] fix(license): rune-aware SPDX tokenizer + honest GPL/LGPL classifier mapping Addresses three RoboRev findings on the license/ package (rstudio/package-manager#19135): 1. NBSP tokenizer (jobs 1103 & 1105): tokenizeSPDX iterated byte-by-byte (rune(expr[i])) while classifying with unicode.IsSpace, so multi-byte UTF-8 whitespace separators (e.g. NBSP U+00A0 in "MITOR Apache-2.0") were misread and the expression was not split. The tokenizer now decodes runes via utf8.DecodeRuneInString. ASCII behavior is unchanged; added NBSP and ideographic-space regressions. 2. LGPLv3 mapping (job 1104): the classifier "GNU Lesser General Public License v3 (LGPLv3)" denotes exactly v3 (a separate LGPLv3+ classifier exists for or-later), but mapped to LGPL-3.0-or-later. Corrected to LGPL-3.0-only. (The analogous GPLv3 entry was already GPL-3.0-only.) 3. Versionless GPL/LGPL (job 1104): "GNU General Public License (GPL)" and "GNU Library or Lesser General Public License (LGPL)" stated no version but mapped to GPL-3.0-only / LGPL-3.0-only, fabricating a version the metadata does not declare. Both are now SPDXIdentifier "" / SPDXNotIncluded true, so they derive to "Unknown". These change license-derivation output: LGPLv3 -> LGPL-3.0-only, and versionless GPL/LGPL -> Unknown. Co-Authored-By: Claude Opus 4.8 (1M context) --- license/classifiers_test.go | 7 +++++++ license/data/pypi_licenses.json | 10 +++++----- license/spdx_expression.go | 12 ++++++------ license/spdx_expression_test.go | 7 +++++++ 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/license/classifiers_test.go b/license/classifiers_test.go index 03c2970..1ba058a 100644 --- a/license/classifiers_test.go +++ b/license/classifiers_test.go @@ -29,6 +29,13 @@ func TestStandardizeLicensePyPIClassifiers(t *testing.T) { {"dedup", []string{"License :: OSI Approved :: BSD License", "License :: OSI Approved :: BSD License"}, []string{"BSD-3-Clause"}}, {"none", []string{"Programming Language :: Python :: 3"}, []string{"Unknown"}}, {"not-included", []string{"License :: Public Domain"}, []string{"Unknown"}}, + // LGPLv3 (without "+") denotes exactly v3, so it must map to + // LGPL-3.0-only, not the more permissive -or-later form. + {"lgplv3-is-v3-only", []string{"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)"}, []string{"LGPL-3.0-only"}}, + // Versionless GPL/LGPL classifiers state no version, so we must not + // fabricate one; they derive to Unknown. + {"versionless-gpl-is-unknown", []string{"License :: OSI Approved :: GNU General Public License (GPL)"}, []string{"Unknown"}}, + {"versionless-lgpl-is-unknown", []string{"License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)"}, []string{"Unknown"}}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { diff --git a/license/data/pypi_licenses.json b/license/data/pypi_licenses.json index 0dfb95a..6e1d89c 100644 --- a/license/data/pypi_licenses.json +++ b/license/data/pypi_licenses.json @@ -170,7 +170,7 @@ { "Classifier": "License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)", "Name": "GNU Lesser General Public License v3 (LGPLv3)", - "SPDXIdentifier": "LGPL-3.0-or-later", + "SPDXIdentifier": "LGPL-3.0-only", "SPDXNotIncluded": false }, { @@ -356,8 +356,8 @@ { "Classifier": "License :: OSI Approved :: GNU General Public License (GPL)", "Name": "GNU General Public License (GPL)", - "SPDXIdentifier": "GPL-3.0-only", - "SPDXNotIncluded": false + "SPDXIdentifier": "", + "SPDXNotIncluded": true }, { "Classifier": "License :: OSI Approved :: Nokia Open Source License", @@ -416,8 +416,8 @@ { "Classifier": "License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)", "Name": "GNU Library or Lesser General Public License (LGPL)", - "SPDXIdentifier": "LGPL-3.0-only", - "SPDXNotIncluded": false + "SPDXIdentifier": "", + "SPDXNotIncluded": true }, { "Classifier": "License :: OSI Approved :: MIT No Attribution License (MIT-0)", diff --git a/license/spdx_expression.go b/license/spdx_expression.go index 3944859..facad6d 100644 --- a/license/spdx_expression.go +++ b/license/spdx_expression.go @@ -5,6 +5,7 @@ package license import ( "strings" "unicode" + "unicode/utf8" ) // ParseSPDXExpression parses an SPDX license expression and returns the individual @@ -49,34 +50,33 @@ func tokenizeSPDX(expr string) []string { } } + // Iterate rune-by-rune (not byte-by-byte) so that multi-byte UTF-8 + // whitespace separators (e.g. NBSP U+00A0) are classified correctly by + // unicode.IsSpace and split the expression like an ASCII space would. i := 0 for i < len(expr) { - ch := rune(expr[i]) + ch, size := utf8.DecodeRuneInString(expr[i:]) + i += size switch { case ch == '(' || ch == ')': flushCurrent() tokens = append(tokens, string(ch)) - i++ case ch == '+': // + suffix attaches to the previous identifier (no whitespace per spec) // but we handle it as part of the identifier current.WriteRune(ch) - i++ case ch == ':': // Part of DocumentRef-X:LicenseRef-Y format current.WriteRune(ch) - i++ case unicode.IsSpace(ch): flushCurrent() - i++ default: current.WriteRune(ch) - i++ } } diff --git a/license/spdx_expression_test.go b/license/spdx_expression_test.go index 4b08852..7cfe6ea 100644 --- a/license/spdx_expression_test.go +++ b/license/spdx_expression_test.go @@ -78,6 +78,13 @@ func TestParseSPDXExpression(t *testing.T) { // Edge cases {"extra whitespace", " MIT OR Apache-2.0 ", []string{"MIT", "Apache-2.0"}}, {"mixed case operators", "MIT Or Apache-2.0", []string{"MIT", "Apache-2.0"}}, + + // Non-ASCII (Unicode) whitespace separators must split like ASCII spaces. + // NBSP (U+00A0) is multi-byte in UTF-8, so a byte-wise tokenizer would + // misread it and fail to split the expression. + {"NBSP-separated OR expression", "MIT OR Apache-2.0", []string{"MIT", "Apache-2.0"}}, + {"NBSP-separated AND expression", "MIT AND Apache-2.0", []string{"MIT", "Apache-2.0"}}, + {"ideographic-space-separated OR expression", "MIT OR Apache-2.0", []string{"MIT", "Apache-2.0"}}, } for _, tc := range cases {