My intention: Facilitate localStorage value transfer from one domain to another
I have a page, domain.com/storage-agent.html which will be embedded in an iframe by diffdomain.com. domain.com/storage-agent.html has the following response header.
Content-Security-Policy: frame-ancestors https://diffdomain.com https://diff.domain.com
(X-Frame-Options: SAMEORIGIN
also exists, but anyway, it'll be ignored by Chrome in favour of frame-ancestors
.)
domain.com/storage-agent.html has a script, that will postMessage
a value from localStorage (of domain.com) and the host/embedder listens to it.
The problem is, when the aforementioned script accesses localStorage.getItem('key')
when it's embedded by diffdomain.com in an iframe, it's null. JSON.stringify(localStorage)
is {}
too, meaning none of the values are available.
But, if I load domain.com/storage-agent.html in a separate tab and do the same localStorage.getItem('key')
in console, I get the value as expected.
If I embed domain.com/storage-agent.html in diff.domain.com, then also things work as expected. So nothing wrong with the script that gets from localStorage.
Can't get my head around what's happening.