Repository navigation
fix: send the Blob and FormData bodies of synchronous relayed XMLHttpRequests - #852
Merged
Merged
Conversation
jkmassel
force-pushed
the
jkmassel/relay-xhr-sync-bodies
branch
from
October 29, 2026 23:25
68a0a6c to
440f617
Compare
7 tasks done
XCFramework BuildThis PR's XCFramework is available for testing. Add the following to your .package(url: "https://github.com/wordpress-mobile/GutenbergKit", branch: "pr-build/852")Built from f419317 |
jkmassel
force-pushed
the
jkmassel/relay-xhr-sync-bodies
branch
from
October 30, 2026 01:20
440f617 to
c4e9c00
Compare
5 tasks done
jkmassel
force-pushed
the
jkmassel/relay-xhr-sync-bodies
branch
2 times, most recently
from
October 30, 2026 02:30
32d0f84 to
2147663
Compare
…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
force-pushed
the
jkmassel/relay-xhr-sync-bodies
branch
from
November 2, 2026 16:40
2147663 to
c218b5e
Compare
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
marked this pull request as ready for review
November 2, 2026 19:18
dcalhoun
approved these changes
Nov 2, 2026
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.
10 tasks done
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Fixes a bug where a synchronous
XMLHttpRequestwith aBlob,FileorFormDatabody 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
Blobbody on the way to a URL scheme handler, so the relay reads every such body into anArrayBufferfirst. That read is asynchronous, and a synchronoussend()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:
POSTBlobFormDatawith a fileWhy WebKit drops the body
Confirmed in WebKit's source (
mainat26d81a1):Blobbody as a reference to theBlob, not as bytes (XMLHttpRequest.cpp:519). A non-emptyFileinFormDatais stored the same way (FormData.cpp:246).WebURLSchemeTaskProxy.cpp:84).Blobreference gets no stream at all (FormDataStreamCFNet.mm:486). That's why aFormDatawith one file loses its text fields too.How?
src/utils/xhr-relay.jsreads the body synchronously before sending it, for synchronous requests only. Asynchronous requests are unchanged.The page has no synchronous way to read a
Blob:FileReaderSyncexists only in workers, andAtomics.waitisn't allowed on a page's main thread. A synchronousXMLHttpRequestfor ablob:URL of the body does read it, with WebKit doing the work while the page waits:x-user-defineddecodes 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 anArrayBuffer, 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.FormDatahas no synchronous serializer either, so the relay builds themultipart/form-databody as aBlobof 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. TheContent-Typeis the relay's boundary, asbufferedBodydoes 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 firesreadystatechange,errorandloadendand throwsNetworkError. 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
Blobpeaked 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 withNetworkErrorwithout 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
src/utils/xhr-relay.test.js: a binaryBlobwith its type, the caller'sContent-Type, the exact multipart output, and an unreadable body and a partial read. Full suite 632/632.XMLHttpRequestRelayLifecycleTestsin real WebKit (runs in CI): newsynchronousPostBlob,synchronousPostEmptyBlob,synchronousPostFormData, three byte order mark,synchronousPostBlobFromIframe,synchronousReadFailsandreopenFromFailedSynchronousReadscenarios compare a direct request with a relayed one./echonow returns a hash of the body, with any multipart boundary replaced. Breaking the byte decoding fails theBlobandFormDatascenarios, and breaking the line break handling failsFormData, so neither is a false pass. The byte order mark and failed-read scenarios fail on the earlier code.POSTs to a local server that hashes what it receives and parses multipart bodies (table below).Filefrom the page's private file system, deleted or cut short after the page got it, alone and inFormData(table below).File, a caller-setContent-Type, 1 MB of random bytes and a 20 MBBlob, on afile://page through the realRestRelay(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.Blob, every byte value, 256 BFile, 5 BBlobwith a caller-setContent-Type, 9 BFormDatawith files, 474 BBlob, 20 MBDirect 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 asapplication/octet-stream(#857). This test server answers withAccess-Control-Allow-Origin: *, so direct requests work under Lockdown here, unlike in the Simulator results below.File, deletedTimeoutError, no eventsNetworkError,errorandloadendFile, cut shortNetworkError,errorandloadendFormDatawith the file, deleted or cut shortNetworkError,errorandloadendThe 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 ablob:URL that doesn't exist.Simulator results (before #857)
The server hashed each body and parsed multipart bodies. "Direct" is the browser's own
XMLHttpRequestto the site.Blob, 10 BNetworkError/ same hashNetworkError/ same hashFormData: non-ASCII text with a line break, a file with a non-ASCII and quoted name, an unnamedBlobNetworkError/ identical parsed entriesBlobNetworkError/ same hash, 1,071 msNetworkErrorin 0.6 s, firingreadystatechange,errorandloadend. A body of exactly 100 MB takes 27 s and reaches the server whole, status 200, with the expected hash.XMLHttpRequestRelayLifecycleTestsin the iOS 27.0 Simulator.f41931785: 38 scenarios match direct and relayed, apart frompostUntypedBlob(fix(ios): send an untyped relayed POST body asapplication/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
XMLHttpRequestthrough the relay.application/octet-stream#857: a relayed body with no type now reaches the site asapplication/octet-stream. This applies here too, so an untypedBlobstill differs from a direct request.