Skip to content
Open
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
70 changes: 70 additions & 0 deletions extractor/filesystem/language/golang/gomod/gosum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package gomod

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/google/osv-scalibr/extractor/filesystem"
"github.com/google/osv-scalibr/fs"
)

func TestGoSumSymlinkEscape(t *testing.T) {
tmpDir := t.TempDir()
scanRoot := filepath.Join(tmpDir, "scan-root")
if err := os.MkdirAll(scanRoot, 0755); err != nil {
t.Fatal(err)
}

// File rahasia di luar root (format go.sum valid)
secretFile := filepath.Join(tmpDir, "secret.gosum")
secretContent := "github.com/example/fake v1.0.0\n"
if err := os.WriteFile(secretFile, []byte(secretContent), 0644); err != nil {
t.Fatal(err)
}

// Symlink di dalam root bernama go.sum
symlinkPath := filepath.Join(scanRoot, "go.sum")
if err := os.Symlink(secretFile, symlinkPath); err != nil {
t.Fatal(err)
}

// File go.mod minimal sebagai pemicu extractor
goModPath := filepath.Join(scanRoot, "go.mod")
if err := os.WriteFile(goModPath, []byte("module example.com\n"), 0644); err != nil {
t.Fatal(err)
}

extractor := &Extractor{}
input := &filesystem.ScanInput{
Path: goModPath,
FS: fs.DirFS(scanRoot),
Info: mustStat(t, goModPath),
}

inv, err := extractor.Extract(context.Background(), input)
if err != nil {
t.Fatal(err)
}

// Verifikasi package dari symlink terbaca
found := false
for _, pkg := range inv.Packages {
if pkg.Name == "github.com/example/fake" && pkg.Version == "v1.0.0" {
found = true
break
}
}
if !found {
t.Errorf("expected package from symlinked go.sum to appear, got %+v", inv.Packages)
}
}

func mustStat(t *testing.T, path string) os.FileInfo {

Check failure on line 64 in extractor/filesystem/language/golang/gomod/gosum_test.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

test helper function should start from t.Helper() (thelper)

Check failure on line 64 in extractor/filesystem/language/golang/gomod/gosum_test.go

View workflow job for this annotation

GitHub Actions / lint (windows-latest)

test helper function should start from t.Helper() (thelper)

Check failure on line 64 in extractor/filesystem/language/golang/gomod/gosum_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

test helper function should start from t.Helper() (thelper)
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
return info
}
Loading