Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions crates/bashkit/src/builtins/curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,24 @@ impl Builtin for Curl {
}

/// Sanitize a multipart field name or filename to prevent header injection.
/// Rejects CR/LF characters (which could inject headers) and escapes double quotes.
/// Rejects CR/LF characters (which could inject headers) and escapes quoted-string
/// metacharacters so names cannot terminate `Content-Disposition` parameters.
fn sanitize_multipart_name(value: &str, label: &str) -> std::result::Result<String, String> {
if value.contains('\r') || value.contains('\n') {
return Err(format!(
"curl: multipart {} contains illegal newline characters\n",
label
));
}
Ok(value.replace('"', "\\\""))

let mut escaped = String::with_capacity(value.len());
for ch in value.chars() {
if ch == '\\' || ch == '"' {
escaped.push('\\');
}
escaped.push(ch);
}
Ok(escaped)
}

/// Execute the actual curl request when http_client feature is enabled.
Expand Down Expand Up @@ -1323,6 +1332,12 @@ mod tests {
assert!(result.stderr.contains("network access not configured"));
}

#[test]
fn test_sanitize_multipart_name_escapes_backslash_before_quote() {
let result = sanitize_multipart_name("foo\\\"; filename=evil", "field name").unwrap();
assert_eq!(result, "foo\\\\\\\"; filename=evil");
}

#[cfg(feature = "http_client")]
mod network_tests {
use super::*;
Expand Down
Loading