11import * as Context from "effect/Context" ;
22import * as Effect from "effect/Effect" ;
33import * as Layer from "effect/Layer" ;
4+ import * as Option from "effect/Option" ;
45import * as Schema from "effect/Schema" ;
56import * as Scope from "effect/Scope" ;
67
8+ import * as ElectronWindow from "../electron/ElectronWindow.ts" ;
9+
710export interface DesktopIpcInvokeEvent {
811 readonly sender : { readonly id : number } ;
12+ readonly senderFrame ?: unknown ;
913}
1014
11- export interface DesktopIpcSyncEvent {
15+ export interface DesktopIpcSyncEvent extends DesktopIpcInvokeEvent {
1216 returnValue : unknown ;
1317}
1418
19+ export type DesktopIpcSenderGuard = ( event : DesktopIpcInvokeEvent ) => Effect . Effect < boolean > ;
20+
1521export type DesktopIpcHandleListener = (
1622 event : DesktopIpcInvokeEvent ,
1723 raw : unknown ,
@@ -52,6 +58,15 @@ export class DesktopIpcUnregistrationError extends Schema.TaggedError<DesktopIpc
5258 }
5359}
5460
61+ export class DesktopIpcUntrustedSenderError extends Schema . TaggedError < DesktopIpcUntrustedSenderError > ( ) (
62+ "DesktopIpcUntrustedSenderError" ,
63+ { channel : Schema . String } ,
64+ ) {
65+ override get message ( ) : string {
66+ return `Rejected ${ this . channel } from a sender that is not the app window.` ;
67+ }
68+ }
69+
5570export const DesktopIpcError = Schema . Union ( [
5671 DesktopIpcRegistrationError ,
5772 DesktopIpcUnregistrationError ,
@@ -80,7 +95,11 @@ export class DesktopIpc extends Context.Service<
8095 }
8196> ( ) ( "@t3tools/desktop/ipc/DesktopIpc" ) { }
8297
83- export const make = ( ipcMain : DesktopIpcMain ) : DesktopIpc [ "Service" ] =>
98+ // ipcMain is shared by every webContents, including preview guests that load arbitrary pages.
99+ export const make = (
100+ ipcMain : DesktopIpcMain ,
101+ isTrustedSender : DesktopIpcSenderGuard ,
102+ ) : DesktopIpc [ "Service" ] =>
84103 DesktopIpc . of ( {
85104 handle : Effect . fn ( "desktop.ipc.registerInvoke" ) ( function * < E , R > ( {
86105 channel,
@@ -98,6 +117,9 @@ export const make = (ipcMain: DesktopIpcMain): DesktopIpc["Service"] =>
98117 runPromise (
99118 Effect . gen ( function * ( ) {
100119 yield * Effect . annotateCurrentSpan ( { channel } ) ;
120+ if ( ! ( yield * isTrustedSender ( event ) ) ) {
121+ return yield * new DesktopIpcUntrustedSenderError ( { channel } ) ;
122+ }
101123 return yield * handler ( raw , event ) ;
102124 } ) . pipe ( Effect . annotateLogs ( { channel } ) , Effect . withSpan ( "desktop.ipc.invoke" ) ) ,
103125 ) ,
@@ -131,6 +153,11 @@ export const make = (ipcMain: DesktopIpcMain): DesktopIpc["Service"] =>
131153 event . returnValue = runSync (
132154 Effect . gen ( function * ( ) {
133155 yield * Effect . annotateCurrentSpan ( { channel } ) ;
156+ // Throwing here would surface as an uncaught main-process exception.
157+ if ( ! ( yield * isTrustedSender ( event ) ) ) {
158+ yield * Effect . logWarning ( "Rejected sync IPC from an untrusted sender." ) ;
159+ return undefined ;
160+ }
134161 return yield * handler ( ) ;
135162 } ) . pipe (
136163 Effect . annotateLogs ( { channel } ) ,
@@ -152,7 +179,28 @@ export const make = (ipcMain: DesktopIpcMain): DesktopIpc["Service"] =>
152179 } ) ,
153180 } ) ;
154181
155- export const layer = ( ipcMain : DesktopIpcMain ) => Layer . succeed ( DesktopIpc , make ( ipcMain ) ) ;
182+ export const layer = ( ipcMain : DesktopIpcMain ) =>
183+ Layer . effect (
184+ DesktopIpc ,
185+ Effect . gen ( function * ( ) {
186+ const electronWindow = yield * ElectronWindow . ElectronWindow ;
187+ return make ( ipcMain , ( event ) =>
188+ Effect . map (
189+ electronWindow . main ,
190+ ( main ) =>
191+ Option . isSome ( main ) &&
192+ main . value . webContents . id === event . sender . id &&
193+ main . value . webContents . mainFrame === event . senderFrame ,
194+ ) ,
195+ ) ;
196+ } ) ,
197+ ) ;
198+
199+ export const layerTest = ( ipcMain : DesktopIpcMain ) =>
200+ Layer . succeed (
201+ DesktopIpc ,
202+ make ( ipcMain , ( ) => Effect . succeed ( true ) ) ,
203+ ) ;
156204
157205export interface DesktopIpcMethodRegistration <
158206 Payload ,
0 commit comments