resolve: stop guessing SPDX ids for ambiguous license aliases (#31) #38

Merged
KofTwentyTwo merged 1 commits from feature/GH-31-spdxnorm-no-guess into develop 2026-06-09 15:17:56 +00:00
2 changed files with 49 additions and 10 deletions

No files matched your search

+20 -9
View File
@@ -41,9 +41,12 @@ func normalizeSPDX(raw string) (string, bool) {
// Then try the curated alias table for the small set of non-SPDX spellings
// that appear ubiquitously in real-world metadata.
if canon, ok := spdxAliases[strings.ToLower(s)]; ok {
// Re-validate the canonical form against the vendored index so the alias
// table can never introduce an id the rest of the tool does not know.
if spdx.Validate(canon) {
// Re-validate the canonical form against the CURATED rendering set (Lookup),
// not merely the full index (Validate): a "resolved" alias must map to an id
// policy.Classify can classify, otherwise the dependency would be marked
// resolved yet be unclassifiable (a resolved-but-unknown contradiction). An
// alias whose target is valid-but-uncurated is left unresolved on purpose.
if _, ok := spdx.Lookup(canon); ok {
return canon, true
}
}
@@ -78,6 +81,20 @@ func validateSingleSPDX(s string) (string, bool) {
// spdxAliases maps lowercased, well-known non-SPDX license spellings seen in
// Maven POM <name> elements and npm package.json fields to their canonical SPDX
// id. Keys are lowercased; the map is intentionally conservative.
//
// INVARIANTS (issue #31): every entry MUST be (1) UNAMBIGUOUS -- the spelling pins
// an exact version/variant/clause-count, so no guessing is required -- and (2) a
// CURATED id (one spdx.Lookup succeeds on), so a resolved alias is always
// classifiable. Deliberately omitted because they fail (1) or (2):
//
// - "bsd" / "bsd license": ambiguous clause count (2- vs 3- vs 4-clause). A bare
// "BSD" cannot be pinned without guessing, so it stays unresolved. Only the
// explicit 3-clause spellings below are kept.
// - "gnu lesser general public license": ambiguous version (2.1 vs 3.0) AND grant
// ("only" vs "or-later"). Cannot be pinned without inventing both, so unresolved.
// - Eclipse Public License ("EPL-2.0"/"EPL-1.0"): valid SPDX ids but outside the
// curated rendering set, so policy.Classify returns Unknown. Resolving them would
// mark a dependency resolved yet unclassifiable; they stay unresolved instead.
var spdxAliases = map[string]string{
"apache 2.0": "Apache-2.0",
"apache license 2.0": "Apache-2.0",
@@ -86,16 +103,10 @@ var spdxAliases = map[string]string{
"apache-2": "Apache-2.0",
"mit license": "MIT",
"the mit license": "MIT",
"bsd": "BSD-2-Clause",
"bsd license": "BSD-2-Clause",
"new bsd license": "BSD-3-Clause",
"3-clause bsd license": "BSD-3-Clause",
"bsd 3-clause": "BSD-3-Clause",
"the bsd 3-clause license": "BSD-3-Clause",
"eclipse public license 2.0": "EPL-2.0",
"eclipse public license - v 2.0": "EPL-2.0",
"eclipse public license 1.0": "EPL-1.0",
"gnu lesser general public license": "LGPL-3.0-or-later",
"isc license": "ISC",
"the unlicense": "Unlicense",
"mozilla public license 2.0": "MPL-2.0",
+29 -1
View File
@@ -4,6 +4,9 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/KofTwentyTwo/license-tool/internal/model"
"github.com/KofTwentyTwo/license-tool/internal/policy"
)
// TestValidateSingleSPDXValidateFailure covers validateSingleSPDX's
@@ -43,10 +46,19 @@ func TestNormalizeSPDX(t *testing.T) {
{"alias the apache software license", "The Apache Software License, Version 2.0", "Apache-2.0", true},
{"alias mit license", "MIT License", "MIT", true},
{"alias bsd 3-clause", "New BSD License", "BSD-3-Clause", true},
{"alias eclipse 2.0", "Eclipse Public License 2.0", "EPL-2.0", true},
{"alias agpl", "GNU Affero General Public License v3.0 or later", "AGPL-3.0-or-later", true},
{"compound expression rejected", "MIT OR Apache-2.0", "", false},
{"unrecognized string", "Totally Made Up License", "", false},
// Ambiguous aliases must NOT be guessed: a bare "BSD" cannot pick a clause
// count and a bare LGPL name cannot pick a version/grant. They stay unresolved.
{"ambiguous bsd no clause count", "BSD", "", false},
{"ambiguous bsd license no clause count", "BSD License", "", false},
{"ambiguous lgpl no version or grant", "GNU Lesser General Public License", "", false},
// Non-curated targets must NOT resolve: EPL ids pass spdx.Validate but are
// outside the curated rendering set, so policy.Classify would return Unknown,
// a resolved-but-unclassifiable contradiction. They stay unresolved.
{"non-curated epl 2.0", "Eclipse Public License 2.0", "", false},
{"non-curated epl 1.0", "Eclipse Public License 1.0", "", false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
@@ -57,6 +69,22 @@ func TestNormalizeSPDX(t *testing.T) {
}
}
// TestSPDXAliasesAreCuratedAndClassifiable asserts every alias-table target is a
// curated SPDX id that policy.Classify can classify (never Unknown). This guards the
// invariant that a "resolved" dependency is always classifiable: an alias that mapped
// to a valid-but-uncurated id (e.g. EPL-*) would be resolved-but-unclassifiable.
func TestSPDXAliasesAreCuratedAndClassifiable(t *testing.T) {
for alias, id := range spdxAliases {
t.Run(alias, func(t *testing.T) {
gotID, ok := normalizeSPDX(alias)
assert.True(t, ok, "alias %q must resolve", alias)
assert.Equal(t, id, gotID)
assert.NotEqual(t, model.CategoryUnknown, policy.Classify(gotID),
"alias %q maps to %q which is not classifiable (not in the curated set)", alias, gotID)
})
}
}
// TestValidateSingleSPDX covers the three rejection branches (parse error,
// multi-id expression, validation failure) plus the success path. A single valid
// id succeeds; "OR"/garbage are rejected.