Skip to content

/careful: heredoc bodies are scanned as shell commands #4843

Not planned

Description

@nghorbani

Since 1.103 made the destructive-command check a default hook, I get approval prompts in auto mode for commands that don't delete anything. Almost all of them have the same cause: the check reads the body of a heredoc as if it were shell.

#1060 fixed this for git commit -m "..." and for -m "$(cat <<'EOF' ... EOF)". Any other heredoc still goes through the statement scan, which splits the body at newlines and backticks and checks each line as a command.

Repro

1.103.1.0 on Windows 11 with Git Bash. I sent each of these as tool_input.command to careful/bin/check-careful.sh:

python - <<'EOF'
t = "the script runs rm -rf on the staging lib folder"
print(t)
EOF
git commit -F - <<'EOF'
staging: rm -rf the lib folder before copying
EOF

Both come back with:

{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"[careful] Destructive: recursive delete (rm -r). This permanently removes files."}}

The same message through git commit -m "$(cat <<'EOF' ... EOF)" returns {}, and so does the Python heredoc once the string no longer says rm.

Agents write heredocs like these all day, for small Python edits and for commit messages, and the text tends to describe the change. The one that made me look was a script rewriting a changelog line about a build step that clears a folder.

What I'd expect

With a quoted delimiter (<<'EOF', <<"EOF", <<\EOF) the body is literal, so it could be skipped the same way the commit message is. With an unquoted delimiter, $(...) and backticks in the body still run, so those parts would still need the scan.

Windows temp folders

A smaller thing. A recursive delete only counts as scratch under /tmp and /var/tmp (_RX_TMP). Git Bash maps /tmp to %TEMP%, but the same folder written as C:/Users/<user>/AppData/Local/Temp/<dir> still asks, and that's the spelling my agent's scratch folder has. Treating the resolved %TEMP% path like /tmp would cover it.

For now I've turned the guard off with gstack-config set destructive_guard off. That works, but I'd rather have it on.

Activity

  1. capy-ai commented on Nov 26, 2026

    @capy-ai
    Contributor

    Thanks, this reproduces on current main (8982588): both of your heredocs come back with ask from careful/bin/check-careful.sh, and the same text through git commit -m "$(cat <<'EOF' … EOF)" returns {}.

    We're picking up the heredoc part as a bug fix (tracking thread GSTA-36). Because this loosens a safety check, the change will only add a narrow exemption on top of the existing scan: the body of a heredoc with a quoted delimiter (<<'EOF', <<"EOF", <<\EOF) is treated as literal text, an unquoted delimiter keeps scanning $(...) and backticks in the body, and the command that opens the heredoc is still checked as it is today. Before it ships we'll replay the guard's test cases against main, so nothing main asks about becomes allowed.

    We'll look at the Windows %TEMP% spelling in the same change, but it only goes in if the resolved path can be shown to be the same folder Git Bash calls /tmp; otherwise it stays as it is.

  2. added
    bug
    S3
    on Nov 26, 2026
  3. capy-ai commented on Nov 26, 2026

    @capy-ai
    Contributor

    Thanks for the detailed report and repros. We worked through an implementation in draft PR #4852 and decided to close this as not planned for now, for three reasons:

    1. python - <<'EOF' cannot safely be exempted by a text check. Because Python executes code, an allowlist/denylist over heredoc text is trivially bypassed (e.g. via pydoc.locate, timeit, or dynamic imports), so keeping prompts on arbitrary Python heredocs is intentional for safety.
    2. General heredoc-header masking in the shell/awk hook proved too fragile. Across four security review passes on a narrow git commit -F - / gh / cat exemption, reviewers reproduced five distinct bash bypasses (including git -c, function/hash -p shadowing, later-statement alias replay of .git/COMMIT_EDITMSG, and unquoted brace expansion). For multi-line commit messages, git commit -m "$(cat <<'EOF' ... EOF)" already passes /careful without prompting.
    3. Windows %TEMP% path handling falls outside our minimal Windows support scope (core install, startup, and core workflows).

    We'd only reconsider git commit -F - <<'EOF' with either an exact, flag-free literal-header match or a real bash AST parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    S3bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions