From b14dbf676cde8331ac31a4eb8d7af02a6d09a0e7 Mon Sep 17 00:00:00 2001 From: Jonathan Yoder Date: Wed, 5 Aug 2026 07:06:58 -0400 Subject: [PATCH] fix(version): an empty specifier set admits every version, per PEP 508 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PEP 508 makes a requirement's version specifier optional, and a requirement without one accepts any version. The canonical spelling of that is an empty specifier set, which is what upstream produces and what upstream treats as containing everything -- measured against pypa/packaging 26.2: Requirement('flask').specifier -> len 0 contains '3.0.0' -> True contains '0.1' -> True contains '99!1.0' -> True contains '2.0.0rc1' -> True Specifiers.Check answered false to all of those. "No constraint" was being read as "no versions" instead of "all versions", which inverts every gate built on a specifier set: a caller filtering candidates against an unconstrained set discards all of them and concludes the package is unsatisfiable rather than unconstrained. THE PACKAGE WAS ALREADY INCONSISTENT WITH ITSELF, which is what makes this a defect rather than a debatable choice. andCheck returns true for an empty AND-group, because a conjunction over no constraints is vacuously true. Zero OR-groups is the same statement one level up, and the two disagreed. This is the root cause of the inversion fixed downstream in posit-dev/go-pyresolver#8, where an absent Requires-Python -- 2,027,153 versions in a production PyPI snapshot -- was read as "compatible with no interpreter at all." That module's accessor stays as intention-revealing API and as protection if it is ever built against an older version of this one. ⚠️ THIS IS A FAIL-OPEN CHANGE, so the blast radius was measured rather than argued. NO INPUT STRING CAN REACH IT: every degenerate input either errors or yields at least one group, verified across "", " ", "\t", ",", ",,", "()", "( )", "|", "||", "-" and "*" through both NewSpecifiers and NewRSpecifiers ("*" yields one group, >=0.0.0). The only route to a zero-group Specifiers is Go's zero value, which today gets the wrong answer anyway. That reachability fact is now a test, so if a future change makes some input parse to zero groups the risk assessment gets redone instead of silently invalidated. Also verified: PPM is unaffected. It pins v0.1.1, and it never calls Check on a possibly-empty value -- src/requirements/filter.go guards specs == "" and returns true first, and PythonCheckSpecifier returns early on a parse error. That workaround existing in the only other consumer is itself evidence that this is the semantics callers expect and currently have to implement themselves. TESTS PIN THREE WRONG IMPLEMENTATIONS, each verified to fail on its own: - dropping the guard (the original defect); - an unconditional `return true`, which would be a far worse bug than the one being fixed, caught by asserting a real constraint still EXCLUDES; - treating the empty set as a ">=0.0.0" floor. That one is subtler than it looks and my first draft of these tests missed it: a dev or pre-release segment sorts BELOW the release it belongs to, so 0.0.0.dev0 and 0.0.0a1 are less than 0.0.0 and a >=0.0.0 floor wrongly excludes them. Upstream admits both under an empty set and rejects both under >=0.0.0. One thing I got wrong while writing this and corrected: I assumed a ">=0.0.0" floor would also wrongly exclude pre-releases generally. It does not. Upstream's contains() admits 2.0.0rc1 under ">=1.0"; PEP 440's default pre-release exclusion is applied by filter(), not by contains(). Conflating those two is a different bug, and the test comment now says so explicitly so the distinction is not lost. CHANGELOG goes in the unreleased 0.2.0 Breaking section, placed first in that list so it does not textually collide with the entry in #20. Co-Authored-By: Claude Opus 5 --- CHANGELOG.md | 10 ++ version/empty_specifiers_test.go | 157 +++++++++++++++++++++++++++++++ version/specifier.go | 20 ++++ 3 files changed, 187 insertions(+) create mode 100644 version/empty_specifiers_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 95f2fea..a502aba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,16 @@ rather than `0.1.3`. ### Breaking +- `Specifiers.Check` returns `true` for a specifier set holding no specifiers, where it + previously returned `false` for every version. PEP 508 makes a requirement's version + specifier optional and an omitted one accepts any version, which upstream spells as an + empty `SpecifierSet` that contains everything — so "no constraint" means "all versions", + not "no versions". The package was already inconsistent with itself here: an empty + *conjunction* was vacuously true while zero *groups* was false. + **This reverses the answer for a zero-value `Specifiers`**, so a consumer using one as a + gate moves from rejecting everything to admitting everything. Note that no input string + can produce such a value — every degenerate input errors — so only a deliberately + constructed or zero-valued `Specifiers` is affected. - `Requirement.String()` renders the marker separator as `"; "` when the requirement has no URL. It previously always rendered `" ; "`. The `" ; "` form is kept when a URL *is* present, because a bare `;` immediately after a URL is ambiguous — a URL diff --git a/version/empty_specifiers_test.go b/version/empty_specifiers_test.go new file mode 100644 index 0000000..fa5c28d --- /dev/null +++ b/version/empty_specifiers_test.go @@ -0,0 +1,157 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +package version + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// An empty set of specifiers admits every version, per PEP 508: a requirement's +// version specifier is optional, and one without it accepts any version. The +// canonical spelling of that is an empty specifier set. +// +// Reference behavior measured against pypa/packaging 26.2: +// +// Requirement('flask').specifier -> len 0 +// contains '3.0.0' -> True +// contains '0.1' -> True +// contains '99!1.0' -> True +// contains '2.0.0rc1' -> True +// +// This package previously answered false to all of those, which inverts every +// gate built on a specifier set: a caller filtering candidates against an +// unconstrained set discarded all of them, and would conclude the package was +// unsatisfiable rather than unconstrained. That is exactly the defect it caused +// downstream in go-pyresolver, where an absent Requires-Python was read as +// "compatible with no interpreter at all". + +func TestEmptySpecifiersAdmitEveryVersion(t *testing.T) { + var unconstrained Specifiers + + // Deliberately spans the shapes most likely to be special-cased somewhere: + // a plain release, a tiny one, an epoch, a pre-release, a post-release, a dev + // release, and a local version. + // + // The last three are the ones that matter most, and they are not padding. + // They sort BELOW 0.0.0, so they are what distinguishes "admit everything" + // from the tempting shortcut of treating the empty set as ">=0.0.0" -- see + // TestEmptySpecifiersAreNotAFloorOfZero. + for _, v := range []string{ + "3.0.0", "0.1", "99!1.0", "2.0.0rc1", "1.0.post1", "1.0.dev0", "1.0+ubuntu.1", + "0.0.0", "0.0.0.dev0", "0.0.0a1", + } { + assert.True(t, unconstrained.Check(MustParse(v)), + "an empty specifier set must admit %s: no constraint means no constraint", v) + } +} + +// TestEmptySpecifiersAgreeWithAndCheck pins the internal consistency that made +// the old behavior indefensible rather than merely debatable. +// +// andCheck returns true for an empty AND-group, because a conjunction over no +// constraints is vacuously true. Zero OR-groups is the same statement one level +// up. The two answered differently, from the same logical shape. +func TestEmptySpecifiersAgreeWithAndCheck(t *testing.T) { + v := MustParse("1.0") + + assert.True(t, andCheck(v, nil), + "precondition: an empty AND-group is vacuously true") + + var unconstrained Specifiers + assert.Equal(t, andCheck(v, nil), unconstrained.Check(v), + "zero OR-groups and an empty AND-group are the same claim and must agree") +} + +// TestEmptySpecifiersStillRenderEmpty guards the other half of the reference's +// behavior: an empty upstream SpecifierSet renders as the empty string, so +// admitting everything must not be implemented by materializing a group into +// ss.specifiers. +func TestEmptySpecifiersStillRenderEmpty(t *testing.T) { + var unconstrained Specifiers + assert.Equal(t, "", unconstrained.String(), + "an empty specifier set renders as the empty string") +} + +// TestEmptySpecifiersAreNotAFloorOfZero rules out the plausible-looking shortcut +// of implementing "no constraint" as ">=0.0.0". +// +// It is not equivalent, and the reason is easy to miss: a dev or pre-release +// segment sorts BELOW the release it belongs to, so 0.0.0.dev0 and 0.0.0a1 are +// less than 0.0.0 and a >=0.0.0 floor excludes them. Measured against +// pypa/packaging 26.2, which admits both under an empty SpecifierSet and rejects +// both under SpecifierSet(">=0.0.0"). +// +// Note this is NOT about pre-release exclusion. A plain ">=1.0" DOES admit +// 2.0.0rc1 in the reference -- PEP 440's default pre-release exclusion is applied +// by filter(), not by contains(). Conflating the two would be a different bug. +func TestEmptySpecifiersAreNotAFloorOfZero(t *testing.T) { + floor, err := NewSpecifiers(">=0.0.0") + require.NoError(t, err) + + var unconstrained Specifiers + + for _, v := range []string{"0.0.0.dev0", "0.0.0a1"} { + ver := MustParse(v) + assert.True(t, unconstrained.Check(ver), + "an empty specifier set must admit %s", v) + assert.False(t, floor.Check(ver), + "precondition: %s sorts below 0.0.0, so a >=0.0.0 floor excludes it -- which "+ + "is exactly why the empty set cannot be implemented as that floor", v) + } +} + +// TestNonEmptySpecifiersStillExclude is the direction that keeps the change from +// being a blanket "always true". Without it, "make the empty case lenient" could +// be satisfied by an accessor that never rejects anything, which would be a far +// worse defect than the one being fixed. +func TestNonEmptySpecifiersStillExclude(t *testing.T) { + for _, tc := range []struct { + spec, admitted, excluded string + }{ + {">=3.9", "3.11", "3.8"}, + {"==1.0", "1.0", "1.1"}, + {"!=1.0", "1.1", "1.0"}, + {"~=1.4.5", "1.4.6", "1.5.0"}, + {"<2.0,>=1.0", "1.5", "2.0"}, + } { + ss, err := NewSpecifiers(tc.spec) + require.NoError(t, err, "spec %q should parse", tc.spec) + + assert.True(t, ss.Check(MustParse(tc.admitted)), + "%q should admit %s", tc.spec, tc.admitted) + assert.False(t, ss.Check(MustParse(tc.excluded)), + "%q must still EXCLUDE %s -- the empty-set change must not make Check "+ + "unconditionally true", tc.spec, tc.excluded) + } +} + +// TestZeroGroupsIsUnreachableByParsing records the fact that bounds the risk of +// this change, so a future reader can see it was measured rather than assumed. +// +// Flipping the empty case from reject-all to admit-all is a fail-open change, and +// would deserve real suspicion if a hostile or malformed input string could reach +// it. None can: every degenerate input either errors or yields at least one group, +// so the only route to a zero-group Specifiers is Go's zero value -- a caller who +// declared one and never assigned it, which today gets the wrong answer anyway. +// +// If a future change makes some input parse to zero groups, this test fails and +// that risk assessment has to be redone. +func TestZeroGroupsIsUnreachableByParsing(t *testing.T) { + identity := func(s string) string { return s } + + for _, in := range []string{"", " ", "\t", ",", ",,", "()", "( )", "|", "||", "-", "*"} { + if ss, err := NewSpecifiers(in); err == nil { + assert.NotZero(t, len(ss.specifiers), + "NewSpecifiers(%q) parsed to ZERO groups; the fail-open risk assessment "+ + "for the empty case assumed this was impossible", in) + } + if ss, err := NewRSpecifiers(in, identity); err == nil { + assert.NotZero(t, len(ss.specifiers), + "NewRSpecifiers(%q) parsed to ZERO groups; the fail-open risk assessment "+ + "for the empty case assumed this was impossible", in) + } + } +} diff --git a/version/specifier.go b/version/specifier.go index b1ddbdd..62e3bc1 100644 --- a/version/specifier.go +++ b/version/specifier.go @@ -271,11 +271,31 @@ func validate(operator, version string) error { } // Check tests if a version satisfies all the specifiers. +// +// # An empty set of specifiers admits EVERY version +// +// PEP 508 makes a requirement's version specifier optional, and a requirement +// without one accepts any version. The canonical spelling of that is an empty +// specifier set: pypa/packaging parses `flask` to a `SpecifierSet(”)` of length +// zero that contains every version, epochs and pre-releases included. +// +// So "no constraint" means "all versions", not "no versions". Reading it the +// other way inverts every gate built on it — a caller filtering candidates +// against an unconstrained set would discard all of them, and would be told the +// package is unsatisfiable rather than unconstrained. +// +// Note that andCheck below already returns true for an empty AND-group, for the +// same reason: a conjunction over no constraints is vacuously true. Zero +// OR-groups is the same statement one level up, and the two must agree. func (ss Specifiers) Check(v Version) bool { if ss.conf.includePreRelease { v.preReleaseIncluded = true } + if len(ss.specifiers) == 0 { + return true + } + for _, s := range ss.specifiers { if andCheck(v, s) { return true