From 54943dd17d4a9fb513f018d507ae30e30a0a1703 Mon Sep 17 00:00:00 2001 From: Le Vivilet Date: Sat, 7 Sep 2024 21:37:23 +0200 Subject: [PATCH] feature: support remote file paths --- src/parts/GetResponse/GetResponse.ts | 3 ++- src/parts/ResolveFilePath/ResolveFilePath.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/parts/ResolveFilePath/ResolveFilePath.ts diff --git a/src/parts/GetResponse/GetResponse.ts b/src/parts/GetResponse/GetResponse.ts index 4cfc9d1c..6965d4b2 100644 --- a/src/parts/GetResponse/GetResponse.ts +++ b/src/parts/GetResponse/GetResponse.ts @@ -2,9 +2,10 @@ import { fileURLToPath } from 'node:url' import * as HandleIndexHtml from '../HandleIndexHtml/HandleIndexHtml.ts' import * as HandleOther from '../HandleOther/HandleOther.ts' import * as HandlePreviewInjected from '../HandlePreviewInjected/HandlePreviewInjected.ts' +import * as ResolveFilePath from '../ResolveFilePath/ResolveFilePath.ts' export const getResponse = async (pathName: string, frameAncestors: string, webViewRoot: string): Promise => { - const filePath = fileURLToPath(`file://${webViewRoot}${pathName}`) + const filePath = ResolveFilePath.resolveFilePath(pathName, webViewRoot) const isHtml = filePath.endsWith('index.html') if (isHtml) { return HandleIndexHtml.handleIndexHtml(filePath, frameAncestors) diff --git a/src/parts/ResolveFilePath/ResolveFilePath.ts b/src/parts/ResolveFilePath/ResolveFilePath.ts new file mode 100644 index 00000000..8f9c7c7b --- /dev/null +++ b/src/parts/ResolveFilePath/ResolveFilePath.ts @@ -0,0 +1,16 @@ +import { fileURLToPath } from 'node:url' + +export const resolveFilePath = (pathName: string, webViewRoot: string) => { + // TODO remove this, double slash should not be allowed + // TODO use path.resolve and verify that file path is in allowed roots + if (pathName.startsWith('/remote//')) { + const filePath = pathName.slice('/remote/'.length) + return fileURLToPath(`file://${filePath}`) + } + if (pathName.startsWith('/remote/')) { + const filePath = pathName.slice('/remote'.length) + return fileURLToPath(`file://${filePath}`) + } + const filePath = fileURLToPath(`file://${webViewRoot}${pathName}`) + return filePath +}