|
580 | 580 | // silently runs the old unit. |
581 | 581 | // |
582 | 582 | // `@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. |
585 | 601 | script.push_str(&format!( |
586 | 602 | r#"unit_file="$units/[email protected]" |
| 603 | +acp_unit=$(printf '%s' "$acp" | sed 's/[\\"]/\\&/g') |
587 | 604 | template=$(cat <<'BUZZ_UNIT_EOF' |
588 | 605 | {unit}BUZZ_UNIT_EOF |
589 | 606 | ) |
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" |
591 | 608 | if cmp -s "$unit_file.new" "$unit_file"; then |
592 | 609 | rm -f "$unit_file.new" |
593 | 610 | else |
@@ -1134,10 +1151,12 @@ mod tests { |
1134 | 1151 | let script = deploy_script(&agent, &config(), UNIT_TEMPLATE, &Pushes::default()).unwrap(); |
1135 | 1152 | assert!(UNIT_TEMPLATE.contains("ExecStart=@BUZZ_ACP_BIN@")); |
1136 | 1153 | 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 | + )); |
1141 | 1160 | // Lingering, or the agent dies when this SSH session ends. It also |
1142 | 1161 | // creates /run/user/$(id -u), so it must precede any bus traffic. |
1143 | 1162 | let linger = script.find("loginctl enable-linger").unwrap(); |
@@ -1301,7 +1320,7 @@ mod tests { |
1301 | 1320 | let unit = |
1302 | 1321 | std ::fs ::read_to_string(root .join(".config/systemd/user/[email protected]")).unwrap(); |
1303 | 1322 | assert!(unit.contains(&format!( |
1304 | | - "ExecStart={}", |
| 1323 | + "ExecStart=\"{}\"", |
1305 | 1324 | root.join("bin/buzz-acp").display() |
1306 | 1325 | ))); |
1307 | 1326 | assert!(!unit.contains("@BUZZ_ACP_BIN@")); |
@@ -1517,10 +1536,11 @@ if [ -z "${{XDG_RUNTIME_DIR:-}}" ]; then |
1517 | 1536 | export XDG_RUNTIME_DIR |
1518 | 1537 | fi |
1519 | 1538 | unit_file="$units/[email protected]" |
| 1539 | +acp_unit=$(printf '%s' "$acp" | sed 's/[\\"]/\\&/g') |
1520 | 1540 | template=$(cat <<'BUZZ_UNIT_EOF' |
1521 | 1541 | {unit}BUZZ_UNIT_EOF |
1522 | 1542 | ) |
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" |
1524 | 1544 | if cmp -s "$unit_file.new" "$unit_file"; then |
1525 | 1545 | rm -f "$unit_file.new" |
1526 | 1546 | else |
@@ -1600,7 +1620,7 @@ systemctl --user restart 'buzz-acp@{slug}.service' |
1600 | 1620 | let unit = |
1601 | 1621 | std ::fs ::read_to_string(root .join(".config/systemd/user/[email protected]")).unwrap(); |
1602 | 1622 | assert!( |
1603 | | - unit.contains(&format!("ExecStart={}", installed.display())), |
| 1623 | + unit.contains(&format!("ExecStart=\"{}\"", installed.display())), |
1604 | 1624 | "{unit}" |
1605 | 1625 | ); |
1606 | 1626 | } |
@@ -1679,7 +1699,7 @@ systemctl --user restart 'buzz-acp@{slug}.service' |
1679 | 1699 | let unit = |
1680 | 1700 | std ::fs ::read_to_string(root .join(".config/systemd/user/[email protected]")).unwrap(); |
1681 | 1701 | assert!(unit.contains(&format!( |
1682 | | - "ExecStart={}", |
| 1702 | + "ExecStart=\"{}\"", |
1683 | 1703 | root.join("bin/buzz-acp").display() |
1684 | 1704 | ))); |
1685 | 1705 | } |
@@ -1744,7 +1764,38 @@ systemctl --user restart 'buzz-acp@{slug}.service' |
1744 | 1764 | let unit = |
1745 | 1765 | std ::fs ::read_to_string(root .join(".config/systemd/user/[email protected]")).unwrap(); |
1746 | 1766 | 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())), |
1748 | 1799 | "{unit}" |
1749 | 1800 | ); |
1750 | 1801 | } |
|
0 commit comments