The analyzer has detected the creation of an operating system command from untrusted data obtained from an external source. This may lead to a command injection vulnerability.
Vulnerabilities of this kind fall under A5:2025 Injection in the OWASP Top 10 Application Security Risks 2025.
The example:
func PingHandler(w http.ResponseWriter, r *http.Request) {
host := r.URL.Query().Get("host")
output, _ := exec.Command("sh", "-c", "ping -c 1 "+host).Output()
w.Write(output)
}
The host query parameter is used to retrieve the address of the host to be tested with the ping command. The value is substituted into the command line without being checked, and then executed via the sh -c command interpreter.
An attacker may pass the following value in the host parameter:
example.com; cat /etc/passwd
The ; character will be interpreted by the command interpreter as a command separator. As a result, two commands will be executed:
ping -c 1 example.com
cat /etc/passwd
The results for both commands will be returned to the attacker in the response body. Similarly, you can use other command-line interpreter constructs: &&, ||, |, $, backticks, and so on. In this way, an attacker can read confidential data, modify or delete files, and download and execute malicious code with application privileges.
To prevent vulnerabilities from arising, it is recommended to always validate input data.
The fixed code:
var hostnameRe = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9.-]*$`)
func PingHandler(w http.ResponseWriter, r *http.Request) {
host := r.URL.Query().Get("host")
if !hostnameRe.MatchString(host) {
http.Error(w, "invalid hostname", http.StatusBadRequest)
return
}
output, err := exec.Command("sh", "-c",
"ping -c 1 " + host).Output()
if err != nil {
http.Error(w, "ping failed", http.StatusInternalServerError)
return
}
w.Write(output)
}
The regular expression ^[a-zA-Z0-9][a-zA-Z0-9.-]*$ allows only letters, digits, periods, and hyphens. Separators and special characters are excluded (;, |, &, etc.). This prevents the transmission of a value that would allow an attacker to execute a different command.
This diagnostic rule is classified as: