İçeriğe geç

OS komut enjeksiyonu (ARCH3102)

Anahtar typescript.security.os-command-injection · Önem error · Kategori security · CWE CWE-78

Komut dizesini kullanıcı girdisiyle oluşturup shell üzerinden çalıştırmak, saldırganların ; veya | gibi karakterlerle keyfi komut yürütmesine olanak tanır.

import { exec } from "child_process";
app.get("/ping", (req, res) => {
// host="8.8.8.8; rm -rf /"
exec(`ping -c 1 ${req.query.host}`, (err, out) => res.send(out));
});
import { execFile } from "child_process";
app.get("/ping", (req, res) => {
const host = String(req.query.host);
if (!/^[a-z0-9.-]+$/i.test(host)) return res.status(400).end();
execFile("ping", ["-c", "1", host], (err, out) => res.send(out));
});

Shell yerine execFile/spawn kullanın, argümanları dizi olarak geçin ve girdiyi doğrulayın.