Skip to content
Open
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
44 changes: 36 additions & 8 deletions crates/goose/src/providers/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,14 +826,32 @@ impl Provider for OpenAiProvider {
}

fn parse_custom_headers(s: String) -> HashMap<String, String> {
s.split(',')
.filter_map(|header| {
let mut parts = header.splitn(2, '=');
let key = parts.next().map(|s| s.trim().to_string())?;
let value = parts.next().map(|s| s.trim().to_string())?;
Some((key, value))
})
.collect()
let mut headers = HashMap::new();
let mut rest = s.as_str();
while !rest.is_empty() {
let Some((key, after_eq)) = rest.split_once('=') else {
break;
};
let key = key.trim();
let (value, remainder) = if let Some(after_quote) = after_eq.strip_prefix('"') {
match after_quote.split_once('"') {
Some((value, after_close)) => {
Comment on lines +837 to +838
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Parse escaped quotes inside quoted header values

The quoted-value branch terminates at the first " character via split_once('"'), even when that quote is escaped inside the value. For inputs like x-meta="{\"a\":1,\"b\":2}",x-id=1, this truncates x-meta to {\ and turns the remaining JSON into the next header key, which can corrupt headers or fail provider initialization when HeaderName::from_bytes runs. This breaks valid quoted header payloads (notably JSON metadata) that contain commas and embedded quotes.

Useful? React with 👍 / 👎.

(value, after_close.strip_prefix(',').unwrap_or(after_close))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Allow whitespace before comma after quoted values

After closing a quoted value, the parser only strips a comma if it is the very next character. A header string such as x-tags="a,b" ,x-other=1 leaves ",x-other" attached to the next key path, producing a key like ,x-other and causing invalid-header-name errors later. This makes otherwise human-formatted inputs fail as soon as a quoted value is followed by space+comma instead of a bare comma.

Useful? React with 👍 / 👎.

}
None => (after_quote, ""),
}
} else {
match after_eq.split_once(',') {
Some((value, after_comma)) => (value, after_comma),
None => (after_eq, ""),
}
};
if !key.is_empty() {
headers.insert(key.to_string(), value.trim().to_string());
}
rest = remainder;
}
headers
}

#[async_trait]
Expand Down Expand Up @@ -1211,6 +1229,16 @@ mod tests {
assert_eq!(models, vec!["m1".to_string(), "m2".to_string()]);
}

#[test]
fn parse_custom_headers_with_commas_in_quoted_values() {
let headers = parse_custom_headers(
r#"Authorization=Bearer token,x-tags="a,b,c",x-path=C:\temp"#.to_string(),
);
assert_eq!(headers.get("Authorization").unwrap(), "Bearer token");
assert_eq!(headers.get("x-tags").unwrap(), "a,b,c");
assert_eq!(headers.get("x-path").unwrap(), r"C:\temp");
}

#[test]
fn from_custom_config_rejects_static_only_without_models() {
let config = base_declarative_config(vec![], Some(false));
Expand Down
Loading