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
5 changes: 3 additions & 2 deletions pkg/dependencies/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ require (
t.Fatalf("ParseGoMod failed: %v", err)
}

if len(result.Purls) != 2 {
t.Errorf("Expected 2 dependencies, got %d", len(result.Purls))
// The two requirements and the standard library (go 1.22).
if len(result.Purls) != 3 {
t.Errorf("Expected 3 dependencies, got %d", len(result.Purls))
}

// Check first dependency
Expand Down
56 changes: 56 additions & 0 deletions pkg/dependencies/parsers/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func ParseGoMod(fileContent []byte, filePath string) (*LocalDependency, error) {
scanner := bufio.NewScanner(bytes.NewReader(fileContent))
inRequireBlock := false
lineNo := 0
var goDirective, toolchainDirective directive

for scanner.Scan() {
lineNo++
Expand All @@ -62,6 +63,15 @@ func ParseGoMod(fileContent []byte, filePath string) (*LocalDependency, error) {
continue
}

if v, ok := strings.CutPrefix(line, "go "); ok && !inRequireBlock {
goDirective = directive{strings.TrimSpace(v), lineNo, strings.TrimSpace(raw)}
continue
}
if v, ok := strings.CutPrefix(line, "toolchain "); ok && !inRequireBlock {
toolchainDirective = directive{strings.TrimSpace(v), lineNo, strings.TrimSpace(raw)}
continue
}

// Check for require block start
if strings.HasPrefix(line, "require (") {
inRequireBlock = true
Expand Down Expand Up @@ -110,9 +120,55 @@ func ParseGoMod(fileContent []byte, filePath string) (*LocalDependency, error) {
return nil, err
}

if v := GoStdVersion(goDirective.value, toolchainDirective.value); v != "" {
d := goDirective
if toolchainDirective.value != "" {
d = toolchainDirective
}
result.Purls = append(result.Purls, LocalPurl{
Purl: BuildPURL("golang", "", GoStdName, v, nil),
Requirement: v,
Line: d.line,
DeclaredText: d.text,
})
}

return result, nil
}

// GoStdName is the module name of the Go standard library: pkg:golang/stdlib@go1.26.11.
// It is the name the Go vulnerability database files standard library advisories
// under, and the one Syft and Grype use; Grype matches nothing on cyclonedx-gomod's
// "std".
const GoStdName = "stdlib"

// directive is a go.mod directive's value and where it was declared.
type directive struct {
value string
line int
text string
}

// GoStdVersion returns the version of the standard library a module is built
// with, from its go.mod directives: the toolchain directive ("go1.26.11") when
// there is one, since that is the toolchain the go command switches to, else the
// go directive ("1.26.11" becomes "go1.26.11"). It returns "" when neither is set.
//
// A go directive without a patch release ("1.26") is kept as it is: it is the
// oldest release the module allows, and a scanner reads it as the first one.
func GoStdVersion(goDirective, toolchainDirective string) string {
if v := strings.TrimSpace(toolchainDirective); v != "" && v != "default" {
if !strings.HasPrefix(v, "go") {
v = "go" + v
}
return v
}
if v := strings.TrimSpace(goDirective); v != "" {
return "go" + v
}
return ""
}

// ParseGoSum parses a go.sum file and extracts dependencies
func ParseGoSum(fileContent []byte, filePath string) (*LocalDependency, error) {
result := &LocalDependency{
Expand Down
70 changes: 69 additions & 1 deletion pkg/dependencies/parsers/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ require github.com/foo/bar v1.2.3
Line: 7,
DeclaredText: "require github.com/foo/bar v1.2.3",
},
{
Purl: "pkg:golang/stdlib@go1.22",
Requirement: "go1.22",
Line: 3,
DeclaredText: "go 1.22",
},
},
},
{
Expand All @@ -87,6 +93,12 @@ require (
Line: 7,
DeclaredText: "github.com/google/uuid v1.6.0",
},
{
Purl: "pkg:golang/stdlib@go1.22",
Requirement: "go1.22",
Line: 3,
DeclaredText: "go 1.22",
},
},
},
{
Expand All @@ -102,6 +114,12 @@ require github.com/spf13/pflag v1.0.10
Line: 3,
DeclaredText: "require github.com/spf13/pflag v1.0.10",
},
{
Purl: "pkg:golang/stdlib@go1.22",
Requirement: "go1.22",
Line: 2,
DeclaredText: "go 1.22",
},
},
},
}
Expand Down Expand Up @@ -174,7 +192,57 @@ go 1.22
if err != nil {
t.Fatalf("ParseGoMod error: %v", err)
}
// The standard library is the only dependency of a module that requires nothing.
if len(result.Purls) != 1 || result.Purls[0].Purl != "pkg:golang/stdlib@go1.22" {
t.Errorf("expected only the standard library for a module-only go.mod, got %+v", result.Purls)
}
}

// The standard library is declared at the toolchain the module builds with:
// the toolchain directive when there is one, else the go directive.
func TestParseGoModStdlib(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
name, content, purl string
line int
}{
{"go directive", "module m\n\ngo 1.26.11\n", "pkg:golang/stdlib@go1.26.11", 3},
{"toolchain wins over go", "module m\n\ngo 1.26\n\ntoolchain go1.26.11\n", "pkg:golang/stdlib@go1.26.11", 5},
{"go directive without patch kept as is", "module m\ngo 1.26\n", "pkg:golang/stdlib@go1.26", 2},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
result, err := ParseGoMod([]byte(tc.content), "go.mod")
if err != nil {
t.Fatalf("ParseGoMod error: %v", err)
}
if len(result.Purls) != 1 || result.Purls[0].Purl != tc.purl || result.Purls[0].Line != tc.line {
t.Errorf("got %+v, want %s at line %d", result.Purls, tc.purl, tc.line)
}
})
}

result, err := ParseGoMod([]byte("module m\n"), "go.mod")
if err != nil {
t.Fatalf("ParseGoMod error: %v", err)
}
if len(result.Purls) != 0 {
t.Errorf("expected 0 purls for module-only go.mod, got %d", len(result.Purls))
t.Errorf("a go.mod without a go directive names no toolchain, got %+v", result.Purls)
}
}

func TestGoStdVersion(t *testing.T) {
t.Parallel()

for _, tc := range []struct{ goDir, toolchain, want string }{
{"1.26.11", "", "go1.26.11"},
{"1.26", "go1.26.11", "go1.26.11"},
{"1.26", "default", "go1.26"},
{"", "", ""},
} {
if got := GoStdVersion(tc.goDir, tc.toolchain); got != tc.want {
t.Errorf("GoStdVersion(%q, %q) = %q, want %q", tc.goDir, tc.toolchain, got, tc.want)
}
}
}
43 changes: 35 additions & 8 deletions pkg/dependencies/resolve/toolchain/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"io"
"strings"

"github.com/scanoss/scanoss.go/pkg/dependencies/parsers"
"github.com/scanoss/scanoss.go/pkg/dependencies/resolve"
"github.com/scanoss/scanoss.go/pkg/dependencies/resolve/internal/build"
)
Expand All @@ -54,12 +55,13 @@ var goUnreachable = []string{
}

type goListModule struct {
Path string
Version string
Main bool
Indirect bool
Replace *struct{ Path, Version string }
Error *struct{ Err string }
Path string
Version string
GoVersion string
Main bool
Indirect bool
Replace *struct{ Path, Version string }
Error *struct{ Err string }
}

type goSelected struct {
Expand Down Expand Up @@ -92,11 +94,11 @@ func resolveGo(b *build.Builder, r *build.Root, run runFunc) {
mods = append(mods, m)
}

main := ""
main, mainGo := "", ""
selected := map[string]goSelected{}
for _, m := range mods {
if m.Main {
main = m.Path
main, mainGo = m.Path, m.GoVersion
continue
}
if m.Error != nil {
Expand All @@ -123,6 +125,8 @@ func resolveGo(b *build.Builder, r *build.Root, run runFunc) {
}
}

declareStd(b, r, run, mainGo)

graph := run("go", "mod", "graph")
if graph.reason != "" {
reason := resolve.ReasonEdgesUnknown
Expand Down Expand Up @@ -162,3 +166,26 @@ func resolveGo(b *build.Builder, r *build.Root, run runFunc) {
}
}
}

type goModEdit struct {
Go string
Toolchain string
}

// declareStd declares the standard library the main module builds with: every
// Go binary links it, and it is where most Go vulnerabilities are. go mod edit
// reads both the go and toolchain directives without the network; if it fails,
// the go directive go list already reported stands in.
func declareStd(b *build.Builder, r *build.Root, run runFunc, goVersion string) {
var mod goModEdit
if out := run("go", "mod", "edit", "-json"); out.reason == "" && json.Unmarshal(out.stdout, &mod) == nil && mod.Go != "" {
goVersion = mod.Go
}
v := parsers.GoStdVersion(goVersion, mod.Toolchain)
if v == "" {
return
}
if id, ok := b.Node("", parsers.GoStdName, v); ok {
r.Declare(id, false)
}
}
5 changes: 5 additions & 0 deletions pkg/dependencies/resolve/toolchain/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func recorded(t *testing.T, dir string) runFunc {
switch strings.Join(append([]string{name}, args...), " ") {
case "go list -e -m -json all":
return output{stdout: read("list.json")}
case "go mod edit -json":
if _, err := os.Stat(filepath.Join(dir, "mod.json")); err != nil {
return failed("go mod edit", errors.New("exit status 1"), nil)
}
return output{stdout: read("mod.json")}
case "go mod graph":
if _, err := os.Stat(filepath.Join(dir, "stderr.txt")); err == nil {
return failed("go mod graph", errors.New("exit status 1"), read("stderr.txt"))
Expand Down
1 change: 1 addition & 0 deletions pkg/dependencies/resolve/toolchain/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func TestGoIntegrationOffline(t *testing.T) {
"pkg:golang/example.com/local",
"pkg:golang/github.com/google/uuid@v1.6.0",
"pkg:golang/github.com/stretchr/testify@v1.12.1",
"pkg:golang/stdlib@go1.22", // read from go.mod, so known offline
}
wantIndirect := []resolve.NodeID{"pkg:golang/go.yaml.in/yaml/v3@v3.0.5"}
if !slices.Equal(direct, wantDirect) || !slices.Equal(indirect, wantIndirect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"id": "pkg:golang/go.yaml.in/yaml/v3@v3.0.5",
"purl": "pkg:golang/go.yaml.in/yaml/v3",
"version": "v3.0.5"
},
{
"id": "pkg:golang/stdlib@go1.22",
"purl": "pkg:golang/stdlib",
"version": "go1.22"
}
],
"roots": [
Expand Down Expand Up @@ -56,6 +61,10 @@
{
"id": "pkg:golang/go.yaml.in/yaml/v3@v3.0.5",
"direct": false
},
{
"id": "pkg:golang/stdlib@go1.22",
"direct": true
}
],
"edges": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"id": "pkg:golang/github.com/google/uuid@v1.6.0",
"purl": "pkg:golang/github.com/google/uuid",
"version": "v1.6.0"
},
{
"id": "pkg:golang/stdlib@go1.22",
"purl": "pkg:golang/stdlib",
"version": "go1.22"
}
],
"roots": [
Expand All @@ -28,6 +33,10 @@
{
"id": "pkg:golang/github.com/google/uuid@v1.6.0",
"direct": true
},
{
"id": "pkg:golang/stdlib@go1.22",
"direct": true
}
],
"edges": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"id": "pkg:golang/example.com/d@v1.0.0",
"purl": "pkg:golang/example.com/d",
"version": "v1.0.0"
},
{
"id": "pkg:golang/stdlib@go1.22",
"purl": "pkg:golang/stdlib",
"version": "go1.22"
}
],
"roots": [
Expand All @@ -35,6 +40,10 @@
{
"id": "pkg:golang/example.com/d@v1.0.0",
"direct": false
},
{
"id": "pkg:golang/stdlib@go1.22",
"direct": true
}
],
"edges": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
"id": "pkg:golang/go.yaml.in/yaml/v3@v3.0.5",
"purl": "pkg:golang/go.yaml.in/yaml/v3",
"version": "v3.0.5"
},
{
"id": "pkg:golang/stdlib@go1.22",
"purl": "pkg:golang/stdlib",
"version": "go1.22"
}
],
"roots": [
Expand Down Expand Up @@ -47,6 +52,10 @@
{
"id": "pkg:golang/go.yaml.in/yaml/v3@v3.0.5",
"direct": false
},
{
"id": "pkg:golang/stdlib@go1.22",
"direct": true
}
],
"edges": []
Expand Down
Loading
Loading