Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(fetch): apply no-cors guards while filling headers
ldm0644ee7b6d24e
diff --git a/moli-renderer-v8/src/network_host/headers/methods/mutation.rs b/moli-renderer-v8/src/network_host/headers/methods/mutation.rsindex 7f323d279..bd8e9a1d7 100644--- a/moli-renderer-v8/src/network_host/headers/methods/mutation.rs+++ b/moli-renderer-v8/src/network_host/headers/methods/mutation.rs@@ -1,7 +1,7 @@use super::super::store::{- HeadersGuard, header_allowed_by_guard, headers_are_immutable, headers_entries, headers_guard,- normalized_header_name_or_throw, normalized_header_value_or_throw, normalized_headers_entries,- set_headers_entries,+ header_allowed_by_guard, header_append_allowed_by_guard, headers_are_immutable,+ headers_entries, headers_guard, normalized_header_name_or_throw,+ normalized_header_value_or_throw, set_headers_entries,};use super::*;use crate::webidl;@@ -33,20 +33,6 @@ fn reject_immutable_headers<'s>(true}-fn candidate_allowed_by_guard(- guard: HeadersGuard,- target_name: &str,- candidate_entries: &[(String, String)],-) -> bool {- if guard != HeadersGuard::RequestNoCors {- return true;- }- normalized_headers_entries(candidate_entries)- .into_iter()- .filter(|(name, _)| name == target_name)- .all(|(name, value)| header_allowed_by_guard(guard, &name, &value))-}-pub(in crate::network_host::headers) fn headers_set_callback<'s>(scope: &mut v8::PinScope<'s, '_>,args: v8::FunctionCallbackArguments<'s>,@@ -79,11 +65,7 @@ pub(in crate::network_host::headers) fn headers_set_callback<'s>(.position(|(entry_name, _)| *entry_name == lower).unwrap_or(entries.len());entries.retain(|(entry_name, _)| *entry_name != lower);- let target_name = lower.clone();entries.insert(insert_at.min(entries.len()), (lower, value));- if !candidate_allowed_by_guard(guard, &target_name, &entries) {- return;- }set_headers_entries(scope, this, &entries);}@@ -133,13 +115,10 @@ pub(in crate::network_host::headers) fn headers_append_callback<'s>(return;};let guard = headers_guard(scope, this);- if !header_allowed_by_guard(guard, &name, &value) {- return;- }let mut entries = headers_entries(scope, this);- entries.push((name.clone(), value));- if !candidate_allowed_by_guard(guard, &name, &entries) {+ if !header_append_allowed_by_guard(guard, &name, &value, &entries) {return;}+ entries.push((name, value));set_headers_entries(scope, this, &entries);}diff --git a/moli-renderer-v8/src/network_host/headers/store.rs b/moli-renderer-v8/src/network_host/headers/store.rsindex 947c644d1..f6b2004b2 100644--- a/moli-renderer-v8/src/network_host/headers/store.rs+++ b/moli-renderer-v8/src/network_host/headers/store.rs@@ -4,7 +4,7 @@ mod projection;pub(crate) use self::entries::{HeadersGuard, filter_headers_for_guard, headers_entries};pub(in crate::network_host::headers) use self::entries::{- header_allowed_by_guard, headers_are_immutable, headers_guard,+ header_allowed_by_guard, header_append_allowed_by_guard, headers_are_immutable, headers_guard,};pub(in crate::network_host) use self::entries::{mark_headers_immutable, normalized_headers_entries, set_headers_entries,diff --git a/moli-renderer-v8/src/network_host/headers/store/entries.rs b/moli-renderer-v8/src/network_host/headers/store/entries.rsindex c6799ce49..897e49259 100644--- a/moli-renderer-v8/src/network_host/headers/store/entries.rs+++ b/moli-renderer-v8/src/network_host/headers/store/entries.rs@@ -88,11 +88,47 @@ pub(crate) fn filter_headers_for_guard(entries: &[(String, String)],guard: HeadersGuard,) -> Vec<(String, String)> {- entries+ let mut filtered = Vec::with_capacity(entries.len());+ for (name, value) in entries {+ if header_append_allowed_by_guard(guard, name, value, &filtered) {+ filtered.push((name.clone(), value.clone()));+ }+ }+ filtered+}++pub(in crate::network_host::headers) fn header_append_allowed_by_guard(+ guard: HeadersGuard,+ name: &str,+ value: &str,+ entries: &[(String, String)],+) -> bool {+ if guard != HeadersGuard::RequestNoCors {+ return header_allowed_by_guard(guard, name, value);+ }+ // Filling Headers runs append for each entry. The no-cors check applies+ // to the resulting combined value, including separators and empty values;+ // a rejected append must leave previously accepted entries intact.+ let mut combined = None::<String>;+ for (_, existing) in entries.iter()- .filter(|(name, value)| header_allowed_by_guard(guard, name, value))- .cloned()- .collect()+ .filter(|(field, _)| field.eq_ignore_ascii_case(name))+ {+ if let Some(combined) = &mut combined {+ combined.push_str(", ");+ combined.push_str(existing);+ } else {+ combined = Some(existing.clone());+ }+ }+ match combined {+ Some(mut combined) => {+ combined.push_str(", ");+ combined.push_str(value);+ header_allowed_by_guard(guard, name, &combined)+ }+ None => header_allowed_by_guard(guard, name, value),+ }}pub(in crate::network_host) fn set_headers_entries(diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rsindex 12f88ba21..c7ee746a4 100644--- a/moli-renderer-v8/src/script_vm/tests/mod.rs+++ b/moli-renderer-v8/src/script_vm/tests/mod.rs@@ -15213,6 +15213,7 @@ mod indexed_db;mod inspector_unwrap;mod lazy_storage;mod lazy_window_surfaces;+mod no_cors_header_fill;mod observer_callbacks;mod post_parse;mod queue_microtask;diff --git a/moli-renderer-v8/src/script_vm/tests/no_cors_header_fill.rs b/moli-renderer-v8/src/script_vm/tests/no_cors_header_fill.rsnew file mode 100644index 000000000..e734925f1--- /dev/null+++ b/moli-renderer-v8/src/script_vm/tests/no_cors_header_fill.rs@@ -0,0 +1,55 @@+use super::*;++#[tokio::test(flavor = "current_thread")]+async fn no_cors_header_fill_uses_append_guards_in_window_and_worker() {+ let fixture = include_str!("../../../tests/fixtures/no-cors-header-fill.js");+ for worker in [false, true] {+ let loader = static_http_loader([]);+ let mut vm =+ new_page_task_executor_test_vm_with_loader("https://no-cors-fill.test/", &loader);+ vm.eval("globalThis.headerFillResult = null;").unwrap();+ let script = if worker {+ let source = format!(+ "{fixture}\nnoCorsFillProbe('https://no-cors-fill.test', false).then(postMessage, error => postMessage({{error: String(error.stack || error)}}));"+ );+ format!(+ r#"+ const workerUrl = URL.createObjectURL(new Blob([{}], {{type: 'text/javascript'}}));+ const worker = new Worker(workerUrl);+ const finish = value => {{+ headerFillResult = value;+ worker.terminate();+ URL.revokeObjectURL(workerUrl);+ }};+ worker.onmessage = event => finish(event.data);+ worker.onerror = event => {{ finish({{error: event.message}}); event.preventDefault(); }};+ "#,+ serde_json::to_string(&source).unwrap()+ )+ } else {+ format!(+ "{fixture}\nnoCorsFillProbe('https://no-cors-fill.test', false).then(value => {{ headerFillResult = value; }}, error => {{ headerFillResult = {{error: String(error.stack || error)}}; }});"+ )+ };+ vm.eval(&script).unwrap();+ advance_page_task_executor_until_eval_equals(+ &mut vm,+ &loader,+ "String(headerFillResult !== null)",+ "true",+ "Request header filling should finish",+ )+ .await;+ let result: serde_json::Value =+ serde_json::from_str(&vm.eval("JSON.stringify(headerFillResult)").unwrap()).unwrap();+ let checks = result["checks"]+ .as_array()+ .unwrap_or_else(|| panic!("worker={worker}: {result}"));+ let failures: Vec<_> = checks+ .iter()+ .filter(|check| check["pass"] != true)+ .collect();+ assert_eq!(result["state"], "pass", "worker={worker}: {failures:?}");+ assert_eq!(checks.len(), 208, "worker={worker}");+ }+}diff --git a/moli-renderer-v8/tests/fixtures/no-cors-header-fill.js b/moli-renderer-v8/tests/fixtures/no-cors-header-fill.jsnew file mode 100644index 000000000..4e55577c8--- /dev/null+++ b/moli-renderer-v8/tests/fixtures/no-cors-header-fill.js@@ -0,0 +1,65 @@+async function noCorsFillProbe(base, transport = true) {+ const checks = [];+ const check = (label, actual, wanted) => checks.push({label, actual, wanted, pass: actual === wanted});+ const cases = [];+ for (const name of ['Accept', 'Accept-Language', 'Content-Language']) {+ for (const [label, values, wanted, mergedAllowed] of [+ ['overflow', ['a'.repeat(126), 'b'], 'a'.repeat(126), false],+ ['at-limit', ['a'.repeat(125), 'b'], 'a'.repeat(125) + ', b', true],+ ['after-limit', ['a'.repeat(125), 'b', 'c'], 'a'.repeat(125) + ', b', false],+ ['empty-first', ['', 'b'.repeat(127)], '', false],+ ]) cases.push([name + '/' + label, name, values, wanted, mergedAllowed]);+ }+ const longType = 'text/plain;x=' + 'a'.repeat(114);+ cases.push(+ ['type/duplicate', 'Content-Type', ['text/plain', 'text/plain'], 'text/plain', false],+ ['type/unsafe', 'Content-Type', ['text/plain;charset=utf8', '"'], 'text/plain;charset=utf8', false],+ ['type/invalid-first', 'Content-Type', ['application/json', 'text/plain'], 'text/plain', false],+ ['type/overflow', 'Content-Type', [longType, 'ok'], longType, false],+ );+ for (const [label, name, values, wanted, mergedAllowed] of cases) {+ const names = [name, name.toLowerCase(), name.toUpperCase()];+ const pairs = values.map((value, index) => [names[index], value]);+ const original = JSON.stringify(pairs);+ const url = base + '/echo?case=' + encodeURIComponent(label);+ const request = new Request(url, {mode: 'no-cors', headers: pairs});+ const joined = values.join(', ');+ const mergedWanted = mergedAllowed ? joined : null;+ check(label + '/sequence', request.headers.get(name), wanted);+ check(label + '/record', new Request(url, {mode: 'no-cors', headers: Object.fromEntries(pairs)}).headers.get(name), wanted);+ const appended = new Request(url, {mode: 'no-cors'});+ for (const [key, value] of pairs) appended.headers.append(key, value);+ check(label + '/append', appended.headers.get(name), wanted);+ check(label + '/clone', request.clone().headers.get(name), wanted);+ const inherited = new Request(url, {mode: 'no-cors'});+ check(label + '/inherited-mode', new Request(inherited, {headers: pairs}).headers.get(name), wanted);+ const cors = new Request(url, {headers: pairs});+ check(label + '/override', new Request(cors, {mode: 'no-cors', headers: pairs}).headers.get(name), wanted);+ check(label + '/headers-init', new Request(url, {mode: 'no-cors', headers: new Headers(pairs)}).headers.get(name), mergedWanted);+ check(label + '/cors', cors.headers.get(name), joined);+ check(label + '/response', new Response(null, {headers: pairs}).headers.get(name), joined);+ check(label + '/input-unchanged', JSON.stringify(pairs), original);+ if (transport) {+ for (const [kind, input, init] of [+ ['direct', url, {mode: 'no-cors', headers: pairs}],+ ['captured', request, undefined],+ ['override', cors, {mode: 'no-cors', headers: pairs}],+ ]) {+ let actual;+ try {+ const response = await fetch(input, init);+ actual = new Headers((await response.json()).headers).get(name);+ } catch (error) { actual = String(error); }+ check(label + '/fetch-' + kind, actual, wanted);+ }+ }+ const reset = name === 'Content-Type' ? 'text/plain' : 'reset';+ request.headers.set(name, reset);+ check(label + '/set', request.headers.get(name), reset);+ request.headers.set(name, 'a'.repeat(129));+ check(label + '/set-rejected', request.headers.get(name), reset);+ request.headers.delete(name);+ check(label + '/delete', request.headers.get(name), null);+ }+ return {state: checks.every(check => check.pass) ? 'pass' : 'fail', checks};+}