Skip to content

The headless browser for AI agents and web scraping

fix(obscura-js): normalize the fetch() request method to uppercase (#969)

Andrey Mnatsakanov2e752b9e5336
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 9aff7260..f5add319 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -7207,7 +7207,11 @@ globalThis.fetch = async (input, init = {}) => {
// whether the input is absolute. _resolveUrl leaves absolute URLs
// unchanged and keeps unparseable input as-is.
url = _resolveUrl(url);
- const method = init.method || (request ? request.method : "GET");
+ // Normalize the method to uppercase, matching the Request constructor, so
+ // fetch(url, {method:"delete"}) and new Request(url, {method:"delete"}) agree
+ // and lowercase standard methods are not rejected by the case-sensitive CORS
+ // checks (#969).
+ const method = String(init.method || (request ? request.method : "GET")).toUpperCase();
const headers = init.headers !== undefined ? init.headers : (request ? request.headers : undefined);
let _h = headers instanceof Headers ? Object.fromEntries(headers.entries()) : (headers || {});
const inheritsRequestBody = init.body === undefined && request !== null;
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index d342f952..1f26dc7a 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -16824,6 +16824,43 @@ mod tests {
);
}
+ // #969: fetch() must normalize the request method to uppercase, matching the
+ // Request constructor, so a lowercase standard method is not wrongly
+ // rejected by the case-sensitive CORS checks.
+ #[tokio::test(flavor = "current_thread")]
+ async fn fetch_normalizes_request_method_to_uppercase() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt
+ .call_function_on_for_cdp(
+ r#"async () => {
+ const original = Deno.core.ops.op_fetch_url;
+ let captured = null;
+ try {
+ Deno.core.ops.op_fetch_url = (url, method) => {
+ captured = method;
+ return JSON.stringify({ status: 200, headers: {}, body: "ok", url });
+ };
+ await fetch(new URL("/api", document.URL), { method: "delete" });
+ return captured;
+ } finally {
+ Deno.core.ops.op_fetch_url = original;
+ }
+ }"#,
+ None,
+ &[],
+ true,
+ true,
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(
+ result.value.unwrap(),
+ serde_json::json!("DELETE"),
+ "fetch() must uppercase the method like the Request constructor"
+ );
+ }
+
#[tokio::test(flavor = "current_thread")]
async fn fetch_and_xhr_forward_browser_credentials_modes() {
let mut rt = setup_runtime("<html><body></body></html>");