flawopen.com/command-injection/Ruby
Learn how to fix Command Injection (CWE-78) in Ruby. Side-by-side vulnerable vs secure code examples for Open3.capture2() and system() array arguments.
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.
Web Application SecurityCWE-918.CWE-918Defense-in-DepthAplikasi menerima input nama host diagnostik atau nama file langsung dari permintaan HTTP.
Backend merangkai perintah shell menggunakan penggabungan string mentah alih-alih vektor argumen terisolasi.
Penyerang menyisipkan karakter meta shell seperti ';', '&&', '|', atau backticks (misal: '127.0.0.1; id') untuk keluar dari konteks perintah.
Shell sistem operasi menjalankan perintah yang disuntikkan dengan hak akses penuh proses web server.
# Backtick execution invokes /bin/sh
def fetch_git_log(branch)
# Input: "main; whoami"
`git log #{branch}`
end
# Multiple arguments to system/Open3 bypass the shell
require 'open3'
def fetch_git_log(branch)
# branch is passed as an isolated argument to git
stdout, stderr, status = Open3.capture3("git", "log", branch)
stdout
end
open() on user-supplied filenames; use File.open().