Document.requestStorageAccess()
The requestStorageAccess()
method of the
Document
interface returns a Promise
that resolves if the
access to first-party storage was granted, and rejects if access was denied.
Conditions for granting storage access
Storage access is granted based on a series of checks described here:
- If the browser is not processing a user gesture, reject.
- If the document already has been granted access, resolve.
- If the document has a null origin, reject.
- If the document's frame is the main frame, resolve.
- If the sub frame's origin is equal to the main frame's, resolve.
- If the sub frame is not sandboxed, skip to step 7.
-
If the sub frame doesn't have the token
allow-storage-access-by-user-activation
, reject. - If the sub frame's parent frame is not the top frame, reject.
- Check any additional rules that the browser has. Examples: allow lists, block lists, on-device classification, user settings, anti-clickjacking heuristics, or prompting the user for explicit permission. Reject if some rule is not fulfilled.
-
Grant the document access to cookies and other site storage and store that fact for
the purposes of future calls to
Document.hasStorageAccess()
andrequestStorageAccess()
.
Assuming all of the requirements above are satisfied, Firefox will automatically grant
storage access to the requesting origin on up to a threshold number of first-party
sites in the current session for the duration of user's session, up to a maximum of 24
hours. After the requesting origin has exceeded the maximum allowable number of storage
access grants, any future call to requestStorageAccess()
during the same
browsing session will prompt the user.
The maximum number of concurrent storage access grants an origin can obtain is a
positive integer currently defined as one percent of the number of top-level sites
visited in the current session or 5, whichever is higher. The threshold is enforced on
the level of site, so for example two storage access grants for
foo.example.com
and bar.example.com
will only count as a
single exception against the limit.
At the time of a requestStorageAccess()
call, if the requesting origin has
storage access to...
...fewer sites than the maximum and has been interacted with as a first party in the last 30 days:
- The user is not prompted.
- The origin is given an ephemeral storage access grant for the current top-level site.
-
The number of sites the requesting origin has storage access to is incremented by
one.
- Note that this number is also incremented when automatic access grants are given through Firefox compatibility heuristics.
- The ephemeral storage access grant is:
- Invalidated at the end of the browser session.
- Not persisted to disk (e.g. will not persist if the browser crashes).
- Reset after 24 hours in the case of a long-running browser session.
...equal or more sites than the maximum or has not been interacted with as a first party in the last 30 days::
- The user is prompted
- If the user clicks "Allow" or "Allow on any site" the request is resolved.
- If the user clicks "Don't Allow", the storage access request is rejected and the requesting origin can re-request once it receives another user interaction.
- If the user allows storage the requesting origin is given a persistent storage access grant on the current top-level site.
- The number of sites the requesting origin has storage access to is incremented by one.
- The persistent storage access permission is:
- Persisted to disk and will remain valid in future browser sessions.
- Reset after 30 days.
When an ephemeral or persistent storage access grant expires, the number of sites the requesting origin has storage access to is decremented by one.
Note: If the requesting origin is not classified
as a tracking origin, the access request is automatically given an ephemeral
storage access grant, which will go away when the page is reloaded. The user is never
shown a prompt in this case, and calling requestStorageAccess()
won't
have any side effects besides changing the value returned by
Document.hasStorageAccess()
.
Debugging
The storage access grant threshold may make it more difficult to test your website
under the condition where Firefox prompts the user for access. To make testing easier,
we have added two preferences in about:config
that control prompting upon
requestStorageAccess()
calls:
-
dom.storage_access.auto_grants
can be set tofalse
to disable the automatic granting of ephemeral storage access grants. All calls torequestStorageAccess()
by origins classified as trackers will trigger a prompt. -
dom.storage_access.max_concurrent_auto_grants
controls the threshold number of storage access grants at which users will begin to receive prompts. For example, if you want to configure Firefox to automatically grant access on the first site whererequestStorageAccess()
is called and then prompt afterwards, you should adjust the value of thedom.storage_access.max_concurrent_auto_grants
preference to 1.
Syntax
var promise = document.requestStorageAccess();
Parameters
None.
Return value
A Promise
that fulfills with undefined
if the access to
first-party storage was granted, and rejects if access was denied.
When the promise gets resolved, the resolve handler will run as if a user gesture is being processed, whether the promise was fulfilled or rejected:
- In the former case, code can then start to call APIs that require user activation and things can move forward.
- In the latter case, code can run to inform the user of why the request failed and what they can do to continue (for example asking them to log in, if that is a requirement).
Examples
document.requestStorageAccess().then(
() => { console.log('access granted') },
() => { console.log('access denied') }
);
Specifications
The API is currently only at the proposal stage — the standardization process has yet to begin. You can currently find specification details of the API at Apple's Introducing Storage Access API blog post, and the Storage Access API proposal in the Privacy CG.
Browser compatibility
BCD tables only load in the browser