Skip to content

Clarifying SecurityError in StorageManager.getDirectory #39859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 18, 2025

Conversation

mpsuzuki
Copy link
Contributor

@mpsuzuki mpsuzuki commented Jun 8, 2025


Description

Add concrete examples of the error in OPFS getDirectory() method, at StorageManager.getDirectory#Exceptions

Motivation

Currently, the documentation states:

SecurityError DOMException
  Thrown if the user agent is not able to map the requested directory to the local OPFS.

I propose adding concrete scenarios that cause this exception, as the lack of detail may lead to confusion for developers implementing OPFS-based storage, like:

SecurityError DOMException
  Thrown if the user agent is not able to map the requested directory to the local OPFS, due to factors such as:
    * Storage or memory constraints that prevent OPFS allocation.
    * Security policies, including restrictions in non-secure contexts (e.g., non-HTTPS sessions).
    * Private browsing mode, where OPFS access is commonly restricted by browsers to prevent persistent data storage.

Additional details

SQLite developers have specifically highlighted OPFS limitations in Incognito and Guest modes in their documentation: Achtung: Restrictions in Incognito and Guest Browsing Modes.

In real-world tests, both Firefox and Safari threw a SecurityError when calling getDirectory() inside Private Browsing mode:

Firefox 128.11.0 error: "Security error when calling GetDirectory"

Safari 17.6 error: "UnknownError: The operation failed for an unknown transient reason (e.g. out of memory)"

While browsers intentionally obscure Private Browsing detection, a small note in MDN can help developers anticipate storage failures in such contexts without directly exposing detection mechanisms.

Would it be possible to add a small clarification about these known limitations to better support developers encountering this issue?

Related issues and pull requests

@mpsuzuki mpsuzuki requested a review from a team as a code owner June 8, 2025 10:17
@mpsuzuki mpsuzuki requested review from wbamberg and removed request for a team June 8, 2025 10:17
@github-actions github-actions bot added Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed labels Jun 8, 2025
@mpsuzuki mpsuzuki force-pushed the pr-describe-errors-in-OPFS-getDirectory branch 7 times, most recently from c7acaec to 14f1dc1 Compare June 10, 2025 16:11
Copy link
Contributor

github-actions bot commented Jun 11, 2025

Preview URLs

(comment last updated: 2025-06-18 15:15:59)

Copy link
Collaborator

@wbamberg wbamberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR. I had a couple of questions about some of the cases you have listed.

Also, do you have any references from the specification about when these errors are expected to be raised?

- : Thrown if the user agent is not able to map the requested directory to the local OPFS, due to factors such as:
- **Storage or memory constraints** that prevent OPFS allocation.
- **Security policies**, including restrictions in non-secure contexts (e.g., non-HTTPS sessions).
- **Private browsing mode**, where OPFS access is commonly restricted by browsers to prevent persistent data storage.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that the API doesn't work in private browsing mode? Our documentation on storage quotas and persistence (https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria#private_browsing) says that in PB mode data is not persisted after the session ends, but not that the API is itself disabled. Do you have a test case where we can try this out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My testcase was found during my experiments with SQLite3 WASM, https://gitlab.com/mpsuzuki/test-sqlite3-wasm. I would update this repository by this weekend, to add local https servers for test.

In my experiment, the Private Browsing mode on Google Chrome and Microsoft Edge allow the clients to write something to OPFS. On the other hand, the Private Browsing mode on Firefox and Apple Safari disallow the clients to write anything on OPFS.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks to me like, yes, Firefox throws when you call this method in PB mode. But in Safari, it looks like navigator.storage is just unavailable:

Screen Shot 2025-06-11 at 10 40 13 AM

So I'm not seeing this (from https://gitlab.com/mpsuzuki/test-sqlite3-wasm#known-limitations):

For example, in the Private Browsing mode, ...Apple Safari returns "Unhandled Promise Rejection: UnknownError: The operation failed for an unknown transient reason (e.g. out of memory)".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we document browser-specific behavior in the browser compatibility table, so we could have a note attached to the Firefox entry for getDirectory saying something like "Throws a SecurityError in private browsing mode".

Copy link
Contributor Author

@mpsuzuki mpsuzuki Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Listing the failures & errors in each browser is ideal, I fully agree, but I'm afraid it is too version-specific and the maintenance can be heavy task.

For example, my Safari 18.5 running on Ventura (macOS 13.7.6) shows the errors like below for my test case. The screenshot is taken by Japanese language environment (I'm sorry), "プライベート" means "private", "バージョン" means "version". navigator.storage is available under the Private Browsing mode. Which version of your Safari?

nsmail

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just I've updated my testkit test-sqlite3-wasm, HTTPS & HTTP servers are added.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can confirm that I get an error on Safari 18.4. But it's not a SecurityError, it's UnknownError:

Screenshot 2025-06-17 at 8 32 25 PM

It is a SecurityError in Firefox.

@github-actions github-actions bot added the size/xs [PR only] 0-5 LoC changed label Jun 12, 2025
@mpsuzuki mpsuzuki force-pushed the pr-describe-errors-in-OPFS-getDirectory branch 8 times, most recently from 288a2a8 to 1feb698 Compare June 17, 2025 01:30
@mpsuzuki
Copy link
Contributor Author

@wbamberg , please could you tell me about the tasks unresolved in my side?

@mpsuzuki mpsuzuki force-pushed the pr-describe-errors-in-OPFS-getDirectory branch from 1feb698 to d37c61b Compare June 18, 2025 03:07
mpsuzuki and others added 3 commits June 18, 2025 15:51
The requirement of the SSL session is
already documented at the beginning of
OPFS documentation. So no need to repeat.
Suggested by wbambarg@github.
Co-authored-by: wbamberg <will@bootbonnet.ca>
@mpsuzuki mpsuzuki force-pushed the pr-describe-errors-in-OPFS-getDirectory branch from 58086ea to 76e3b91 Compare June 18, 2025 06:51
Copy link
Collaborator

@wbamberg wbamberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thank you!

@wbamberg wbamberg merged commit 7fad234 into mdn:main Jun 18, 2025
8 checks passed
@mpsuzuki
Copy link
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed size/xs [PR only] 0-5 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants