Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(cors): parse Content-Type parameters without rejecting commas

ldm004302d49fb94
diff --git a/moli-fetch/src/headers.rs b/moli-fetch/src/headers.rs
index 285d528fb..5f1c3827d 100644
--- a/moli-fetch/src/headers.rs
+++ b/moli-fetch/src/headers.rs
@@ -254,10 +254,18 @@ mod tests {
"content-type",
"text/html"
));
- assert!(!is_no_cors_safelisted_request_header(
+ assert!(is_no_cors_safelisted_request_header(
"content-type",
"text/plain;charset=UTF-8, text/plain"
));
+ for value in [
+ "text/plain, text/plain",
+ "application/json, text/plain",
+ "text/plain, application/json",
+ "text/plain;charset=\"utf8\", extra",
+ ] {
+ assert!(!is_no_cors_safelisted_request_header("content-type", value));
+ }
assert!(!is_no_cors_safelisted_request_header("range", "bytes=0-1"));
}
diff --git a/moli-web-mime/src/parse.rs b/moli-web-mime/src/parse.rs
index 5def66406..447e5b0a5 100644
--- a/moli-web-mime/src/parse.rs
+++ b/moli-web-mime/src/parse.rs
@@ -9,8 +9,8 @@ pub fn parse_mime(input: &str) -> Option<mime::Mime> {
}
pub fn request_header_content_type_essence(input: &str) -> Option<String> {
- if input.contains(',') {
- return None;
- }
+ // Fetch parses one MIME type here, rather than extracting a type from a
+ // comma-separated response header. Commas invalidate the type/subtype,
+ // but are allowed in parameters.
mime_essence(input)
}
diff --git a/moli-web-mime/src/tests.rs b/moli-web-mime/src/tests.rs
index db5977c71..e475520a9 100644
--- a/moli-web-mime/src/tests.rs
+++ b/moli-web-mime/src/tests.rs
@@ -63,6 +63,22 @@ fn extracts_request_header_content_type_essence_for_fetch_rules() {
request_header_content_type_essence("text/plain, text/plain"),
None
);
+ for input in [
+ "application/json, text/plain",
+ "text/plain, application/json",
+ ] {
+ assert_eq!(request_header_content_type_essence(input), None, "{input}");
+ }
+ for input in [
+ "text/plain;charset=utf8, extra",
+ "text/plain;charset=utf8, application/json",
+ ] {
+ assert_eq!(
+ request_header_content_type_essence(input).as_deref(),
+ Some("text/plain"),
+ "{input}"
+ );
+ }
assert_eq!(request_header_content_type_essence("text"), None);
assert_eq!(request_header_content_type_essence("text/"), None);
assert_eq!(request_header_content_type_essence("te xt/plain"), None);