Skip to content

Commit

Permalink
feat: IPCメッセージのsenderを確認する (VOICEVOX#2151)
Browse files Browse the repository at this point in the history
* feat: IPCメッセージの`sender`を確認する

* Apply suggestions from code review

---------

Co-authored-by: Hiroshiba <[email protected]>
  • Loading branch information
sabonerune and Hiroshiba authored Jul 1, 2024
1 parent bf9fd0f commit 4ea08d1
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/backend/electron/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function ipcMainHandle(
...args: unknown[]
) => {
try {
validateIpcSender(event);
return listener(event, ...args);
} catch (e) {
log.error(e);
Expand All @@ -38,3 +39,20 @@ export function ipcMainSend(
): void {
return win.webContents.send(channel, ...args);
}

/** IPCメッセージの送信元を確認する */
const validateIpcSender = (event: IpcMainInvokeEvent) => {
let isValid: boolean;
const senderUrl = new URL(event.senderFrame.url);
if (process.env.VITE_DEV_SERVER_URL != undefined) {
const devServerUrl = new URL(process.env.VITE_DEV_SERVER_URL);
isValid = senderUrl.origin === devServerUrl.origin;
} else {
isValid = senderUrl.protocol === "app:";
}
if (!isValid) {
throw new Error(
`不正なURLからのIPCメッセージを検出しました。senderUrl: ${senderUrl.toString()}`,
);
}
};

0 comments on commit 4ea08d1

Please sign in to comment.