0

I have below code in my js file, Checkmarx flagged a command injection vulnerability at execSync('scripts/beautify_yaml.sh '+ yamlPath).toString();

function beautifyYaml(yamlPath) {
    return execSync('scripts/beautify_yaml.sh '+ yamlPath).toString();
}

I added a validation check before execSync function but checkmarx still flags the same command injection issue at execSync function. Is there another way to resolve command injection issue at line execSync('scripts/beautify_yaml.sh '+ yamlPath).toString();

Validation check:

function beautifyYaml(yamlPath) {
    const reg = new RegExp("^[a-zA-Z0-9\-_.\/]+$");
    if (!reg.test(yamlPath)) {
        throw new Error('Invalid YAML file path.');
    }
    return execSync('scripts/beautify_yaml.sh '+ yamlPath).toString();
}
  • Does this answer your question? [How do I escape a string for a shell command in node?](https://stackoverflow.com/questions/1779858/how-do-i-escape-a-string-for-a-shell-command-in-node) – Justinas Jul 25 '23 at 10:02
  • And probably disallow paths like `../` , `~/`, `/` and might be some other paths which would allow to navigate outside your data directory – Renat Jul 25 '23 at 10:04

0 Answers0