Skip to content

Commit e8eb9be

Browse files
committed
Add command injection tests for CLI argument parsing libraries
1 parent 16e9e8e commit e8eb9be

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import express from 'express';
2+
import { Command } from 'commander';
3+
import { exec } from 'child_process';
4+
import arg from 'arg';
5+
const app = express();
6+
app.use(express.json());
7+
8+
app.post('/Command', (req, res) => {
9+
const args = req.body.args || []; // $ MISSING: Source
10+
const program = new Command();
11+
program.option('--cmd <value>', 'Command to execute');
12+
program.parse(args, { from: 'user' });
13+
const options = program.opts();
14+
exec(options.cmd); // $ MISSING: Alert
15+
});
16+
17+
app.post('/arg', (req, res) => {
18+
const argsArray = req.body.args || []; // $ MISSING: Source
19+
const parsed = arg({ '--cmd': String }, { argv: argsArray });
20+
exec(parsed['--cmd']); // $ MISSING: Alert
21+
});
22+
23+
app.post('/commandLineArgs', (req, res) => {
24+
const commandLineArgs = require('command-line-args');
25+
const optionDefinitions = [{ name: 'cmd', type: String }];
26+
const options = commandLineArgs(optionDefinitions, { argv: req.body.args || [] }); // $ MISSING: Source
27+
if (!options.cmd) return res.status(400).send({ error: 'Missing --cmd' });
28+
exec(options.cmd); // $ MISSING: Alert
29+
});
30+
31+
app.post('/yargs', (req, res) => {
32+
const yargs = require('yargs/yargs');
33+
const args = req.body.args || []; // $ MISSING: Source
34+
const parsed = yargs(args).option('cmd', {
35+
type: 'string',
36+
describe: 'Command to execute',
37+
demandOption: true
38+
}).parse();
39+
40+
exec(parsed.cmd); // $ MISSING: Alert
41+
});

0 commit comments

Comments
 (0)