A command line built at run time is handed to a shell
What it finds
exec.Command("sh", "-c", "tar xf " + name): the shell interprets the whole string, so a value that contains ;, $(...), quotes or spaces runs commands of its own choosing or changes the arguments. Passing the program and each argument to exec.Command separately needs no shell and no quoting.
The call must be Command or CommandContext of os/exec, resolved through the imports, whose program is a constant naming a shell (sh, bash, zsh, dash, ksh, cmd, cmd.exe, powershell or pwsh, with or without a directory), followed by -c, /c or -Command, and whose command string is not a constant of the file.
From the rule's source, shell_command_built.rs.
package p import ( "context" "os/exec") const listing = "ls -la" func f(ctx context.Context, name string) error { if err := exec.Command("sh", "-c", "tar xf "+name).Run(); err != nil {tx0 reports: this command line is built at run time and run by sh return err } if err := exec.CommandContext(ctx, "/bin/bash", "-c", name).Run(); err != nil {tx0 reports: this command line is built at run time and run by bash return err } if err := exec.Command("sh", "-c", listing).Run(); err != nil { return err } return exec.Command("tar", "xf", name).Run()} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/go:shell-command-built" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/go:shell-command-built] reason="..."tx0 explain tx0/go:shell-command-built shows the same in the terminal. Found a false positive? Report it.