Skip to content

Commit 3f80025

Browse files
authored
fix: revert stricter number validation when using --nested-jars-depth (#752)
* Revert "fix: enforce stricter number validation when using --nested-jars-depth flag (#736)" This reverts commit ec4f55e. * chore: removed unused test file
1 parent 9edd8bb commit 3f80025

5 files changed

Lines changed: 14 additions & 369 deletions

File tree

lib/analyzer/static-analyzer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ import {
5656
getRpmSqliteDbFileContent,
5757
getRpmSqliteDbFileContentAction,
5858
} from "../inputs/rpm/static";
59-
import { resolveNestedJarsOption } from "../option-utils";
6059
import { isTrue } from "../option-utils";
6160
import { ImageType, ManifestFile, PluginOptions } from "../types";
6261
import {
@@ -319,7 +318,8 @@ export async function analyze(
319318
}
320319

321320
function getNestedJarsDesiredDepth(options: Partial<PluginOptions>) {
322-
const nestedJarsOption = resolveNestedJarsOption(options);
321+
const nestedJarsOption =
322+
options["nested-jars-depth"] || options["shaded-jars-depth"];
323323
let nestedJarsDepth = 1;
324324
const depthNumber = Number(nestedJarsOption);
325325
if (!isNaN(depthNumber) && depthNumber >= 0) {

lib/option-utils.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
1-
import { PluginOptions } from "./types";
2-
export { isTrue, isNumber, isStrictNumber };
1+
export { isTrue, isNumber };
32

43
function isTrue(value?: boolean | string): boolean {
54
return String(value).toLowerCase() === "true";
65
}
76

8-
// This strictly follows the ECMAScript Language Specification: https://262.ecma-international.org/5.1/#sec-9.3
97
function isNumber(value?: boolean | string): boolean {
108
return !isNaN(Number(value));
119
}
12-
13-
// Must be a finite numeric value, excluding booleans, Infinity, and non-numeric strings
14-
function isStrictNumber(value?: boolean | string): boolean {
15-
if (typeof value === "boolean" || !value?.trim().length) {
16-
return false;
17-
}
18-
19-
const num = Number(value);
20-
return Number.isFinite(num);
21-
}
22-
23-
export function resolveNestedJarsOption(options?: Partial<PluginOptions>) {
24-
const safeOptions = options || {};
25-
26-
return [
27-
safeOptions["nested-jars-depth"],
28-
safeOptions["shaded-jars-depth"],
29-
].find(isDefined);
30-
}
31-
32-
export function isDefined(value?: string | boolean): boolean {
33-
return value !== "" && value != null;
34-
}

lib/scan.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ import { ImageName } from "./extractor/image";
99
import { ExtractAction, ExtractionResult } from "./extractor/types";
1010
import { fullImageSavePath } from "./image-save-path";
1111
import { getArchivePath, getImageType } from "./image-type";
12-
import {
13-
isDefined,
14-
isStrictNumber,
15-
isTrue,
16-
resolveNestedJarsOption,
17-
} from "./option-utils";
12+
import { isNumber, isTrue } from "./option-utils";
1813
import * as staticModule from "./static";
1914
import { ImageType, PluginOptions, PluginResponse } from "./types";
2015
import { isValidDockerImageReference } from "./utils";
@@ -45,24 +40,20 @@ async function getAnalysisParameters(
4540
throw new Error("No image identifier or path provided");
4641
}
4742

43+
const nestedJarsDepth =
44+
options["nested-jars-depth"] || options["shaded-jars-depth"];
4845
if (
49-
isDefined(options["shaded-jars-depth"]) &&
50-
isDefined(options["nested-jars-depth"])
46+
(isTrue(nestedJarsDepth) || isNumber(nestedJarsDepth)) &&
47+
isTrue(options["exclude-app-vulns"])
5148
) {
52-
throw new Error(
53-
"Cannot use --shaded-jars-depth together with --nested-jars-depth, please use the latter",
54-
);
55-
}
56-
57-
const nestedJarsDepth = resolveNestedJarsOption(options);
58-
if (isStrictNumber(nestedJarsDepth) && isTrue(options["exclude-app-vulns"])) {
5949
throw new Error(
6050
"To use --nested-jars-depth, you must not use --exclude-app-vulns",
6151
);
6252
}
6353

6454
if (
65-
(!isStrictNumber(nestedJarsDepth) &&
55+
(!isNumber(nestedJarsDepth) &&
56+
!isTrue(nestedJarsDepth) &&
6657
typeof nestedJarsDepth !== "undefined") ||
6758
Number(nestedJarsDepth) < 0
6859
) {

test/system/application-scans/java.spec.ts

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,26 @@ describe("jar binaries scanning", () => {
9696
imageNameAndTag = `docker-archive:${fixturePath}`;
9797
});
9898

99-
it(`should unpack 1 level of jars if ${flagName} flag is missing`, async () => {
99+
it("should return nested (second-level) jar in the result", async () => {
100100
// Act
101101
pluginResult = await scan({
102102
path: imageNameAndTag,
103103
"app-vulns": true,
104+
[flagName]: true,
104105
});
105106

107+
// Assert
106108
fingerprints =
107109
pluginResult.scanResults[1].facts[0].data.fingerprints;
108110

109111
expect(fingerprints).toContainEqual(nestedJar);
110112
});
111113

112-
it(`should unpack 1 level of jars if ${flagName} flag is ''`, async () => {
114+
it(`should unpack 1 level of jars if ${flagName} flag is missing`, async () => {
113115
// Act
114116
pluginResult = await scan({
115117
path: imageNameAndTag,
116118
"app-vulns": true,
117-
[flagName]: "",
118119
});
119120

120121
fingerprints =
@@ -145,17 +146,6 @@ describe("jar binaries scanning", () => {
145146
expect(fingerprints).not.toContainEqual(nestedJar);
146147
});
147148

148-
it(`should throw if ${flagName} flag is set to true`, async () => {
149-
// Act + Assert
150-
await expect(
151-
scan({
152-
path: imageNameAndTag,
153-
"app-vulns": true,
154-
[flagName]: true,
155-
}),
156-
).rejects.toThrow();
157-
});
158-
159149
it(`should throw if ${flagName} flag is set to -1`, async () => {
160150
// Act + Assert
161151
await expect(
@@ -167,17 +157,6 @@ describe("jar binaries scanning", () => {
167157
).rejects.toThrow();
168158
});
169159

170-
it(`should throw if ${flagName} flag is set to ' '`, async () => {
171-
// Act + Assert
172-
await expect(
173-
scan({
174-
path: imageNameAndTag,
175-
"app-vulns": true,
176-
[flagName]: " ",
177-
}),
178-
).rejects.toThrow();
179-
});
180-
181160
it("should throw error if exclude-app-vulns flag is true", async () => {
182161
// Act
183162
await expect(
@@ -488,23 +467,5 @@ describe("jar binaries scanning", () => {
488467
});
489468
},
490469
);
491-
describe("conflicting flags", () => {
492-
const fixturePath = getFixture(
493-
"docker-archives/docker-save/java-uberjar.tar",
494-
);
495-
const imageNameAndTag = `docker-archive:${fixturePath}`;
496-
497-
it(`should throw if both --shaded-jars-depth and --nested-jars-depth flags are set`, async () => {
498-
// Act + Assert
499-
await expect(
500-
scan({
501-
path: imageNameAndTag,
502-
"app-vulns": true,
503-
"shaded-jars-depth": "2",
504-
"nested-jars-depth": "4",
505-
}),
506-
).rejects.toThrow();
507-
});
508-
});
509470
});
510471
});

0 commit comments

Comments
 (0)