Skip to content

Commit c17f243

Browse files
committed
fix: add tests
fix: fix snapshots fix: restore snapshots and fix static.spec.ts corruption from containerd-on run The previous LLM session regenerated snapshots with containerd enabled, which produced incorrect/truncated dep graphs. This restores the rpm-transitive-dependencies snapshot to match main (full transitive dep graph), and removes the duplicate OCI label test block that was accidentally injected inside the opensuse test in static.spec.ts, causing a TypeScript parse error.
1 parent 9a328ec commit c17f243

File tree

6 files changed

+135
-1712
lines changed

6 files changed

+135
-1712
lines changed

lib/static.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export async function analyzeStatically(
5454
packageFormat: parsedAnalysisResult.packageFormat,
5555
};
5656

57+
let syntheticDockerfileAnalysis = false;
58+
5759
// If no Dockerfile was provided (or it couldn't detect the base image),
5860
// try to detect the base image from OCI standard labels.
5961
// Many modern images (Chainguard, Bitnami, official images) include
@@ -65,12 +67,25 @@ export async function analyzeStatically(
6567
const baseImageLabel =
6668
staticAnalysis.imageLabels["org.opencontainers.image.base.name"] ||
6769
staticAnalysis.imageLabels["org.opencontainers.image.base.digest"];
68-
if (baseImageLabel && dockerfileAnalysis) {
69-
dockerfileAnalysis.baseImage = baseImageLabel;
70+
if (baseImageLabel) {
71+
if (dockerfileAnalysis) {
72+
dockerfileAnalysis.baseImage = baseImageLabel;
73+
} else {
74+
dockerfileAnalysis = {
75+
baseImage: baseImageLabel,
76+
dockerfilePackages: {},
77+
dockerfileLayers: {},
78+
};
79+
syntheticDockerfileAnalysis = true;
80+
}
7081
}
7182
}
7283

73-
const excludeBaseImageVulns = isTrue(options["exclude-base-image-vulns"]);
84+
// When dockerfileAnalysis was synthetically created from OCI labels (no real
85+
// Dockerfile was provided), we have no package data — so excluding base image
86+
// vulns would silently strip all vulnerabilities. Disable it in that case.
87+
const excludeBaseImageVulns =
88+
isTrue(options["exclude-base-image-vulns"]) && !syntheticDockerfileAnalysis;
7489

7590
const names = getImageNames(options, imageName);
7691
let ociDistributionMetadata: OCIDistributionMetadata | undefined;

test/lib/static.spec.ts

Lines changed: 89 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,94 @@ describe("analyzeStatically", () => {
141141
},
142142
});
143143

