浏览代码

chore: add allowed protocols for openExternal

Konstantinos Kaloutas 3 年之前
父节点
当前提交
d238c88afd
共有 1 个文件被更改,包括 6 次插入0 次删除
  1. 6 0
      resources/js/preload.js

+ 6 - 0
resources/js/preload.js

@@ -5,6 +5,8 @@ const { ipcRenderer, contextBridge, shell, clipboard, webFrame } = require('elec
 const IS_MAC = process.platform === 'darwin'
 const IS_MAC = process.platform === 'darwin'
 const IS_WIN32 = process.platform === 'win32'
 const IS_WIN32 = process.platform === 'win32'
 
 
+const ALLOWED_EXTERNAL_PROTOCOLS = ['https:', 'http:', 'mailto:']
+
 function getFilePathFromClipboard () {
 function getFilePathFromClipboard () {
   if (IS_WIN32) {
   if (IS_WIN32) {
     const rawFilePath = clipboard.read('FileNameW')
     const rawFilePath = clipboard.read('FileNameW')
@@ -63,6 +65,10 @@ contextBridge.exposeInMainWorld('apis', {
   },
   },
 
 
   async openExternal (url, options) {
   async openExternal (url, options) {
+    const protocol = new URL(url).protocol
+    if (!ALLOWED_EXTERNAL_PROTOCOLS.includes(protocol)) {
+      throw new Error('illegal protocol')
+    }
     await shell.openExternal(url, options)
     await shell.openExternal(url, options)
   },
   },