Skip to content

Commit 9bc0f62

Browse files
fix(backend-ssh): quote the resolved harness path in ExecStart
ExecStart= splits an unquoted value on whitespace, so a "buzz-acp path on the server" naming a directory with a space in it — which the config schema accepts — made systemd run the first word with the rest as arguments, and the unit never started. The SSH resolution code already shell-quotes such a path; systemd's command-line syntax is a different question. Write the substituted path double-quoted, escaping backslash and double quote first since systemd unquotes C-style escapes inside double quotes. The escape pass filters through sed rather than editing in place, so the portability argument that kept sed -i out still holds. Signed-off-by: Troy Hoffman <[email protected]>
1 parent 285924d commit 9bc0f62

3 files changed

Lines changed: 75 additions & 18 deletions

File tree

‎crates/buzz-backend-ssh/assets/[email protected]‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ Type=simple
1313
# chmod 600; systemd reads it as the owning user.
1414
EnvironmentFile=%h/.config/buzz-acp/%i.env
1515
# Absolute path, substituted at install time from the host's resolved
16-
# `buzz-acp`. systemd does not expand environment variables in the program
17-
# position, and the shell indirection that would work around that is not worth
18-
# adding to a unit whose environment carries a private key.
16+
# `buzz-acp` and double-quoted there — systemd splits an unquoted value on
17+
# whitespace, and the configurable `buzz-acp path on the server` may name a
18+
# directory that contains some. systemd does not expand environment variables
19+
# in the program position, and the shell indirection that would work around
20+
# that is not worth adding to a unit whose environment carries a private key.
1921
ExecStart=@BUZZ_ACP_BIN@
2022
Restart=always
2123
RestartSec=5

