Skip to content

Lightpanda: the headless browser designed for AI and automation

fetch: reject a body on GET and HEAD requests

Jay Hemnaniccdb8d47247b
diff --git a/src/browser/tests/net/request.html b/src/browser/tests/net/request.html
index 7411d236d..1b1f454de 100644
--- a/src/browser/tests/net/request.html
+++ b/src/browser/tests/net/request.html
@@ -647,6 +647,18 @@
}
</script>
+<script id=get_head_body>
+ {
+ const url = 'https://example.com/api';
+ testing.expectError('TypeError', () => new Request(url, { body: 'x' }));
+ testing.expectError('TypeError', () => new Request(url, { method: 'HEAD', body: '' }));
+ testing.expectEqual('GET', new Request(url, { body: null }).method);
+
+ const post = new Request(url, { method: 'POST', body: 'x' });
+ testing.expectError('TypeError', () => new Request(post, { method: 'GET' }));
+ }
+</script>
+
<script id=request_body_matches_stream>
const open = new ReadableStream({ start(c) { c.enqueue(new TextEncoder().encode('x')); } });
const r1 = new Request('https://example.com', { method: 'POST', body: open, duplex: 'half' });
diff --git a/src/browser/webapi/net/Request.zig b/src/browser/webapi/net/Request.zig
index d81e1a7da..43928a303 100644
--- a/src/browser/webapi/net/Request.zig
+++ b/src/browser/webapi/net/Request.zig
@@ -205,6 +205,10 @@ pub fn init(input: Input, opts_: ?InitOpts, exec: *const Execution) !*Request {
},
};
+ if ((body != null or body_stream != null) and (method == .GET or method == .HEAD)) {
+ return exec.js.typeError("Request with GET/HEAD method cannot have body");
+ }
+
const signal = if (opts.signal) |s|
s
else switch (input) {