The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 23:15 UTC
fix(web): skip production Clerk on unsupported browser origins (#20424)
Co-authored-by: Fable 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
r-near committed
Oct 19, 2026 at 21:36 UTC
9d68e1eeb37f3d72c930ef406222633f30004210
2 files changed
+108
-3
apps/web/src/cloud/publicConfig.test.ts
+76
-1
@@ -1,4 +1,4 @@
1
-import { afterEach, describe, expect, it, vi } from "vite-plus/test";
1
+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
3
import {
4
CloudPublicConfigMissingError,
@@ -8,6 +8,7 @@ import {
8
9
afterEach(() => {
10
vi.unstubAllEnvs();
11
+ vi.unstubAllGlobals();
12
});
13
14
describe("hasCloudPublicConfig", () => {
@@ -35,6 +36,80 @@ describe("hasCloudPublicConfig", () => {
36
expect(hasCloudPublicConfig()).toBe(false);
37
});
38
39
+ describe("browser origins", () => {
40
+ beforeEach(() => {
41
+ vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", `pk_live_${btoa("clerk.example.com$")}`);
42
+ vi.stubEnv("VITE_CLERK_JWT_TEMPLATE", "t3-relay");
43
+ vi.stubEnv("VITE_T3CODE_RELAY_URL", "https://relay.example.test");
44
+ });
45
+
46
+ it.each(["https://example.com", "https://nightly.app.example.com:443"])(
47
+ "enables production auth on the configured root and subdomains: %s",
48
+ (origin) => {
49
+ vi.stubGlobal("window", { location: new URL(origin) });
50
+
51
+ expect(hasCloudPublicConfig()).toBe(true);
52
+ },
53
+ );
54
+
55
+ it.each(["http://localhost:3773", "https://machine.example.ts.net"])(
56
+ "disables production auth on private browser origins: %s",
57
+ (origin) => {
58
+ vi.stubGlobal("window", { location: new URL(origin) });
59
+
60
+ expect(hasCloudPublicConfig()).toBe(false);
61
+ },
62
+ );
63
+
64
+ it.each(["http://app.example.com:443", "https://app.example.com:3773"])(
65
+ "rejects unsupported schemes and ports even on the configured domain: %s",
66
+ (origin) => {
67
+ vi.stubGlobal("window", { location: new URL(origin) });
68
+
69
+ expect(hasCloudPublicConfig()).toBe(false);
70
+ },
71
+ );
72
+
73
+ it.each(["https://notexample.com", "https://example.com.evil.test"])(
74
+ "rejects domain lookalikes: %s",
75
+ (origin) => {
76
+ vi.stubGlobal("window", { location: new URL(origin) });
77
+
78
+ expect(hasCloudPublicConfig()).toBe(false);
79
+ },
80
+ );
81
+
82
+ it.each([
83
+ ["https://tenant.clerk.accounts.dev", true],
84
+ ["https://other.clerk.accounts.dev", false],
85
+ ])("keeps a provider hostname scoped to its own instance: %s", (origin, expected) => {
86
+ vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", `pk_live_${btoa("tenant.clerk.accounts.dev$")}`);
87
+ vi.stubGlobal("window", { location: new URL(origin) });
88
+
89
+ expect(hasCloudPublicConfig()).toBe(expected);
90
+ });
91
+
92
+ it("keeps development keys available on private browser origins", () => {
93
+ vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", `pk_test_${btoa("tenant.clerk.accounts.dev$")}`);
94
+ vi.stubGlobal("window", { location: new URL("http://localhost:3773") });
95
+
96
+ expect(hasCloudPublicConfig()).toBe(true);
97
+ });
98
+
99
+ it("keeps production keys available in the Electron auth shell", () => {
100
+ vi.stubGlobal("window", { location: new URL("file:///app/index.html"), desktopBridge: {} });
101
+
102
+ expect(hasCloudPublicConfig()).toBe(true);
103
+ });
104
+
105
+ it("disables cloud auth for an invalid production key without throwing", () => {
106
+ vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", "pk_live_!");
107
+ vi.stubGlobal("window", { location: new URL("https://app.example.com") });
108
+
109
+ expect(hasCloudPublicConfig()).toBe(false);
110
+ });
111
+ });
112
+
113
it("reports the missing Clerk JWT template as structured configuration", () => {
114
vi.stubEnv("VITE_CLERK_JWT_TEMPLATE", "");
115
apps/web/src/cloud/publicConfig.ts
+32
-2
@@ -1,4 +1,7 @@
1
-import { relayClerkTokenOptions } from "@t3tools/shared/relayAuth";
1
+import {
2
+ clerkFrontendApiHostnameFromPublishableKey,
3
+ relayClerkTokenOptions,
4
+} from "@t3tools/shared/relayAuth";
5
import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
6
import * as Schema from "effect/Schema";
7
@@ -78,7 +81,34 @@ export function resolveRelayTracingConfig() {
81
82
export function hasCloudPublicConfig(): boolean {
83
const config = resolveCloudPublicConfig();
81
- return Boolean(config.clerkPublishableKey && config.clerkJwtTemplate && config.relayUrl);
84
+ if (!config.clerkPublishableKey || !config.clerkJwtTemplate || !config.relayUrl) {
85
+ return false;
86
+ }
87
+
88
+ // Electron has its own auth runtime; development keys also support private browser origins.
89
+ if (
90
+ typeof window === "undefined" ||
91
+ window.desktopBridge !== undefined ||
92
+ !config.clerkPublishableKey.startsWith("pk_live_")
93
+ ) {
94
+ return true;
95
+ }
96
+
97
+ try {
98
+ // Custom Frontend API hosts use clerk.<production domain>.
99
+ const domain = clerkFrontendApiHostnameFromPublishableKey(config.clerkPublishableKey).replace(
100
+ /^clerk\./u,
101
+ "",
102
+ );
103
+ const { hostname, protocol, port } = window.location;
104
+ return (
105
+ protocol === "https:" &&
106
+ port === "" &&
107
+ (hostname === domain || hostname.endsWith(`.${domain}`))
108
+ );
109
+ } catch {
110
+ return false;
111
+ }
112
}
113
114
export function resolveRelayClerkTokenOptions() {