I'm trying to store a value on another domain using an iframe (actually, I'm using the xauth library at /). However, when I try to store anything using Chrome, it es back with "QUOTA_EXCEEDED_ERR: DOM Exception 22", which I've e to recognize as an access error. I've mocked up a couple of very simple pages below to duplicate the effect:
File 1.html:
<html>
<head/>
<iframe src='http://127.0.0.1/2.html' />
</html>
File 2.html:
<html>
<head/>
<script>
console.log(localStorage);
localStorage.setItem('test', '123');
</script>
</html>
If I place both of these on my local server and access localhost/1.html it embeds a frame from 127.0.0.1 (which Chrome considers a separate domain), and I get the same access error as above. At a guess, it looks like even though I'm embedding an iframe from another domain, and the script inside that iframe references the localStorage for that domain properly (as I can see with the console.log(localStorage) line), the permissions for writing to localStorage are ing from the top page's domain.
In short, it looks like no iframe can write to localStorage in Chrome. Does anybody know if there's a way around this particular security "feature"? Or am I doing something wrong?
I'm trying to store a value on another domain using an iframe (actually, I'm using the xauth library at http://xauth/info/). However, when I try to store anything using Chrome, it es back with "QUOTA_EXCEEDED_ERR: DOM Exception 22", which I've e to recognize as an access error. I've mocked up a couple of very simple pages below to duplicate the effect:
File 1.html:
<html>
<head/>
<iframe src='http://127.0.0.1/2.html' />
</html>
File 2.html:
<html>
<head/>
<script>
console.log(localStorage);
localStorage.setItem('test', '123');
</script>
</html>
If I place both of these on my local server and access localhost/1.html it embeds a frame from 127.0.0.1 (which Chrome considers a separate domain), and I get the same access error as above. At a guess, it looks like even though I'm embedding an iframe from another domain, and the script inside that iframe references the localStorage for that domain properly (as I can see with the console.log(localStorage) line), the permissions for writing to localStorage are ing from the top page's domain.
In short, it looks like no iframe can write to localStorage in Chrome. Does anybody know if there's a way around this particular security "feature"? Or am I doing something wrong?
Share Improve this question asked May 25, 2012 at 22:37 ChrisChris 4052 gold badges5 silver badges13 bronze badges 4- Note I've also checked my localStorage objects for both localhost and 127.0.0.1 and guaranteed that they're empty, so I know for a fact that the "Quota exceeded" error really is erroneous; I certainly haven't exceeded the 5MB quota. – Chris Commented May 25, 2012 at 22:39
- did you solve this? I'm getting the same issue – Lloyd Commented Oct 1, 2012 at 16:48
- Use postMessage... stackoverflow./questions/40461120/… – Zvi Redler Commented Aug 7, 2019 at 13:37
- As Zvi Redler said, have you tried using HTML5 postMessage to municate between your two origins using an IFRAME? You'll need a listener and a call to postMessage. Please let us know what you find out. – StephenKC Commented Jan 19, 2021 at 19:45
3 Answers
Reset to default 6The problem only occurs when third-party cookies are disabled. Newer versions of Firefox and Opera are also blocking it. In IE and Edge it is still possible although third-party cookies are disabled. If the localStorage would not be blocked in the iframe, a web tracker could simply include a iframe, read the cookie, send it to the parent script, and then send it to the server.
The reason why this is not blocked in IE and Edge is that these browser allow websites to send third-party cookies, which were previously set as first-party cookies, to the server although third-party cookies are blocked. For example, if a user visits facebook on a regular basis, he gets first-party cookies from facebook. When he then visits other websites with facebook's share button, facebook can track him although third-party cookies are disabled. I really do not know why IE and Edge do not block third-party cookie sending, but I would not use these browsers anyway.
The errors the browsers show when third-party cookies are disabled:
Chrome and Opera: Uncaught DOMException: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.
Firefox: SecurityError: The operation is insecure.
IE and Edge: No error, access to localStorage in iframe is possible although third-party cookies are disabled.
So in conclusion, it is not possible to bypass this security feature (in Chrome, Firefox, Opera) and this is good in order to ensure users' privacy.
This is an old post, but if someone else see it- you can use postMessage
https://stackoverflow./a/40469196/4836581
Well, localStorage
is domain-based and there is no reason for your example code to fail. What it actually does is to set the test
item to 123 for 127.0.0.1 whereas it will leave the localhost localStorage
empty.
This might not be the answer to your initial problem of QUOTA_EXCEEDED_ERR, but just try to switch to private browsing on Chrome (Ctrl+Shift+N) to see if you still have the error. Without further information on what you were initially doing, I can't tell much but I believe that quota exceeded means what it means...
And I think Chrome's quota is 2.5mb unlike FF which has 5mb of localStorage quota.