mirror of
https://github.com/morluto/rea.git
synced 2026-11-02 21:06:04 +00:00
39 lines
954 B
TypeScript
39 lines
954 B
TypeScript
import { realpath, stat } from "node:fs/promises";
|
|
import { isAbsolute } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
/** Resolve one local file URL while rejecting remote hosts and encoded separators. */
|
|
export const authorizedElectronFile = async (
|
|
value: string,
|
|
): Promise<string | undefined> => {
|
|
if (/%(?:2f|5c)/iu.test(value)) return undefined;
|
|
let url: URL;
|
|
try {
|
|
url = new URL(value);
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
if (
|
|
url.protocol !== "file:" ||
|
|
url.hostname !== "" ||
|
|
url.username !== "" ||
|
|
url.password !== ""
|
|
)
|
|
return undefined;
|
|
let path: string;
|
|
try {
|
|
path = fileURLToPath(url);
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
if (!isAbsolute(path) || path.includes("\0")) return undefined;
|
|
let canonical: string;
|
|
try {
|
|
if (!(await stat(path)).isFile()) return undefined;
|
|
canonical = await realpath(path);
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
return canonical;
|
|
};
|