Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

A builder's shell command is checked with its quotes joined, and naming the agent marker counts too (#1033)

Tanish Manku915fe6366ba2
diff --git a/crates/engine/src/tools/interaction_tools.rs b/crates/engine/src/tools/interaction_tools.rs
index 934eacb6..32c1a6a2 100644
--- a/crates/engine/src/tools/interaction_tools.rs
+++ b/crates/engine/src/tools/interaction_tools.rs
@@ -636,11 +636,15 @@ fn publishes(c: &str) -> bool {
}
/// Whether a lower-cased command names what a builder mustn't change from the shell either: the
-/// project's manifest (where its approvals are kept), its inbox (where answers are written), or the
-/// tool that sets approvals (`craft-anything-cli` can call it). A command that reaches them without
-/// naming them (a script that builds the path) isn't caught here.
+/// project's manifest (where its approvals are kept), its inbox (where answers are written), the
+/// tool that sets approvals (`craft-anything-cli` can call it) or the marker that makes our CLI call
+/// as an agent ([`crate::AGENT_ENV`]). Read with quotes and escapes taken out, as the shell joins
+/// `approv'als-set'`. A command that reaches them without naming them (a script that builds the
+/// path) isn't caught: with commands allowed, the builder is trusted that far.
fn names_records(c: &str, ws: Option<&Workspace>) -> bool {
- c.contains("craft.json") || c.contains("approvals_set") || c.contains("approvals-set") || ws.is_some_and(|w| mentions(c, &w.inbox_dir()))
+ let joined: String = c.chars().filter(|ch| !matches!(ch, '"' | '\'' | '\\' | '`')).collect();
+ let agent = crate::AGENT_ENV.to_ascii_lowercase();
+ ["craft.json", "approvals_set", "approvals-set", agent.as_str()].iter().any(|n| joined.contains(n)) || ws.is_some_and(|w| mentions(c, &w.inbox_dir()))
}
/// A program's name as typed, without its folder or a Windows extension (`/usr/bin/npm`, `npm.cmd`).
@@ -1150,6 +1154,9 @@ mod tests {
"python -c \"import json; d=json.load(open('craft.json'))\"",
"echo '{}' > ../craft.json",
&format!("echo yes > \"{inbox}/1.answer.json\""),
+ // The shell joins quoted pieces, and the marker can be taken away first.
+ "env -u CRAFT_ANYTHING_AGENT craft-anything-cli project-approv'als-set' --kind publish --mode allow",
+ "unset CRAFT_ANYTHING_AGENT; cat craft.\"json\"",
] {
assert_eq!(kind_of(Some(&w), "Bash", &json!({"command": cmd})), ActionKind::System, "{cmd}");
assert!(matches!(outcome(Some(&w), true, "Bash", &json!({"command": cmd})), Outcome::Refuse(_)), "{cmd}");
diff --git a/docs/claude-code.md b/docs/claude-code.md
index 98a541ad..d95e5fea 100644
--- a/docs/claude-code.md
+++ b/docs/claude-code.md
@@ -31,7 +31,10 @@ integration does, why it is shaped this way, and what remains to confirm.
delete or system change allows that whole kind for the project. A builder's own call to
`project_approvals_set` can only make a kind stricter, also through `craft-anything-cli` run
from its shell (agents we start have `CRAFT_ANYTHING_AGENT` set), and a command that names
- `craft.json`, the inbox or the approvals tool counts as a system change.
+ `craft.json`, the inbox, the approvals tool or that variable counts as a system change. These
+ raise the bar; they aren't a wall: a builder allowed to run any command runs with the person's
+ own permissions and could change those files another way, so "commands: allow" (Auto mode's
+ default) trusts the builder.
- Questions: Claude Code's own question tool is disabled for these runs (`--disallowedTools
AskUserQuestion`); the agent uses our `core_person_ask` tool, which shows a card with buttons.
- Before a build starts, `claude auth status` is checked. If Claude Code isn't signed in, the run
diff --git a/docs/pitfalls.md b/docs/pitfalls.md
index 643b1119..2420ac91 100644
--- a/docs/pitfalls.md
+++ b/docs/pitfalls.md
@@ -69,6 +69,9 @@ Don't close or rewrite other people's work without asking.
comments were acted on; the fixes went to its branch and the threads were answered "Fixed", while
`main` had the code without them. Read `gh pr view --json state,headRefOid` before pushing to a PR's
branch, and land anything late in a new PR from `main`.
+It happened again with the check in place: `gh pr view 1022 --json state -q .state && … git push`
+printed `MERGED` and went on, because printing a state isn't failing. The check has to stop the
+command: `test "$(gh pr view <n> --json state -q .state)" = OPEN && …`.
**A report checked against main, not the release it ran.** #736 (a build looping on an empty `app/`)
was read against main, where the stop after two unchanged turns already existed, so it looked fixed.