electron: [Bug]: Missing permissions dialog for FileSystem API.

Preflight Checklist

Electron Version

12.0.0

What operating system are you using?

Ubuntu

Operating System Version

Linux meerkat 5.8.0-7642-generic #47~1614007149~20.10~82fb226-Ubuntu SMP Tue Feb 23 02:59:01 UTC x86_64 x86_64 x86_64 GNU/Linux

What arch are you using?

x64

Last Known Working Electron version

n/a

Expected Behavior

When using the File System Access API, attempting to write to a file using the FileSystemWritableFileStream API should cause a permission prompt to pop up that the user can accept or deny.

In Electron’s case, it should probably trigger the setPermissionRequestHandler handler with a permission set to something like filesystem and a path to the file being requested.

Actual Behavior

When invoking fileHandle.createWritable(), the following error gets thrown:

Uncaught DOMException: The request is not allowed by the user agent or the platform in the current context.

Testcase Gist URL

https://github.com/RangerMauve/electron-filesystem-prompt-example

Continuation of #23608

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 18
  • Comments: 40 (7 by maintainers)

Commits related to this issue

Most upvoted comments

This issue is still relevant!

Just commenting so that the team knows another person would like to see the File System Access API to be supported by electron.

Hello from Electron 25.0.1. Not stale. 😃

I also need support for the File System Access API (aka Native File System API). However, we don’t need the permissions dialogue to show, since we have File Access in Electron apps. We simply need the system to grant access automatically to read the filesystem if we have the file or directory handle (which can be serialized to IndexedDB and re-used across app launches)

Hey folks - i’ve opened a draft PR. Feedback welcome.

Making some progress:

Screenshot 2024-02-22 at 5 28 26 PM

Just commenting so that the team knows another person would like to see the File System Access API to be supported by electron. Not only do I need to complicate my code base by having BOTH Electrons API and File System Access API, but Electrons dialog API does not provide an easy way to create a Writable File Stream as the File System Access API does. I am able to use the File System Access API for saving/creating a new file, but I get permissions issues when I try to use the filesystem handler that gets returned.

Hoping for support in the future!

I tried Electron 16.0.4 version in my mac: 12.0.1, Must add below line to enable the experimental feature

app.commandLine.appendSwitch("enable-experimental-web-platform-features");

then the permission will alway be true (won’t prompt the window)

This is still an issue in Electron 20

Still an isssu in 23+

I’ve just tested with 15.3.0 on Windows 11, and the bug is still unresolved. You can use the File System Access API to open and read the contents of a directory, and to pick a single file, and that all works well, but as soon as you try to use the filesystem handle that you serialized to indexedDB, the access is denied and no verify prompt is issued.

The way NWJS does it is simply always to return true when you run fileHandle.queryPermissions(). No prompt is shown, access is simply granted. The beauty of this is that you don’t need to code special use of Node APIs, you can simply code once, and it’ll work both in a PWA and in Electron, with the latter acting the same way as if you had used Node APIs.

The only workaround is to use Node APIs to get the path to a file or folder, and store that instead of the fileSystemHandle. But this is clunky.

Unfortunately in Electron 30.0.1 this seems broken. Even using “enable-experimental-web-platform-features” now no longer enables the user to open a file automatically on opening the app. Console.log shows DOMException: Failed to execute 'requestPermission' on 'FileSystemHandle': User activation is required to request permissions.

This is now the worst of both worlds. The Permissions dialogue doesn’t show AND access is blocked. Prior to this (at least in 22.3.25, I didn’t test releases in-between) we could bypass all such permissions by adding the “enable-experimental-web-platform-features” option.

To be clear, if a user provides a gesture, then access is granted. But as we don’t get the Persistent Permissions prompt (no prompt at all), this means that auto-opening of a file on startup is now blocked!

@codebytere Can you re-open? (EDIT: ignore, see below)

It’s good to know! Unfortunately for me, I already worked around this by using Electron APIs, complicating my codebase when I was hoping to simplify it. I’ll definitely look to revert at some point to the File System Access API: it’s much better IMHO.

Still an issue on v13 (13.1.9) and v14 (14.0.0), tested on Windows 10

@ckerr Can you kindly add 15-x-y label to this issue, and also add platform/windows or platform/all, because it relates to a Chromium API that is not properly supported and the issue is present on Windows and Linux at least (I confirm the bug persists on both OS’s with 15.3.0).

hey @Jaifroid - yes, the API added doesn’t support Persistent Permissions. I can definitely look to address this is a follow-up but it would involve far more considerations and design decisions than the original PR which is why it was not included in the original PR’s scope

So, I added this code to main.cjs:

// Set default File System Access permissions
mainWindow.webContents.session.setPermissionRequestHandler((webContents, permission, callback, details) => {
    console.log('Setting permission for: ' + permission); // 'fileSystem'
    console.log('File System Access details:', details); 
    callback(true);
});

It is called when I pick a file. But it is not called when I attempt to access the file on startup of the app.

On startup, I retrieve the file handle and call fileHandle.queryPermission(opts). Instead of going through the broker in main.cjs, this merely returns ‘prompt’ instead of ‘granted’ for the permission. When I try to do fileHandle.requestPermission(opts) the broker isn’t called either, and I get the error DOMException: Failed to execute 'requestPermission' on 'FileSystemHandle': User activation is required to request permissions..

@codebytere Is this a known issue? Is there a workaround? Should I open a separate issue?

Edit: from Chrome 122 onwards there are Persistent Permissions, but there doesn’t seem to be a way to engage with these and grant permanent permission.

PS I don’t want to sound ungrateful for the work done and for addressing this long-standing issue. It’s great that we can now explicitly set the behaviour. Persistent permissions are very recent. It seems to me that the easiest way of solving would be to bypass the user gesture block as happened with “enable-experimental-web-platform-features”. Or else explicit support could be added to the existing broker for persistent permissions.

Not a solution, just a workaround: it’s possible to write your own polyfill for showSaveFilePicker using ipc and Node’s fs API – a simple example in https://gist.github.com/zbynek/f0d3bca71db9cbfd9ec30873ab04a45e .

Leaving another comment because I’ve been credited twice now for the workaround which @altjz mentioned here: https://github.com/electron/electron/issues/28422#issuecomment-987504138. Can’t believe this API is still locked behind experimental web platform features after all this time.

I only need to be able to read files. @RayDawn’s tip above, to use app.commandLine.appendSwitch("enable-experimental-web-platform-features"); works very well for my purpose. Obviously it would be good if someone else could verify whether this also enables writing to picked files (and re-using a serialized file handle). There is no permissions dialogue, by the way, it simply returns permission granted as if the user had given permission. It works (for reading) on picked files and picked folders.

I tried Electron 16.0.4 version in my mac: 12.0.1, Must add below line to enable the experimental feature

app.commandLine.appendSwitch("enable-experimental-web-platform-features");

then the permission will alway be true (won’t prompt the window)

Holy hell this was a lifesaver. I’ve been searching for what feels like ages, I was beginning to think I was the only person having this problem.

To add to this: Verified that this is still a problem on Electron version 17.0.0, platform is macOS.