fix(ai): validate provider endpoints and bound the transport
Accept only http(s) endpoints and refuse URLs that could inject curl config directives. Restrict the transfer to http,https, keep redirects disabled (so a header cannot follow one to another origin) and bound the response with max-filesize. Add endpoint and transport regression tests.
This commit is contained in:
parent
d395fd3464
commit
9b5c1155b0
2 changed files with 120 additions and 8 deletions
|
|
@ -183,6 +183,41 @@ impl SystemFacts {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Maximum accepted provider response size (16 MiB). A larger response is
|
||||||
|
/// treated as hostile and refused rather than buffered.
|
||||||
|
pub const MAX_RESPONSE_BYTES: u64 = 16 * 1024 * 1024;
|
||||||
|
|
||||||
|
/// Validate a provider endpoint before it is handed to the transport.
|
||||||
|
///
|
||||||
|
/// Only `http(s)` is allowed, and the URL may not contain whitespace, control
|
||||||
|
/// characters, quotes or backslashes: the transport embeds it in a curl config
|
||||||
|
/// file, so those characters could otherwise inject curl directives. Local and
|
||||||
|
/// LAN endpoints remain valid (that is the point of the explicit endpoint), but
|
||||||
|
/// unsafe schemes such as `file:`, `gopher:` or `scp:` are refused.
|
||||||
|
pub fn validate_endpoint(url: &str) -> Result<(), AiError> {
|
||||||
|
let trimmed = url.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
return Err(AiError::ProviderUnavailable(
|
||||||
|
"no provider endpoint configured".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let lower = trimmed.to_ascii_lowercase();
|
||||||
|
if !(lower.starts_with("http://") || lower.starts_with("https://")) {
|
||||||
|
return Err(AiError::Provider(format!(
|
||||||
|
"refusing non-http(s) endpoint scheme: {trimmed}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if trimmed
|
||||||
|
.chars()
|
||||||
|
.any(|c| c.is_whitespace() || c.is_control() || c == '"' || c == '\\')
|
||||||
|
{
|
||||||
|
return Err(AiError::Provider(
|
||||||
|
"endpoint contains a character that is unsafe in a transport config".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// The instructions given to the model. They are deliberately explicit about
|
/// The instructions given to the model. They are deliberately explicit about
|
||||||
/// the closed action set and the evidence requirement.
|
/// the closed action set and the evidence requirement.
|
||||||
pub const SYSTEM_PROMPT: &str = "\
|
pub const SYSTEM_PROMPT: &str = "\
|
||||||
|
|
@ -357,11 +392,7 @@ pub fn complete_proposal_at(
|
||||||
"no AI provider selected".into(),
|
"no AI provider selected".into(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
if endpoint.trim().is_empty() {
|
validate_endpoint(endpoint)?;
|
||||||
return Err(AiError::ProviderUnavailable(
|
|
||||||
"no provider endpoint configured".into(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
facts.validate_no_forbidden()?;
|
facts.validate_no_forbidden()?;
|
||||||
let request = build_request(provider, model, facts);
|
let request = build_request(provider, model, facts);
|
||||||
let body = serde_json::to_string(&request)?;
|
let body = serde_json::to_string(&request)?;
|
||||||
|
|
@ -488,6 +519,18 @@ mod tests {
|
||||||
assert!(complete_proposal_at(ProviderKind::Local, "", "", &facts(), &transport).is_err());
|
assert!(complete_proposal_at(ProviderKind::Local, "", "", &facts(), &transport).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn endpoint_validation_rejects_unsafe_schemes_and_injection() {
|
||||||
|
assert!(validate_endpoint("https://api.deepseek.com/chat/completions").is_ok());
|
||||||
|
assert!(validate_endpoint("http://192.168.1.10:11434/v1/chat/completions").is_ok());
|
||||||
|
assert!(validate_endpoint("http://127.0.0.1:8080/v1/chat/completions").is_ok());
|
||||||
|
assert!(validate_endpoint("file:///etc/shadow").is_err());
|
||||||
|
assert!(validate_endpoint("scp://host/path").is_err());
|
||||||
|
assert!(validate_endpoint("https://x/v1\"\nheader = \"A: b").is_err());
|
||||||
|
assert!(validate_endpoint("https://x/v1 with space").is_err());
|
||||||
|
assert!(validate_endpoint("").is_err());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn request_has_no_secret_and_pins_the_schema() {
|
fn request_has_no_secret_and_pins_the_schema() {
|
||||||
let request = build_request(ProviderKind::DeepSeek, "", &facts());
|
let request = build_request(ProviderKind::DeepSeek, "", &facts());
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,9 @@ impl ModelTransport for CurlTransport<'_> {
|
||||||
url: &str,
|
url: &str,
|
||||||
body: &str,
|
body: &str,
|
||||||
) -> std::result::Result<String, AiError> {
|
) -> std::result::Result<String, AiError> {
|
||||||
|
// Fail closed on an unsafe scheme or a URL that could inject curl
|
||||||
|
// config directives before anything is written.
|
||||||
|
phoebus_ai::provider::validate_endpoint(url)?;
|
||||||
if self.sandboxed {
|
if self.sandboxed {
|
||||||
return self.send_sandboxed(url, body);
|
return self.send_sandboxed(url, body);
|
||||||
}
|
}
|
||||||
|
|
@ -96,13 +99,20 @@ impl ModelTransport for CurlTransport<'_> {
|
||||||
Some(key) => format!("header = \"Authorization: Bearer {}\"\n", key.expose()),
|
Some(key) => format!("header = \"Authorization: Bearer {}\"\n", key.expose()),
|
||||||
None => String::new(),
|
None => String::new(),
|
||||||
};
|
};
|
||||||
|
// `proto` restricts the transfer to http(s); `location` is deliberately
|
||||||
|
// absent so a redirect is never followed (a redirect could send the
|
||||||
|
// Authorization header to another origin). `max-filesize` bounds the
|
||||||
|
// response so a hostile or broken endpoint cannot exhaust memory.
|
||||||
let config = format!(
|
let config = format!(
|
||||||
"url = \"{url}\"\n\
|
"url = \"{url}\"\n\
|
||||||
|
proto = \"=http,https\"\n\
|
||||||
header = \"Content-Type: application/json\"\n\
|
header = \"Content-Type: application/json\"\n\
|
||||||
{auth}\
|
{auth}\
|
||||||
data = \"@{path}\"\n\
|
data = \"@{path}\"\n\
|
||||||
silent\nshow-error\nfail-with-body\nmax-time = 120\n",
|
silent\nshow-error\nfail-with-body\nmax-time = 120\n\
|
||||||
path = request.path().display()
|
connect-timeout = 30\nmax-filesize = {max}\n",
|
||||||
|
path = request.path().display(),
|
||||||
|
max = phoebus_ai::provider::MAX_RESPONSE_BYTES,
|
||||||
);
|
);
|
||||||
let spec = CommandSpec::new("ai", "query model", "curl")
|
let spec = CommandSpec::new("ai", "query model", "curl")
|
||||||
.args(["-sS", "-K", "-"])
|
.args(["-sS", "-K", "-"])
|
||||||
|
|
@ -127,10 +137,13 @@ impl CurlTransport<'_> {
|
||||||
let escaped = body.replace('\\', "\\\\").replace('"', "\\\"");
|
let escaped = body.replace('\\', "\\\\").replace('"', "\\\"");
|
||||||
let config = format!(
|
let config = format!(
|
||||||
"url = \"{url}\"\n\
|
"url = \"{url}\"\n\
|
||||||
|
proto = \"=http,https\"\n\
|
||||||
header = \"Content-Type: application/json\"\n\
|
header = \"Content-Type: application/json\"\n\
|
||||||
{auth}\
|
{auth}\
|
||||||
data = \"{escaped}\"\n\
|
data = \"{escaped}\"\n\
|
||||||
silent\nshow-error\nfail-with-body\nmax-time = 120\n"
|
silent\nshow-error\nfail-with-body\nmax-time = 120\n\
|
||||||
|
connect-timeout = 30\nmax-filesize = {max}\n",
|
||||||
|
max = phoebus_ai::provider::MAX_RESPONSE_BYTES,
|
||||||
);
|
);
|
||||||
let out = crate::sandbox::run(
|
let out = crate::sandbox::run(
|
||||||
self.exec,
|
self.exec,
|
||||||
|
|
@ -207,6 +220,62 @@ mod tests {
|
||||||
assert_eq!(exec.redactor().redact("sk-secret-value"), "<redacted>");
|
assert_eq!(exec.redactor().redact("sk-secret-value"), "<redacted>");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn curl_transport_refuses_an_unsafe_endpoint() {
|
||||||
|
let runner = RecordingRunner::new();
|
||||||
|
let exec = make_exec(runner.clone());
|
||||||
|
let transport = CurlTransport::new(&exec, None);
|
||||||
|
for bad in [
|
||||||
|
"file:///etc/passwd",
|
||||||
|
"gopher://127.0.0.1:70/",
|
||||||
|
"http://example.invalid/v1\"; header = \"X: y",
|
||||||
|
"https://example.invalid/v1\nheader = \"X: y\"",
|
||||||
|
] {
|
||||||
|
let err = transport
|
||||||
|
.send(ProviderKind::Local, bad, "{}")
|
||||||
|
.expect_err("unsafe endpoint must be refused");
|
||||||
|
assert!(
|
||||||
|
matches!(err, AiError::Provider(_)),
|
||||||
|
"unexpected error for {bad}: {err:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
runner.recorded().is_empty(),
|
||||||
|
"curl must not run for a refused URL"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn curl_transport_bounds_the_response_and_disables_redirects() {
|
||||||
|
let runner = RecordingRunner::new();
|
||||||
|
runner.set_response(
|
||||||
|
"curl",
|
||||||
|
CommandOutput {
|
||||||
|
status: 0,
|
||||||
|
success: true,
|
||||||
|
stdout: b"{}".to_vec(),
|
||||||
|
stderr: Vec::new(),
|
||||||
|
duration: Duration::from_millis(1),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let exec = make_exec(runner.clone());
|
||||||
|
let transport = CurlTransport::new(&exec, None);
|
||||||
|
transport
|
||||||
|
.send(
|
||||||
|
ProviderKind::Local,
|
||||||
|
"http://127.0.0.1:11434/v1/chat/completions",
|
||||||
|
"{}",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let stdin = String::from_utf8(runner.recorded()[0].stdin.clone().unwrap()).unwrap();
|
||||||
|
assert!(stdin.contains("max-filesize"), "{stdin}");
|
||||||
|
assert!(stdin.contains("proto = \"=http,https\""), "{stdin}");
|
||||||
|
assert!(
|
||||||
|
!stdin.contains("location"),
|
||||||
|
"redirects must stay disabled: {stdin}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn curl_transport_omits_auth_without_a_key() {
|
fn curl_transport_omits_auth_without_a_key() {
|
||||||
let runner = RecordingRunner::new();
|
let runner = RecordingRunner::new();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue