package main // The scan of record (ADR-0093 ยง4, WP-05) drops the repository's suppression // files from the tree it scans. These tests check the DIRECTORY that comes // back, not the list of names: a dropSuppression that reports the files but // returns the original tree (`return source, removed, nil`) still scans with // them, and fails here. // // Written on the build VM, not run there (owner decision 2026-10-11). Runs // with `go test ./...` after `dagger develop` has generated internal/dagger. import ( "context" "errors" "path" "reflect" "sort" "strings" "testing" ) // memTree is an in-memory directory: Glob matches like Dagger's (`**/` is // any depth below the top, a bare name is the top level only), WithoutFiles // returns a NEW tree without the paths. type memTree struct { files map[string]bool globErr error } func newTree(paths ...string) memTree { files := map[string]bool{} for _, p := range paths { files[p] = true } return memTree{files: files} } func (t memTree) Glob(_ context.Context, pattern string) ([]string, error) { if t.globErr != nil { return nil, t.globErr } var out []string for p := range t.files { if name, ok := strings.CutPrefix(pattern, "**/"); ok { if strings.Contains(p, "/") && path.Base(p) == name { out = append(out, p) } } else if p == pattern { out = append(out, p) } } sort.Strings(out) return out, nil } func (t memTree) WithoutFiles(paths []string) memTree { files := map[string]bool{} for p := range t.files { files[p] = true } for _, p := range paths { delete(files, p) } return memTree{files: files} } func drop(t memTree) (memTree, []string, error) { return dropSuppression(context.Background(), t, t.Glob, memTree.WithoutFiles) } func (t memTree) list() []string { var out []string for p := range t.files { out = append(out, p) } sort.Strings(out) return out } func TestTheScanOfRecordTreeHasNoSuppressionFile(t *testing.T) { tree := newTree( "main.tf", "src/app.py", ".gitleaksignore", ".trivyignore", "trivy-secret.yaml", "deploy/.trivyignore.yaml", "svc/a/.semgrepignore", "svc/a/trivy.yaml", "infra/.checkov.yml", ) got, removed, err := drop(tree) if err != nil { t.Fatal(err) } if want := []string{"main.tf", "src/app.py"}; !reflect.DeepEqual(got.list(), want) { t.Fatalf("scanned tree = %q, want %q (suppression files must be GONE from it)", got.list(), want) } sort.Strings(removed) want := []string{ ".gitleaksignore", ".trivyignore", "deploy/.trivyignore.yaml", "infra/.checkov.yml", "svc/a/.semgrepignore", "svc/a/trivy.yaml", "trivy-secret.yaml", } if !reflect.DeepEqual(removed, want) { t.Fatalf("reported %q, want %q", removed, want) } } // Legal direction: a tree without suppression keeps every file, and the // application's own config files of similar names are not touched. func TestATreeWithoutSuppressionIsScannedWhole(t *testing.T) { tree := newTree("main.tf", "config/trivy.json", "docs/gitleaks.md", "app/.semgrepignore.md") got, removed, err := drop(tree) if err != nil { t.Fatal(err) } if len(removed) != 0 { t.Fatalf("nothing to remove, reported %q", removed) } if !reflect.DeepEqual(got.list(), tree.list()) { t.Fatalf("tree changed: %q", got.list()) } } func TestATreeThatCannotBeListedIsNotAScanOfRecord(t *testing.T) { tree := newTree(".trivyignore") tree.globErr = errors.New("engine gone") if _, _, err := drop(tree); err == nil { t.Fatal("a tree that cannot be listed must fail the scan of record") } } func TestTrivyReadsNoConfigFromTheTreeInTheScanOfRecord(t *testing.T) { for _, sub := range []string{"fs", "config"} { args := trivyArgs(sub, "/tmp/r.json", true) for _, flag := range []string{"--config=", "--ignorefile=", "--secret-config="} { if !contains(args, flag) { t.Errorf("trivy %s of record lacks %s: %q", sub, flag, args) } } if args[len(args)-1] != "." { t.Errorf("the target must stay last: %q", args) } // Every other caller scans as before. for _, a := range trivyArgs(sub, "/tmp/r.json", false) { if strings.HasPrefix(a, "--config=") || strings.HasPrefix(a, "--ignorefile=") || strings.HasPrefix(a, "--secret-config=") { t.Errorf("trivy %s outside the scan of record must not change: %q", sub, a) } } } } func TestInlineIgnoresAreReportedPerFile(t *testing.T) { got := inlineIgnoredFiles("./infra/main.tf\n\n./Dockerfile\n") want := []string{"inline:infra/main.tf", "inline:Dockerfile"} if !reflect.DeepEqual(got, want) { t.Fatalf("got %q, want %q", got, want) } if got := inlineIgnoredFiles(""); len(got) != 0 { t.Fatalf("no file changed, reported %q", got) } } // The neutraliser must rewrite both comment spellings, and only them. func TestTheInlineIgnoreScriptRewritesBothSpellings(t *testing.T) { for _, s := range []string{"(trivy|tfsec):ignore", "sed -i -E", "*.tf", "Dockerfile*", "*.yaml"} { if !strings.Contains(inlineIgnoreScript, s) { t.Errorf("inlineIgnoreScript lacks %q", s) } } } func contains(xs []string, x string) bool { for _, v := range xs { if v == x { return true } } return false }