From 18f2b49b6744792c52eb6ffb6f14bde52bbb1622 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Mon, 9 Jun 2025 01:56:04 +0200 Subject: [PATCH] fix loading themes with spaces in their name --- src/main/index.ts | 32 ++++++++++++++++++++------------ src/main/ipcMain.ts | 2 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index a001a490..95301ff7 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -16,8 +16,9 @@ * along with this program. If not, see . */ -import { app, protocol } from "electron"; +import { app, net, protocol } from "electron"; import { join } from "path"; +import { pathToFileURL } from "url"; import { initCsp } from "./csp"; import { ensureSafePath } from "./ipcMain"; @@ -27,21 +28,27 @@ import { installExt } from "./utils/extensions"; if (IS_VESKTOP || !IS_VANILLA) { app.whenReady().then(() => { - // Source Maps! Maybe there's a better way but since the renderer is executed - // from a string I don't think any other form of sourcemaps would work - protocol.registerFileProtocol("vencord", ({ url: unsafeUrl }, cb) => { - let url = unsafeUrl.slice("vencord://".length); + protocol.handle("vencord", ({ url: unsafeUrl }) => { + let url = decodeURI(unsafeUrl).slice("vencord://".length).replace(/\?v=\d+$/, ""); + if (url.endsWith("/")) url = url.slice(0, -1); + if (url.startsWith("/themes/")) { const theme = url.slice("/themes/".length); + const safeUrl = ensureSafePath(THEMES_DIR, theme); if (!safeUrl) { - cb({ statusCode: 403 }); - return; + return new Response(null, { + status: 404 + }); } - cb(safeUrl.replace(/\?v=\d+$/, "")); - return; + + return net.fetch(pathToFileURL(safeUrl).toString()); } + + // Source Maps! Maybe there's a better way but since the renderer is executed + // from a string I don't think any other form of sourcemaps would work + switch (url) { case "renderer.js.map": case "vencordDesktopRenderer.js.map": @@ -49,10 +56,11 @@ if (IS_VESKTOP || !IS_VANILLA) { case "vencordDesktopPreload.js.map": case "patcher.js.map": case "vencordDesktopMain.js.map": - cb(join(__dirname, url)); - break; + return net.fetch(pathToFileURL(join(__dirname, url)).toString()); default: - cb({ statusCode: 403 }); + return new Response(null, { + status: 404 + }); } }); diff --git a/src/main/ipcMain.ts b/src/main/ipcMain.ts index 62785867..3979a1bc 100644 --- a/src/main/ipcMain.ts +++ b/src/main/ipcMain.ts @@ -35,7 +35,7 @@ import { makeLinksOpenExternally } from "./utils/externalLinks"; mkdirSync(THEMES_DIR, { recursive: true }); export function ensureSafePath(basePath: string, path: string) { - const normalizedBasePath = normalize(basePath); + const normalizedBasePath = normalize(basePath + "/"); const newPath = join(basePath, path); const normalizedPath = normalize(newPath); return normalizedPath.startsWith(normalizedBasePath) ? normalizedPath : null;