Skip to content

Truly independent web browser

LibWeb: Don't sniff a UTF-16 BOM in Body.text()

Shannon Boothd60c8b4ab882
diff --git a/Libraries/LibWeb/Fetch/Body.cpp b/Libraries/LibWeb/Fetch/Body.cpp
index f4ef9fd89fb..1e45a7b25a9 100644
--- a/Libraries/LibWeb/Fetch/Body.cpp
+++ b/Libraries/LibWeb/Fetch/Body.cpp
@@ -217,7 +217,7 @@ void BodyMixin::text(GC::Ref<WebIDL::Promise> promise) const
auto decoder = TextCodec::decoder_for("UTF-8"sv);
VERIFY(decoder.has_value());
- auto utf8_text = MUST(TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, bytes));
+ auto utf8_text = MUST(decoder->to_utf8(bytes, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Replacement));
return JS::PrimitiveString::create(realm.vm(), Utf16String::from_utf8(utf8_text));
}));
}
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 27275f92e97..9f653cfdb5c 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -43,6 +43,7 @@ Text/input/SiteIsolation/popup-opened-onto-a-coop-page-leaves-its-opener.html
Text/input/wpt-import/picture-in-picture/
; fetching a file only works from an HTTP server.
+Text/input/wpt-import/fetch/api/basic/text-utf8.any.html
Crash/HTML/media-element-load-with-pending-source-buffer-append.html
Text/input/HTML/HTMLMediaElement-failed-range-response-after-metadata.html
Crash/HTML/video-track-switch-during-seek.html
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/api/basic/text-utf8.any.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/basic/text-utf8.any.txt
new file mode 100644
index 00000000000..4fc6ca028c8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/api/basic/text-utf8.any.txt
@@ -0,0 +1,35 @@
+Harness status: OK
+
+Found 30 tests
+
+30 Pass
+Pass UTF-8 with BOM with Request.text()
+Pass UTF-8 with BOM with Response.text()
+Pass UTF-8 with BOM with fetched data (UTF-8 charset)
+Pass UTF-8 with BOM with fetched data (UTF-16 charset)
+Pass UTF-8 with BOM (Response object)
+Pass UTF-8 with BOM (Request object)
+Pass UTF-8 without BOM with Request.text()
+Pass UTF-8 without BOM with Response.text()
+Pass UTF-8 without BOM with fetched data (UTF-8 charset)
+Pass UTF-8 without BOM with fetched data (UTF-16 charset)
+Pass UTF-8 without BOM (Response object)
+Pass UTF-8 without BOM (Request object)
+Pass UTF-16BE with BOM decoded as UTF-8 with Request.text()
+Pass UTF-16BE with BOM decoded as UTF-8 with Response.text()
+Pass UTF-16BE with BOM decoded as UTF-8 with fetched data (UTF-8 charset)
+Pass UTF-16BE with BOM decoded as UTF-8 with fetched data (UTF-16 charset)
+Pass UTF-16BE with BOM decoded as UTF-8 (Response object)
+Pass UTF-16BE with BOM decoded as UTF-8 (Request object)
+Pass UTF-16LE with BOM decoded as UTF-8 with Request.text()
+Pass UTF-16LE with BOM decoded as UTF-8 with Response.text()
+Pass UTF-16LE with BOM decoded as UTF-8 with fetched data (UTF-8 charset)
+Pass UTF-16LE with BOM decoded as UTF-8 with fetched data (UTF-16 charset)
+Pass UTF-16LE with BOM decoded as UTF-8 (Response object)
+Pass UTF-16LE with BOM decoded as UTF-8 (Request object)
+Pass UTF-16 without BOM decoded as UTF-8 with Request.text()
+Pass UTF-16 without BOM decoded as UTF-8 with Response.text()
+Pass UTF-16 without BOM decoded as UTF-8 with fetched data (UTF-8 charset)
+Pass UTF-16 without BOM decoded as UTF-8 with fetched data (UTF-16 charset)
+Pass UTF-16 without BOM decoded as UTF-8 (Response object)
+Pass UTF-16 without BOM decoded as UTF-8 (Request object)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/basic/text-utf8.any.html b/Tests/LibWeb/Text/input/wpt-import/fetch/api/basic/text-utf8.any.html
new file mode 100644
index 00000000000..41fc6eb3f48
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/basic/text-utf8.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>Fetch: Request and Response text() should decode as UTF-8</title>
+<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>
+<script src="../resources/utils.js"></script>
+<div id=log></div>
+<script src="../../../fetch/api/basic/text-utf8.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/basic/text-utf8.any.js b/Tests/LibWeb/Text/input/wpt-import/fetch/api/basic/text-utf8.any.js
new file mode 100644
index 00000000000..05c8c88825d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/basic/text-utf8.any.js
@@ -0,0 +1,74 @@
+// META: title=Fetch: Request and Response text() should decode as UTF-8
+// META: global=window,worker
+// META: script=../resources/utils.js
+
+function testTextDecoding(body, expectedText, urlParameter, title)
+{
+ var arrayBuffer = stringToArray(body);
+
+ promise_test(function(test) {
+ var request = new Request("", {method: "POST", body: arrayBuffer});
+ return request.text().then(function(value) {
+ assert_equals(value, expectedText, "Request.text() should decode data as UTF-8");
+ });
+ }, title + " with Request.text()");
+
+ promise_test(function(test) {
+ var response = new Response(arrayBuffer);
+ return response.text().then(function(value) {
+ assert_equals(value, expectedText, "Response.text() should decode data as UTF-8");
+ });
+ }, title + " with Response.text()");
+
+ promise_test(function(test) {
+ return fetch("../resources/status.py?code=200&type=text%2Fplain%3Bcharset%3DUTF-8&content=" + urlParameter).then(function(response) {
+ return response.text().then(function(value) {
+ assert_equals(value, expectedText, "Fetched Response.text() should decode data as UTF-8");
+ });
+ });
+ }, title + " with fetched data (UTF-8 charset)");
+
+ promise_test(function(test) {
+ return fetch("../resources/status.py?code=200&type=text%2Fplain%3Bcharset%3DUTF-16&content=" + urlParameter).then(function(response) {
+ return response.text().then(function(value) {
+ assert_equals(value, expectedText, "Fetched Response.text() should decode data as UTF-8");
+ });
+ });
+ }, title + " with fetched data (UTF-16 charset)");
+
+ promise_test(function(test) {
+ return new Response(body).arrayBuffer().then(function(buffer) {
+ assert_array_equals(new Uint8Array(buffer), encode_utf8(body), "Response.arrayBuffer() should contain data encoded as UTF-8");
+ });
+ }, title + " (Response object)");
+
+ promise_test(function(test) {
+ return new Request("", {method: "POST", body: body}).arrayBuffer().then(function(buffer) {
+ assert_array_equals(new Uint8Array(buffer), encode_utf8(body), "Request.arrayBuffer() should contain data encoded as UTF-8");
+ });
+ }, title + " (Request object)");
+
+}
+
+var utf8WithBOM = "\xef\xbb\xbf\xe4\xb8\x89\xe6\x9d\x91\xe3\x81\x8b\xe3\x81\xaa\xe5\xad\x90";
+var utf8WithBOMAsURLParameter = "%EF%BB%BF%E4%B8%89%E6%9D%91%E3%81%8B%E3%81%AA%E5%AD%90";
+var utf8WithoutBOM = "\xe4\xb8\x89\xe6\x9d\x91\xe3\x81\x8b\xe3\x81\xaa\xe5\xad\x90";
+var utf8WithoutBOMAsURLParameter = "%E4%B8%89%E6%9D%91%E3%81%8B%E3%81%AA%E5%AD%90";
+var utf8Decoded = "三村かな子";
+testTextDecoding(utf8WithBOM, utf8Decoded, utf8WithBOMAsURLParameter, "UTF-8 with BOM");
+testTextDecoding(utf8WithoutBOM, utf8Decoded, utf8WithoutBOMAsURLParameter, "UTF-8 without BOM");
+
+var utf16BEWithBOM = "\xfe\xff\x4e\x09\x67\x51\x30\x4b\x30\x6a\x5b\x50";
+var utf16BEWithBOMAsURLParameter = "%fe%ff%4e%09%67%51%30%4b%30%6a%5b%50";
+var utf16BEWithBOMDecodedAsUTF8 = "��N\tgQ0K0j[P";
+testTextDecoding(utf16BEWithBOM, utf16BEWithBOMDecodedAsUTF8, utf16BEWithBOMAsURLParameter, "UTF-16BE with BOM decoded as UTF-8");
+
+var utf16LEWithBOM = "\xff\xfe\x09\x4e\x51\x67\x4b\x30\x6a\x30\x50\x5b";
+var utf16LEWithBOMAsURLParameter = "%ff%fe%09%4e%51%67%4b%30%6a%30%50%5b";
+var utf16LEWithBOMDecodedAsUTF8 = "��\tNQgK0j0P[";
+testTextDecoding(utf16LEWithBOM, utf16LEWithBOMDecodedAsUTF8, utf16LEWithBOMAsURLParameter, "UTF-16LE with BOM decoded as UTF-8");
+
+var utf16WithoutBOM = "\xe6\x00\xf8\x00\xe5\x00\x0a\x00\xc6\x30\xb9\x30\xc8\x30\x0a\x00";
+var utf16WithoutBOMAsURLParameter = "%E6%00%F8%00%E5%00%0A%00%C6%30%B9%30%C8%30%0A%00";
+var utf16WithoutBOMDecoded = "\ufffd\u0000\ufffd\u0000\ufffd\u0000\u000a\u0000\ufffd\u0030\ufffd\u0030\ufffd\u0030\u000a\u0000";
+testTextDecoding(utf16WithoutBOM, utf16WithoutBOMDecoded, utf16WithoutBOMAsURLParameter, "UTF-16 without BOM decoded as UTF-8");
diff --git a/Tests/LibWeb/Text/input/wpt-import/fetch/api/resources/status.py b/Tests/LibWeb/Text/input/wpt-import/fetch/api/resources/status.py
new file mode 100644
index 00000000000..05a59d5a637
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/fetch/api/resources/status.py
@@ -0,0 +1,11 @@
+from wptserve.utils import isomorphic_encode
+
+def main(request, response):
+ code = int(request.GET.first(b"code", 200))
+ text = request.GET.first(b"text", b"OMG")
+ content = request.GET.first(b"content", b"")
+ type = request.GET.first(b"type", b"")
+ status = (code, text)
+ headers = [(b"Content-Type", type),
+ (b"X-Request-Method", isomorphic_encode(request.method))]
+ return status, headers, content