Unescaped Variable in Remote Command Medium

Unescaped variables in remote commands (like those used with SSH) are expanded locally before being sent to the remote host, leading to unexpected behavior. To ensure variables are expanded on the remote host, escape the dollar sign or use single quotes.

Detector ID
shell/unescaped-variable-in-remote-command@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-
Tags
-

Noncompliant example

1
2# Noncompliant: `$HOME` expands on the client side, potentially using the wrong home directory.
3ssh user@remote "ls -l $HOME/documents"

Compliant example

1
2# Compliant: `\$HOME` is passed literally to the remote host, expanding to the correct remote home directory.
3ssh user@remote "ls -l \$HOME/documents"