Skip to content
TypeScript

fix(shared): preview_open reports about: and mailto: as unsupported instead of misreading them (#29614)

Alex Southwell870496686157
diff --git a/packages/shared/src/preview.test.ts b/packages/shared/src/preview.test.ts
index 231b141bf03..d381156090b 100644
--- a/packages/shared/src/preview.test.ts
+++ b/packages/shared/src/preview.test.ts
@@ -179,4 +179,18 @@ describe("normalizePreviewUrl", () => {
);
}
});
+
+ it("reports explicit non-web schemes instead of reading them as a host", () => {
+ for (const [rawUrl, protocol] of [
+ ["about:blank", "about:"],
+ ["mailto:[email protected]", "mailto:"],
+ ["javascript:alert(1)", "javascript:"],
+ ] as const) {
+ expect(() => normalizePreviewUrl(rawUrl)).toThrow(
+ expect.objectContaining({ reason: "unsupported-protocol", protocol }),
+ );
+ }
+ expect(normalizePreviewUrl("localhost:3000")).toBe("http://localhost:3000/");
+ expect(normalizePreviewUrl("devbox:8080")).toBe("https://devbox:8080/");
+ });
});
diff --git a/packages/shared/src/preview.ts b/packages/shared/src/preview.ts
index f279c07f738..6740b50d26e 100644
--- a/packages/shared/src/preview.ts
+++ b/packages/shared/src/preview.ts
@@ -185,6 +185,14 @@ export function normalizePreviewUrl(rawUrl: string): string {
if (trimmed.length === 0) {
throw new PreviewUrlNormalizationError({ inputLength: rawUrl.length, reason: "empty" });
}
+ const explicitProtocol = previewUrlProtocol(trimmed);
+ if (explicitProtocol !== undefined && KNOWN_NON_WEB_SCHEMES.has(explicitProtocol)) {
+ throw new PreviewUrlNormalizationError({
+ inputLength: rawUrl.length,
+ reason: "unsupported-protocol",
+ protocol: explicitProtocol,
+ });
+ }
const useHttp = LOOPBACK_PREFIX_PATTERN.test(trimmed);
const candidate = URL_SCHEME_WITH_AUTHORITY_PATTERN.test(trimmed)
? trimmed