Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion advisory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
})
Expand Down
76 changes: 73 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,24 +457,94 @@ 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
}

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 {
Expand Down
47 changes: 47 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down