wordpress-playground icon indicating copy to clipboard operation
wordpress-playground copied to clipboard

Issue with cross-origin isolation

Open swissspidy opened this issue 1 year ago • 4 comments

I’m not 100% sure whether this issue I’m having needs fixing in playground or Gutenberg.

I’m trying to get my Media Experiments plugin running in playground. To try it: https://playground.wordpress.net/?mode=seamless&blueprint-url=https://raw.githubusercontent.com/swissspidy/media-experiments/main/blueprints/playground.json

The plugin sets Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp HTTP headers on the post editor page to force cross-origin isolation. The block editor is iframed. The Iframe component tries to access node.contentDocument but because the iframe seems to be on a different origin, that property is null and the editor crashes.

Screenshot 2024-01-17 at 14 45 39

Do you have any idea how this issue could be resolved? I’d really love folks to be able to test my plugin in playground.

swissspidy avatar Jan 17 '24 13:01 swissspidy

This is an interesting problem @swissspidy! And it goes deep.

So first, Gutenberg uses a blob as a source for that iframe. Unfortunately, blob-based iframes cannot be controlled by service workers so that wouldn't work in Playground. Therefore, Playground patches Gutenberg to load the document as follows: src="/empty.html".

This means there's a network request involved, and so there is also a HTTP response where specific cross-origin headers may be required. My last attempt to use these Cross-Origin headers broke YouTube embeds, though. I just tried making the editor iframe credentialless (via <iframe credentialless>), but that also made it uncontrolled by the Service Worker.

I'm not sure if there's a good solution here, given how easily Service Workers give up. Could you perhaps disable the Cross-Origin isolation when the plugin is running in Playground?

Here's some related resources:

  • https://github.com/WordPress/wordpress-playground/issues/646
  • https://github.com/WordPress/wordpress-playground/issues/586
  • https://github.com/WordPress/wordpress-playground/issues/411
  • https://github.com/WordPress/wordpress-playground/pull/695

adamziel avatar Jan 18 '24 13:01 adamziel

Could you perhaps disable the Cross-Origin isolation when the plugin is running in Playground?

What I just tried now though is disabling the editor iframing in Gutenberg by registering a fake block with an old API version. See https://github.com/swissspidy/media-experiments/commit/91b983d919572ea0ab50a36d5029776c74f9e6f7

That now makes the post editor at least work again, but turns out SharedArrayBuffer is still not available because of #695.

Unfortunately the majority of the plugin's features, such as using wasm-vips to compress images, require SharedArrayBuffer and thus cross-origin isolation.

I'll try to see whether I can turn off those features to have at least a very trimmed down version working in playground. Can't hurt to add some hardening for such cases in my plugin.

swissspidy avatar Jan 18 '24 14:01 swissspidy

Surfacing an in-person chat on CloudFest:

The only solution seems to be adding an on/off switch to Playground to enable/disable the CORP headers. Both modes have value:

  • Without CORP headers, media embeds and external scripts typically work as expected
  • With CORP headers, SharedArrayBuffer works as expected

There doesn't seem to be a middle-ground where we can get both at the same time.

adamziel avatar Mar 19 '24 19:03 adamziel

Related: https://github.com/swissspidy/media-experiments/pull/384

adamziel avatar Mar 23 '24 19:03 adamziel