From 869b3ee233449761e8ca9c8e5186a8b0355ba286 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 19 Aug 2024 21:42:24 +0200 Subject: [PATCH 1/5] fix(electron): sandbox for production env --- .github/workflows/node.yml | 13 ++++++----- .gitignore | 5 +++- .vscode/launch.json | 3 ++- package.json | 33 +++++++++++++++------------ src/main/index.ts | 2 +- src/preload/index.ts | 22 ++++++++++-------- src/renderer/src/App.vue | 10 ++++---- vitest.config.js => vitest.config.mjs | 0 8 files changed, 49 insertions(+), 39 deletions(-) rename vitest.config.js => vitest.config.mjs (100%) diff --git a/.github/workflows/node.yml b/.github/workflows/node.yml index 20389bf..7d75f23 100644 --- a/.github/workflows/node.yml +++ b/.github/workflows/node.yml @@ -25,17 +25,18 @@ jobs: fail-fast: true max-parallel: 4 matrix: - node-version: ['latest'] + version: ['latest'] os: [macos-latest, ubuntu-latest, windows-latest] steps: - name: Checkout Code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup Node.js - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: - node-version: ${{ matrix.node-version }} + node-version: ${{ matrix.version }} + cache: npm - name: Install Dependencies run: npm install --force @@ -74,7 +75,7 @@ jobs: - name: Upload Artifact uses: actions/upload-artifact@v3 with: - name: release_on_${{ matrix.os }}_node_${{ matrix.node-version }} + name: release_on_${{ matrix.os }}_node_${{ matrix.version }} path: dist/ retention-days: 62 if-no-files-found: warn @@ -82,5 +83,5 @@ jobs: - name: Download Artifact uses: actions/download-artifact@v3 with: - name: release_on_${{ matrix.os }}_node_${{ matrix.node-version }} + name: release_on_${{ matrix.os }}_node_${{ matrix.version }} path: dist/ diff --git a/.gitignore b/.gitignore index 01a25ef..8d18597 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,7 @@ backlog.md out # examples -examples \ No newline at end of file +examples + +# owasp dependency-check-report +dependency-check-report.html \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index a377561..3a987e1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -14,7 +14,8 @@ "--sourcemap" ], "env": { - "REMOTE_DEBUGGING_PORT": "9222" + "REMOTE_DEBUGGING_PORT": "9222", + "NODE_ENV": "development", } }, { diff --git a/package.json b/package.json index aaaa78a..6346787 100644 --- a/package.json +++ b/package.json @@ -18,19 +18,21 @@ "typecheck:node": "tsc --noEmit -p tsconfig.node.json --composite false", "typecheck:web": "vue-tsc --noEmit -p tsconfig.web.json --composite false", "typecheck": "npm run typecheck:node && npm run typecheck:web", - "start": "electron-vite preview", - "dev": "electron-vite dev", - "build": "npm run typecheck && electron-vite build", - "postinstall": "electron-builder install-app-deps", + "dev:env": "cross-env NODE_ENV=development DEBUG=true", + "dev": "npm run dev:env && electron-vite dev", + "build:env": "cross-env NODE_ENV=production", + "build": "npm run typecheck && npm run build:env && electron-vite build", + "start": "npm run build:env && electron-vite preview", "build:win": "npm run build && electron-builder --win --config", "build:mac": "npm run build && electron-builder --mac --config", - "build:linux": "npm run build && electron-builder --linux --config" + "build:linux": "npm run build && electron-builder --linux --config", + "postinstall": "electron-builder install-app-deps" }, "dependencies": { "@electron-toolkit/preload": "^3.0.1", "@electron-toolkit/utils": "^3.0.0", "electron-updater": "^6.2.1", - "vuetify": "^3.6.13", + "vuetify": "^3.7.0", "xml2js": "^0.6.2" }, "devDependencies": { @@ -39,24 +41,25 @@ "@electron-toolkit/tsconfig": "^1.0.1", "@mdi/font": "^7.4.47", "@rushstack/eslint-patch": "^1.10.4", - "@types/node": "^22.0.2", + "@types/node": "^22.4.1", "@types/xml2js": "^0.4.14", - "@typescript-eslint/eslint-plugin": "^8.0.0", - "@vitejs/plugin-vue": "^5.1.1", + "@typescript-eslint/eslint-plugin": "^8.2.0", + "@vitejs/plugin-vue": "^5.1.2", "@vue/eslint-config-prettier": "^9.0.0", "@vue/eslint-config-typescript": "^13.0.0", - "electron": "^31.3.0", + "cross-env": "^7.0.3", + "electron": "^31.4.0", "electron-builder": "^24.13.3", "electron-vite": "^2.3.0", - "eslint": "^8.57.0", + "eslint": "^9.9.0", "eslint-plugin-vue": "^9.27.0", "less": "^4.2.0", "prettier": "^3.3.3", "typescript": "^5.5.4", - "vite": "^5.3.5", - "vite-tsconfig-paths": "^4.3.2", - "vitest": "^2.0.4", - "vue": "^3.4.34", + "vite": "^5.4.1", + "vite-tsconfig-paths": "^5.0.1", + "vitest": "^2.0.5", + "vue": "^3.4.38", "vue-tsc": "^2.0.29" }, "license": "MIT", diff --git a/src/main/index.ts b/src/main/index.ts index a59546c..b6f40d5 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -14,7 +14,7 @@ function createWindow(): void { autoHideMenuBar: false, webPreferences: { preload: resolve(__dirname, '../preload/index.js'), - sandbox: false, + sandbox: process.env.NODE_ENV === 'production', disableBlinkFeatures: 'Auxclick', webSecurity: true, nodeIntegration: false, diff --git a/src/preload/index.ts b/src/preload/index.ts index cf0f57f..04588d8 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -1,5 +1,4 @@ import { contextBridge, ipcRenderer } from 'electron' -import { electronAPI } from '@electron-toolkit/preload' import { ConfigData } from '../types/ConfigData' import ApiResponses from '../enums/ApiResponses' @@ -25,23 +24,28 @@ const api: RendererAPI = { onSaveFileDialog: (data: ConfigData) => ipcRenderer.invoke('save-file-dialog', data), onSetAnnotation: (data: { channelId: string; annotation: string }) => ipcRenderer.invoke('set-annotation', data), - resetData: () => ipcRenderer.invoke('reset-data'), + resetData: () => ipcRenderer.invoke('reset-data') } // Use `contextBridge` APIs to expose Electron APIs to // renderer only if context isolation is enabled, otherwise // just add to the DOM global. +console.log(process.env.NODE_ENV) if (process.contextIsolated) { try { - contextBridge.exposeInMainWorld('electron', electronAPI) + // Expose APIs conditionally based on the environment + if (process.env.NODE_ENV === 'development' || process.env.DEBUG) { + import('@electron-toolkit/preload') + .then((module) => { + const electronAPI = module.electronAPI + contextBridge.exposeInMainWorld('electron', electronAPI) + }) + .catch((err) => { + console.error('Failed to load electronAPI in development mode:', err) + }) + } contextBridge.exposeInMainWorld('api', api) } catch (error) { console.error(error) } -} else { - // Fallback for older Electron versions or when context isolation is disabled. - // @ts-ignore (define in dts) - window.electron = electronAPI - // @ts-ignore (define in dts) - window.api = api } diff --git a/src/renderer/src/App.vue b/src/renderer/src/App.vue index 9d013c2..316e5f1 100644 --- a/src/renderer/src/App.vue +++ b/src/renderer/src/App.vue @@ -75,17 +75,15 @@ const handleSelectNone = (): void => { - All - + /> - None - + />