Enable Shared Array Buffer in Cross-Domain

997 Views Asked by At

I am facing a problem, SAB(SharedArrayBuffer) does not work in cross-domain environment. Target IFrame's source is on another domain, I added aliases to hosts file(Windows OS). I am sending SAB through postMessage to IFrame, but it simply does not either send or receive. If IFrame's source is something like this: "./frames/frame.html", SAB starts magically work. What can be done to make SAB work in cross-domain?

I am using Google Chrome, in Console window it does not show errors. When using Firefox, it provides this error: Cannot post message containing a shared memory object to a cross-origin window.

Website is hosted using webpack with headers:

Cross-Origin-Opener-Policy : same-origin
Cross-Origin-Embedder-Policy : require-corp
1

There are 1 best solutions below

1
Marc T. On

If your cross-domain environment consists of subdomains to the same parent domain (e.g. one.domain.com and two.domain.com) or even sub.domain.com and domain.com then this shows precisely which headers you need to set up on each domain: https://stackoverflow.com/a/74923275/7326344

In the iframe's .htaccess (on sub.domain.com) I needed:

Header set Cross-Origin-Embedder-Policy "require-corp"
Header set Cross-Origin-Opener-Policy "same-origin"
Header set Cross-Origin-Resource-Policy "same-site"

In the root document's .htaccess (on domain.com) I needed:

Header set Cross-Origin-Embedder-Policy "require-corp"
Header set Cross-Origin-Opener-Policy "same-origin"