From b35dfc2599ad84b21d278ca2c330e46b5e58c956 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 15 Jul 2017 23:50:27 +0800 Subject: [PATCH] reject malformed CLI parameters (#2239) fixes #2237 --- bin/uglifyjs | 22 +++++++++++++--------- lib/propmangle.js | 2 +- test/mocha/cli.js | 26 ++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/bin/uglifyjs b/bin/uglifyjs index 68d67c2f..8cbb3cad 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -35,11 +35,11 @@ else if (process.argv.indexOf("options") >= 0) program.helpInformation = functio } return text.join("\n"); }; -program.option("-p, --parse ", "Specify parser options.", parse_js("parse", true)); -program.option("-c, --compress [options]", "Enable compressor/specify compressor options.", parse_js("compress", true)); -program.option("-m, --mangle [options]", "Mangle names/specify mangler options.", parse_js("mangle", true)); -program.option("--mangle-props [options]", "Mangle properties/specify mangler options.", parse_js("mangle-props", true)); -program.option("-b, --beautify [options]", "Beautify output/specify output options.", parse_js("beautify", true)); +program.option("-p, --parse ", "Specify parser options.", parse_js()); +program.option("-c, --compress [options]", "Enable compressor/specify compressor options.", parse_js()); +program.option("-m, --mangle [options]", "Mangle names/specify mangler options.", parse_js()); +program.option("--mangle-props [options]", "Mangle properties/specify mangler options.", parse_js()); +program.option("-b, --beautify [options]", "Beautify output/specify output options.", parse_js()); program.option("-o, --output ", "Output file (default STDOUT)."); program.option("--comments [filter]", "Preserve copyright comments in the output."); program.option("--config-file ", "Read minify() options from JSON file."); @@ -310,7 +310,7 @@ function read_file(path, default_value) { } } -function parse_js(flag, constants) { +function parse_js(flag) { return function(value, options) { options = options || {}; try { @@ -328,7 +328,7 @@ function parse_js(flag, constants) { if (node instanceof UglifyJS.AST_Assign) { var name = node.left.print_to_string(); var value = node.right; - if (!constants) { + if (flag) { options[name] = value; } else if (value instanceof UglifyJS.AST_Array) { options[name] = value.elements.map(to_string); @@ -351,14 +351,18 @@ function parse_js(flag, constants) { } })); } catch(ex) { - options[value] = null; + if (flag) { + fatal("Error parsing arguments for '" + flag + "': " + value); + } else { + options[value] = null; + } } return options; } } function parse_source_map() { - var parse = parse_js("sourceMap", true); + var parse = parse_js(); return function(value, options) { var hasContent = options && "content" in options; var settings = parse(value, options); diff --git a/lib/propmangle.js b/lib/propmangle.js index f17909d7..49490755 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -76,7 +76,7 @@ function mangle_properties(ast, options) { only_cache: false, regex: null, reserved: null, - }); + }, true); var reserved = options.reserved; if (!Array.isArray(reserved)) reserved = []; diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 8bfdc85a..1d847dc9 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -593,8 +593,8 @@ describe("bin/uglifyjs", function () { done(); }); }); - it("Should work with --mangle reserved=[]", function (done) { - var command = uglifyjscmd + ' test/input/issue-505/input.js -m reserved=[callback]'; + it("Should work with --mangle reserved=[]", function(done) { + var command = uglifyjscmd + " test/input/issue-505/input.js -m reserved=[callback]"; exec(command, function (err, stdout) { if (err) throw err; @@ -603,8 +603,8 @@ describe("bin/uglifyjs", function () { done(); }); }); - it("Should work with --mangle reserved=false", function (done) { - var command = uglifyjscmd + ' test/input/issue-505/input.js -m reserved=false'; + it("Should work with --mangle reserved=false", function(done) { + var command = uglifyjscmd + " test/input/issue-505/input.js -m reserved=false"; exec(command, function (err, stdout) { if (err) throw err; @@ -613,4 +613,22 @@ describe("bin/uglifyjs", function () { done(); }); }); + it("Should fail with --mangle-props reserved=[in]", function(done) { + var command = uglifyjscmd + " test/input/issue-505/input.js --mangle-props reserved=[in]"; + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.ok(/^Supported options:\n[\s\S]*?\nERROR: `reserved=\[in]` is not a supported option/.test(stderr), stderr); + done(); + }); + }); + it("Should fail with --define a-b", function(done) { + var command = uglifyjscmd + " test/input/issue-505/input.js --define a-b"; + exec(command, function (err, stdout, stderr) { + assert.ok(err); + assert.strictEqual(stdout, ""); + assert.strictEqual(stderr, "Error parsing arguments for 'define': a-b\n"); + done(); + }); + }); }); -- 2.34.1