Skip to content

Commit f687135

Browse files
ravarora2BuzzPRReviewAssistant1
andcommitted
fix(desktop): reject legacy CLI invocations
Return a visible failure before native startup when old scripts reach the desktop executable instead of the legacy CLI. Co-authored-by: BuzzPRReviewAssistant1 <a581a7e8115222acdc32ffb14bfce55d48c24478d98e155f8eba126b69b83c7b@buzz.block.builderlab.xyz> Signed-off-by: Ravneet Arora <[email protected]>
1 parent 859c995 commit f687135

3 files changed

Lines changed: 183 additions & 1 deletion

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//! An old bundle's `buzz` CLI path can resolve to this app's `Buzz` on macOS.
2+
//! Recognize only its command names and options, leaving OS launch arguments
3+
//! and deep links to the desktop host. Never inspect or log command payloads.
4+
use std::ffi::OsStr;
5+
6+
pub(crate) fn is_legacy_cli_arg(arg: &OsStr) -> bool {
7+
let Some(arg) = arg.to_str() else {
8+
return false;
9+
};
10+
// Inspect only argv[1]. A deep link or an OS launch option may have values
11+
// that happen to match CLI words; those are not CLI invocations.
12+
let option = arg.split_once('=').map_or(arg, |(name, _)| name);
13+
matches!(
14+
arg,
15+
"help"
16+
| "agents"
17+
| "messages"
18+
| "channels"
19+
| "canvas"
20+
| "reactions"
21+
| "emoji"
22+
| "dms"
23+
| "users"
24+
| "workflows"
25+
| "feed"
26+
| "social"
27+
| "notes"
28+
| "repos"
29+
| "projects"
30+
| "patches"
31+
| "issues"
32+
| "pr"
33+
| "media"
34+
| "upload"
35+
| "mem"
36+
| "pack"
37+
| "moderation"
38+
| "--help"
39+
| "-h"
40+
| "--version"
41+
| "-V"
42+
) || matches!(
43+
option,
44+
"--relay" | "--private-key" | "--auth-tag" | "--format"
45+
)
46+
}
47+
48+
#[cfg(test)]
49+
mod tests {
50+
use super::is_legacy_cli_arg;
51+
52+
#[test]
53+
fn recognizes_legacy_commands_and_global_options() {
54+
for arg in [
55+
"help",
56+
"agents",
57+
"messages",
58+
"channels",
59+
"canvas",
60+
"reactions",
61+
"emoji",
62+
"dms",
63+
"users",
64+
"workflows",
65+
"feed",
66+
"social",
67+
"notes",
68+
"repos",
69+
"projects",
70+
"patches",
71+
"issues",
72+
"pr",
73+
"media",
74+
"upload",
75+
"mem",
76+
"pack",
77+
"moderation",
78+
"--help",
79+
"-h",
80+
"--version",
81+
"-V",
82+
"--relay",
83+
"--relay=https://example.com",
84+
"--private-key",
85+
"--private-key=secret",
86+
"--auth-tag",
87+
"--auth-tag={}",
88+
"--format",
89+
"--format=compact",
90+
] {
91+
assert!(is_legacy_cli_arg(arg.as_ref()), "{arg}");
92+
}
93+
}
94+
95+
#[test]
96+
fn preserves_deep_links_and_unrecognized_launch_arguments() {
97+
for arg in [
98+
"buzz://message?channel=messages&id=help",
99+
"buzz://channel/general",
100+
"buzz://join?relay=wss://example.com&code=notes",
101+
"-psn_0_12345",
102+
"-NSDocumentRevisionsDebugMode",
103+
"--allow-file-access-from-files",
104+
"--",
105+
"",
106+
"Messages",
107+
"messages.txt",
108+
"messages=send",
109+
"--formatting",
110+
"--help=something",
111+
] {
112+
assert!(!is_legacy_cli_arg(arg.as_ref()), "{arg}");
113+
}
114+
}
115+
116+
#[cfg(unix)]
117+
#[test]
118+
fn preserves_non_unicode_launch_arguments() {
119+
use std::os::unix::ffi::OsStrExt;
120+
assert!(!is_legacy_cli_arg(std::ffi::OsStr::from_bytes(b"\xff")));
121+
}
122+
}

‎src-tauri/src/main.rs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
22

3-
fn main() {
3+
mod legacy_cli_invocation;
4+
5+
fn main() -> std::process::ExitCode {
6+
// Reject before native startup, including the single-instance plugin, which
7+
// would otherwise foreground the app and report success without doing work.
8+
if std::env::args_os()
9+
.nth(1)
10+
.as_deref()
11+
.is_some_and(legacy_cli_invocation::is_legacy_cli_arg)
12+
{
13+
eprintln!(
14+
"This executable is the Buzz desktop app. The legacy Buzz CLI is not \
15+
included in this app bundle; no CLI command was executed. Update \
16+
your script to use a separately installed Buzz CLI."
17+
);
18+
return std::process::ExitCode::from(2);
19+
}
420
buzz_foundation_lib::run();
21+
std::process::ExitCode::SUCCESS
522
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! Exercise the real executable: these commands must exit before any GUI or
2+
//! single-instance initialization, even with no configured desktop identity.
3+
use std::process::{Command, Output, Stdio};
4+
use std::time::{Duration, Instant};
5+
6+
fn invoke(args: &[&str]) -> Output {
7+
let mut child = Command::new(env!("CARGO_BIN_EXE_Buzz"))
8+
.args(args)
9+
.stdin(Stdio::null())
10+
.stdout(Stdio::piped())
11+
.stderr(Stdio::piped())
12+
.spawn()
13+
.unwrap();
14+
let deadline = Instant::now() + Duration::from_secs(10);
15+
while child.try_wait().unwrap().is_none() {
16+
if Instant::now() >= deadline {
17+
let _ = child.kill();
18+
let _ = child.wait();
19+
panic!("Legacy CLI invocation started a long-running app instead of exiting");
20+
}
21+
std::thread::sleep(Duration::from_millis(20));
22+
}
23+
child.wait_with_output().unwrap()
24+
}
25+
26+
#[test]
27+
fn legacy_cli_commands_fail_without_echoing_payloads() {
28+
for args in [
29+
vec!["--help"],
30+
vec!["messages", "send", "--content", "private-message-payload"],
31+
vec!["--private-key=private-key-payload", "messages", "list"],
32+
vec!["--format", "compact", "channels", "list"],
33+
] {
34+
let output = invoke(&args);
35+
assert_eq!(output.status.code(), Some(2));
36+
assert!(output.stdout.is_empty());
37+
let stderr = String::from_utf8(output.stderr).unwrap();
38+
assert!(stderr.contains("Buzz desktop app"));
39+
assert!(stderr.contains("no CLI command was executed"));
40+
assert!(!stderr.contains("private-message-payload"));
41+
assert!(!stderr.contains("private-key-payload"));
42+
}
43+
}

0 commit comments

Comments
 (0)