content icon indicating copy to clipboard operation
content copied to clipboard

Storage access documentation is misleading

Open mhenr18 opened this issue 4 years ago • 0 comments

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/Storage_Access_API/Using

What specific section or headline is this issue about?

Accessing a user's cookies in an embedded cross-origin iframe

What information was incorrect, unhelpful, or incomplete?

This section:

Note that access requests are automatically denied unless the embedded content is currently processing a user gesture such as a tap or click — so this code needs to be run inside some kind of user gesture-based event handler, for example:

btn.addEventListener('click', () => {
  // run code here
});

Seems to imply that the following code would work:

btn.addEventListener('click', () => {
  // run code here
  document.hasStorageAccess().then((hasAccess) => {
    if (!hasAccess) {
      return document.requestStorageAccess();
    }
  }).then(() => {
    // Now we have first-party storage access!
  
    // Let's access some items from the first-party cookie jar
    document.cookie = "foo=bar";              // set a cookie
    localStorage.setItem("username", "John"); // access a localStorage entry
  }).catch(() => {
    // error obtaining storage access.
  });
});

However that doesn't work properly in Safari - the requestStorageAccess call only works if made synchronously from the button's event handler.

What did you expect to see?

I'd expect to see a snippet that looks like this (including the check for whether hasStorageAccess is present, as it's not available in every major browser yet):

function doThingsWithFirstPartyStorageAccess () {
  // Let's access some items from the first-party cookie jar
  document.cookie = "foo=bar";              // set a cookie
  localStorage.setItem("username", "John"); // access a localStorage entry
}

if (document.hasStorageAccess == null) {
  // This browser doesn't support the Storage Access API, so let's just hope we have access!
  doThingsWithFirstPartyStorageAccess()
} else {
  document.hasStorageAccess().then((hasAccess) => {
    if (hasAccess) {
      // we already have access, so do things immediately!
      doThingsWithFirstPartyStorageAccess();
    } else {
      // we don't have access, so we need to request it. this request has to happen within
      // an event handler for a user interaction (e.g clicking)
      btn.addEventListener('click', () => {
        document.requestStorageAccess().then(() => {
          doThingsWithFirstPartyStorageAccess();
        }).catch(err => {
          // error obtaining storage access.
          console.error('error obtaining storage access', err)
        })
      })
    }
  })
}

Do you have any supporting links, references, or citations?

https://bugs.webkit.org/show_bug.cgi?id=210298#c8

Do you have anything more you want to share?

No response

MDN metadata

Page report details
  • Folder: en-us/web/api/storage_access_api/using
  • MDN URL: https://developer.mozilla.org/en-US/docs/Web/API/Storage_Access_API/Using
  • GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/web/api/storage_access_api/using/index.md
  • Last commit: https://github.com/mdn/content/commit/0717d1c0a7528b2dd44d065fe90d860e3bdc2e6a
  • Document last modified: 2022-07-23T13:37:43.000Z

mhenr18 avatar Aug 04 '22 06:08 mhenr18