diff --git a/advisory_test.go b/advisory_test.go index 2a0069a..dc81986 100644 --- a/advisory_test.go +++ b/advisory_test.go @@ -13,6 +13,11 @@ type npmAdvisoryRange struct { Native string `json:"npm_native"` } +// Advisory ranges that list exact versions with spaces, which npm intersects. +var unsatisfiableNPMAdvisoryRanges = map[string]bool{ + "1.1.2 1.2.2": true, +} + func TestNPMAdvisoryRangesParse(t *testing.T) { filename := filepath.Join("testdata", "local", "data", "npm_advisory.json") data, err := os.ReadFile(filename) @@ -28,7 +33,14 @@ func TestNPMAdvisoryRangesParse(t *testing.T) { } for _, advisoryRange := range ranges { t.Run(fmt.Sprintf("%04d", advisoryRange.TestIndex), func(t *testing.T) { - if _, err := ParseNative(advisoryRange.Native, schemeNPM); err != nil { + _, err := ParseNative(advisoryRange.Native, schemeNPM) + if unsatisfiableNPMAdvisoryRanges[advisoryRange.Native] { + if err == nil { + t.Errorf("ParseNative(%q, npm) succeeded, want unsatisfiable range error", advisoryRange.Native) + } + return + } + if err != nil { t.Errorf("ParseNative(%q, npm): %v", advisoryRange.Native, err) } }) diff --git a/parser.go b/parser.go index 1459d13..adf45e0 100644 --- a/parser.go +++ b/parser.go @@ -457,17 +457,25 @@ func (p *Parser) parseNpmRange(s string) (*Range, error) { // Handle space-separated AND constraints if strings.ContainsAny(s, " \t\r\n") && !strings.Contains(s, " - ") { parts := tokenizeNpmConstraints(s) + ranges := make([]*Range, 0, len(parts)) var result *Range - for _, part := range parts { + for i, part := range parts { r, err := p.parseNpmSingleRange(part) if err != nil { return nil, err } + ranges = append(ranges, r) if result == nil { result = r - } else { - result = result.Intersect(r) + continue } + result = result.Intersect(r) + if result.IsEmpty() { + return nil, npmUnsatisfiableError(parts, ranges, i) + } + } + if len(ranges) > 1 { + result.RawConstraints = npmIntersectedRaw(ranges) } return result, nil } @@ -475,6 +483,68 @@ func (p *Parser) parseNpmRange(s string) (*Range, error) { return p.parseNpmSingleRange(s) } +// npmIntersectedRaw returns the raw constraints of the comparators that set +// the tightest lower and upper bound, since listing every comparator would +// serialize as a union. It returns nil when the merged intervals should be +// serialized instead. +func npmIntersectedRaw(ranges []*Range) []Interval { + var out Interval + lower, upper := -1, -1 + for i, r := range ranges { + raw := r.RawConstraints + if len(raw) == 0 { + raw = r.Intervals + } + if len(r.Intervals) != 1 || len(raw) != 1 { + return nil + } + bound := r.Intervals[0] + if bound.Min != "" && (lower < 0 || tighterLower(bound, ranges[lower].Intervals[0])) { + lower = i + out.Min, out.MinInclusive = raw[0].Min, raw[0].MinInclusive + } + if bound.Max != "" && (upper < 0 || tighterUpper(bound, ranges[upper].Intervals[0])) { + upper = i + out.Max, out.MaxInclusive = raw[0].Max, raw[0].MaxInclusive + } + } + if lower < 0 && upper < 0 { + return nil + } + return []Interval{out} +} + +func tighterLower(a, b Interval) bool { + c := CompareVersions(a.Min, b.Min) + return c > 0 || (c == 0 && !a.MinInclusive && b.MinInclusive) +} + +func tighterUpper(a, b Interval) bool { + c := CompareVersions(a.Max, b.Max) + return c < 0 || (c == 0 && !a.MaxInclusive && b.MaxInclusive) +} + +// npmUnsatisfiableError names the comparator at index last and an earlier one +// it cannot be satisfied together with. +func npmUnsatisfiableError(parts []string, ranges []*Range, last int) error { + conflict := 0 + for i := range last { + if ranges[i].Intersect(ranges[last]).IsEmpty() { + conflict = i + break + } + } + return fmt.Errorf("space-separated npm comparators are intersected: %s and %s match no version", + npmComparatorString(parts[conflict]), npmComparatorString(parts[last])) +} + +func npmComparatorString(part string) string { + if strings.ContainsAny(part[:1], "<>=!~^") { + return part + } + return "=" + part +} + // tokenizeNpmConstraints splits an npm constraint string into individual constraints, // properly handling operators followed by spaces (e.g., ">= 1.0.0" stays as one token). func tokenizeNpmConstraints(s string) []string { diff --git a/parser_test.go b/parser_test.go index ec648d1..81fc2c2 100644 --- a/parser_test.go +++ b/parser_test.go @@ -201,6 +201,53 @@ func TestParseNpmRangeErrors(t *testing.T) { } } +func TestParseNpmIntersectionToVers(t *testing.T) { + tests := []struct { + input string + want string + }{ + {">=5.0.3 >=4.2.1", "vers:npm/>=5.0.3"}, + {">=4.2.1 >=5.0.3 <6.0.0 <7.0.0", "vers:npm/>=5.0.3|<6.0.0"}, + {">1.0.0 >=1.0.0", "vers:npm/>1.0.0"}, + {"<=2.1 >=1.1 >=1.0", "vers:npm/>=1.1.0|<=2.1.0"}, + {"<=2.1 <2.1.5", "vers:npm/<2.1.5"}, + {"^1.2.3 >=1.5.0", "vers:npm/>=1.5.0|<2.0.0"}, + {">=1.0.0 <2.0.0 || >=3.0.0 >=3.1.0", "vers:npm/>=1.0.0|<2.0.0|>=3.1.0"}, + } + for _, tt := range tests { + r, err := ParseNative(tt.input, schemeNPM) + if err != nil { + t.Errorf("ParseNative(%q, npm): %v", tt.input, err) + continue + } + if got := ToVersString(r, schemeNPM); got != tt.want { + t.Errorf("ParseNative(%q, npm) = %q, want %q", tt.input, got, tt.want) + } + } +} + +func TestParseNpmUnsatisfiableIntersection(t *testing.T) { + tests := []struct { + input string + want string + }{ + {"1.1.2 1.2.2", "space-separated npm comparators are intersected: =1.1.2 and =1.2.2 match no version"}, + {">=2.0.0 <1.0.0", "space-separated npm comparators are intersected: >=2.0.0 and <1.0.0 match no version"}, + {">=1.0.0 ^1.2.3 <1.1.0", "space-separated npm comparators are intersected: ^1.2.3 and <1.1.0 match no version"}, + {"1.0.0 || >=2.0.0 <1.0.0", "space-separated npm comparators are intersected: >=2.0.0 and <1.0.0 match no version"}, + } + for _, tt := range tests { + _, err := ParseNative(tt.input, schemeNPM) + if err == nil { + t.Errorf("ParseNative(%q, npm) succeeded", tt.input) + continue + } + if err.Error() != tt.want { + t.Errorf("ParseNative(%q, npm) error = %q, want %q", tt.input, err, tt.want) + } + } +} + func TestParseGemRange(t *testing.T) { tests := []struct { name string diff --git a/testdata/vers-spec b/testdata/vers-spec index 76c7291..2c387b6 160000 --- a/testdata/vers-spec +++ b/testdata/vers-spec @@ -1 +1 @@ -Subproject commit 76c729180ce3ffb24b2e9f90e34585150fe0ffcf +Subproject commit 2c387b682dfbc93d39dac497175f5057d36eebd3