From 194fb0e49fea1c3057dbf6663f4db6b01378a413 Mon Sep 17 00:00:00 2001 From: James Maes Date: Tue, 9 Jun 2026 07:59:28 -0500 Subject: [PATCH] fix(#31): resolve ambiguous license aliases to unresolved instead of guessing The spdxAliases table fabricated SPDX ids, violating the package's "never guess" invariant: - "bsd" / "bsd license" invented a clause count (could be 2/3/4-clause). - "gnu lesser general public license" invented BOTH a version (2.1 vs 3.0) and the grant ("only" vs "or-later"). These wrong guesses flowed into deny/allow verdicts and the incompatibility table, where -only vs -or-later and clause counts are decisive. Such ambiguous spellings now leave normalizeSPDX returning ok=false, so the caller emits an unresolved dependency with a reason rather than a fabricated id. Only unambiguous, exact spellings remain (explicit 3-clause BSD, Apache, MIT, ISC, MPL-2.0, Unlicense, the explicit AGPL "v3.0 or later" spelling). Also fixes the related resolved-but-unclassifiable contradiction: alias re-validation now uses spdx.Lookup (curated rendering set) instead of spdx.Validate (full index). The Eclipse Public License aliases (EPL-2.0 / EPL-1.0) passed Validate but are outside the curated set, so policy.Classify returned Unknown while the dependency was still marked resolved. They are now left unresolved. Any future alias whose target is valid-but-uncurated is structurally rejected by the Lookup gate. Closes #31 --- internal/resolve/spdxnorm.go | 29 ++++++++++++++++++++--------- internal/resolve/spdxnorm_test.go | 30 +++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/internal/resolve/spdxnorm.go b/internal/resolve/spdxnorm.go index 21a88c3..b8145db 100644 --- a/internal/resolve/spdxnorm.go +++ b/internal/resolve/spdxnorm.go @@ -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 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", diff --git a/internal/resolve/spdxnorm_test.go b/internal/resolve/spdxnorm_test.go index 756ae46..3454058 100644 --- a/internal/resolve/spdxnorm_test.go +++ b/internal/resolve/spdxnorm_test.go @@ -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. -- 2.54.0