Lightpanda: the headless browser designed for AI and automation
fetch: reject no-cors requests with a non-safelisted method
Lucien Coffeb0ed19408ba9
diff --git a/src/browser/tests/net/fetch.html b/src/browser/tests/net/fetch.htmlindex 91d779fca..ce9cef066 100644--- a/src/browser/tests/net/fetch.html+++ b/src/browser/tests/net/fetch.html@@ -67,6 +67,23 @@}</script>+<script id=fetch_no_cors_method type=module>+ {+ const state = await testing.async();+ let error;+ try {+ await fetch('http://127.0.0.1:9582/xhr', { mode: 'no-cors', method: 'PUT' });+ } catch (e) {+ error = e.toString();+ }+ state.resolve();++ await state.done(() => {+ testing.expectEqual("TypeError: 'PUT' is unsupported in no-cors mode", error);+ });+ }+</script>+<script id=fetch_redirect type=module>{const state = await testing.async();diff --git a/src/browser/tests/net/request.html b/src/browser/tests/net/request.htmlindex 84d41214b..7411d236d 100644--- a/src/browser/tests/net/request.html+++ b/src/browser/tests/net/request.html@@ -261,6 +261,20 @@testing.expectError('TypeError', () => new Request('https://example.com/api', { mode: 'navigate' }));</script>+<script id=no_cors_method>+ {+ const url = 'https://example.com/api';+ testing.expectError("TypeError: 'PUT' is unsupported in no-cors mode", () => new Request(url, { mode: 'no-cors', method: 'put' }));+ testing.expectEqual('POST', new Request(url, { mode: 'no-cors', method: 'post' }).method);++ // mode and method may come from the input Request.+ const put = new Request(url, { method: 'PUT' });+ testing.expectError('TypeError', () => new Request(put, { mode: 'no-cors' }));+ const no_cors = new Request(url, { mode: 'no-cors' });+ testing.expectError('TypeError', () => new Request(no_cors, { method: 'DELETE' }));+ }+</script>+<script id=body_used type=module>const state = await testing.async();diff --git a/src/browser/webapi/net/Fetch.zig b/src/browser/webapi/net/Fetch.zigindex ed1e3ce4a..48be2ffcd 100644--- a/src/browser/webapi/net/Fetch.zig+++ b/src/browser/webapi/net/Fetch.zig@@ -78,7 +78,10 @@ pub fn init(input: Input, options: ?InitOpts, exec: *const Execution) !js.Promis// A bad RequestInit (e.g. an invalid priority) must reject the promise,// not throw synchronously.const request = Request.init(input, options, exec) catch {- resolver.rejectError("fetch init error", .{ .type_error = "Failed to construct Request" });+ // Request.init may have set a specific message (see Local.typeError).+ const env = exec.js.env;+ defer env.error_message = null;+ resolver.rejectError("fetch init error", .{ .type_error = env.error_message orelse "Failed to construct Request" });return resolver.promise();};diff --git a/src/browser/webapi/net/Request.zig b/src/browser/webapi/net/Request.zigindex 7d983fbb5..d81e1a7da 100644--- a/src/browser/webapi/net/Request.zig+++ b/src/browser/webapi/net/Request.zig@@ -20,6 +20,7 @@ const std = @import("std");const lp = @import("lightpanda");const http = @import("../../../network/http.zig");+const CorsGate = @import("../../../network/CorsGate.zig");const Page = @import("../../Page.zig");const js = @import("../../js/js.zig");@@ -149,6 +150,12 @@ pub fn init(input: Input, opts_: ?InitOpts, exec: *const Execution) !*Request {.url => .cors,.request => |r| if (r._mode == .navigate and opts_ != null) .@"same-origin" else r._mode,};+ // https://fetch.spec.whatwg.org/#dom-request+ if (mode == .@"no-cors" and !CorsGate.isSafelistedMethod(method)) {+ return switch (method) {+ inline else => |m| exec.js.typeError("'" ++ @tagName(m) ++ "' is unsupported in no-cors mode"),+ };+ }const cache: Cache = opts.cache orelse switch (input) {.url => .default,.request => |r| r._cache,