Skip to content

Truly independent web browser

LibWeb: Replace lone surrogates in serialized XHR Document bodies

Shannon Boothf18aa22af0bb
diff --git a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
index 496a1a00a71..c1351be9b22 100644
--- a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
+++ b/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
@@ -706,7 +706,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(NullableDocumentOrXMLHttpRequestB
// 2. If body is a Document, then set this’s request body to body, serialized, converted, and UTF-8 encoded.
if (body.has<GC::Ref<DOM::Document>>()) {
auto string_serialized_document = TRY(body.get<GC::Ref<DOM::Document>>()->serialize_fragment(HTML::RequireWellFormed::No));
- auto string_serialized_document_utf8 = string_serialized_document.to_utf8();
+ auto string_serialized_document_utf8 = string_serialized_document.to_well_formed_utf8();
m_request_body = Fetch::Infrastructure::byte_sequence_as_body(associated_realm, string_serialized_document_utf8.bytes());
}
// 3. Otherwise:
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 474f32c93a0..cdc41220401 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -61,6 +61,7 @@ Text/input/wpt-import/video-rvfc/
; fetching a file only works from an HTTP server.
Text/input/wpt-import/fetch/api/basic/text-utf8.any.html
Text/input/wpt-import/fetch/api/abort/general.any.html
+Text/input/wpt-import/xhr/send-entity-body-document-bogus.htm
Crash/HTML/media-element-load-with-pending-source-buffer-append.html
Text/input/HTML/HTMLMediaElement-bodyless-response.html
Text/input/HTML/HTMLMediaElement-failed-range-response-after-metadata.html
diff --git a/Tests/LibWeb/Text/expected/wpt-import/xhr/send-entity-body-document-bogus.txt b/Tests/LibWeb/Text/expected/wpt-import/xhr/send-entity-body-document-bogus.txt
new file mode 100644
index 00000000000..2ae048cc157
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/xhr/send-entity-body-document-bogus.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Pass
+Pass Serializing documents through XMLHttpRequest: ''
+Pass Serializing documents through XMLHttpRequest: '<test:test/>'
+Pass Serializing documents through XMLHttpRequest: '<test:test test:test="gee"/>'
+Pass Serializing documents through XMLHttpRequest: '<test:test test:test="gee" x="�"/>'
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/xhr/resources/content.py b/Tests/LibWeb/Text/input/wpt-import/xhr/resources/content.py
new file mode 100644
index 00000000000..764ca367bbc
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/xhr/resources/content.py
@@ -0,0 +1,20 @@
+from wptserve.utils import isomorphic_encode
+
+def main(request, response):
+ response_ctype = b''
+
+ if b"response_charset_label" in request.GET:
+ response_ctype = b";charset=" + request.GET.first(b"response_charset_label")
+
+ headers = [(b"Content-type", b"text/plain" + response_ctype),
+ (b"X-Request-Method", isomorphic_encode(request.method)),
+ (b"X-Request-Query", isomorphic_encode(request.url_parts.query) if request.url_parts.query else b"NO"),
+ (b"X-Request-Content-Length", request.headers.get(b"Content-Length", b"NO")),
+ (b"X-Request-Content-Type", request.headers.get(b"Content-Type", b"NO"))]
+
+ if b"content" in request.GET:
+ content = request.GET.first(b"content")
+ else:
+ content = request.body
+
+ return headers, content
diff --git a/Tests/LibWeb/Text/input/wpt-import/xhr/send-entity-body-document-bogus.htm b/Tests/LibWeb/Text/input/wpt-import/xhr/send-entity-body-document-bogus.htm
new file mode 100644
index 00000000000..1693f6441e3
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/xhr/send-entity-body-document-bogus.htm
@@ -0,0 +1,26 @@
+<!doctype html>
+<title>XMLHttpRequest: send() - Document with serialization errors</title>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<div id="log"></div>
+<script>
+function serialize(input, output) {
+ async_test(t => {
+ const client = new XMLHttpRequest
+ client.open("POST", "resources/content.py")
+ client.send(input)
+ client.onload = t.step_func_done(() => {
+ assert_equals(client.responseText, output)
+ })
+ }, "Serializing documents through XMLHttpRequest: '" + output + "'")
+}
+
+var doc = document.implementation.createDocument(null, null, null)
+serialize(doc, "")
+doc.appendChild(doc.createElement("test:test"))
+serialize(doc, "<test:test/>")
+doc.childNodes[0].setAttribute("test:test", "gee")
+serialize(doc, "<test:test test:test=\"gee\"/>")
+doc.childNodes[0].setAttribute("x", "\uD800")
+serialize(doc, "<test:test test:test=\"gee\" x=\"\uFFFD\"/>")
+</script>