Repository navigation
fix: reject '|' inside a constraint's version segments - #320
Open
januththedev wants to merge 1 commit into
Open
januththedev wants to merge 1 commit into
januththedev wants to merge 1 commit into
Conversation
januththedev
force-pushed
the
fix/constraint-pipe-charclass
branch
from
October 3, 2026 10:37
6a89e72 to
2ce10f5
Compare
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.
|was accepted inside a constraint's version segmentsDescription
cvRegexis written with a character class where an alternation was clearly intended:Inside
[...],|is a literal member character, not an alternation operator. The intent was(0-9|x|X|\*). The only unintended member is|, so|is now accepted anywhere a version segment may appear — even though it is this grammar's OR separator and is never legal in a version segment per semver.org.cvRegexbuildsconstraintRegex,constraintRangeRegex,findConstraintRegexandvalidConstraintRegex, so this affects the whole constraint grammar.Two concrete failure modes
(a) Garbage silently accepted, and the input discarded. In
parseConstraint, when the major segment is a wildcard the code takes theisX(m[3])branch and buildsver = "0.0.0" + m[6]— droppingm[3]entirely, so it is never validated:*.|2Check==*for every version*.2|3Check==*1.*.2|3Check==1.xA clearly malformed constraint parses successfully and then evaluates as a completely different, far more permissive range.
(b) The library's internal "unreachable" branch becomes reachable.
constraints.go:311and:330are both commented "The constraintRegex should catch any regex parsing errors. So, we should never get here." and returnfmt.Errorf("constraint parser error"). BecausecvRegexwrongly accepts|, such strings survivevalidConstraintRegex, then fail insideNewVersionduringparseConstraint— so user input reaches a path documented as impossible, and callers get the opaque internal error instead of the publicimproper constraint: %q.Affected:
1|2,1.2|3,1.2.3|4,x|2,*|2,>=1.2.3|,>=1.2.3 | 2.0.0(a single-pipe typo for||), and1.2.3 | | 2.0.0.The change
Dropping the
|is sufficient:x,Xand*were already member characters, so removing the pipe makes the class exactly the set the author intended.Tests
TestNewConstraintRejectsPipeInVersioncovers 11 malformed inputs above, asserting each now returns the publicimproper constrainterror rather than being accepted or returning the internalconstraint parser error. It also asserts 15 legal wildcard constraints (*,x,X,1.x,1.X,1.2.x,*.2,1.*,*.*.*,^1.x,~1.x,>=1.x,<=2.x,!=1.x) still parse, so the class cannot be over-narrowed.go test ./...→ 50 top-level tests, 43 subtests, 0 failures. The baseline on unmodified HEAD was 49 top-level with 0 failures, so the delta is exactly this one test.go vet ./...clean.FuzzNewConstraint76,403 execs andFuzzNewVersion931,069 execs pass, with no crashers.I checked the 22 open PRs and 24 open issues: nothing touches
cvRegexor the|character. PR #315 covers a separatecomparePrePartoverflow that I also found and discarded as in-flight.