R285 Pass text through a shell from a file written with a quoted heredoc

Default option

Write a text that you pass to a command through a shell, when it runs over more than one line or holds $, a backtick, a backslash, a quote or !, into a file with a quoted heredoc first, and pass the file to the command.

Reason

Inside double quotes the shell expands $ and backticks: a value in the text is replaced by whatever the shell finds and a code span in Markdown runs as a command, with no error, so the text arrives changed and still reads well, and a number in it may state a fact that was never looked up (Art. VII §1(2)). When the text is quoted from someone else, a $(…) in it runs on the agent’s own machine, a command injection others can exploit (Art. II §7). In the same double quotes, a " in the text ends the string early, and a backslash before $, a backtick, " or another backslash is dropped. Single quotes break on the first apostrophe, and ! can set off history expansion. A heredoc with a quoted delimiter passes every character as written, and the file serves a command that reads only files. Inside double quotes, a short text on one line with none of these characters passes unchanged, so it may stay on the command line.

Verification

The text covered is what a command takes as content, such as a message or a prompt for an agent, a request body, the body of a pull request, issue or comment, or a commit message; flags, paths and identifiers are not. A quoted heredoc is one whose delimiter is quoted, as in <<'EOF'. Passing the file means the command reads it, through an option such as --body-file or -F, or through "$(cat <file>)". The shell does not expand $ or backticks again in what that substitution puts inside double quotes, but it drops the trailing newlines and runs into the limit on the length of one argument, so an option that reads the file is the better choice where the command has one.

Violated when an agent passes such a text to a command written into the command line, in any quoting, writes it with a heredoc whose delimiter is not quoted, or feeds a heredoc to the command without writing it to a file.

Provisions of the constitution it cites

Relations

Parent
R284 How text reaches a command through a shell
Requires
none
Conflicts with
none

Metadata

ID
285
Revision
4
Kind
rule
Status
active
Slug
shell-text-through-a-file
Default level
project
Constitution revisions
>=1
Check
none
Uses
Instructions instructions
Original language
English en
Translations
Chinese zh