@@ -10,7 +10,8 @@ const { ipcMain, dialog, BrowserWindow } = require('electron');
1010const path = require ( 'path' ) ;
1111const fsp = require ( 'fs/promises' ) ;
1212const os = require ( 'os' ) ;
13- const { identifier : APP_IDENTIFIER } = require ( './package.json' ) ;
13+ const { identifier : APP_IDENTIFIER } = require ( './config' ) ;
14+ const { assertTrusted } = require ( './ipc-security' ) ;
1415
1516// Electron IPC only preserves Error.message when errors cross the IPC boundary (see
1617// https://github.com/electron/electron/issues/24427). To preserve error.code for FS
@@ -47,25 +48,32 @@ function getAppDataDir() {
4748
4849function registerFsIpcHandlers ( ) {
4950 // Directory APIs
50- ipcMain . handle ( 'get-documents-dir' , ( ) => {
51+ ipcMain . handle ( 'get-documents-dir' , ( event ) => {
52+ assertTrusted ( event ) ;
5153 // Match Tauri's documentDir which ends with a trailing slash
5254 return path . join ( os . homedir ( ) , 'Documents' ) + path . sep ;
5355 } ) ;
5456
55- ipcMain . handle ( 'get-home-dir' , ( ) => {
57+ ipcMain . handle ( 'get-home-dir' , ( event ) => {
58+ assertTrusted ( event ) ;
5659 // Match Tauri's homeDir which ends with a trailing slash
5760 const home = os . homedir ( ) ;
5861 return home . endsWith ( path . sep ) ? home : home + path . sep ;
5962 } ) ;
6063
61- ipcMain . handle ( 'get-temp-dir' , ( ) => {
64+ ipcMain . handle ( 'get-temp-dir' , ( event ) => {
65+ assertTrusted ( event ) ;
6266 return os . tmpdir ( ) ;
6367 } ) ;
6468
65- ipcMain . handle ( 'get-app-data-dir' , ( ) => getAppDataDir ( ) ) ;
69+ ipcMain . handle ( 'get-app-data-dir' , ( event ) => {
70+ assertTrusted ( event ) ;
71+ return getAppDataDir ( ) ;
72+ } ) ;
6673
6774 // Get Windows drive letters (returns null on non-Windows platforms)
68- ipcMain . handle ( 'get-windows-drives' , async ( ) => {
75+ ipcMain . handle ( 'get-windows-drives' , async ( event ) => {
76+ assertTrusted ( event ) ;
6977 if ( process . platform !== 'win32' ) {
7078 return null ;
7179 }
@@ -86,26 +94,30 @@ function registerFsIpcHandlers() {
8694
8795 // Dialogs
8896 ipcMain . handle ( 'show-open-dialog' , async ( event , options ) => {
97+ assertTrusted ( event ) ;
8998 const win = BrowserWindow . fromWebContents ( event . sender ) ;
9099 const result = await dialog . showOpenDialog ( win , options ) ;
91100 return result . filePaths ;
92101 } ) ;
93102
94103 ipcMain . handle ( 'show-save-dialog' , async ( event , options ) => {
104+ assertTrusted ( event ) ;
95105 const win = BrowserWindow . fromWebContents ( event . sender ) ;
96106 const result = await dialog . showSaveDialog ( win , options ) ;
97107 return result . filePath ;
98108 } ) ;
99109
100110 // FS operations
101111 ipcMain . handle ( 'fs-readdir' , async ( event , dirPath ) => {
112+ assertTrusted ( event ) ;
102113 return fsResult (
103114 fsp . readdir ( dirPath , { withFileTypes : true } )
104115 . then ( entries => entries . map ( e => ( { name : e . name , isDirectory : e . isDirectory ( ) } ) ) )
105116 ) ;
106117 } ) ;
107118
108119 ipcMain . handle ( 'fs-stat' , async ( event , filePath ) => {
120+ assertTrusted ( event ) ;
109121 return fsResult (
110122 fsp . stat ( filePath ) . then ( stats => ( {
111123 isFile : stats . isFile ( ) ,
@@ -122,12 +134,30 @@ function registerFsIpcHandlers() {
122134 ) ;
123135 } ) ;
124136
125- ipcMain . handle ( 'fs-mkdir' , ( event , dirPath , options ) => fsResult ( fsp . mkdir ( dirPath , options ) ) ) ;
126- ipcMain . handle ( 'fs-unlink' , ( event , filePath ) => fsResult ( fsp . unlink ( filePath ) ) ) ;
127- ipcMain . handle ( 'fs-rmdir' , ( event , dirPath , options ) => fsResult ( fsp . rm ( dirPath , options ) ) ) ;
128- ipcMain . handle ( 'fs-rename' , ( event , oldPath , newPath ) => fsResult ( fsp . rename ( oldPath , newPath ) ) ) ;
129- ipcMain . handle ( 'fs-read-file' , ( event , filePath ) => fsResult ( fsp . readFile ( filePath ) ) ) ;
130- ipcMain . handle ( 'fs-write-file' , ( event , filePath , data ) => fsResult ( fsp . writeFile ( filePath , Buffer . from ( data ) ) ) ) ;
137+ ipcMain . handle ( 'fs-mkdir' , ( event , dirPath , options ) => {
138+ assertTrusted ( event ) ;
139+ return fsResult ( fsp . mkdir ( dirPath , options ) ) ;
140+ } ) ;
141+ ipcMain . handle ( 'fs-unlink' , ( event , filePath ) => {
142+ assertTrusted ( event ) ;
143+ return fsResult ( fsp . unlink ( filePath ) ) ;
144+ } ) ;
145+ ipcMain . handle ( 'fs-rmdir' , ( event , dirPath , options ) => {
146+ assertTrusted ( event ) ;
147+ return fsResult ( fsp . rm ( dirPath , options ) ) ;
148+ } ) ;
149+ ipcMain . handle ( 'fs-rename' , ( event , oldPath , newPath ) => {
150+ assertTrusted ( event ) ;
151+ return fsResult ( fsp . rename ( oldPath , newPath ) ) ;
152+ } ) ;
153+ ipcMain . handle ( 'fs-read-file' , ( event , filePath ) => {
154+ assertTrusted ( event ) ;
155+ return fsResult ( fsp . readFile ( filePath ) ) ;
156+ } ) ;
157+ ipcMain . handle ( 'fs-write-file' , ( event , filePath , data ) => {
158+ assertTrusted ( event ) ;
159+ return fsResult ( fsp . writeFile ( filePath , Buffer . from ( data ) ) ) ;
160+ } ) ;
131161}
132162
133163module . exports = {
0 commit comments