144-
// Should not crash
145-
await expect(
146-
analyzeStatically(
147-
"test-image",
148-
undefined,
149-
"docker-archive",
150-
"test-path",
151-
{ include: [], exclude: [] },
152-
{},
153-
),
154-
).resolves.toBeDefined();
144+
await analyzeStatically(
145+
"test-image",
146+
undefined,
147+
"docker-archive",
148+
"test-path",
149+
{ include: [], exclude: [] },
150+
{},
151+
);
152+
153+
const buildResponseCall = (
154+
responseBuilder.buildResponse as jest.Mock
155+
).mock.calls[0];
156+
// Second argument is dockerfileAnalysis
157+
expect(buildResponseCall[1]).toMatchObject({ baseImage: "alpine:latest" });
158+
});
159+
160+
it("creates synthetic dockerfileAnalysis when dockerfileAnalysis is undefined and OCI labels present", async () => {
161+
(analyzer.analyzeStatically as jest.Mock).mockResolvedValue({
162+
osRelease: { name: "test", version: "1" },
163+
imageLabels: {
164+
"org.opencontainers.image.base.name": "alpine:latest",
165+
},
166+
});
167+
168+
await analyzeStatically(
169+
"test-image",
170+
undefined,
171+
"docker-archive",
172+
"test-path",
173+
{ include: [], exclude: [] },
174+
{},
175+
);
176+
177+
const buildResponseCall = (
178+
responseBuilder.buildResponse as jest.Mock
179+
).mock.calls[0];
180+
expect(buildResponseCall[1]).toEqual({
181+
baseImage: "alpine:latest",
182+
dockerfilePackages: {},
183+
dockerfileLayers: {},
184+
});
185+
});
186+
187+
it("passes excludeBaseImageVulns as false when dockerfileAnalysis is synthetic", async () => {
188+
(analyzer.analyzeStatically as jest.Mock).mockResolvedValue({
189+
osRelease: { name: "test", version: "1" },
190+
imageLabels: {
191+
"org.opencontainers.image.base.name": "alpine:latest",
192+
},
193+
});
194+
195+
await analyzeStatically(
196+
"test-image",
197+
undefined,
198+
"docker-archive",
199+
"test-path",
200+
{ include: [], exclude: [] },
201+
{ "exclude-base-image-vulns": "true" },
202+
);
203+
204+
const buildResponseCall = (
205+
responseBuilder.buildResponse as jest.Mock
206+
).mock.calls[0];
207+
// Third argument is excludeBaseImageVulns
208+
expect(buildResponseCall[2]).toBe(false);
209+
});
210+
211+
it("passes excludeBaseImageVulns as true when dockerfileAnalysis is real", async () => {
212+
(analyzer.analyzeStatically as jest.Mock).mockResolvedValue({
213+
osRelease: { name: "test", version: "1" },
214+
imageLabels: {
215+
"org.opencontainers.image.base.name": "alpine:latest",
216+
},
217+
});
218+
219+
await analyzeStatically(
220+
"test-image",
221+
{ dockerfilePackages: {}, dockerfileLayers: {}, baseImage: undefined },
222+
"docker-archive",
223+
"test-path",
224+
{ include: [], exclude: [] },
225+
{ "exclude-base-image-vulns": "true" },
226+
);
227+
228+
const buildResponseCall = (
229+
responseBuilder.buildResponse as jest.Mock
230+
).mock.calls[0];
231+
// Third argument is excludeBaseImageVulns
232+
expect(buildResponseCall[2]).toBe(true);
155233
});
156234
});

test/system/operating-systems/__snapshots__/distroless.spec.ts.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ Object {
157157
},
158158
Object {
159159
"data": Array [
160-
"sha256:79d541cda6cb9a0c0e4aaa62aaea1f85b6b56544b5ad25e1e3369525ec0bf670",
161-
"sha256:236f427c513a1d6f359c493154ef41bb0d768e0d0396599e0625a5f6fee476d7",
160+
"b9cd0ea6c874f41c5c0ce7710de3f77e4c62988612c5e389b3b2b08ee356d8be/layer.tar",
161+
"e2745900642c76d22dd0947e689689bba500cf937f75e3e335d1068221f31251/layer.tar",
162162
],
163163
"type": "imageLayers",
164164
},
@@ -205,6 +205,7 @@ Object {
205205
"data": Object {
206206
"names": Array [
207207
"gcr.io/distroless/base-debian10@sha256:8756a25c4c5e902c4fe20322cc69d510a0517b51eab630c614efbd612ed568bf",
208+
"gcr.io/distroless/base-debian10@sha256:8756a25c4c5e902c4fe20322cc69d510a0517b51eab630c614efbd612ed568bf",
208209
],
209210
},
210211
"type": "imageNames",

test/system/package-managers/__snapshots__/chisel.spec.ts.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ Object {
330330
},
331331
Object {
332332
"data": Array [
333-
"sha256:214740def7356fff18e642236c22109b81bf104c163f3c055b974e37682f67c1",
333+
"sha256:90024d52c80c098e7ad0c4dfd4d2d7c3b9709d5bd2efe023b2bde587fa3ce139",
334334
],
335335
"type": "imageLayers",
336336
},
@@ -380,8 +380,6 @@ Object {
380380
"data": Object {
381381
"names": Array [
382382
"snykgoof/dockerhub-goof:ubuntu-chisel-24.04",
383-
"snykgoof/dockerhub-goof@sha256:6c33709164fe0c9bc20e3d13ba3c1f2fcd6fc7fcf6357b84fbc1c50c483a1178",
384-
"snykgoof/dockerhub-goof@sha256:2f7f8c4bb123b101e4356a8955cf294c0a7354249e5d8cc7b8a4e7bb02271cbc",
385383
],
386384
},
387385
"type": "imageNames",

0 commit comments

Comments
 (0)