Skip to content

Conversation

@sabonerune
Copy link
Contributor

内容

セキュリティのベストプラクティスに基づきリモートコンテンツからのセッション権限リクエストのハンドリングを行います。

5. リモートコンテンツからのセッション権限リクエストのハンドリング

その他

とりあえず開発時はオリジンがVITE_DEV_SERVER_URLのオリジンと一致している場合、リリース時はappプロトコルの場合は無条件にリクエストを許可し、それ以外は全て拒否しています。
もっと安全にするなら権限をホワイトリスト形式で許可するべきかもしれません。

@voicevox-preview-pages
Copy link

voicevox-preview-pages bot commented Sep 23, 2025

🚀 プレビュー用ページを作成しました 🚀

更新時点でのコミットハッシュ:c7b121e

const parsedUrl = new URL(webContents.getURL());
const parsedRequestingUrl = new URL(requestingUrl);
let isAppUrl: boolean;
if (import.meta.env.VITE_DEV_SERVER_URL != undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import.meta.env.DEVのほうが適していると思います。
一応これはDead Code Eliminationで消えると思いますが、まぁ一応...

Suggested change
if (import.meta.env.VITE_DEV_SERVER_URL != undefined) {
if (import.meta.env.DEV) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

型定義上import.meta.env.VITE_DEV_SERVER_URL != undefinedは消せないのですよね。
そうなると冗長な気がします。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ん~、となるとassertNotNull(import.meta.env.VITE_DEV_SERVER_URL)を使ってもいいかも?DEVかつVITE_DEV_SERVER_URL == undefinedはunreachableなのでそれを防げますし。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

たぶん要するにこういうことですよね:

Suggested change
if (import.meta.env.VITE_DEV_SERVER_URL != undefined) {
if (import.meta.env.DEV != undefined) {
assertNonNullable(import.meta.env.VITE_DEV_SERVER_URL)

assertNonNullableはこれ↓
https://github.com/VOICEVOX/voicevox/blob/d2a9ebf67267b1ce79a6840fe0519193a16dcc04/src/type/utility.ts#L14C17-L14C34

@Hiroshiba Hiroshiba requested a review from Copilot September 27, 2025 09:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements security best practices by adding a handler to manage session permission requests from remote content in the Electron application. The handler follows Electron's security recommendations to prevent unauthorized access to system permissions.

  • Adds a permission request handler that filters requests based on origin/protocol
  • Implements different logic for development (VITE_DEV_SERVER_URL origin) vs production (app: protocol) environments
  • Rejects all permission requests from untrusted remote content by default

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 166 to 185
// リモートコンテンツからのセッション権限リクエストを全て拒否
void app.whenReady().then(() => {
session.defaultSession.setPermissionRequestHandler(
(webContents, permission, callback, { requestingUrl }) => {
const parsedUrl = new URL(webContents.getURL());
const parsedRequestingUrl = new URL(requestingUrl);
let isAppUrl: boolean;
if (import.meta.env.VITE_DEV_SERVER_URL != undefined) {
const { origin } = new URL(import.meta.env.VITE_DEV_SERVER_URL);
isAppUrl =
parsedUrl.origin === origin && parsedRequestingUrl.origin === origin;
} else {
isAppUrl =
parsedUrl.protocol === "app:" &&
parsedRequestingUrl.protocol === "app:";
}
return callback(isAppUrl);
},
);
});
Copy link

Copilot AI Sep 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The URL constructor can throw an error if webContents.getURL() or requestingUrl contain invalid URLs. This should be wrapped in a try-catch block to prevent the application from crashing, and invalid URLs should be treated as untrusted (return false).

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

@sabonerune sabonerune Sep 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これはあり得ないような?
起こるとしたらそれはElectronのバグしかないような気がします。
(パースできないURLにElectronがアクセスしているかリクエスト元のURLが偽装されているということになる)

@Hiroshiba
Copy link
Member

ping @sevenc-nanashi

@sabonerune sabonerune requested a review from Hiroshiba October 3, 2025 15:27
Copy link
Member

@sevenc-nanashi sevenc-nanashi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良さそう。

@sevenc-nanashi
Copy link
Member

マージします。

@Hiroshiba Hiroshiba enabled auto-merge October 12, 2025 03:56
@Hiroshiba Hiroshiba added this pull request to the merge queue Oct 12, 2025
Merged via the queue into VOICEVOX:main with commit 23266c2 Oct 12, 2025
11 checks passed
@sabonerune sabonerune deleted the feat/permission-request-handler branch October 13, 2025 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants