Skip to content
Snippets Groups Projects
security.js 1.04 KiB
Newer Older
  • Learn to ignore specific revisions
  • James Long's avatar
    James Long committed
    const electron = require('electron');
    
    
    electron.app.on('web-contents-created', function (event, contents) {
    
      contents.on('will-attach-webview', function (event, webPreferences) {
    
    James Long's avatar
    James Long committed
        delete webPreferences.preloadURL;
        delete webPreferences.preload;
    
        webPreferences.nodeIntegration = false;
        webPreferences.webSecurity = true;
        webPreferences.allowRunningInsecureContent = false;
        webPreferences.experimentalFeatures = false;
        webPreferences.enableBlinkFeatures = false;
    
        // For now, we never use <webview>. Just disable it entirely.
        event.preventDefault();
      });
    
    
      contents.on('will-navigate', event => {
    
    James Long's avatar
    James Long committed
        event.preventDefault();
      });
    
    
      contents.on('new-window', event => {
    
    James Long's avatar
    James Long committed
        event.preventDefault();
      });
    });
    
    
    electron.app.on('ready', function () {
    
      electron.session.defaultSession.setPermissionRequestHandler(
        function (webContents, permission, callback) {
          const url = webContents.getURL();
          if (url.startsWith('file://')) {
            callback(true);
          } else {
            callback(false);
          }
        },
      );
    
    James Long's avatar
    James Long committed
    });