SKY-D212 · Security in code
Command injection
Command injection happens when outside text becomes part of a command that a shell runs. Skylos reports SKY-D212 when data from a request, a function parameter or input() reaches shell command text, such as subprocess.run(..., shell=True) or os.system(), or chooses which program runs.
- Languages
- Python, TypeScript/JavaScript, Go, Java, Kotlin, C#, PHP, Rust, Ruby, Dart, Shell
- Quality gate
- counted toward the gate limits; a CRITICAL finding fails the gate on its own (full and diff scans)
- Finding type
- review
- Go engine ID
- SKY-G212, reported as SKY-D212
Why it matters
A visitor who sends example.com; cat /etc/passwd as a host name runs a second command on your server, with the server's files, keys and network access. One such endpoint is enough to take over the machine.
What Skylos flags
import subprocess from flask import Flask, request app = Flask(__name__) @app.post("/ping")def ping(): host = request.form.get("host", "") out = subprocess.run(f"ping -c 1 {host}", shell=True, capture_output=True) return {"output": out.stdout.decode()}What Skylos leaves alone
import shleximport subprocess from flask import Flask, request app = Flask(__name__) @app.post("/ping")def ping(): host = request.form.get("host", "") command = f"ping -c 1 {shlex.quote(host)}" out = subprocess.run(command, shell=True, capture_output=True) return {"output": out.stdout.decode()}Why Skylos leaves it alone
shlex.quote() wraps the host in single quotes and escapes any quote inside it, so the shell sees one word. Skylos checks where the quoted value lands: after a fixed program name, outside other quotes, with no $(, backtick, comment or unknown text before it. The line still gets SKY-D209, because it uses shell=True.
More languages
TypeScript
Flagged
import express from "express";import { exec } from "child_process"; const app = express(); app.get("/ping", (req, res) => { const host = String(req.query.host ?? ""); exec(`ping -c 1 ${host}`, (err, stdout) => { res.json({ ok: !err, output: stdout }); });}); app.listen(3000);Not flagged
import express from "express";import { execFile } from "child_process"; const app = express(); app.get("/ping", (req, res) => { const host = String(req.query.host ?? ""); execFile("ping", ["-c", "1", host], (err, stdout) => { res.json({ ok: !err, output: stdout }); });}); app.listen(3000);Why Skylos leaves it alone
execFile() starts ping directly with an argument array and no shell, so the host is one argument and cannot start a new command.
Go
Flagged
package main import ( "net/http" "os/exec") func ping(w http.ResponseWriter, r *http.Request) { host := r.URL.Query().Get("host") err := exec.Command("sh", "-c", "ping -c 1 "+host).Run() if err != nil { http.Error(w, "host unreachable", http.StatusBadGateway) return } w.WriteHeader(http.StatusNoContent)} func main() { http.HandleFunc("/ping", ping) http.ListenAndServe(":8080", nil)}Not flagged
package main import ( "net/http" "os/exec") func ping(w http.ResponseWriter, r *http.Request) { host := r.URL.Query().Get("host") err := exec.Command("ping", "-c", "1", host).Run() if err != nil { http.Error(w, "host unreachable", http.StatusBadGateway) return } w.WriteHeader(http.StatusNoContent)} func main() { http.HandleFunc("/ping", ping) http.ListenAndServe(":8080", nil)}Why Skylos leaves it alone
The program is the fixed literal ping and the host is a separate argument. Go's exec.Command does not use a shell, so Skylos only checks the program, or the -c script of a shell such as sh.
How Skylos decides
- In Python, finds shell-string calls (
os.system,os.popen,subprocess.getoutput,subprocess.getstatusoutput,asyncio.create_subprocess_shell),subprocess.*calls withshell=Trueor ashell=value that is not a literalFalse,Noneor0, an SSH client'sexec_command(), the program argument ofos.exec*,os.spawn*,os.posix_spawn*andpty.spawn, and the script after-cwhen the program is a shell (["bash", "-c", script]). Import aliases such asimport subprocess as spare resolved; a local function with the same name is not a sink. - Treats a value as tainted when it comes from
request.*,input()or a function parameter (exceptselfandcls), directly or through assignments, f-strings,+,%and.format().sys.argvand environment variables are not sources for this rule. - Leaves an argument list for a fixed program alone (
subprocess.run(["git", "fetch", ref])): an outside argument to a fixed program is not a shell command. - Accepts
shlex.quote()andshlex.join()only where the quoted value stays one shell word: after a program name made of plain characters that is not a shell or code runner (sh,python,eval,xargsand others), outside quotes, and with no$(, backtick, here-document, comment or unknown text before it."echo '" + shlex.quote(x) + "'"is still reported. - Does not recognise allowlist checks: a value tested with
if host not in ALLOWED: abort(400)is still tainted afterwards. - TypeScript and JavaScript: request data from an Express, Fastify, Koa, NestJS or Next.js handler that reaches
child_process.exec()orexecSync()command text is CRITICAL. Separately, anyX.exec()call in a file that loads a command module is HIGH, unlessXis a proven RegExp or comes from a database package (const { R } = require("redbean-node"),pg,knex, ...). A command that is one string literal is kept but not listed by default. - Go (reported by the Go engine as SKY-G212):
exec.Command,exec.CommandContext,os.StartProcessandsyscall.Execwhose program comes from request or network input, andsh -c/bash -cscripts built from a variable.os.Args, flags and environment variables are not sources there either. Java, Kotlin, C#, PHP, Ruby, Dart and shell scripts have their own checks for outside data reaching a process call. - Rust:
Command::new()with a non-literal program, or the code ofsh -c/cmd /C/powershell -Command, is listed only when request data is an actix-web or axum extractor (Path,Query,Json,Form, ...), a RocketJson/Form/Dataguard or text route parameter (#[get("/x/<name>")]withname: &str), or the data methods of anHttpRequest/Request(uri(),query_string(),headers(), ...). A program chosen by an environment variable,std::env::argsor a function's parameter stays in the report as a review item that is not listed and does not fail a gate. A request value passed to a helper in the same file is reported at that call.
How to fix it
Run a fixed program with an argument list and no shell. Each value is then one argument and can never start another command. Check the value too (for example that it is a host name), so it cannot pass an option such as -f to the program.
import subprocess from flask import Flask, request app = Flask(__name__) @app.post("/ping")def ping(): host = request.form.get("host", "")Removed: out = subprocess.run(f"ping -c 1 {host}", shell=True, capture_output=True)Added: out = subprocess.run(["ping", "-c", "1", "--", host], capture_output=True) return {"output": out.stdout.decode()}When it can be wrong
- A helper that receives the command as a parameter, such as
def run(cmd): subprocess.run(cmd, shell=True), is reported even when every caller passes a constant. Parameters count as untrusted. - A value checked against an allowlist before the call is still reported: the check is not recognised.
- The same line usually also gets SKY-D209 (
shell=True) or SKY-D203 (os.system), which report the call itself whatever its input.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Silence one line and say why. Only the listed rule is hidden. A bare
# noqadoes not hide HIGH or CRITICAL security findings.Python subprocess.run(cmd, shell=True) # skylos: ignore[SKY-D212] cmd is from COMMANDSAccept the findings you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D212"]turns the rule off for the whole project, real injections included.