Skip to content

Truly independent web browser

LibWeb: Import WPT coverage for malformed compressed response bodies

Andreas Kling48f301d251a3
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index aaa73387e35..df85ec06301 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -1081,6 +1081,10 @@ Text/input/wpt-import/fetch/api/request/request-init-contenttype.any.html
Text/input/wpt-import/fetch/api/request/request-init-contenttype.any.worker.html
Text/input/wpt-import/fetch/api/response/response-init-contenttype.any.html
Text/input/wpt-import/fetch/api/response/response-init-contenttype.any.worker.html
+; Malformed compressed bodies must be fetched from the HTTP server.
+Text/input/wpt-import/fetch/content-encoding/br/bad-br-body.https.any.html
+Text/input/wpt-import/fetch/content-encoding/gzip/bad-gzip-body.any.html
+Text/input/wpt-import/fetch/content-encoding/zstd/bad-zstd-body.https.any.html
Text/input/wpt-import/fetch/content-encoding/br/big-br-body.https.any.html
Text/input/wpt-import/fetch/content-encoding/br/big-br-body.https.any.sharedworker.html
Text/input/wpt-import/fetch/content-encoding/br/big-br-body.https.any.worker.html
diff --git a/Tests/LibWeb/Text/expected/Fetch/malformed-content-encoding-cookies.txt b/Tests/LibWeb/Text/expected/Fetch/malformed-content-encoding-cookies.txt
new file mode 100644
index 00000000000..3adc7e7c258
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Fetch/malformed-content-encoding-cookies.txt
@@ -0,0 +1,2 @@
+GET fetch: TypeError
+POST fetch: TypeError
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/bad-br-body.https.any.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/bad-br-body.https.any.txt
new file mode 100644
index 00000000000..ea864f79100
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/bad-br-body.https.any.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 6 tests
+
+6 Fail
+Fail Consuming the body of a resource with bad br content with arrayBuffer() should reject
+Fail Consuming the body of a resource with bad br content with blob() should reject
+Fail Consuming the body of a resource with bad br content with bytes() should reject
+Fail Consuming the body of a resource with bad br content with formData() should reject
+Fail Consuming the body of a resource with bad br content with json() should reject
+Fail Consuming the body of a resource with bad br content with text() should reject
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/bad-gzip-body.any.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/bad-gzip-body.any.txt
new file mode 100644
index 00000000000..f1c2448fd14
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/bad-gzip-body.any.txt
@@ -0,0 +1,12 @@
+Harness status: OK
+
+Found 7 tests
+
+7 Fail
+Fail Fetching a resource with bad gzip content should still resolve
+Fail Consuming the body of a resource with bad gzip content with arrayBuffer() should reject
+Fail Consuming the body of a resource with bad gzip content with blob() should reject
+Fail Consuming the body of a resource with bad gzip content with bytes() should reject
+Fail Consuming the body of a resource with bad gzip content with formData() should reject
+Fail Consuming the body of a resource with bad gzip content with json() should reject
+Fail Consuming the body of a resource with bad gzip content with text() should reject
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/bad-zstd-body.https.any.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/bad-zstd-body.https.any.txt
new file mode 100644
index 00000000000..0719ad6aa41
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/bad-zstd-body.https.any.txt
@@ -0,0 +1,12 @@
+Harness status: OK
+
+Found 7 tests
+
+7 Fail
+Fail Fetching a resource with bad zstd content should still resolve
+Fail Consuming the body of a resource with bad zstd content with arrayBuffer() should reject
+Fail Consuming the body of a resource with bad zstd content with blob() should reject
+Fail Consuming the body of a resource with bad zstd content with bytes() should reject
+Fail Consuming the body of a resource with bad zstd content with formData() should reject
+Fail Consuming the body of a resource with bad zstd content with json() should reject
+Fail Consuming the body of a resource with bad zstd content with text() should reject
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/Fetch/malformed-content-encoding-cookies.html b/Tests/LibWeb/Text/input/Fetch/malformed-content-encoding-cookies.html
new file mode 100644
index 00000000000..80d24454f11
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Fetch/malformed-content-encoding-cookies.html
@@ -0,0 +1,48 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ for (const method of ["GET", "POST"]) {
+ const host = uniqueLocalhostHostname(`malformed-content-encoding-${method.toLowerCase()}`);
+ const responseURL = new URL(await httpTestServer().createEcho(method, `/malformed-content-encoding-${method}`, {
+ status: 200,
+ headers: {
+ "Access-Control-Allow-Origin": location.origin,
+ "Access-Control-Allow-Credentials": "true",
+ "Content-Type": "text/plain",
+ "Content-Encoding": "gzip",
+ "Set-Cookie": "malformedbodycookie=stored; Path=/",
+ "Cache-Control": "no-store",
+ },
+ body: "not actually gzip",
+ }));
+ responseURL.hostname = host;
+ try {
+ const response = await fetch(responseURL, { method, credentials: "include" });
+ println(`${method} status: ${response.status}`);
+ try {
+ await response.text();
+ println(`${method} body unexpectedly resolved`);
+ } catch (error) {
+ println(`${method} body: ${error.name}`);
+ }
+ const echoURL = new URL(await httpTestServer().createEcho("GET", `/malformed-content-encoding-cookie-${method}`, {
+ status: 200,
+ headers: {
+ "Access-Control-Allow-Origin": location.origin,
+ "Access-Control-Allow-Credentials": "true",
+ "Content-Type": "application/json",
+ },
+ body: "$HEADERS",
+ reflect_headers_in_body: true,
+ }));
+ echoURL.hostname = host;
+ const headers = await (await fetch(echoURL, { credentials: "include" })).json();
+ const cookie = headers["Cookie"]?.some(value => value.includes("malformedbodycookie=stored"));
+ println(`${method} cookie stored: ${!!cookie}`);
+ } catch (error) {
+ println(`${method} fetch: ${error.name}`);
+ }
+ }
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/br/bad-br-body.https.any.html b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/br/bad-br-body.https.any.html
new file mode 100644
index 00000000000..2e6bfa475df
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/br/bad-br-body.https.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script>
+self.GLOBAL = {
+ isWindow: function() { return true; },
+ isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
+};
+</script>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../../../fetch/content-encoding/br/bad-br-body.https.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/br/bad-br-body.https.any.js b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/br/bad-br-body.https.any.js
new file mode 100644
index 00000000000..af5df674da6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/br/bad-br-body.https.any.js
@@ -0,0 +1,17 @@
+// META: global=window
+
+[
+ "arrayBuffer",
+ "blob",
+ "bytes",
+ "formData",
+ "json",
+ "text"
+].forEach(method => {
+ promise_test(t => {
+ return fetch("resources/bad-br-body.py").then(res => {
+ assert_equals(res.status, 200);
+ return promise_rejects_js(t, TypeError, res[method]());
+ });
+ }, "Consuming the body of a resource with bad br content with " + method + "() should reject");
+});
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/br/resources/bad-br-body.py b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/br/resources/bad-br-body.py
new file mode 100644
index 00000000000..0710e7ffdee
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/br/resources/bad-br-body.py
@@ -0,0 +1,3 @@
+def main(request, response):
+ headers = [(b"Content-Encoding", b"br")]
+ return headers, b"not actually br"
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/gzip/bad-gzip-body.any.html b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/gzip/bad-gzip-body.any.html
new file mode 100644
index 00000000000..af06823facf
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/gzip/bad-gzip-body.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script>
+self.GLOBAL = {
+ isWindow: function() { return true; },
+ isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
+};
+</script>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../../../fetch/content-encoding/gzip/bad-gzip-body.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/gzip/bad-gzip-body.any.js b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/gzip/bad-gzip-body.any.js
new file mode 100644
index 00000000000..77a183f408b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/gzip/bad-gzip-body.any.js
@@ -0,0 +1,23 @@
+// META: global=window,worker
+
+promise_test((test) => {
+ return fetch("resources/bad-gzip-body.py").then(res => {
+ assert_equals(res.status, 200);
+ });
+}, "Fetching a resource with bad gzip content should still resolve");
+
+[
+ "arrayBuffer",
+ "blob",
+ "bytes",
+ "formData",
+ "json",
+ "text"
+].forEach(method => {
+ promise_test(t => {
+ return fetch("resources/bad-gzip-body.py").then(res => {
+ assert_equals(res.status, 200);
+ return promise_rejects_js(t, TypeError, res[method]());
+ });
+ }, "Consuming the body of a resource with bad gzip content with " + method + "() should reject");
+});
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/gzip/resources/bad-gzip-body.py b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/gzip/resources/bad-gzip-body.py
new file mode 100644
index 00000000000..a79b94ed041
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/gzip/resources/bad-gzip-body.py
@@ -0,0 +1,3 @@
+def main(request, response):
+ headers = [(b"Content-Encoding", b"gzip")]
+ return headers, b"not actually gzip"
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/zstd/bad-zstd-body.https.any.html b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/zstd/bad-zstd-body.https.any.html
new file mode 100644
index 00000000000..572f58de96b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/zstd/bad-zstd-body.https.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script>
+self.GLOBAL = {
+ isWindow: function() { return true; },
+ isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
+};
+</script>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../../../fetch/content-encoding/zstd/bad-zstd-body.https.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/zstd/bad-zstd-body.https.any.js b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/zstd/bad-zstd-body.https.any.js
new file mode 100644
index 00000000000..c59980c2c6f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/zstd/bad-zstd-body.https.any.js
@@ -0,0 +1,23 @@
+// META: global=window,worker
+
+promise_test((test) => {
+ return fetch("resources/bad-zstd-body.py").then(res => {
+ assert_equals(res.status, 200);
+ });
+}, "Fetching a resource with bad zstd content should still resolve");
+
+[
+ "arrayBuffer",
+ "blob",
+ "bytes",
+ "formData",
+ "json",
+ "text"
+].forEach(method => {
+ promise_test(t => {
+ return fetch("resources/bad-zstd-body.py").then(res => {
+ assert_equals(res.status, 200);
+ return promise_rejects_js(t, TypeError, res[method]());
+ });
+ }, "Consuming the body of a resource with bad zstd content with " + method + "() should reject");
+});
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/zstd/resources/bad-zstd-body.py b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/zstd/resources/bad-zstd-body.py
new file mode 100644
index 00000000000..496f26881dc
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/content-encoding/zstd/resources/bad-zstd-body.py
@@ -0,0 +1,3 @@
+def main(request, response):
+ headers = [(b"Content-Encoding", b"zstd")]
+ return headers, b"not actually zstd"