2026-07-24 13:16:35 +00:00
|
|
|
import {
|
|
|
|
|
browserOriginSchema,
|
|
|
|
|
sanitizeBrowserUrl,
|
|
|
|
|
} from "../domain/browserObservation.js";
|
|
|
|
|
import type { JavaScriptRuntimeLocation } from "../domain/javascriptRuntimeObservation.js";
|
2026-09-29 16:28:46 +08:00
|
|
|
import { authorizedElectronFile } from "./ElectronFileScope.js";
|
2026-07-24 13:16:35 +00:00
|
|
|
|
|
|
|
|
export type RuntimeLocationDecision =
|
|
|
|
|
| { readonly allowed: true; readonly location: JavaScriptRuntimeLocation }
|
|
|
|
|
| {
|
|
|
|
|
readonly allowed: false;
|
2026-09-29 16:28:46 +08:00
|
|
|
readonly reason: "unsupported_location";
|
2026-07-24 13:16:35 +00:00
|
|
|
};
|
|
|
|
|
|
2026-09-29 16:28:46 +08:00
|
|
|
/** Resolve a protocol location exposed by the explicitly selected Inspector endpoint. */
|
2026-07-24 13:16:35 +00:00
|
|
|
export const authorizeRuntimeLocation = async (
|
|
|
|
|
value: string,
|
|
|
|
|
): Promise<RuntimeLocationDecision> => {
|
2026-09-29 16:28:46 +08:00
|
|
|
if (value.startsWith("node:") && value.length > 0)
|
2026-07-24 13:16:35 +00:00
|
|
|
return {
|
|
|
|
|
allowed: true,
|
|
|
|
|
location: { kind: "builtin", specifier: value },
|
|
|
|
|
};
|
|
|
|
|
if (value.startsWith("file:")) {
|
2026-09-29 16:28:46 +08:00
|
|
|
const filePath = await authorizedElectronFile(value);
|
2026-07-24 13:16:35 +00:00
|
|
|
return filePath === undefined
|
2026-09-29 16:28:46 +08:00
|
|
|
? { allowed: false, reason: "unsupported_location" }
|
2026-07-24 13:16:35 +00:00
|
|
|
: { allowed: true, location: { kind: "file", file_path: filePath } };
|
|
|
|
|
}
|
|
|
|
|
let url: URL;
|
|
|
|
|
try {
|
|
|
|
|
url = new URL(value);
|
|
|
|
|
} catch {
|
|
|
|
|
return { allowed: false, reason: "unsupported_location" };
|
|
|
|
|
}
|
|
|
|
|
if (!["http:", "https:"].includes(url.protocol))
|
|
|
|
|
return { allowed: false, reason: "unsupported_location" };
|
|
|
|
|
const parsedOrigin = browserOriginSchema.safeParse(url.origin);
|
2026-09-29 16:28:46 +08:00
|
|
|
if (!parsedOrigin.success)
|
|
|
|
|
return { allowed: false, reason: "unsupported_location" };
|
2026-07-24 13:16:35 +00:00
|
|
|
return {
|
|
|
|
|
allowed: true,
|
|
|
|
|
location: {
|
|
|
|
|
kind: "url",
|
|
|
|
|
origin: parsedOrigin.data,
|
|
|
|
|
sanitized_url: sanitizeBrowserUrl(value).url,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|
2026-08-05 19:07:46 +08:00
|
|
|
|
|
|
|
|
/** Authorize a target whose Node Inspector URL omits its main entry path. */
|
|
|
|
|
export const authorizeRuntimeTargetLocation = async (
|
|
|
|
|
value: string,
|
2026-09-29 16:28:46 +08:00
|
|
|
): Promise<RuntimeLocationDecision> => authorizeRuntimeLocation(value);
|