electron: Extensions don't work with file:// protocol since 9.0.0

Extracted from #23662, https://github.com/electron/electron/issues/23662#issuecomment-632344005, per https://github.com/electron/electron/issues/23662#issuecomment-637077040 (cc @nornagon).

Extensions get loaded for http:// and https:// protocols, but don’t work for file:// protocol, which is commonly used.

This also affects devtools extensions.

Appears to be an undocumented regression from 8.x.

Chrome by default behaves the same, but it has an Allow access to file URLs switch (the fileAccess flag) on extension page at chrome://extensions/, which enables the extension to run on file:// urls (which can be verified on Content scripts tab). Any hint how to do that in Electron? React-devtools works on file:// protocol in Chrome with that flag enabled, but does not work in Electron since 9.0.0, as there are no ways to toggle that setting.

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version:
    • 9.0.0, 9.0.2
  • Operating System:
    • Linux xps 5.6.14-arch1-1 #1 SMP PREEMPT Wed, 20 May 2020 20:43:19 +0000 x86_64 GNU/Linux
  • Last Known Working Electron version:
    • 8.3.1

Expected Behavior

Installed extensions should load for file:// protocol automatically, or/and there should be an opt-in/out-out for that (like opt-in in Chrome).

Extension content script present in Content scripts tab.

Actual Behavior

Extensions are not loaded if the for pages requested over file:// protocol.

No extension content script present in Content scripts tab.

To Reproduce

index.js:

const { app, session, BrowserWindow } = require('electron');
const path = require('path');

(async () => {
  await app.whenReady()
  await session.defaultSession.loadExtension(path.join(__dirname, 'extensions/test'))
  const bw = new BrowserWindow({webPreferences: {sandbox: true}})
  //await bw.loadURL('about:blank')
  //await bw.loadURL('https://example.org/')
  await bw.loadURL(`file://${path.resolve('simple.html')}`)
  bw.show()
  bw.openDevTools()
})()

Put anything in simple.html.

Example extension (but you can use any other, e.g. https://github.com/electron/electron/tree/master/spec-main/fixtures/extensions/chrome-api or react-devtools)

extensions/test/main.js

console.log('extension loaded')

extensions/test/manifest.json

{
  "name": "test",
  "version": "1.0",
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["main.js"],
      "run_at": "document_start"
    }
  ],
  "permissions": [ "file:///*", "http://*/*", "https://*/*" ],
  "manifest_version": 2
}

Screenshots

https://: Screenshot_20200608_142638

file://: Screenshot_20200608_142102

Additional Information

This breaks devtools extension and is a part (if not the main reason) behind #23662.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 41
  • Comments: 24 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Great that this is being worked on! What’s the current status?

By looking at the historical duration between major releases I get the impression that Electron 11 isn’t too far away, which will result in Electron 8, the last version with this working, becoming unsupported. This is a blocker for me and it would be nice to not have to use an unsupported version after Electron 11 has been released.

Incredibly interested in this fix! Thank you @ChALkeR for working on it (and, of course, @nornagon for all of your work on the Chrome Extension API).

Thank you @nornagon!!

Looking forward to a fix. Currently writing an unfair amount of scripts to compensate for this issue. Glad It’s being worked on.

@RangerMauve I’d probably start by searching for kFileScheme in the extensions code: https://source.chromium.org/search?q=kFileScheme f:extensions

From here it looks like maybe if the extension requests access to file:// urls it might be allowed? There’s also a constant called Extension::ALLOW_FILE_ACCESS that looks like it might be relevant.

Hopefully that’s enough to get you started!

I’d like to take a shot at this. Is there somewhere in the code where there’s a decision being made on whether a particular content script should get injected or not?

Yep, “store not found” with Redux DevTools. https://github.com/electron/electron/issues/24638

I would be happy to sponsor this work, if someone thinks they can fix it.

@ChALkeR - any chance this fix might be finished before the next 10.X. series release? It’s a blocker for us.

Thank you!