Skip to content

Commit 9893aa8

Browse files
authored
fix: only use the commit in api queries if the package does not have other info (#349)
* fix: only use the commit in api queries if the package does not have other info * test: update tests
1 parent bef74d6 commit 9893aa8

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

pkg/database/api-check.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ import (
1717
func (db APIDB) buildAPIPayload(pkg internal.PackageDetails) apiQuery {
1818
var query apiQuery
1919

20-
if pkg.Commit == "" {
20+
// this mirrors the logic used by the osv-scalibr vulnmatch enricher
21+
if pkg.Name != "" && pkg.Ecosystem != "" && pkg.Version != "" {
2122
query.Package.Name = pkg.Name
2223
query.Package.Ecosystem = pkg.Ecosystem
2324
query.Version = pkg.Version
24-
} else {
25+
} else if pkg.Commit != "" {
2526
query.Commit = pkg.Commit
2627
}
2728

pkg/database/api-check_test.go

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,12 @@ func TestAPIDB_Check_WithCommit(t *testing.T) {
431431
mux := http.NewServeMux()
432432

433433
mux.HandleFunc("/querybatch", func(w http.ResponseWriter, r *http.Request) {
434-
expectRequestPayload(t, r, []apiQuery{{Commit: "abc123"}})
434+
expectRequestPayload(t, r, []apiQuery{
435+
{
436+
Version: "1.0.0",
437+
Package: apiPackage{Name: "my-package", Ecosystem: lockfile.NpmEcosystem},
438+
},
439+
})
435440

436441
jsonData := jsonMarshalQueryBatchResponse(t, []objectsWithIDs{{}})
437442

@@ -464,6 +469,80 @@ func TestAPIDB_Check_WithCommit(t *testing.T) {
464469
}
465470
}
466471

472+
func TestAPIDB_Check_WithCommitOnly(t *testing.T) {
473+
t.Parallel()
474+
475+
mux := http.NewServeMux()
476+
477+
mux.HandleFunc("/querybatch", func(w http.ResponseWriter, r *http.Request) {
478+
expectRequestPayload(t, r, []apiQuery{{Commit: "abc123"}})
479+
480+
jsonData := jsonMarshalQueryBatchResponse(t, []objectsWithIDs{{}})
481+
482+
_, _ = w.Write(jsonData)
483+
})
484+
485+
ts := httptest.NewServer(mux)
486+
t.Cleanup(ts.Close)
487+
488+
db, err := database.NewAPIDB(database.Config{URL: ts.URL}, false, 1)
489+
490+
if err != nil {
491+
t.Fatalf("Check() unexpected error \"%v\"", err)
492+
}
493+
494+
vulns, err := db.Check([]internal.PackageDetails{{Commit: "abc123"}})
495+
496+
if err != nil {
497+
t.Fatalf("unexpected error \"%v\"", err)
498+
}
499+
500+
if len(vulns) != 1 {
501+
t.Fatalf("expected to get 1 package but got %d", len(vulns))
502+
}
503+
504+
if len(vulns[0]) != 0 {
505+
t.Fatalf("expected to get 0 vulnerabilities but got %d", len(vulns[0]))
506+
}
507+
}
508+
509+
func TestAPIDB_Check_WithCommitAndSomeFields(t *testing.T) {
510+
t.Parallel()
511+
512+
mux := http.NewServeMux()
513+
514+
mux.HandleFunc("/querybatch", func(w http.ResponseWriter, r *http.Request) {
515+
expectRequestPayload(t, r, []apiQuery{{Commit: "abc123"}})
516+
517+
jsonData := jsonMarshalQueryBatchResponse(t, []objectsWithIDs{{}})
518+
519+
_, _ = w.Write(jsonData)
520+
})
521+
522+
ts := httptest.NewServer(mux)
523+
t.Cleanup(ts.Close)
524+
525+
db, err := database.NewAPIDB(database.Config{URL: ts.URL}, false, 1)
526+
527+
if err != nil {
528+
t.Fatalf("Check() unexpected error \"%v\"", err)
529+
}
530+
531+
vulns, err := db.Check([]internal.PackageDetails{{Commit: "abc123", Ecosystem: "npm"}})
532+
533+
if err != nil {
534+
t.Fatalf("unexpected error \"%v\"", err)
535+
}
536+
537+
if len(vulns) != 1 {
538+
t.Fatalf("expected to get 1 package but got %d", len(vulns))
539+
}
540+
541+
if len(vulns[0]) != 0 {
542+
t.Fatalf("expected to get 0 vulnerabilities but got %d", len(vulns[0]))
543+
}
544+
}
545+
467546
func TestAPIDB_Check_Batches(t *testing.T) {
468547
t.Parallel()
469548

0 commit comments

Comments
 (0)