DOMException: Failed to read the 'localStorage' property from 'Window': In Chrome Incognito mode and running in Iframe
Asked Answered
H

2

10

Can anyone please help me on this issue as I'm getting this same error message on my site when I run it inside iframe of a separate domain in incognito mode only? You can access site from here.

  • No issue on non-incognito mode.
  • No issue without iframe as working an individual site
  • No issue with those sites that don't use localStorage even running inside iframe or without iframe

Error in console -

"DOMException: Failed to read the 'localStorage' property from 'Window': Access is denied for this document"

Thanks

Hellenistic answered 31/7, 2020 at 16:3 Comment(0)
P
3

I ran into this issue and determined it was because a dependency of our app was throwing an error when trying to access local storage. When the app runs in an iframe in incognito, accessing local storage throws an error. If a dependency does not handle a call to local storage with try / catch, that crashes the app.

The same behavior can be reproduced in the Brave browser, by disabling cookies / local storage.

A good solution would be filing a bug with the library that throws that error (it should be as simple as wrapping that reference in try / catch and ignoring the error).

We also fixed this by adding a proxy for the local storage object that will do the same thing.

<script>
  let storageOrUndefined;

  try {
    storageOrUndefined = window.localStorage;
  } catch (e) {
    storageOrUndefined = undefined;
  }

  const isIframe = window !== window.top;
 
  if (isIframe) {
      const lsProxy = new Proxy(
      {},
      {
        get: (_, prop, __) => (argument) => {
          if (!storageOrUndefined) {
            return null;
          }

          try {
            return storageOrUndefined[prop](argument);
          } catch (e) {
            return null;
          }
        },
      }
    );

    Object.defineProperty(window, "localStorage", {
      value: lsProxy,
      configurable: true,
      enumerable: true,
      writable: false,
    });
  }
</script>
Pruinose answered 4/2, 2023 at 20:46 Comment(1)
lifesaver!! I'd been researching all kinds of workarounds but this just works! I adapted it to create both local and session storage but this got me out of a hole. Thank you!Frederigo
A
1

Update 4/8/2020

block third-party cookies

enter image description here

allow third-party cookies

enter image description here

From the screenshot above, you can see third-party cookies from

https://www.googletagmanager.com
https://cstatic.weborama.fr
https://www.google.com

Removed the direct dependency between page displaying and cookies from these websites.





Follow these steps to unchecked block third-party cookies in Chrome settings.

This exception is thrown when the "Block third-party cookies and site data" checkbox is set in Content Settings.

To find the setting, open Chrome settings, type "third" in the search box, check Allow all cookies in General settings.

enter image description here

Arched answered 3/8, 2020 at 8:25 Comment(4)
Thank you for your answer but I cannot contact my each and every user and ask them to change this setting. Is there any way to solve this apart from doing anything at user end?Hellenistic
It's impossible. Reliance on third-party cookies is not an optimal solution. So you need to change on server.Arched
Still my problem not solved. Still site not working in chrome incognito mode. It works when I allow all cookies from browser settings. Any help with other solution?Hellenistic
You could check the sameSite attribute to allow the thrid party cookies. And the demo to show how to use SameSite cookies.Arched

© 2022 - 2024 — McMap. All rights reserved.