Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions src/javascript/detectors/code_injection/code_injection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
// {fact rule=code-injection@v1.0 defects=1}
var express = require('express')
var app = express()
var vm = require('vm')
var exec = require("child_process")
function codeInjectionNoncompliant() {
app.get('/perform/:action', (req, res) => {
const sandbox = {
actionToPerform: req.params.action
}
const code = 'performAction(sandbox.actionToPerform)'
vm.createContext(sandbox)
// Noncompliant: user-supplied input evaluated as a script.
vm.runInContext(code, sandbox)
res.send('Action performed successfully!')
app.get('/read/logfile', (req, res) => {
const command = req.query.command
const parameter = req.query.parameter
const lines = req.query.lines
// Noncompliant: passing user-supplied datas into the shell command.
exec(command + " " + parameter + " " + lines + " ./logfile.txt" , (error, stdout, stderr) => {
res.send(stdout)
})
})
}
// {/fact}
Expand All @@ -25,21 +24,25 @@ function codeInjectionNoncompliant() {
// {fact rule=code-injection@v1.0 defects=0}
var express = require('express')
var app = express()
var vm = require('vm')
var exec = require("child_process")
function codeInjectionCompliant() {
app.get('/perform/:action', (req, res) => {
const sandbox = {
actionToPerform: req.params.action
}
const code = 'performAction(sandbox.actionToPerform)'
vm.createContext(sandbox)
// Compliant: user-supplied parameter must be in allow-list to be evaluated.
if(sandbox.actionToPerform.match(/^pull|fetch|add|commit$/)) {
vm.runInContext(code, sandbox)
res.send('Action performed successfully!')
app.get('/read/logfile', (req, res) => {
const command = req.query.command
const parameter = req.query.parameter
const lines = req.query.lines

const allowedCommands = ['head', 'tail']
const allowedParameters = ['-n', '-c']

// Compliant: validating user-supplied datas before passing them into the shell command.
if ( allowedCommands.indexOf(command) != -1 && allowedParameters.indexOf(parameter) != -1 && !isNaN(lines)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you fix indentation:
if (allowedCommands.indexOf(command) != -1)
Note no tailing space after (

exec(command + " " + parameter + " " + lines + " ./logfile.txt" , (error, stdout, stderr) => {
res.send(stdout)
})
}
else
else {
res.send('Invalid action')
}
})
}
// {/fact}