jcode icon indicating copy to clipboard operation
jcode copied to clipboard

bash gate: destructive commands inside "$(...)", backticks or an unquoted heredoc are rated Safe

Open mg-mg-mg opened this issue 1 month ago • 1 comments

Summary

The bash risk gate rates destructive commands Safe when they sit inside a command substitution that is itself inside double quotes, or inside backticks:

Command Verdict on master a61c38e
echo $(rm -rf ~) Catastrophic
x=$(rm -rf ~) Catastrophic
echo "$(rm -rf ~)" Safe
echo `rm -rf ~` Safe
echo "`rm -rf ~`" Safe
cat <<EOF, then $(rm -rf ~), then EOF (unquoted heredoc) Safe

All four Safe rows run rm -rf ~ in bash.

Cause

tokenize resolves double quotes into a single word. The content of "$(rm -rf ~)" becomes one literal token, $(rm -rf ~), and is never split into a segment, so rm is never seen as a program. Backticks are not treated as substitution at all. In an unquoted heredoc the body is removed by without_heredoc_bodies (#922), but the shell still expands $(…) in it.

Expected

A destructive command inside "$(…)", inside backticks, or in an unquoted heredoc body is assessed like the same command at top level. The quoted-heredoc case (<<'EOF') stays inert, because the shell does not expand it.

Notes

  • I found this while fixing the opposite problem, an over-firing case, in #1751 / #1752. That PR does not change any of these verdicts. I checked before and after.
  • A possible approach is to have the tokenizer recursively assess the contents of $(…) and backtick spans in unquoted and double-quoted contexts, and to keep unquoted heredoc bodies for substitution scanning only.

Related: #725 (subshell and case bodies), #604.

mg-mg-mg avatar Oct 07 '26 17:10 mg-mg-mg

Thanks, this was a clear and well-scoped report. Fixed in #1764: substitution bodies from $(...) and backticks (outside single quotes) and from unquoted heredoc bodies are now assessed recursively like top-level commands. Quoted heredocs and single-quoted text stay inert. Every row of your table now rates Catastrophic, with regression tests for both directions. It doesn't change the #1751/#1752 over-firing fix.

--- — Jcode agent (automated triage), on behalf of @1jehuang

1jehuang avatar Oct 08 '26 02:10 1jehuang