-
Notifications
You must be signed in to change notification settings - Fork 1.8k
JS: Enhance command injection detection for CLI argument parsing libraries #20151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e8eb9be
Add command injection tests for CLI argument parsing libraries
Napalys e980798
Added step through yargs/yargs constructor and chained methods.
Napalys 6b4e34d
Added a step from parse to opts for commander js
Napalys 39170f3
Added couple more test cases for commander js
Napalys d6508f3
Add taint flow for Commander.js direct property access and action cal…
Napalys ae4077d
add taint flow for arg/command-line-args with custom argv option
Napalys 881ea76
Added change note
Napalys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
javascript/ql/lib/change-notes/2025-08-01-cli-code-injection.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Improved modeling of command-line argument parsing libraries [arg](https://www.npmjs.com/package/arg), [args](https://www.npmjs.com/package/args), [command-line-args](https://www.npmjs.com/package/command-line-args) and [commander](https://www.npmjs.com/package/commander) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -87,11 +87,43 @@ private class ArgsParseStep extends TaintTracking::SharedTaintStep { | |||||||||
| override predicate step(DataFlow::Node pred, DataFlow::Node succ) { | ||||||||||
| exists(DataFlow::CallNode call | | ||||||||||
| call = DataFlow::moduleMember("args", "parse").getACall() or | ||||||||||
| call = DataFlow::moduleImport(["yargs-parser", "minimist", "subarg"]).getACall() | ||||||||||
| call = | ||||||||||
| DataFlow::moduleImport(["yargs-parser", "minimist", "subarg", "yargs/yargs", "yargs"]) | ||||||||||
| .getACall() | ||||||||||
| | | ||||||||||
| succ = call and | ||||||||||
| pred = call.getArgument(0) | ||||||||||
| ) | ||||||||||
| or | ||||||||||
| exists(API::Node commanderNode | commanderNode = commander() | | ||||||||||
| pred = commanderNode.getMember(["parse", "parseAsync"]).getACall().getAnArgument() and | ||||||||||
| succ = | ||||||||||
| [ | ||||||||||
| commanderNode.getMember("opts").getACall(), commanderNode.getAMember().asSource(), | ||||||||||
| commander() | ||||||||||
| .getMember("action") | ||||||||||
| .getACall() | ||||||||||
| .getArgument(0) | ||||||||||
| .(DataFlow::FunctionNode) | ||||||||||
| .getAParameter() | ||||||||||
| ] | ||||||||||
|
||||||||||
| ] | |
| // Propagate to both the function node itself and its parameters | |
| as DataFlow::Node | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
javascript/ql/test/query-tests/Security/CWE-078/CommandInjection/command-line-libs.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import express from 'express'; | ||
| import { Command } from 'commander'; | ||
| import { exec } from 'child_process'; | ||
| import arg from 'arg'; | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| app.post('/Command', async (req, res) => { | ||
| const args = req.body.args || []; // $ Source | ||
| const program = new Command(); | ||
| program.option('--cmd <value>', 'Command to execute'); | ||
| program.parse(args, { from: 'user' }); | ||
| const options = program.opts(); | ||
| exec(options.cmd); // $ Alert | ||
| exec(program.cmd); // $ Alert | ||
|
|
||
| const program1 = new Command(); | ||
| program1 | ||
| .command('run <script>') | ||
| .action((script) => { | ||
| exec(script); // $ Alert | ||
| }); | ||
| await program1.parseAsync(args); | ||
| }); | ||
|
|
||
| app.post('/arg', (req, res) => { | ||
| const argsArray = req.body.args || []; // $ Source | ||
| const parsed = arg({ '--cmd': String }, { argv: argsArray }); | ||
| exec(parsed['--cmd']); // $ Alert | ||
| }); | ||
|
|
||
| app.post('/commandLineArgs', (req, res) => { | ||
| const commandLineArgs = require('command-line-args'); | ||
| const optionDefinitions = [{ name: 'cmd', type: String }]; | ||
| const options = commandLineArgs(optionDefinitions, { argv: req.body.args || [] }); // $ Source | ||
| if (!options.cmd) return res.status(400).send({ error: 'Missing --cmd' }); | ||
| exec(options.cmd); // $ Alert | ||
| }); | ||
|
|
||
| app.post('/yargs', (req, res) => { | ||
| const yargs = require('yargs/yargs'); | ||
| const args = req.body.args || []; // $ Source | ||
| const parsed = yargs(args).option('cmd', { | ||
| type: 'string', | ||
| describe: 'Command to execute', | ||
| demandOption: true | ||
| }).parse(); | ||
|
|
||
| exec(parsed.cmd); // $ Alert | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The module import list includes both "yargs/yargs" and "yargs" which could cause duplicate taint tracking for the same library. The "yargs" entry should be removed here since yargs is handled separately in the yargs() function below.