Skip to content

fix: send the Blob and FormData bodies of synchronous relayed XMLHttpRequests - #852

Merged
jkmassel merged 4 commits into
trunkfrom
jkmassel/relay-xhr-sync-bodies
Nov 2, 2026
Merged

jkmassel merged 4 commits into
trunkfrom
jkmassel/relay-xhr-sync-bodies

Conversation

@jkmassel

@jkmassel jkmassel commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

What?

Fixes a bug where a synchronous XMLHttpRequest with a Blob, File or FormData body reached the site with an empty body through the REST relay, and still reported success.

Why?

The relay can only send bytes. WebKit drops a Blob body on the way to a URL scheme handler, so the relay reads every such body into an ArrayBuffer first. That read is asynchronous, and a synchronous send() can't wait for it, so #849 sent the body as it was.

Without Lockdown Mode, this request worked before #849. Measured in the iOS 27.0 Simulator:

Synchronous POST Direct (before #849) Relayed (#849)
Blob 200, server got 10 bytes 200, server got 0 bytes
FormData with a file 200, server got 293 bytes 200, server got 0 bytes

Why WebKit drops the body

Confirmed in WebKit's source (main at 26d81a1):

  1. The page stores a Blob body as a reference to the Blob, not as bytes (XMLHttpRequest.cpp:519). A non-empty File in FormData is stored the same way (FormData.cpp:246).
  2. Only the networking process turns that reference into bytes. A scheme handler's request skips it: the page sends it straight to the app (WebURLSchemeTaskProxy.cpp:84).
  3. In the app, a body holding any Blob reference gets no stream at all (FormDataStreamCFNet.mm:486). That's why a FormData with one file loses its text fields too.

How?

src/utils/xhr-relay.js reads the body synchronously before sending it, for synchronous requests only. Asynchronous requests are unchanged.

The page has no synchronous way to read a Blob: FileReaderSync exists only in workers, and Atomics.wait isn't allowed on a page's main thread. A synchronous XMLHttpRequest for a blob: URL of the body does read it, with WebKit doing the work while the page waits:

const url = URL.createObjectURL( new Blob( [ '-', blob ] ) );
reader.open( 'GET', url, false );
reader.overrideMimeType( 'text/plain; charset=x-user-defined' );
reader.send();

x-user-defined decodes each byte to one character (bytes from 128 up become U+F780–U+F7FF), so the character codes modulo 256 are the original bytes. The relay sends those as an ArrayBuffer, which WebKit delivers. The extra - is there because decoding removes a byte order mark from the start of the text, so a body starting with one would lose it.

FormData has no synchronous serializer either, so the relay builds the multipart/form-data body as a Blob of text parts and the files themselves, then reads that. It follows the HTML standard's encoding: line breaks in names and values become CRLF, and CR, LF and " in names are escaped. The Content-Type is the relay's boundary, as bufferedBody does for asynchronous requests.

A body that can't be read whole fails the request, and nothing is sent. The relay re-opens the request for an undecodable data: URL, so WebKit itself fires readystatechange, error and loadend and throws NetworkError. The read's length is checked too: a file that shrank after the page got it reads as a short success, which would otherwise upload a cut-off body.

Cost

The read holds several times the body in the page for a moment. A 250 MB Blob peaked at about +1.7 GB in a macOS web view, about 7 times the body, past the roughly 1.5 GB that was killed in #747. The system ending the web view can't be caught, so a body over 100 MB now fails with NetworkError without being read. Synchronous requests freeze the page and are deprecated, so nothing that large is expected. Asynchronous relayed bodies are already held whole.

Testing Instructions

  • Unit tests in src/utils/xhr-relay.test.js: a binary Blob with its type, the caller's Content-Type, the exact multipart output, and an unreadable body and a partial read. Full suite 632/632.
  • XMLHttpRequestRelayLifecycleTests in real WebKit (runs in CI): new synchronousPostBlob, synchronousPostEmptyBlob, synchronousPostFormData, three byte order mark, synchronousPostBlobFromIframe, synchronousReadFails and reopenFromFailedSynchronousRead scenarios compare a direct request with a relayed one. /echo now returns a hash of the body, with any multipart boundary replaced. Breaking the byte decoding fails the Blob and FormData scenarios, and breaking the line break handling fails FormData, so neither is a false pass. The byte order mark and failed-read scenarios fail on the earlier code.
  • Simulator, iOS 27.0, Lockdown Mode off and forced on: synchronous relayed POSTs to a local server that hashes what it receives and parses multipart bodies (table below).
  • Simulator, real unreadable files: a file-backed File from the page's private file system, deleted or cut short after the page got it, alone and in FormData (table below).
  • iPhone 15 Pro, iOS 27.2, real Lockdown Mode: the lifecycle scenarios plus a File, a caller-set Content-Type, 1 MB of random bytes and a 20 MB Blob, on a file:// page through the real RestRelay (table below). Every body's hash matches one computed separately on the Mac. With the fix taken out, the same run delivers 0 bytes for each non-empty body.
Synchronous body, real Lockdown Mode Direct Relayed Relayed without this fix
Blob, every byte value, 256 B 256 B 256 B 0 B
File, 5 B 5 B 5 B 0 B
Blob with a caller-set Content-Type, 9 B 9 B 9 B 0 B
FormData with files, 474 B 474 B 474 B 0 B
Blob, 20 MB 20 MB 20 MB, about 1.9 s longer 0 B

Direct and relayed bodies have the same hash in every row with the fix in place. The 32 other scenarios, including byte order marks and a failed read, behave the same both ways, except an untyped Blob, which is sent as application/octet-stream (#857). This test server answers with Access-Control-Allow-Origin: *, so direct requests work under Lockdown here, unlike in the Simulator results below.

Synchronous body, file changed after the page got it (Simulator, Lockdown off) Direct Relayed
File, deleted TimeoutError, no events NetworkError, error and loadend
File, cut short NetworkError, error and loadend Same
FormData with the file, deleted or cut short 200, server gets 0 bytes NetworkError, error and loadend

The relay fails where WebKit sends an empty FormData, because reporting a failure is better than losing the body without saying so. The page's private file system doesn't exist under Lockdown, so real unreadable files weren't tested there. On the phone, a read was made to fail with a blob: URL that doesn't exist.

Simulator results (before #857)

The server hashed each body and parsed multipart bodies. "Direct" is the browser's own XMLHttpRequest to the site.

Body Lockdown off: direct / relayed Lockdown on: direct / relayed
text Blob, 10 B same hash both ways NetworkError / same hash
every byte value 0–255 same hash both ways NetworkError / same hash
FormData: non-ASCII text with a line break, a file with a non-ASCII and quoted name, an unnamed Blob identical parsed entries, both 721 B NetworkError / identical parsed entries
20 MB Blob same hash; 38 ms / 81 ms NetworkError / same hash, 1,071 ms
  • Size limit, iPhone 15 Pro, iOS 27.2, real Lockdown Mode, relayed: a body of 100 MB + 1 byte fails with NetworkError in 0.6 s, firing readystatechange, error and loadend. A body of exactly 100 MB takes 27 s and reaches the server whole, status 200, with the expected hash.
  • After the size limit commit: JS unit tests, and XMLHttpRequestRelayLifecycleTests in the iOS 27.0 Simulator.
  • Whole scenario list on the phone at f41931785: 38 scenarios match direct and relayed, apart from postUntypedBlob (fix(ios): send an untyped relayed POST body as application/octet-stream #857). 100 MB of 0xC8, the worst case for the read's memory, gets status 200 and the same hash relayed and direct, in 26.0 s and 15.5 s including building the body.

Related

@jkmassel jkmassel added [Type] Bug An existing feature does not function as intended Webviews iOS labels Oct 29, 2026
@jkmassel jkmassel self-assigned this Oct 29, 2026
@jkmassel jkmassel added this to the 0.20.0 milestone Oct 29, 2026
@jkmassel
jkmassel force-pushed the jkmassel/relay-xhr-sync-bodies branch from 68a0a6c to 440f617 Compare October 29, 2026 23:25
@wpmobilebot

wpmobilebot commented Oct 29, 2026 •

Copy link
Copy Markdown

XCFramework Build

This PR's XCFramework is available for testing. Add the following to your Package.swift:

.package(url: "https://github.com/wordpress-mobile/GutenbergKit", branch: "pr-build/852")

Built from f419317

@jkmassel
jkmassel added this pull request to stack #854 October 30, 2026 00:14
@jkmassel
jkmassel force-pushed the jkmassel/relay-xhr-sync-bodies branch from 440f617 to c4e9c00 Compare October 30, 2026 01:20
@jkmassel
jkmassel force-pushed the jkmassel/relay-xhr-sync-bodies branch 2 times, most recently from 32d0f84 to 2147663 Compare October 30, 2026 02:30
Base automatically changed from jkmassel/relay-xhr-override to trunk November 2, 2026 16:40
…Requests

WebKit drops a Blob or FormData body on the way to the relay, and a
synchronous request can't wait for the asynchronous read that the relay
uses otherwise, so these requests reached the site with an empty body
and still succeeded.

The body is now read synchronously, with a synchronous XMLHttpRequest
for a blob: URL of it, decoded as x-user-defined. FormData is first
serialized as multipart/form-data into a Blob that holds its files. A
body that can't be read fails the request with a NetworkError.
@jkmassel
jkmassel force-pushed the jkmassel/relay-xhr-sync-bodies branch from 2147663 to c218b5e Compare November 2, 2026 16:40
The unit tests fake the synchronous read of a blob: URL, so nothing in
CI ran it. The lifecycle suite now sends these bodies directly and
through the relay, and /echo returns a hash of the body so the
comparison covers the bytes as well as their count.
…owser does

A body starting with a byte order mark lost it, because decoding takes
the mark as the text's encoding. The read now starts with one extra
character.

A file that shrank after the page got it reads as a short success, so
the relay sent the cut-off body and reported success. The read's length
is now checked against the Blob's size.

A body that couldn't be read threw from send() but fired no events and
left readyState at OPENED. The browser now fails the request itself, so
readystatechange, error and loadend fire as they do for any failure.
@jkmassel
jkmassel marked this pull request as ready for review November 2, 2026 19:18
@jkmassel
jkmassel requested a review from dcalhoun November 2, 2026 19:18
Comment thread src/utils/xhr-relay.js Resolved
Comment thread src/utils/xhr-relay.js Resolved
The synchronous read holds several times the body in memory: a 250 MB
Blob peaked at about 1.7 GB in the web view, past what the system
allows. It ends the web view, which the page can't catch, so the
failure handling never runs.

A body over 100 MB now fails the request with a NetworkError without
being read. Nothing is known to send one that large synchronously.

The class comment now says an abort during the read applies to
asynchronous requests, since a synchronous read can't be aborted.
@jkmassel
jkmassel merged commit 8a297ab into trunk Nov 2, 2026
27 checks passed
@jkmassel
jkmassel deleted the jkmassel/relay-xhr-sync-bodies branch November 2, 2026 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

iOS [Type] Bug An existing feature does not function as intended Webviews

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants