|
| 1 | +package assurance |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "runtime" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +type listedPackage struct { |
| 16 | + ImportPath string |
| 17 | + Imports []string |
| 18 | +} |
| 19 | + |
| 20 | +func TestRemediationPackageDependencyBoundaries(t *testing.T) { |
| 21 | + root := repositoryRoot(t) |
| 22 | + const module = "github.com/bomly-dev/bomly-cli/" |
| 23 | + |
| 24 | + t.Run("central derivation", func(t *testing.T) { |
| 25 | + packages := goListDependencies(t, root, "./internal/remediation") |
| 26 | + assertDirectImportsAbsent(t, packages, module+"internal/remediation", map[string]string{ |
| 27 | + "net": "network access", |
| 28 | + "net/http": "HTTP access", |
| 29 | + "os": "filesystem or process environment access", |
| 30 | + "os/exec": "subprocess execution", |
| 31 | + module + "internal/system": "filesystem or subprocess access", |
| 32 | + module + "internal/matchers/cache": "cache filesystem access", |
| 33 | + }) |
| 34 | + assertDependenciesAbsent(t, packages, map[string]string{ |
| 35 | + module + "internal/git": "Git filesystem or subprocess access", |
| 36 | + module + "internal/matchers/cache": "cache filesystem access", |
| 37 | + module + "internal/plugin": "native plugin execution", |
| 38 | + module + "internal/system": "filesystem or subprocess access", |
| 39 | + }) |
| 40 | + }) |
| 41 | + |
| 42 | + // These packages also own detector resolution, so their complete dependency |
| 43 | + // graphs legitimately include internal/system and os/exec. At package |
| 44 | + // granularity, enforce that remediation hints do not acquire network, |
| 45 | + // cache, plugin, Git, or central-policy dependencies. Request immutability |
| 46 | + // and read-only provider behavior are covered by the remediation contract |
| 47 | + // tests named in EXECUTION_BOUNDARIES.md. |
| 48 | + detectorHintPackages := []struct { |
| 49 | + name string |
| 50 | + path string |
| 51 | + }{ |
| 52 | + {name: "shared", path: "./internal/detectors"}, |
| 53 | + {name: "cargo", path: "./internal/detectors/cargo"}, |
| 54 | + {name: "composer", path: "./internal/detectors/composer"}, |
| 55 | + {name: "gomod", path: "./internal/detectors/gomod"}, |
| 56 | + {name: "gradle", path: "./internal/detectors/gradle"}, |
| 57 | + {name: "maven", path: "./internal/detectors/maven"}, |
| 58 | + {name: "bun", path: "./internal/detectors/node/bun"}, |
| 59 | + {name: "npm", path: "./internal/detectors/node/npm"}, |
| 60 | + {name: "pnpm", path: "./internal/detectors/node/pnpm"}, |
| 61 | + {name: "yarn", path: "./internal/detectors/node/yarn"}, |
| 62 | + {name: "python", path: "./internal/detectors/python"}, |
| 63 | + {name: "ruby", path: "./internal/detectors/ruby"}, |
| 64 | + } |
| 65 | + t.Run("detector hint packages", func(t *testing.T) { |
| 66 | + for _, target := range detectorHintPackages { |
| 67 | + target := target |
| 68 | + t.Run(target.name, func(t *testing.T) { |
| 69 | + packages := goListDependencies(t, root, target.path) |
| 70 | + importPath := module + strings.TrimPrefix(target.path, "./") |
| 71 | + assertDirectImportsAbsent(t, packages, importPath, map[string]string{ |
| 72 | + "net": "network access", |
| 73 | + "net/http": "HTTP access", |
| 74 | + module + "internal/git": "Git access", |
| 75 | + module + "internal/matchers/cache": "cache filesystem access", |
| 76 | + module + "internal/plugin": "native plugin execution", |
| 77 | + module + "internal/remediation": "central remediation policy", |
| 78 | + }) |
| 79 | + assertDependenciesAbsent(t, packages, map[string]string{ |
| 80 | + module + "internal/git": "Git access", |
| 81 | + module + "internal/matchers/cache": "cache filesystem access", |
| 82 | + module + "internal/plugin": "native plugin execution", |
| 83 | + module + "internal/remediation": "central remediation policy", |
| 84 | + }) |
| 85 | + }) |
| 86 | + } |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +func goListDependencies(t *testing.T, root, packagePath string) map[string]listedPackage { |
| 91 | + t.Helper() |
| 92 | + cmd := exec.Command("go", "list", "-deps", "-json", packagePath) |
| 93 | + cmd.Dir = root |
| 94 | + output, err := cmd.Output() |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("go list dependencies for %s: %v", packagePath, err) |
| 97 | + } |
| 98 | + decoder := json.NewDecoder(bytes.NewReader(output)) |
| 99 | + packages := map[string]listedPackage{} |
| 100 | + for { |
| 101 | + var listed listedPackage |
| 102 | + err := decoder.Decode(&listed) |
| 103 | + if errors.Is(err, io.EOF) { |
| 104 | + break |
| 105 | + } |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("decode go list output for %s: %v", packagePath, err) |
| 108 | + } |
| 109 | + packages[listed.ImportPath] = listed |
| 110 | + } |
| 111 | + return packages |
| 112 | +} |
| 113 | + |
| 114 | +func assertDirectImportsAbsent(t *testing.T, packages map[string]listedPackage, importPath string, forbidden map[string]string) { |
| 115 | + t.Helper() |
| 116 | + target, ok := packages[importPath] |
| 117 | + if !ok { |
| 118 | + t.Fatalf("go list omitted target package %q", importPath) |
| 119 | + } |
| 120 | + for _, imported := range target.Imports { |
| 121 | + if reason, found := forbidden[imported]; found { |
| 122 | + t.Errorf("package %q directly imports %q, which permits %s", importPath, imported, reason) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func assertDependenciesAbsent(t *testing.T, packages map[string]listedPackage, forbidden map[string]string) { |
| 128 | + t.Helper() |
| 129 | + for importPath, reason := range forbidden { |
| 130 | + if _, found := packages[importPath]; found { |
| 131 | + t.Errorf("transitive package graph includes %q, which permits %s", importPath, reason) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func repositoryRoot(t *testing.T) string { |
| 137 | + t.Helper() |
| 138 | + _, source, _, ok := runtime.Caller(0) |
| 139 | + if !ok { |
| 140 | + t.Fatal("resolve assurance test source") |
| 141 | + } |
| 142 | + return filepath.Clean(filepath.Join(filepath.Dir(source), "..", "..")) |
| 143 | +} |
0 commit comments