1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! Open URL in the default browser — mirrors `packages/coding-agent/src/utils/open-browser.ts`.
//!
//! Cross-platform utility to open a URL in the user's default browser.
//! Intentionally avoids invoking a shell to prevent command injection from
//! attacker-controlled URLs.
//!
//! # Platform Support
//!
//! - **macOS**: Uses `open` command
//! - **Windows**: Uses `rundll32 url.dll,FileProtocolHandler`
//! - **Linux/Unix**: Uses `xdg-open`
//!
//! # Security
//!
//! This implementation deliberately does NOT use a shell. On Windows, using
//! `cmd /c start` would allow attacker-controlled URLs to inject metacharacters
//! (&, |, ^, etc.) that cmd.exe re-parses before `start` runs.
//!
//! # Example
//!
//! ```rust
//! use rpi_tools::utils::open_browser::open_browser;
//!
//! // Open a URL in the default browser
//! if let Err(e) = open_browser("https://example.com") {
//! eprintln!("Failed to open browser: {}", e);
//! }
//! ```
use Command;
/// Open a URL in the default browser. Returns `Ok(())` if the browser launch
/// command was successfully invoked (not if the browser actually opened the URL).
///
/// # Arguments
///
/// * `url` - The URL to open. Should be a valid URL string.
///
/// # Returns
///
/// * `Ok(())` - The browser launch command was invoked successfully.
/// * `Err(String)` - Failed to invoke the browser launch command.
///
/// # Platform-specific behavior
///
/// - **macOS**: Runs `open <url>`
/// - **Windows**: Runs `rundll32 url.dll,FileProtocolHandler <url>`
/// - **Linux/Unix**: Runs `xdg-open <url>`
///
/// # Security notes
///
/// This function intentionally does NOT invoke a shell. The URL is passed
/// directly as an argument to the platform-specific launcher, preventing
/// command injection from malicious URLs.
/// Pure platform mapping used by [`open_browser`] — resolves which launcher
/// command to run, but spawns nothing.
///
/// Split out from [`open_browser`] so the platform selection can be unit-tested
/// without actually launching the user's browser.
/// Open a file with the default application. Returns `Ok(())` if the launch
/// command was successfully invoked.
///
/// # Arguments
///
/// * `path` - The file path to open.
///
/// # Returns
///
/// * `Ok(())` - The launch command was invoked successfully.
/// * `Err(String)` - Failed to invoke the launch command.