Skip to content

Commit 1caaa6d

Browse files
committed
fix: validate run attach server url
1 parent 7954d02 commit 1caaa6d

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

src/cortex-cli/src/run_cmd/execution.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ use super::output::{copy_to_clipboard, send_notification};
1717
use super::session::{SessionMode, resolve_session_id};
1818
use super::system::check_file_descriptor_limits;
1919

20+
fn validate_attach_url(server_url: &str) -> Result<()> {
21+
let parsed = reqwest::Url::parse(server_url).with_context(|| {
22+
format!(
23+
"--attach expects an HTTP(S) server URL, got '{server_url}'. Use --file/-f to attach local files."
24+
)
25+
})?;
26+
27+
match parsed.scheme() {
28+
"http" | "https" => Ok(()),
29+
_ => bail!(
30+
"--attach expects an HTTP(S) server URL, got '{server_url}'. Use --file/-f to attach local files."
31+
),
32+
}
33+
}
34+
2035
impl RunCli {
2136
/// Run the command.
2237
pub async fn run(self) -> Result<()> {
@@ -136,6 +151,7 @@ impl RunCli {
136151

137152
// Execute based on whether we're attaching to a server or running locally
138153
if let Some(ref server_url) = self.attach {
154+
validate_attach_url(server_url)?;
139155
self.run_attached(server_url, &message, &attachments, session_mode)
140156
.await
141157
} else {

src/cortex-cli/tests/run_attach.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use std::process::Command;
2+
3+
#[test]
4+
fn run_attach_rejects_filesystem_paths() {
5+
let output = Command::new(env!("CARGO_BIN_EXE_Cortex"))
6+
.args([
7+
"run",
8+
"--dry-run",
9+
"--attach",
10+
"/nonexistent/file.txt",
11+
"test",
12+
])
13+
.output()
14+
.expect("cortex binary should run");
15+
16+
assert!(
17+
!output.status.success(),
18+
"expected non-zero exit; stdout: {}; stderr: {}",
19+
String::from_utf8_lossy(&output.stdout),
20+
String::from_utf8_lossy(&output.stderr)
21+
);
22+
23+
let stderr = String::from_utf8_lossy(&output.stderr);
24+
assert!(
25+
stderr.contains("--attach expects an HTTP(S) server URL"),
26+
"stderr did not explain the invalid --attach value: {stderr}"
27+
);
28+
}
29+
30+
#[test]
31+
fn run_attach_allows_http_server_urls() {
32+
let output = Command::new(env!("CARGO_BIN_EXE_Cortex"))
33+
.args([
34+
"run",
35+
"--dry-run",
36+
"--attach",
37+
"http://localhost:3000",
38+
"test",
39+
])
40+
.output()
41+
.expect("cortex binary should run");
42+
43+
assert!(
44+
output.status.success(),
45+
"expected zero exit; stdout: {}; stderr: {}",
46+
String::from_utf8_lossy(&output.stdout),
47+
String::from_utf8_lossy(&output.stderr)
48+
);
49+
50+
let stderr = String::from_utf8_lossy(&output.stderr);
51+
assert!(stderr.contains("Server attachment not yet fully implemented"));
52+
}

0 commit comments

Comments
 (0)