Skip to content

feat: send XMLHttpRequest requests through the REST relay - #849

Merged
jkmassel merged 7 commits into
trunkfrom
jkmassel/relay-xhr-override
Nov 2, 2026
Merged

jkmassel merged 7 commits into
trunkfrom
jkmassel/relay-xhr-override

Conversation

@jkmassel

@jkmassel jkmassel commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

What?

Sends the page's XMLHttpRequest requests through the native REST relay, as fetch requests 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 for fetch only. 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.js replaces the global XMLHttpRequest with 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 the fetch relay 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 as httpBodyStream, even a string or ArrayBuffer. That's in WebKit's XMLHttpRequest.cpp. RestRelay only copied httpBody, so the site received 0 bytes. tus-js-client and most upload code register a listener. RestRelay.swift now reads the stream too.

This also applies to relayed fetch requests. The only fetch body that arrives as a stream is a photo-picker File in FormData; 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 use gbk-upload:.

2. Blob and FormData bodies are dropped

As with fetch, WebKit drops a Blob, or FormData holding one, so the page reads it into an ArrayBuffer first (bufferedBody, now shared with fetch-relay.js). The request starts once the body is read. An abort() during the read ends the request with the same events the browser fires for its own abort, and nothing reaches the server.

3. timeout is ignored

WebKit never times out an XHR that a scheme handler answers. The page sends the timeout in an X-GBK-Relay-Timeout header, which is never forwarded. RestRelaySchemeHandler.swift fails the task with URLError(.timedOut) when it runs out, which WebKit reports as a normal timeout event — before or after the response headers.

4. No upload progress

WebKit fires no upload progress, load or loadend for 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:

Upload progress events First event
Direct (before) 65 72 ms
Relayed (this PR) 1 4,055 ms, at 100%

Under 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

  • responseURL is the URL the caller opened. A redirect native code followed doesn't show in it.
  • withCredentials reads back as set, but the request goes without cookies: the relay answers Access-Control-Allow-Origin: *.
  • The upload events the page fires itself have isTrusted: false.
  • timeout is read when the request is sent. Changing it after send() has no effect.
  • Native code drops Content-Length, so download progress has lengthComputable: false and a total of 0, and getResponseHeader('Content-Length') is null. This already happens for fetch.

These, and the rest that remain for fetch and XHR, are collected in #858.

Testing Instructions

  • JS unit tests: src/utils/xhr-relay.test.js (new), fetch-relay.test.js, editor-environment.test.js; full suite 564/564
  • Re-opening a request from a listener during send(), and setting withCredentials after abort(), behave as with the browser's own XHR: unit tests, and real WebKit in the iOS 27.0 Simulator
  • Swift tests for the streamed body, the timeout, and the stripped timeout header (RestRelayHandleTests); relay suites 84/84
  • Simulator, iOS 27.0: XMLHttpRequestRelayLifecycleTests runs 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 before 15de7f59
  • Simulator, iOS 27.0: what WebKit hands a scheme handler for each XHR and fetch body, with and without an upload listener, sync and async; timeouts; abort and error event sequences compared with the browser's own
  • Simulator, real editor against wp-env, Lockdown Mode forced on and off: 22 jQuery and plain XHR calls (table below)
  • iPhone 15 Pro, iOS 27, real Lockdown Mode: the same 22 calls against wp-env over the LAN. All matched the Simulator.

Not tested: a real plugin (Jetpack's tus-js-client VideoPress uploader) against a production site. It needs a production Jetpack site with VideoPress, which wasn't available. The request shape it relies on, a Blob body with an upload listener, is covered by the raw Blob upload on the iPhone below. This isn't a step for the reviewer.

The 22 calls, on the iPhone 15 Pro with Lockdown Mode
Call Result
Control: browser's own XHR, straight to the site network error, status 0
jQuery GET /wp/v2/posts 200
jQuery create draft → edit → delete each succeeded
jQuery POST admin-ajax.php (unknown action) 400, body "0" (WordPress's own answer)
jQuery GET api.wordpress.org (another host) 200
jQuery FormData upload, 2 MB, with progress 201, 2,097,165 bytes stored
jQuery timeout: 1, abort() timeout, abort
XHR GET, responseType: 'json' 200, X-WP-Total readable
XHR create draft → delete each succeeded
XHR FormData upload, 3 MB, with progress 201, 3,145,729 bytes stored
XHR raw Blob upload, 5 MB, with progress 201, 5,242,887 bytes stored
XHR timeout = 5 loadstart → timeout → loadend
XHR abort() loadstart → abort → loadend
XHR synchronous GET 200
XHR another host 200
XHR unreachable port on the site's host error, status 0
XHR missing route 404, rest_no_route

Every post and file the test created was deleted afterwards.

Related

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.
@jkmassel jkmassel added [Type] Enhancement A suggestion for improvement. iOS Webviews 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
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.
@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/849")

Built from c25302e

WebKit streams the body once the request's upload object exists, which
reading xhr.upload creates, with or without a listener.
…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).
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
jkmassel marked this pull request as ready for review October 30, 2026 02:19
@jkmassel
jkmassel requested a review from dcalhoun 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 dcalhoun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested VideoPress uploads on iOS with LDM enabled and disabled. Both worked as expected.

@jkmassel
jkmassel merged commit 5618090 into trunk Nov 2, 2026
25 checks passed
@jkmassel
jkmassel deleted the jkmassel/relay-xhr-override branch November 2, 2026 16:40
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 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

iOS [Type] Enhancement A suggestion for improvement. Webviews

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants