Skip to content

Commit 8d8f64f

Browse files
committed
Mitigate CLI argument type conversion
Yargs automatically converts numeric CLI arguments to numbers. As the fuzzer argument handling only expects strings, enforce the argument types.
1 parent 6ccf724 commit 8d8f64f

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

packages/core/cli.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import yargs, { Argv } from "yargs";
1919
import { startFuzzing } from "./core";
20-
import { ensureFilepath } from "./utils";
20+
import { prepareArgs } from "./utils";
2121
import { defaultOptions, processOptions, fromSnakeCase } from "./options";
2222

2323
// Use yargs to parse command line arguments and provide a nice CLI experience.
@@ -227,17 +227,7 @@ yargs(process.argv.slice(2))
227227
},
228228
// eslint-disable-next-line @typescript-eslint/no-explicit-any
229229
(args: any) => {
230-
// Transform arguments to common format, add compound properties and
231-
// remove framework specific ones.
232-
const options = {
233-
...args,
234-
fuzz_target: ensureFilepath(args.fuzz_target),
235-
fuzzer_options: (args.corpus ?? []).concat(args._),
236-
};
237-
delete options._;
238-
delete options.corpus;
239-
delete options.$0;
240-
230+
const options = prepareArgs(args);
241231
// noinspection JSIgnoredPromiseFromCall
242232
startFuzzing(processOptions(options, fromSnakeCase));
243233
},

packages/core/utils.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ensureFilepath } from "./utils";
17+
import { ensureFilepath, prepareArgs } from "./utils";
1818

1919
import path from "path";
2020

@@ -35,4 +35,24 @@ describe("core", () => {
3535
expect(ensureFilepath("filename.js")).toMatch(expectedPath);
3636
});
3737
});
38+
describe("prepareArgs", () => {
39+
it("converts fuzzer args to strings", () => {
40+
const args = {
41+
_: ["-some_arg=value", "-other_arg", 123],
42+
corpus: ["directory1", "directory2"],
43+
fuzz_target: "filename.js",
44+
};
45+
const options = prepareArgs(args);
46+
expect(options).toEqual({
47+
fuzz_target: "file://" + path.join(process.cwd(), "filename.js"),
48+
fuzzer_options: [
49+
"directory1",
50+
"directory2",
51+
"-some_arg=value",
52+
"-other_arg",
53+
"123",
54+
],
55+
});
56+
});
57+
});
3858
});

packages/core/utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,26 @@ export function ensureFilepath(filePath: string): string {
3939
? fullPath
4040
: fullPath + ".js";
4141
}
42+
43+
/**
44+
* Transform arguments to common format, add compound properties and
45+
* remove framework specific ones, so that the result can be passed on to the
46+
* regular option handling code.
47+
*
48+
* The function is extracted to "utils" as importing "cli" in tests directly
49+
* tries to parse command line arguments.
50+
*/
51+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
52+
export function prepareArgs(args: any) {
53+
const options = {
54+
...args,
55+
fuzz_target: ensureFilepath(args.fuzz_target),
56+
fuzzer_options: (args.corpus ?? [])
57+
.concat(args._)
58+
.map((e: unknown) => e + ""),
59+
};
60+
delete options._;
61+
delete options.corpus;
62+
delete options.$0;
63+
return options;
64+
}

0 commit comments

Comments
 (0)