Catching "Failed to read the 'cookie' property from 'Document'"

See original GitHub issue

If 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
bigb06commented, Jul 19, 2021

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.

1reaction
carhartlcommented, Sep 9, 2019

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found