|
1 | 1 | import * as elf from "elfy"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as path from "path"; |
2 | 4 |
|
3 | 5 | import { extractContent, scan } from "../../../lib"; |
4 | 6 | import { getGoModulesContentAction } from "../../../lib/go-parser"; |
| 7 | +import { GoBinary } from "../../../lib/go-parser/go-binary"; |
5 | 8 | import { getFixture } from "../../util"; |
6 | 9 |
|
7 | 10 | describe("gomodules binaries scanning", () => { |
@@ -94,3 +97,180 @@ describe("parse go modules from various versions of compiled binaries", () => { |
94 | 97 | expect(pluginResult).toMatchSnapshot(); |
95 | 98 | }); |
96 | 99 | }); |
| 100 | + |
| 101 | +/** |
| 102 | + * Unit Tests: Stripped/CGo Binary Support |
| 103 | + * |
| 104 | + * Tests GoBinary class directly with a stripped binary fixture (no .gopclntab section). |
| 105 | + * Validates module-level dependency extraction from .go.buildinfo. |
| 106 | + * |
| 107 | + * Fixture: test/fixtures/go-binaries/no-pcln-tab |
| 108 | + * - Source: github.com/rootless-containers/rootlesskit/cmd/rootlesskit-docker-proxy |
| 109 | + * - Go Version: 1.17.11 |
| 110 | + * - Dependencies: 3 modules |
| 111 | + * - Expected output verified with: go version -m test/fixtures/go-binaries/no-pcln-tab |
| 112 | + */ |
| 113 | +describe("Stripped Go binary without .gopclntab: no-pcln-tab fixture", () => { |
| 114 | + const fixturesPath = path.join(__dirname, "../../fixtures/go-binaries"); |
| 115 | + const noPclnTabPath = path.join(fixturesPath, "no-pcln-tab"); |
| 116 | + |
| 117 | + // Expected dependencies for no-pcln-tab fixture based on `go version -m` |
| 118 | + const expectedDepsNoPcln = [ |
| 119 | + { name: "github.com/pkg/errors", version: "v0.9.1" }, |
| 120 | + { name: "github.com/sirupsen/logrus", version: "v1.8.1" }, |
| 121 | + { name: "golang.org/x/sys", version: "v0.0.0-20210119212857-b64e53b001e4" }, |
| 122 | + ]; |
| 123 | + |
| 124 | + it("should have .go.buildinfo but no .gopclntab", () => { |
| 125 | + const fileContent = fs.readFileSync(noPclnTabPath); |
| 126 | + const binary = elf.parse(fileContent); |
| 127 | + |
| 128 | + const goBuildInfo = binary.body.sections.find( |
| 129 | + (section) => section.name === ".go.buildinfo", |
| 130 | + ); |
| 131 | + const goPclnTab = binary.body.sections.find( |
| 132 | + (section) => section.name === ".gopclntab", |
| 133 | + ); |
| 134 | + |
| 135 | + expect(goBuildInfo).toBeDefined(); |
| 136 | + expect(goPclnTab).toBeUndefined(); |
| 137 | + }); |
| 138 | + |
| 139 | + it("should extract 3 module-level dependencies from .go.buildinfo", async () => { |
| 140 | + const fileContent = fs.readFileSync(noPclnTabPath); |
| 141 | + const binary = elf.parse(fileContent); |
| 142 | + |
| 143 | + const goBinary = new GoBinary(binary); |
| 144 | + const depGraph = await goBinary.depGraph(); |
| 145 | + |
| 146 | + const deps = depGraph |
| 147 | + .getPkgs() |
| 148 | + .filter((pkg) => pkg.name !== depGraph.rootPkg.name); |
| 149 | + |
| 150 | + expectedDepsNoPcln.forEach((expectedDep) => { |
| 151 | + const found = deps.find( |
| 152 | + (dep) => |
| 153 | + dep.name === expectedDep.name && dep.version === expectedDep.version, |
| 154 | + ); |
| 155 | + expect(found).toBeDefined(); |
| 156 | + }); |
| 157 | + |
| 158 | + expect(deps.length).toBe(expectedDepsNoPcln.length); |
| 159 | + expect(depGraph.rootPkg.name).toBe( |
| 160 | + "github.com/rootless-containers/rootlesskit", |
| 161 | + ); |
| 162 | + }); |
| 163 | + |
| 164 | + it("should report module-level dependencies (not package-level)", async () => { |
| 165 | + const fileContent = fs.readFileSync(noPclnTabPath); |
| 166 | + const binary = elf.parse(fileContent); |
| 167 | + |
| 168 | + const goBinary = new GoBinary(binary); |
| 169 | + |
| 170 | + const hasPackageLevelInfo = goBinary.modules.some( |
| 171 | + (mod) => mod.packages.length > 0, |
| 172 | + ); |
| 173 | + |
| 174 | + expect(hasPackageLevelInfo).toBe(false); |
| 175 | + expect(goBinary.modules.length).toBe(3); |
| 176 | + }); |
| 177 | +}); |
| 178 | + |
| 179 | +/** |
| 180 | + * Test Image: test/fixtures/docker-archives/stripped-go-binaries-minimal.tar.gz |
| 181 | + * - Size: 18 MB compressed, 62 MB uncompressed |
| 182 | + * - Source: elastic-agent-complete:8.18.8 |
| 183 | + * - Binaries: 2 stripped Go binaries |
| 184 | + * 1. fleet-server (76 modules) |
| 185 | + * 2. osquery-extension.ext (10 modules) - we currently filter out binaries with extensions TODO-fix this |
| 186 | + */ |
| 187 | +describe("Stripped and CGo Go binaries detection scan handler test", () => { |
| 188 | + const testImagePath = getFixture( |
| 189 | + "docker-archives/stripped-go-binaries-minimal.tar.gz", |
| 190 | + ); |
| 191 | + jest.setTimeout(180000); |
| 192 | + const getScanOptions = () => { |
| 193 | + return { |
| 194 | + path: `docker-archive:${testImagePath}`, |
| 195 | + "app-vulns": true, |
| 196 | + }; |
| 197 | + }; |
| 198 | + |
| 199 | + it("should detect stripped/CGo Go binaries missing .gopclntab section", async () => { |
| 200 | + const pluginResult = await scan(getScanOptions()); |
| 201 | + |
| 202 | + const goModules = pluginResult.scanResults.filter( |
| 203 | + (r) => r.identity.type === "gomodules", |
| 204 | + ); |
| 205 | + |
| 206 | + expect(goModules.length).toBeGreaterThanOrEqual(1); |
| 207 | + |
| 208 | + const detectedBinaries: { |
| 209 | + fleetServer: { targetFile: string; moduleCount: number } | null; |
| 210 | + osqueryExt: { targetFile: string; moduleCount: number } | null; |
| 211 | + } = { |
| 212 | + fleetServer: null, |
| 213 | + osqueryExt: null, |
| 214 | + }; |
| 215 | + |
| 216 | + goModules.forEach((result) => { |
| 217 | + const targetFile = result.identity.targetFile || ""; |
| 218 | + const depGraphFact = result.facts.find((f) => f.type === "depGraph"); |
| 219 | + const depGraph = depGraphFact?.data; |
| 220 | + |
| 221 | + if (!depGraph) { |
| 222 | + return; |
| 223 | + } |
| 224 | + |
| 225 | + const packages = depGraph.getPkgs(); |
| 226 | + const moduleCount = packages.length; |
| 227 | + |
| 228 | + if (targetFile.includes("fleet-server")) { |
| 229 | + detectedBinaries.fleetServer = { targetFile, moduleCount }; |
| 230 | + } |
| 231 | + }); |
| 232 | + |
| 233 | + if (detectedBinaries.fleetServer) { |
| 234 | + expect(detectedBinaries.fleetServer.moduleCount).toEqual(76); |
| 235 | + } else { |
| 236 | + fail("fleet-server not detected"); |
| 237 | + } |
| 238 | + |
| 239 | + const detectedCount = |
| 240 | + Object.values(detectedBinaries).filter(Boolean).length; |
| 241 | + expect(detectedCount).toBe(1); |
| 242 | + }); |
| 243 | + |
| 244 | + it("should report module-level dependencies (not package-level) for stripped/CGo binaries", async () => { |
| 245 | + const pluginResult = await scan(getScanOptions()); |
| 246 | + |
| 247 | + const goModules = pluginResult.scanResults.filter( |
| 248 | + (r) => r.identity.type === "gomodules", |
| 249 | + ); |
| 250 | + |
| 251 | + expect(goModules.length).toEqual(1); |
| 252 | + |
| 253 | + const fleetServer = goModules.find((r) => |
| 254 | + r.identity.targetFile?.includes("fleet-server"), |
| 255 | + ); |
| 256 | + |
| 257 | + if (!fleetServer) { |
| 258 | + return; |
| 259 | + } |
| 260 | + |
| 261 | + const depGraphFact = fleetServer.facts.find((f) => f.type === "depGraph"); |
| 262 | + const depGraph = depGraphFact?.data; |
| 263 | + |
| 264 | + expect(depGraph).toBeDefined(); |
| 265 | + |
| 266 | + const packages = depGraph.getPkgs(); |
| 267 | + const sampleDeps = packages.slice(0, 10); |
| 268 | + |
| 269 | + sampleDeps.forEach((pkg: any) => { |
| 270 | + expect(pkg.name).toBeDefined(); |
| 271 | + if (pkg.version !== undefined) { |
| 272 | + expect(typeof pkg.version).toBe("string"); |
| 273 | + } |
| 274 | + }); |
| 275 | + }); |
| 276 | +}); |
0 commit comments