‎crates/buzz-backend-ssh/src/deploy.rs‎

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -580,14 +580,31 @@ fi
580580
// silently runs the old unit.
581581
//
582582
// `@BUZZ_ACP_BIN@` is substituted with parameter expansion rather than
583-
// `sed`: `sed -i` is a GNU extension BSD and macOS hosts reject, and any
584-
// `s///` would need a delimiter no resolved path can contain.
583+
// `sed -i`, a GNU extension BSD and macOS hosts reject, and any in-place
584+
// `s///` over the template would need a delimiter no resolved path can
585+
// contain.
586+
//
587+
// The substituted value is quoted per systemd's own command-line syntax,
588+
// which is not the shell's. `ExecStart=` splits an unquoted value on
589+
// whitespace, so a `buzz-acp path on the server` pointing at
590+
// `/opt/buzz tools/buzz-acp` would make systemd run `/opt/buzz` with
591+
// `tools/buzz-acp` as an argument — the schema accepts such a path, and
592+
// `quote()` already makes it safe as a *shell* argument, which is a
593+
// different question. Inside double quotes systemd unquotes C-style
594+
// escapes, so `\` and `"` are escaped first; a filtering `sed` is portable
595+
// even where `sed -i` is not.
596+
//
597+
// The quotes are literals in `printf`'s single-quoted format rather than
598+
// shell-escaped inside the value, so the three interpolations stay plain
599+
// `%s` arguments — the unit template's own `%i`/`%h` specifiers ride
600+
// through untouched for the same reason.
585601
script.push_str(&format!(
586602
r#"unit_file="$units/[email protected]"
603+
acp_unit=$(printf '%s' "$acp" | sed 's/[\\"]/\\&/g')
587604
template=$(cat <<'BUZZ_UNIT_EOF'
588605
{unit}BUZZ_UNIT_EOF
589606
)
590-
printf '%s\n' "${{template%%@BUZZ_ACP_BIN@*}}$acp${{template#*@BUZZ_ACP_BIN@}}" > "$unit_file.new"
607+
printf '%s"%s"%s\n' "${{template%%@BUZZ_ACP_BIN@*}}" "$acp_unit" "${{template#*@BUZZ_ACP_BIN@}}" > "$unit_file.new"
591608
if cmp -s "$unit_file.new" "$unit_file"; then
592609
rm -f "$unit_file.new"
593610
else
@@ -1134,10 +1151,12 @@ mod tests {
11341151
let script = deploy_script(&agent, &config(), UNIT_TEMPLATE, &Pushes::default()).unwrap();
11351152
assert!(UNIT_TEMPLATE.contains("ExecStart=@BUZZ_ACP_BIN@"));
11361153
assert!(UNIT_TEMPLATE.contains("EnvironmentFile=%h/.config/buzz-acp/%i.env"));
1137-
// Substitution is parameter expansion, not `sed`: `sed -i` is a GNU
1138-
// extension that BSD and macOS hosts reject.
1139-
assert!(!script.contains("sed "));
1140-
assert!(script.contains("${template%%@BUZZ_ACP_BIN@*}$acp${template#*@BUZZ_ACP_BIN@}"));
1154+
// Substitution is parameter expansion, not `sed -i`: in-place editing
1155+
// is a GNU extension that BSD and macOS hosts reject.
1156+
assert!(!script.contains("sed -i"));
1157+
assert!(script.contains(
1158+
r#"printf '%s"%s"%s\n' "${template%%@BUZZ_ACP_BIN@*}" "$acp_unit" "${template#*@BUZZ_ACP_BIN@}""#
1159+
));
11411160
// Lingering, or the agent dies when this SSH session ends. It also
11421161
// creates /run/user/$(id -u), so it must precede any bus traffic.
11431162
let linger = script.find("loginctl enable-linger").unwrap();
@@ -1301,7 +1320,7 @@ mod tests {
13011320
let unit =
13021321
std::fs::read_to_string(root.join(".config/systemd/user/[email protected]")).unwrap();
13031322
assert!(unit.contains(&format!(
1304-
"ExecStart={}",
1323+
"ExecStart=\"{}\"",
13051324
root.join("bin/buzz-acp").display()
13061325
)));
13071326
assert!(!unit.contains("@BUZZ_ACP_BIN@"));
@@ -1517,10 +1536,11 @@ if [ -z "${{XDG_RUNTIME_DIR:-}}" ]; then
15171536
export XDG_RUNTIME_DIR
15181537
fi
15191538
unit_file="$units/[email protected]"
1539+
acp_unit=$(printf '%s' "$acp" | sed 's/[\\"]/\\&/g')
15201540
template=$(cat <<'BUZZ_UNIT_EOF'
15211541
{unit}BUZZ_UNIT_EOF
15221542
)
1523-
printf '%s\n' "${{template%%@BUZZ_ACP_BIN@*}}$acp${{template#*@BUZZ_ACP_BIN@}}" > "$unit_file.new"
1543+
printf '%s"%s"%s\n' "${{template%%@BUZZ_ACP_BIN@*}}" "$acp_unit" "${{template#*@BUZZ_ACP_BIN@}}" > "$unit_file.new"
15241544
if cmp -s "$unit_file.new" "$unit_file"; then
15251545
rm -f "$unit_file.new"
15261546
else
@@ -1600,7 +1620,7 @@ systemctl --user restart 'buzz-acp@{slug}.service'
16001620
let unit =
16011621
std::fs::read_to_string(root.join(".config/systemd/user/[email protected]")).unwrap();
16021622
assert!(
1603-
unit.contains(&format!("ExecStart={}", installed.display())),
1623+
unit.contains(&format!("ExecStart=\"{}\"", installed.display())),
16041624
"{unit}"
16051625
);
16061626
}
@@ -1679,7 +1699,7 @@ systemctl --user restart 'buzz-acp@{slug}.service'
16791699
let unit =
16801700
std::fs::read_to_string(root.join(".config/systemd/user/[email protected]")).unwrap();
16811701
assert!(unit.contains(&format!(
1682-
"ExecStart={}",
1702+
"ExecStart=\"{}\"",
16831703
root.join("bin/buzz-acp").display()
16841704
)));
16851705
}
@@ -1744,7 +1764,38 @@ systemctl --user restart 'buzz-acp@{slug}.service'
17441764
let unit =
17451765
std::fs::read_to_string(root.join(".config/systemd/user/[email protected]")).unwrap();
17461766
assert!(
1747-
unit.contains(&format!("ExecStart={}", installed.display())),
1767+
unit.contains(&format!("ExecStart=\"{}\"", installed.display())),
1768+
"{unit}"
1769+
);
1770+
}
1771+
1772+
/// `ExecStart=` splits an unquoted value on whitespace, so a configured
1773+
/// `buzz-acp path on the server` inside a directory with a space in it
1774+
/// would make systemd run the first word and pass the rest as arguments.
1775+
/// The schema accepts such a path and `quote()` makes it a safe *shell*
1776+
/// argument, which is a different question from what systemd parses.
1777+
#[cfg(unix)]
1778+
#[test]
1779+
fn a_resolved_path_containing_whitespace_stays_one_word_in_exec_start() {
1780+
let root = sandbox_host("spaced-acp", HostAcp::Missing);
1781+
let acp = seed_stub(&root.join("buzz tools"), "buzz-acp", "#!/bin/sh\nexit 0\n");
1782+
let config = SshConfig {
1783+
buzz_acp_path: Some(acp.display().to_string()),
1784+
..config()
1785+
};
1786+
let agent = Agent::from_request(&request()).unwrap();
1787+
let script = deploy_script(&agent, &config, UNIT_TEMPLATE, &Pushes::default()).unwrap();
1788+
let output = run_in_sandbox(&root, &script);
1789+
assert!(
1790+
output.status.success(),
1791+
"deploy failed: {}",
1792+
String::from_utf8_lossy(&output.stderr)
1793+
);
1794+
1795+
let unit =
1796+
std::fs::read_to_string(root.join(".config/systemd/user/[email protected]")).unwrap();
1797+
assert!(
1798+
unit.contains(&format!("ExecStart=\"{}\"\n", acp.display())),
17481799
"{unit}"
17491800
);
17501801
}

‎docs/remote-agents.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,13 @@ WantedBy=default.target
411411
- `ExecStart` is an absolute path, substituted at install time from the host's resolved `buzz-acp`.
412412
systemd does not expand environment variables in the program position, and the shell indirection
413413
that would work around that is not worth adding to a unit whose environment carries a private key.
414-
The substitution is shell parameter expansion, not `sed`: `sed -i` is a GNU extension that BSD and
415-
macOS hosts reject. Resolution runs *first*; the install only fills an empty `$acp`, so a deploy
416-
that installed `buzz-acp` writes the path of the copy it just installed, not a stale one.
414+
The substitution is shell parameter expansion, not in-place editing: `sed -i` is a GNU extension
415+
that BSD and macOS hosts reject. Resolution runs *first*; the install only fills an empty `$acp`,
416+
so a deploy that installed `buzz-acp` writes the path of the copy it just installed, not a stale
417+
one. The path is written **double-quoted, per systemd's command-line syntax** — `ExecStart=` splits
418+
an unquoted value on whitespace, and `buzz_acp_path` may legitimately name a directory containing
419+
some, which would otherwise make systemd run the first word with the rest as arguments. `\` and `"`
420+
are escaped on the way in, since systemd unquotes C-style escapes inside double quotes.
417421

418422
The instance name is the agent name made unit-safe — lowercased, non-alphanumerics collapsed to `-`,
419423
truncated to 32 characters — followed by the first 12 hex characters of the agent's `pubkey`. The

0 commit comments

Comments
 (0)