Skip to content

Commit

Permalink
feat: appプロトコルでfetch可能なディレクトリを制限する (#2153)
Browse files Browse the repository at this point in the history
* feat: appプロトコルでfetch可能なディレクトリを制限する

* fix: 参考URL追加

* fix: メンテナンス性向上のため条件を簡略化

Co-authored-by: Hiroshiba <hihokaruta@gmail.com>

---------

Co-authored-by: Hiroshiba <hihokaruta@gmail.com>
  • Loading branch information
sabonerune and Hiroshiba authored Jul 6, 2024
1 parent 8a4cdd2 commit 3ce6c7e
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/backend/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import path from "path";

import fs from "fs";
import { pathToFileURL } from "url";
import {
app,
protocol,
Expand Down Expand Up @@ -428,8 +429,23 @@ async function createWindow() {
// ソフトウェア起動時はプロトコルを app にする
if (process.env.VITE_DEV_SERVER_URL == undefined) {
protocol.handle("app", (request) => {
const filePath = path.join(__dirname, new URL(request.url).pathname);
return net.fetch(`file://${filePath}`);
// 読み取り先のファイルがインストールディレクトリ内であることを確認する
// ref: https://www.electronjs.org/ja/docs/latest/api/protocol#protocolhandlescheme-handler
const { pathname } = new URL(request.url);
const pathToServe = path.resolve(path.join(__dirname, pathname));
const relativePath = path.relative(__dirname, pathToServe);
const isUnsafe =
path.isAbsolute(relativePath) ||
relativePath.startsWith("..") ||
relativePath === "";
if (isUnsafe) {
log.error(`Bad Request URL: ${request.url}`);
return new Response("bad", {
status: 400,
headers: { "content-type": "text/html" },
});
}
return net.fetch(pathToFileURL(pathToServe).toString());
});
}

Expand Down

0 comments on commit 3ce6c7e

Please sign in to comment.