Catching "Failed to read the 'cookie' property from 'Document'"
See original GitHub issueIf the script is executed inside an iframe with a sandbox attribute, it can throw an error:
Stack: Error: Failed to read the ‘cookie’ property from ‘Document’: The document is sandboxed and lacks the ‘allow-same-origin’ flag.
test.html
<iframe src="iframe.html" sandbox="allow-scripts"></iframe>
Script inside iframe.html sufficient to reproduce the error:
document.cookie;
Possible solution: replace this check in get and set functions:
if (typeof document === 'undefined') {
return;
}
by this:
try { document.cookie; } catch (e) { return; }
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Java webview: "Failed to read the 'cookie' property from ...
But when I want to debug my webview with Chrome Dev Tools, I get: Uncaught SecurityError: Failed to read the 'cookie' property from...
Read more >Using iFrame, getting error: Failed to read the 'cookie' property ...
Failed to read the 'cookie' property from 'Document': The document is sandboxed and lacks the 'allow-same-origin' flag.
Read more >Things not available when someone blocks all cookies
Uncaught DOMException : Failed to read the 'localStorage' property from 'Window': Access is denied for this document.
Read more >728 - Failed to set the 'cookie' property on 'Document' - Monorail
Issue 728: Failed to set the 'cookie' property on 'Document': Cookies are disabled inside 'data:' URLs. Reported by schedule daqpi.
Read more >Error: Permission denied to access property "x" - JavaScript
There was attempt to access an object for which you have no permission. This is likely an <iframe> element loaded from a different...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m having quite the same error but no iframe here: Failed to read the ‘cookie’ property from ‘Document’ : Access is denied for this document
It happens only on chrome and I can’t reproduce the problem : disable cookies, privacy sandbox, … I just log error since few days and it happens on chrome 91.x with android 8 to 11.
Why would you execute/load the script when having explicitly sandboxed the iframe and thus expecting it to not work?
Asked differently: if we were to catch the error and swallow it silently, how would a developer know that she had accidentally sandboxed the iframe?