Skip to content

Commit ccebe0f

Browse files
authored
feat: implement coarse versioning in go (#5225)
Re-implements the `EE:XXXXXXXX.YYYYYYYY.ZZZZZZZZ` comparable version string generation in Go from Python. The implementations are mostly the same as the Python ones, but the semantic submodule in scalibr is generally much more lenient in accepting technically invalid versions (dunno if we should be being more strict about this). Differences are mostly due to how we want to handle invalid versions, which I don't think really appears in the OSV database. I'll do a pass over the AffectedVersions in the database once the worker is migrated to Go to make sure this is all consistent anyway. I created a program & test that compiles every single unique version (from `affected[].versions` and `affected[].ranges[]`) in every single OSV record, and verifies that `Parse` and `Coarse` both error on the same strings, and `Coarse` maintains monotonicity. This test doesn't run by default because a) the versions list is 22MB big and b) it takes a while to generate and run. I've also added some fuzzing tests to help catch edge cases if we want to run them for a bit. It's already helped me find a few edge cases in dpkg and packagist, which is nice. The regular tests run the fuzzers with only the explicit seed corpus. To do full fuzzing, you need to run it manually. APK is currently not implemented due to transitivity issues in scalibr that I'm looking to fix (google/osv-scalibr#1932)
1 parent 184085f commit ccebe0f

23 files changed

Lines changed: 1028 additions & 29 deletions

‎CONTRIBUTING.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ By default, this skips long tests, enable them by setting the `LONG_TESTS` varia
9797
LONG_TESTS=1 make api-server-tests
9898
```
9999

100+
There are additional tests for testing coarse version ordering logic for ecosystem version
101+
parsers. These are tested against the entire OSV database.
102+
```shell
103+
# in go/
104+
# you must generate the list of all versions by running
105+
./osv/ecosystem/testdata/regen_coarse_test_data.sh
106+
# then run the tests
107+
RUN_COARSE_LARGE_TEST=1 go test ./osv/ecosystem -run TestCoarseMonotonicityLarge
108+
```
109+
100110
##### Test result generation
101111

102112
Many tests are written using a

‎go/cmd/extract_versions/main.go‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Package main implements a tool to extract unique versions from OSV vulnerabilities zip file.
2+
package main
3+
4+
import (
5+
"archive/zip"
6+
"fmt"
7+
"io"
8+
"maps"
9+
"os"
10+
"runtime"
11+
"slices"
12+
"sync"
13+
14+
"github.com/ossf/osv-schema/bindings/go/osvschema"
15+
"google.golang.org/protobuf/encoding/protojson"
16+
)
17+
18+
func main() {
19+
if len(os.Args) != 3 {
20+
fmt.Println("Usage: extract_versions <zipfile> <outputfile>")
21+
os.Exit(1)
22+
}
23+
zipFile := os.Args[1]
24+
outputFile := os.Args[2]
25+
26+
zipReader, err := zip.OpenReader(zipFile)
27+
if err != nil {
28+
panic(err)
29+
}
30+
defer zipReader.Close()
31+
32+
versionChan := make(chan string, 1000)
33+
var wg sync.WaitGroup
34+
35+
// Worker pool
36+
numWorkers := runtime.NumCPU()
37+
fileChan := make(chan *zip.File)
38+
39+
// Start workers
40+
for range numWorkers {
41+
wg.Add(1)
42+
go func() {
43+
defer wg.Done()
44+
for file := range fileChan {
45+
r, err := file.Open()
46+
if err != nil {
47+
fmt.Fprintln(os.Stderr, err)
48+
continue
49+
}
50+
bytes, err := io.ReadAll(r)
51+
r.Close()
52+
if err != nil {
53+
fmt.Fprintln(os.Stderr, err)
54+
continue
55+
}
56+
var vuln osvschema.Vulnerability
57+
if err := protojson.Unmarshal(bytes, &vuln); err != nil {
58+
fmt.Fprintln(os.Stderr, err)
59+
continue
60+
}
61+
for _, affected := range vuln.GetAffected() {
62+
for _, version := range affected.GetVersions() {
63+
versionChan <- version
64+
}
65+
for _, ranges := range affected.GetRanges() {
66+
if ranges.GetType() == osvschema.Range_GIT {
67+
continue
68+
}
69+
for _, event := range ranges.GetEvents() {
70+
if event.GetIntroduced() != "" {
71+
versionChan <- event.GetIntroduced()
72+
}
73+
if event.GetFixed() != "" {
74+
versionChan <- event.GetFixed()
75+
}
76+
if event.GetLastAffected() != "" {
77+
versionChan <- event.GetLastAffected()
78+
}
79+
}
80+
}
81+
}
82+
}
83+
}()
84+
}
85+
86+
// Collector
87+
allVersions := make(map[string]struct{})
88+
doneChan := make(chan struct{})
89+
go func() {
90+
for v := range versionChan {
91+
allVersions[v] = struct{}{}
92+
}
93+
close(doneChan)
94+
}()
95+
96+
// Feed files to workers
97+
for _, file := range zipReader.File {
98+
fileChan <- file
99+
}
100+
close(fileChan)
101+
102+
// Wait for workers to finish
103+
wg.Wait()
104+
close(versionChan)
105+
106+
// Wait for collector to finish
107+
<-doneChan
108+
109+
// Sort versions
110+
vers := slices.Sorted(maps.Keys(allVersions))
111+
112+
// Write to output file
113+
f, err := os.Create(outputFile)
114+
if err != nil {
115+
panic(err)
116+
}
117+
defer f.Close()
118+
119+
for _, v := range vers {
120+
if _, err := fmt.Fprintln(f, v); err != nil {
121+
panic(err)
122+
}
123+
}
124+
125+
fmt.Printf("Successfully extracted %d unique versions to %s\n", len(vers), outputFile)
126+
}

‎go/osv/ecosystem/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testdata/all_versions.txt

‎go/osv/ecosystem/apk.go‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
package ecosystem
1616

17-
import "github.com/google/osv-scalibr/semantic"
17+
import (
18+
"regexp"
19+
20+
"github.com/google/osv-scalibr/semantic"
21+
)
1822

1923
// apkEcosystem is an ecosystem for ecosystems using Alpine Package Keeper versioning.
2024
type apkEcosystem struct{}
@@ -30,7 +34,18 @@ func (e apkEcosystem) Parse(version string) (Version, error) {
3034
return SemanticVersionWrapper[semantic.AlpineVersion]{ver}, nil
3135
}
3236

37+
//nolint:unused
38+
var apkCoarseVersioner = CoarseVersioner{
39+
Separators: regexp.MustCompile(`[.]`),
40+
Truncate: regexp.MustCompile(`(?:\.0|[_-])`),
41+
ImplicitSplit: true,
42+
EmptyAs: &[]string{""}[0],
43+
}
44+
3345
func (e apkEcosystem) Coarse(_ string) (string, error) {
46+
// TODO(michaelkedar): semantic.AlpineVersion currently breaks transitivity rules
47+
// (a < b, b < c, c < a) in some cases with invalid versions.
48+
// Which makes coarse versions kinda broken.
3449
return "", ErrCoarseNotSupported
3550
}
3651

0 commit comments

Comments
 (0)