Repository navigation
feat: send XMLHttpRequest requests through the REST relay - #849
Merged
Merged
Conversation
Plugin code that uses `XMLHttpRequest` (jQuery's `$.ajax`, `tus-js-client`, axios) failed CORS under iOS Lockdown Mode, because only `fetch` went through the relay. `xhr-relay.js` replaces the global `XMLHttpRequest` with a subclass that points relayed requests at the relay URL and leaves the rest to the browser's own XHR. What XHR needed beyond the URL, measured in the iOS 27.0 Simulator: - An XHR with an upload listener hands its body to the scheme handler as `httpBodyStream`, even a string. The relay only copied `httpBody`, so those uploads arrived empty. `RestRelay` now reads either. - `Blob` and `FormData` holding a file are dropped, as with `fetch`, so the page reads them into an `ArrayBuffer` first. - WebKit ignores `timeout` for scheme requests. The page sends it in `X-GBK-Relay-Timeout`, and native code fails the task with `URLError(.timedOut)`, which the page sees as a `timeout` event. - WebKit fires no upload `progress`, `load` or `loadend`. The page fires them when the response headers arrive.
3 tasks
A synchronous request's listeners can open the request again before send() returns, and abort() leaves the request unsent. The relay marked the request sent after send() returned, and refused withCredentials after an abort, so both threw InvalidStateError where the browser's own XMLHttpRequest does not.
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/849")Built from c25302e |
8 tasks done
WebKit streams the body once the request's upload object exists, which reading xhr.upload creates, with or without a listener.
10 tasks done
…er's order WebKit never counts a relayed upload as complete, because only its network code does that. So after the relay fired the upload's `load` and `loadend` as the headers arrived, an abort, timeout or error fired the upload's `abort`/`timeout`/`error` and `loadend` a second time. A listener the relay adds when the caller first reads `xhr.upload` runs before the caller's own, and stops those once the upload is complete. Also matched to the browser's own XMLHttpRequest in the iOS 27.0 Simulator: - An `abort()` from the upload's `progress` listener ends the upload there, with no `load`. - An `abort()` from the upload's `load` listener hides the response that had arrived: the caller doesn't hear its headers, and `responseURL` and `responseText` read as empty until the request is opened again. - `setRequestHeader` while a `Blob` or `FormData` body is read throws `InvalidStateError`, as it does once a request is sent. - An abort while the body is read gives the upload's `loadstart` the body's size rather than 0.
Runs each scenario in a real web view twice, once with WebKit's own XMLHttpRequest and once through the relay, against a local server, and expects the caller to see the same events and result. The relay is read from `src/`, so the suite tests the code as it is. Three known differences are marked as known issues: a `Blob` or `FormData` body's `loadstart` fires once it is read, and two in the native relay that predate the XMLHttpRequest relay (an untyped body goes upstream as `application/x-www-form-urlencoded`, and a chunked response cut off partway is relayed as a success).
This was referenced Oct 30, 2026
jkmassel
added a commit
that referenced
this pull request
Oct 30, 2026
WebKit fires no upload `progress` events for a request a scheme handler answers, so a relayed XHR upload sat at 0% until the response arrived. Without Lockdown Mode that was a regression from #849. The page names each relayed upload in an `X-GBK-Relay-Upload` header, which the relay strips. Native code forwards `URLSession`'s `didSendBodyData`, at most every 50 ms, to `window.editor.relayUploadProgress`, and the page fires `progress` on that request's `xhr.upload`. Matched to the browser's own XMLHttpRequest in the iOS 27.0 Simulator: - The report for the whole body always goes, and the upload's `load` and `loadend` fire on it, once the body is sent. The response's headers still fire them if they arrive first. - A listener added after `send()` hears no upload events, including from the request that stands in for one aborted, or whose body could not be read, before it was sent. The progress registry lives outside the class, so the relayed XMLHttpRequest has no static the browser's lacks. Fixes #850.
The browser fires only the upload's `loadstart` for an empty body. The relay
also fired `progress`, `load` and `loadend` at 0/0, for `send('')`, an empty
`ArrayBuffer` or an empty `Blob`. Measured in the iOS 27.0 Simulator, and now
compared there by `postEmptyString` and `postEmptyArrayBuffer`.
Also documents two differences that remain, measured the same way:
- Changing `timeout` after `send()` has no effect on a relayed request.
- Download `progress` has `lengthComputable` false, because native code drops
`Content-Length`.
jkmassel
marked this pull request as ready for review
October 30, 2026 02:19
jkmassel
added a commit
that referenced
this pull request
Oct 30, 2026
WebKit fires no upload `progress` events for a request a scheme handler answers, so a relayed XHR upload sat at 0% until the response arrived. Without Lockdown Mode that was a regression from #849. The page names each relayed upload in an `X-GBK-Relay-Upload` header, which the relay strips. Native code forwards `URLSession`'s `didSendBodyData`, at most every 50 ms, to `window.editor.relayUploadProgress`, and the page fires `progress` on that request's `xhr.upload`. Matched to the browser's own XMLHttpRequest in the iOS 27.0 Simulator: - The report for the whole body always goes, and the upload's `load` and `loadend` fire on it, once the body is sent. The response's headers still fire them if they arrive first. - A listener added after `send()` hears no upload events, including from the request that stands in for one aborted, or whose body could not be read, before it was sent. The progress registry lives outside the class, so the relayed XMLHttpRequest has no static the browser's lacks. Fixes #850.
`settle()` waited one timer turn and assumed the relay had read the body by then. A read can take two turns while other reads are running, as in a full parallel test run: 1 in 100 reads did, measured in Node 24 with 20 other reads in flight. `counts the time spent reading the body` failed that way once, because its fake clock was gone by the time the read finished. `settle()` now waits for every read the relay started. With each read slowed by one timer turn, the old helper fails 6 tests and the new one none.
jkmassel
added a commit
that referenced
this pull request
Oct 30, 2026
WebKit fires no upload `progress` events for a request a scheme handler answers, so a relayed XHR upload sat at 0% until the response arrived. Without Lockdown Mode that was a regression from #849. The page names each relayed upload in an `X-GBK-Relay-Upload` header, which the relay strips. Native code forwards `URLSession`'s `didSendBodyData`, at most every 50 ms, to `window.editor.relayUploadProgress`, and the page fires `progress` on that request's `xhr.upload`. Matched to the browser's own XMLHttpRequest in the iOS 27.0 Simulator: - The report for the whole body always goes, and the upload's `load` and `loadend` fire on it, once the body is sent. The response's headers still fire them if they arrive first. - A listener added after `send()` hears no upload events, including from the request that stands in for one aborted, or whose body could not be read, before it was sent. The progress registry lives outside the class, so the relayed XMLHttpRequest has no static the browser's lacks. Fixes #850.
dcalhoun
approved these changes
Nov 2, 2026
dcalhoun
left a comment
Member
There was a problem hiding this comment.
I tested VideoPress uploads on iOS with LDM enabled and disabled. Both worked as expected.
jkmassel
added a commit
that referenced
this pull request
Nov 2, 2026
WebKit fires no upload `progress` events for a request a scheme handler answers, so a relayed XHR upload sat at 0% until the response arrived. Without Lockdown Mode that was a regression from #849. The page names each relayed upload in an `X-GBK-Relay-Upload` header, which the relay strips. Native code forwards `URLSession`'s `didSendBodyData`, at most every 50 ms, to `window.editor.relayUploadProgress`, and the page fires `progress` on that request's `xhr.upload`. Matched to the browser's own XMLHttpRequest in the iOS 27.0 Simulator and on an iPhone with Lockdown Mode on: - The report for the whole body always goes, and the upload's `load` and `loadend` fire on it, once the body is sent. The response's headers still fire them if they arrive first. - A listener added after `send()` hears no upload events, including while a `Blob` or `FormData` body is read, and from the request that stands in for one aborted, or whose body could not be read, before it was sent. The page can't see listeners, so it goes by whether `xhr.upload` was read before `send()`. A caller that read it then and added a listener afterwards hears events the browser wouldn't fire. The progress registry lives outside the class, so the relayed XMLHttpRequest has no static the browser's lacks. Fixes #850.
2 tasks
jkmassel
added a commit
that referenced
this pull request
Nov 2, 2026
WebKit fires no upload `progress` events for a request a scheme handler answers, so a relayed XHR upload sat at 0% until the response arrived. Without Lockdown Mode that was a regression from #849. The page names each relayed upload in an `X-GBK-Relay-Upload` header, which the relay strips. Native code forwards `URLSession`'s `didSendBodyData`, at most every 50 ms, to `window.editor.relayUploadProgress`, and the page fires `progress` on that request's `xhr.upload`. Matched to the browser's own XMLHttpRequest in the iOS 27.0 Simulator and on an iPhone with Lockdown Mode on: - The report for the whole body always goes, and the upload's `load` and `loadend` fire on it, once the body is sent. The response's headers still fire them if they arrive first. - A listener added after `send()` hears no upload events, including while a `Blob` or `FormData` body is read, and from the request that stands in for one aborted, or whose body could not be read, before it was sent. The page can't see listeners, so it goes by whether `xhr.upload` was read before `send()`. A caller that read it then and added a listener afterwards hears events the browser wouldn't fire. The progress registry lives outside the class, so the relayed XMLHttpRequest has no static the browser's lacks. Fixes #850.
jkmassel
added a commit
that referenced
this pull request
Nov 2, 2026
WebKit fires no upload `progress` events for a request a scheme handler answers, so a relayed XHR upload sat at 0% until the response arrived. Without Lockdown Mode that was a regression from #849. The page names each relayed upload in an `X-GBK-Relay-Upload` header, which the relay strips. Native code forwards `URLSession`'s `didSendBodyData`, at most every 50 ms, to `window.editor.relayUploadProgress`, and the page fires `progress` on that request's `xhr.upload`. Matched to the browser's own XMLHttpRequest in the iOS 27.0 Simulator and on an iPhone with Lockdown Mode on: - The report for the whole body always goes, and the upload's `load` and `loadend` fire on it, once the body is sent. The response's headers still fire them if they arrive first. - A listener added after `send()` hears no upload events, including while a `Blob` or `FormData` body is read, and from the request that stands in for one aborted, or whose body could not be read, before it was sent. The page can't see listeners, so it goes by whether `xhr.upload` was read before `send()`. A caller that read it then and added a listener afterwards hears events the browser wouldn't fire. The progress registry lives outside the class, so the relayed XMLHttpRequest has no static the browser's lacks. Fixes #850.
jkmassel
added a commit
that referenced
this pull request
Nov 3, 2026
WebKit fires no upload `progress` events for a request a scheme handler answers, so a relayed XHR upload sat at 0% until the response arrived. Without Lockdown Mode that was a regression from #849. The page names each relayed upload in an `X-GBK-Relay-Upload` header, which the relay strips. Native code forwards `URLSession`'s `didSendBodyData`, at most every 50 ms, to `window.editor.relayUploadProgress`, and the page fires `progress` on that request's `xhr.upload`. Matched to the browser's own XMLHttpRequest in the iOS 27.0 Simulator and on an iPhone with Lockdown Mode on: - The report for the whole body always goes, and the upload's `load` and `loadend` fire on it, once the body is sent. The response's headers still fire them if they arrive first. - A listener added after `send()` hears no upload events, including while a `Blob` or `FormData` body is read, and from the request that stands in for one aborted, or whose body could not be read, before it was sent. The page can't see listeners, so it goes by whether `xhr.upload` was read before `send()`. A caller that read it then and added a listener afterwards hears events the browser wouldn't fire. The progress registry lives outside the class, so the relayed XMLHttpRequest has no static the browser's lacks. Fixes #850.
jkmassel
added a commit
that referenced
this pull request
Nov 3, 2026
* feat: report the upload progress of relayed XMLHttpRequests WebKit fires no upload `progress` events for a request a scheme handler answers, so a relayed XHR upload sat at 0% until the response arrived. Without Lockdown Mode that was a regression from #849. The page names each relayed upload in an `X-GBK-Relay-Upload` header, which the relay strips. Native code forwards `URLSession`'s `didSendBodyData`, at most every 50 ms, to `window.editor.relayUploadProgress`, and the page fires `progress` on that request's `xhr.upload`. Matched to the browser's own XMLHttpRequest in the iOS 27.0 Simulator and on an iPhone with Lockdown Mode on: - The report for the whole body always goes, and the upload's `load` and `loadend` fire on it, once the body is sent. The response's headers still fire them if they arrive first. - A listener added after `send()` hears no upload events, including while a `Blob` or `FormData` body is read, and from the request that stands in for one aborted, or whose body could not be read, before it was sent. The page can't see listeners, so it goes by whether `xhr.upload` was read before `send()`. A caller that read it then and added a listener afterwards hears events the browser wouldn't fire. The progress registry lives outside the class, so the relayed XMLHttpRequest has no static the browser's lacks. Fixes #850. * test: tighten two tests from review The check that the relayed XMLHttpRequest adds no static members now compares the exact list, so any new static fails it, not only one named reportUploadProgress. StalledUploadServer listens on 127.0.0.1 only, as LifecycleHTTPServer does. Every client connects there.
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?
Sends the page's
XMLHttpRequestrequests through the native REST relay, asfetchrequests already are. Plugin code that uses XHR — jQuery's$.ajax,tus-js-client, axios — now works under iOS Lockdown Mode without changes.Why?
Under Lockdown Mode the editor's
file://page loses its CORS exemption, so every direct request to the site fails. The relay fixed that forfetchonly. Any plugin sending XHR still failed: a direct XHR to the site under Lockdown Mode ends in a network error (confirmed on an iPhone 15 Pro).How?
src/utils/xhr-relay.jsreplaces the globalXMLHttpRequestwith a subclass. It changes only the URL of a relayed request, so the browser's own XHR still handles events,responseType, headers and synchronous requests. It relays the same requests thefetchrelay does (isRelayed). On Android, which has no relay, it does nothing.A URL swap wasn't enough. Measured in the iOS 27.0 Simulator, WebKit treats XHR to a scheme handler differently from
fetch:1. Bodies of XHRs with an upload object arrived empty
Once anything reads
xhr.upload(adding an upload listener does), WebKit hands the XHR's body to the scheme handler ashttpBodyStream, even a string orArrayBuffer. That's in WebKit'sXMLHttpRequest.cpp.RestRelayonly copiedhttpBody, so the site received 0 bytes.tus-js-clientand most upload code register a listener.RestRelay.swiftnow reads the stream too.This also applies to relayed
fetchrequests. The onlyfetchbody that arrives as a stream is a photo-pickerFileinFormData; it used to be sent empty, and is now read whole into memory like other relayed bodies. Core media uploads don't take this path — they usegbk-upload:.2.
BlobandFormDatabodies are droppedAs with
fetch, WebKit drops aBlob, orFormDataholding one, so the page reads it into anArrayBufferfirst (bufferedBody, now shared withfetch-relay.js). The request starts once the body is read. Anabort()during the read ends the request with the same events the browser fires for its own abort, and nothing reaches the server.3.
timeoutis ignoredWebKit never times out an XHR that a scheme handler answers. The page sends the timeout in an
X-GBK-Relay-Timeoutheader, which is never forwarded.RestRelaySchemeHandler.swiftfails the task withURLError(.timedOut)when it runs out, which WebKit reports as a normaltimeoutevent — before or after the response headers.4. No upload progress
WebKit fires no upload
progress,loadorloadendfor scheme requests. The page fires them when the response headers arrive.Known regression: upload progress without Lockdown Mode
The relay takes every request, Lockdown Mode or not. Without Lockdown Mode, a direct XHR upload reported real progress; through the relay it jumps from 0 to 100% at the end. Measured in the Simulator, 8 MB to a server reading at ~2 MB/s:
progresseventsUnder Lockdown Mode there's nothing to regress: the upload failed outright before. Real progress needs native code to report
URLSession's upload progress back to the page, which is tracked in #850, to address after this PR.Other differences a caller can see
responseURLis the URL the caller opened. A redirect native code followed doesn't show in it.withCredentialsreads back as set, but the request goes without cookies: the relay answersAccess-Control-Allow-Origin: *.isTrusted: false.timeoutis read when the request is sent. Changing it aftersend()has no effect.Content-Length, so downloadprogresshaslengthComputable: falseand atotalof 0, andgetResponseHeader('Content-Length')isnull. This already happens forfetch.These, and the rest that remain for
fetchand XHR, are collected in #858.Testing Instructions
src/utils/xhr-relay.test.js(new),fetch-relay.test.js,editor-environment.test.js; full suite 564/564send(), and settingwithCredentialsafterabort(), behave as with the browser's own XHR: unit tests, and real WebKit in the iOS 27.0 SimulatorRestRelayHandleTests); relay suites 84/84XMLHttpRequestRelayLifecycleTestsruns 23 scenarios with the browser's own XHR and through the relay, and all 23 match. These include an empty-body upload, which fired extra upload events before15de7f59fetchbody, with and without an upload listener, sync and async; timeouts; abort and error event sequences compared with the browser's ownNot tested: a real plugin (Jetpack's
tus-js-clientVideoPress uploader) against a production site. It needs a production Jetpack site with VideoPress, which wasn't available. The request shape it relies on, aBlobbody with an upload listener, is covered by the rawBlobupload on the iPhone below. This isn't a step for the reviewer.The 22 calls, on the iPhone 15 Pro with Lockdown Mode
GET /wp/v2/postsPOST admin-ajax.php(unknown action)"0"(WordPress's own answer)GET api.wordpress.org(another host)FormDataupload, 2 MB, with progresstimeout: 1,abort()timeout,abortGET,responseType: 'json'X-WP-TotalreadableFormDataupload, 3 MB, with progressBlobupload, 5 MB, with progresstimeout = 5loadstart → timeout → loadendabort()loadstart → abort → loadendGETerror, status 0rest_no_routeEvery post and file the test created was deleted afterwards.
Related
BloborFormDatabody reaches the site empty here, because the body can't be read before a synchronoussend(). fix: send the Blob and FormData bodies of synchronous relayed XMLHttpRequests #852 reads it synchronously.urland headers thatfetchwould #815 edits the same relay files; onlyRestRelayHandleTests.swiftconflicts.