Search code examples
javascriptfilefirefox-addon-webextensionslocal-files

Security error in firefox ext while fetching a file


While fetching a files contents with the following code in a background.js file:

function readFile(filename) {
    let response = fetch(`file:///${filename}`);
    console.log(response);
}

I get the following error:

Security Error: Content at moz-extension://86d386b7-2904-441f-8dbe-47f0af9b6bfb/_generated_background_page.html may not load or link to file:///C:/Users/rando/Downloads/jest-26.6.1.zip.
TypeError: NetworkError when attempting to fetch resource.

My manifest file looks like this:

  "permissions": [
    "webNavigation",
    "webRequest",
    "webRequestBlocking",
    "cookies",
    "tabs",
    "http://*/*",
    "https://*/*",
    "ws://*/*",
    "wss://*/*",
    "storage",
    "downloads",
    "sessions",
    "file://*/*"
  ],
  "background": {
    "scripts": [
      "app/scripts/background.js"
    ],
    "persistent": true
  },

How do I solve this error and fetch the contents of the file using the background.js file?


Solution

  • Extension access to local file system file:/// has been blocked since Firefox 57 for security reasons.

    Extensions can use the input type="file" to launch the file picker on user-click.

    Extensions also can:

    • read files that are packed within the extension
    • communicate with a software on the local computer via Native messaging
    • get files via HTTP/s, if the localhost has a server running