fix(subset): stop eq comparator's prerelease tuple leaking into isolated gt/lt check - #911
Open
afonsojanu wants to merge 1 commit into
Open
afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
…ted gt/lt check
simpleSubset() checks a bare eq comparator (e.g. `1.2.3-0`) against a
gt/lt comparator in the same simple range by calling
satisfies(eq, String(gt)). Converting gt to a standalone range string
loses the fact that eq itself supplies a matching prerelease tuple, so
satisfies() wrongly excludes the prerelease and the whole simple range
gets misclassified as a null set. That makes subset() report `true`
even when a version exists that matches the sub range but not the dom
range, e.g.:
semver.subset('>1.0.0 1.2.3-0', '1.2.3') // true, should be false
semver.satisfies('1.2.3-0', '>1.0.0 1.2.3-0') // true
semver.satisfies('1.2.3-0', '1.2.3') // false
Using gt.test(eq) / lt.test(eq) instead does a direct version
comparison without re-deriving prerelease eligibility from a
one-comparator string, matching how the rest of this function already
checks dom comparators (see the surrounding c.test(...) calls).
Added regression cases to test/ranges/subset.js; full suite and
coverage stay green.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
semver.subset()can returntruefor a range pair where it should returnfalse, when the sub range is a simple (AND'd) range that combines a bare/prerelease equality comparator with a>/>=/</<=comparator sharing the same major.minor.patch tuple.Repro:
Since a version in the sub range is not in the dom range,
subset()returningtrueis incorrect by definition.Another minimal case:
Root cause
In
ranges/subset.js,simpleSubset()checks whether the bare eq comparator (e.g.1.2.3-0) is compatible with the range'sgt/ltbound like this:String(gt)turns thegtcomparator into a standalone one-comparator range string (e.g.'>1.0.0'). Passing that tosatisfies()re-applies semver's "a prerelease version only satisfies a range if some comparator in that range shares its tuple and carries a prerelease tag" rule against that single comparator alone, losing the fact thateq(also part of the same simple range) already supplies a comparator with the matching tuple and a prerelease tag. Sosatisfies(eq, String(gt))incorrectly returnsfalse, and the function returnsnull(its "null set" sentinel), whichsubset()then treats as "this simple range contributes nothing to consider" — ultimately causing it to reporttruefor a sub/dom pair that isn't actually a subset relationship.Fix
Use
gt.test(eq)/lt.test(eq)instead, which does a direct version comparison without re-deriving prerelease eligibility from a freshly-built single-comparator string. This matches how the rest of the same function already checks dom comparators a few lines down (c.test(gt.semver),c.test(lt.semver)).Test plan
test/ranges/subset.jsreproducing the false positive (verified they fail against the pre-fix code and pass with the fix).npx tap test/ranges/subset.js— all 86 assertions pass, 100% coverage maintained.npx tap test/— full suite passes, no regressions.npm run lint— clean.includePrerelease) before and after the fix; the false-positive class this PR targets (subset() sayingtruewhile satisfies() finds a counterexample) no longer occurs in 100k+ generated pairs.