Skip to content

Commit 332b924

Browse files
committed
fix: fix snapshots
1 parent b4a28f0 commit 332b924

File tree

22 files changed

+240
-4411
lines changed

22 files changed

+240
-4411
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/app-os/__snapshots__/globs.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ Object {
19281928
},
19291929
Object {
19301930
"data": Array [
1931-
"sha256:756975cb9c7e7933d824af9319b512dd72a50894232761d06ef3be59981df838",
1931+
"sha256:114ca5b7280f3b49e94a67659890aadde83d58a8bde0d9020b2bc8c902c3b9de",
19321932
],
19331933
"type": "imageLayers",
19341934
},

0 commit comments

Comments
 (0)