TypeScript
fix(web): preserve explicit preview navigation URLs (#8902)
Yukun Shan098bf5329727
diff --git a/apps/web/src/browser/browserTargetResolver.test.ts b/apps/web/src/browser/browserTargetResolver.test.tsindex cbce157f9a0..c2b3432402e 100644--- a/apps/web/src/browser/browserTargetResolver.test.ts+++ b/apps/web/src/browser/browserTargetResolver.test.ts@@ -25,7 +25,7 @@ describe("browser target resolver", () => {});});- it("maps localhost URL navigation onto a remote Tailscale IPv4 host", async () => {+ it("preserves explicit loopback URL navigation for a remote Tailscale environment", async () => {readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://100.65.180.100:3773" });const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");expect(@@ -35,13 +35,29 @@ describe("browser target resolver", () => {}),).toEqual({requestedUrl: "http://localhost:5173/dashboard?mode=test#results",- resolvedUrl: "http://100.65.180.100:5173/dashboard?mode=test#results",- resolutionKind: "direct-private-network",+ resolvedUrl: "http://localhost:5173/dashboard?mode=test#results",+ resolutionKind: "direct",environmentId: "environment-1",});});- it("preserves URL credentials when mapping localhost onto a remote host", async () => {+ it("preserves explicit IPv4 loopback URL navigation for a private network environment", async () => {+ readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.50:3773" });+ const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");+ expect(+ resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {+ kind: "url",+ url: "http://127.0.0.1:5999/",+ }),+ ).toEqual({+ requestedUrl: "http://127.0.0.1:5999/",+ resolvedUrl: "http://127.0.0.1:5999/",+ resolutionKind: "direct",+ environmentId: "environment-1",+ });+ });++ it("preserves URL credentials on explicit loopback navigation", async () => {readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://100.65.180.100:3773" });const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");expect(@@ -49,10 +65,10 @@ describe("browser target resolver", () => {kind: "url",url: "http://user:p%40ss@localhost:5173/dashboard",}).resolvedUrl,- ).toBe("http://user:p%[email protected]:5173/dashboard");+ ).toBe("http://user:p%40ss@localhost:5173/dashboard");});- it("maps credentialed localhost URLs onto private IPv6 hosts", async () => {+ it("preserves credentialed loopback URLs for private IPv6 environments", async () => {readPreparedConnection.mockReturnValue({httpBaseUrl: "http://[fd7a:115c:a1e0::53]:3773",});@@ -62,10 +78,10 @@ describe("browser target resolver", () => {kind: "url",url: "http://user:p%40ss@localhost:5173/dashboard?mode=test#results",}).resolvedUrl,- ).toBe("http://user:p%40ss@[fd7a:115c:a1e0::53]:5173/dashboard?mode=test#results");+ ).toBe("http://user:p%40ss@localhost:5173/dashboard?mode=test#results");});- it("maps schemeless localhost navigation onto a remote environment host", async () => {+ it("preserves schemeless localhost navigation for a remote environment", async () => {readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });const { resolveBrowserNavigationTarget } = await import("./browserTargetResolver");expect(@@ -73,7 +89,7 @@ describe("browser target resolver", () => {kind: "url",url: "localhost:3000/app",}).resolvedUrl,- ).toBe("http://192.168.1.25:3000/app");+ ).toBe("localhost:3000/app");});it("keeps localhost navigation local for a local environment", async () => {@@ -117,12 +133,12 @@ describe("browser target resolver", () => {port: 5173,}),).toThrow(/authenticated preview gateway/);- expect(() =>+ expect(resolveBrowserNavigationTarget(EnvironmentId.make("environment-1"), {kind: "url",url: "http://localhost:5173",}),- ).toThrow(/authenticated preview gateway/);+ ).toMatchObject({ resolvedUrl: "http://localhost:5173", resolutionKind: "direct" });});it("normalizes schemeless localhost server-picker values", async () => {@@ -136,6 +152,14 @@ describe("browser target resolver", () => {).toBe("http://localhost:3000/app");});+ it("maps discovered loopback servers onto a remote environment host", async () => {+ readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://192.168.1.25:3773" });+ const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");+ expect(+ resolveDiscoveredServerUrl(EnvironmentId.make("environment-1"), "localhost:3000/app"),+ ).toBe("http://192.168.1.25:3000/app");+ });+it("preserves localhost server-picker values when the prepared base is 127.0.0.1", async () => {readPreparedConnection.mockReturnValue({ httpBaseUrl: "http://127.0.0.1:3773" });const { resolveDiscoveredServerUrl } = await import("./browserTargetResolver");diff --git a/apps/web/src/browser/browserTargetResolver.ts b/apps/web/src/browser/browserTargetResolver.tsindex 684247e2802..c06c60b5f74 100644--- a/apps/web/src/browser/browserTargetResolver.ts+++ b/apps/web/src/browser/browserTargetResolver.ts@@ -207,30 +207,6 @@ export function resolveBrowserNavigationTarget(target: BrowserNavigationTarget,): PreviewUrlResolution {if (target.kind === "url") {- let parsed: URL | null = null;- try {- parsed = new URL(normalizePreviewUrl(target.url));- } catch {- // Preserve the existing direct-navigation behavior so the preview host- // reports malformed URL errors through its normal navigation path.- }- if (parsed && isLoopbackHost(parsed.hostname)) {- const environmentUrl = readEnvironmentUrl(environmentId);- if (parsed.hostname === "0.0.0.0" || !isLocalLoopbackHost(environmentUrl.hostname)) {- return resolveEnvironmentPortTarget(- environmentId,- {- kind: "environment-port",- port: Number(parsed.port || (parsed.protocol === "https:" ? 443 : 80)),- protocol: parsed.protocol === "https:" ? "https" : "http",- path: `${parsed.pathname}${parsed.search}${parsed.hash}`,- },- environmentUrl,- target.url,- parsed,- );- }- }return {requestedUrl: target.url,resolvedUrl: target.url,@@ -244,10 +220,20 @@ export function resolveBrowserNavigationTarget(export function resolveDiscoveredServerUrl(environmentId: EnvironmentId, rawUrl: string): string {try {const normalizedUrl = normalizePreviewUrl(rawUrl);- return resolveBrowserNavigationTarget(environmentId, {- kind: "url",- url: normalizedUrl,- }).resolvedUrl;+ const parsed = new URL(normalizedUrl);+ if (!isLoopbackHost(parsed.hostname)) return normalizedUrl;+ return resolveEnvironmentPortTarget(+ environmentId,+ {+ kind: "environment-port",+ port: Number(parsed.port || (parsed.protocol === "https:" ? 443 : 80)),+ protocol: parsed.protocol === "https:" ? "https" : "http",+ path: `${parsed.pathname}${parsed.search}${parsed.hash}`,+ },+ readEnvironmentUrl(environmentId),+ rawUrl,+ parsed,+ ).resolvedUrl;} catch {return rawUrl;}