-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix: parse OPENAI_CUSTOM_HEADERS with commas inside quoted values #9306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)) => { | ||
| (value, after_close.strip_prefix(',').unwrap_or(after_close)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
After closing a quoted value, the parser only strips a comma if it is the very next character. A header string such as 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] | ||
|
|
@@ -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)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The quoted-value branch terminates at the first
"character viasplit_once('"'), even when that quote is escaped inside the value. For inputs likex-meta="{\"a\":1,\"b\":2}",x-id=1, this truncatesx-metato{\and turns the remaining JSON into the next header key, which can corrupt headers or fail provider initialization whenHeaderName::from_bytesruns. This breaks valid quoted header payloads (notably JSON metadata) that contain commas and embedded quotes.Useful? React with 👍 / 👎.