Skip to content

Commit 9edd8bb

Browse files
authored
fix: add target-refence to application scan results (#751)
* fix: apply target-reference option to application scan results * test: apply target-reference option to application scan results
1 parent d9a0b98 commit 9edd8bb

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

lib/response-builder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ async function buildResponse(
163163
target: {
164164
image: depGraph.rootPkg.name,
165165
},
166+
...(options &&
167+
options["target-reference"] && {
168+
targetReference: options["target-reference"],
169+
}),
166170
};
167171
});
168172

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { scan } from "../../../lib";
2+
import { getFixture } from "../../util";
3+
4+
describe("target-reference flag", () => {
5+
it("applies target-reference to OS scan result", async () => {
6+
const fixturePath = getFixture(
7+
"docker-archives/skopeo-copy/rpm-npm-yarn.tar",
8+
);
9+
const imageNameAndTag = `docker-archive:${fixturePath}`;
10+
const targetReference = "my-custom-reference";
11+
12+
const pluginResult = await scan({
13+
path: imageNameAndTag,
14+
"target-reference": targetReference,
15+
"exclude-app-vulns": true,
16+
});
17+
18+
expect(pluginResult.scanResults).toHaveLength(1);
19+
20+
// OS scan result should have targetReference
21+
const osScanResult = pluginResult.scanResults[0];
22+
expect(osScanResult.targetReference).toEqual(targetReference);
23+
});
24+
25+
it("applies target-reference to both OS and application scan results", async () => {
26+
const fixturePath = getFixture(
27+
"docker-archives/skopeo-copy/rpm-npm-yarn.tar",
28+
);
29+
const imageNameAndTag = `docker-archive:${fixturePath}`;
30+
const targetReference = "my-custom-reference";
31+
32+
const pluginResult = await scan({
33+
path: imageNameAndTag,
34+
"target-reference": targetReference,
35+
});
36+
37+
// Should have OS scan result + application scan results
38+
expect(pluginResult.scanResults.length).toBeGreaterThan(1);
39+
40+
// All scan results should have the targetReference
41+
for (const scanResult of pluginResult.scanResults) {
42+
expect(scanResult.targetReference).toEqual(targetReference);
43+
}
44+
});
45+
46+
it("does not include targetReference when option is not provided", async () => {
47+
const fixturePath = getFixture(
48+
"docker-archives/skopeo-copy/rpm-npm-yarn.tar",
49+
);
50+
const imageNameAndTag = `docker-archive:${fixturePath}`;
51+
52+
const pluginResult = await scan({
53+
path: imageNameAndTag,
54+
});
55+
56+
// Should have OS scan result + application scan results
57+
expect(pluginResult.scanResults.length).toBeGreaterThan(1);
58+
59+
// No scan results should have targetReference
60+
for (const scanResult of pluginResult.scanResults) {
61+
expect(scanResult.targetReference).toBeUndefined();
62+
}
63+
});
64+
});

0 commit comments

Comments
 (0)