@devries-cameron20 / t3code Mirror · read-only / commits / e0f3abd1a4
Fork 0

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 25, 05:00 UTC

Fix remote pairing CORS responses (#2594)

Ben Vargas committed May 8, 2026 at 17:52 UTC e0f3abd1a4e14112498c48077e387e5eeb5c629d
4 files changed +141 -22
apps/server/src/auth/http.ts
+12 -3
@@ -14,6 +14,7 @@ import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstab
14 import { AuthError, ServerAuth } from "./Services/ServerAuth.ts";
15 import { SessionCredentialService } from "./Services/SessionCredentialService.ts";
16 import { deriveAuthClientMetadata } from "./utils.ts";
17 +import { browserApiCorsHeaders } from "../httpCors.ts";
18
19 export const respondToAuthError = (error: AuthError) =>
20 Effect.gen(function* () {
@@ -27,7 +28,7 @@ export const respondToAuthError = (error: AuthError) =>
28 {
29 error: error.message,
30 },
30 - { status: error.status ?? 500 },
31 + { status: error.status ?? 500, headers: browserApiCorsHeaders },
32 );
33 });
34
@@ -38,7 +39,10 @@ export const authSessionRouteLayer = HttpRouter.add(
39 const request = yield* HttpServerRequest.HttpServerRequest;
40 const serverAuth = yield* ServerAuth;
41 const session = yield* serverAuth.getSessionState(request);
41 - return HttpServerResponse.jsonUnsafe(session, { status: 200 });
42 + return HttpServerResponse.jsonUnsafe(session, {
43 + status: 200,
44 + headers: browserApiCorsHeaders,
45 + });
46 }),
47 );
48
@@ -81,7 +85,10 @@ export const authBootstrapRouteLayer = HttpRouter.add(
85 deriveAuthClientMetadata({ request }),
86 );
87
84 - return yield* HttpServerResponse.jsonUnsafe(result.response, { status: 200 }).pipe(
88 + return yield* HttpServerResponse.jsonUnsafe(result.response, {
89 + status: 200,
90 + headers: browserApiCorsHeaders,
91 + }).pipe(
92 HttpServerResponse.setCookie(sessions.cookieName, result.sessionToken, {
93 expires: DateTime.toDate(result.response.expiresAt),
94 httpOnly: true,
@@ -114,6 +121,7 @@ export const authBearerBootstrapRouteLayer = HttpRouter.add(
121 );
122 return HttpServerResponse.jsonUnsafe(result satisfies AuthBearerBootstrapResult, {
123 status: 200,
124 + headers: browserApiCorsHeaders,
125 });
126 }).pipe(Effect.catchTag("AuthError", (error) => respondToAuthError(error))),
127 );
@@ -128,6 +136,7 @@ export const authWebSocketTokenRouteLayer = HttpRouter.add(
136 const result = yield* serverAuth.issueWebSocketToken(session);
137 return HttpServerResponse.jsonUnsafe(result satisfies AuthWebSocketTokenResult, {
138 status: 200,
139 + headers: browserApiCorsHeaders,
140 });
141 }).pipe(Effect.catchTag("AuthError", (error) => respondToAuthError(error))),
142 );
apps/server/src/http.ts
+11 -3
@@ -28,6 +28,11 @@ import { ProjectFaviconResolver } from "./project/Services/ProjectFaviconResolve
28 import { ServerAuth } from "./auth/Services/ServerAuth.ts";
29 import { respondToAuthError } from "./auth/http.ts";
30 import { ServerEnvironment } from "./environment/Services/ServerEnvironment.ts";
31 +import {
32 + browserApiCorsAllowedHeaders,
33 + browserApiCorsAllowedMethods,
34 + browserApiCorsHeaders,
35 +} from "./httpCors.ts";
36
37 const PROJECT_FAVICON_CACHE_CONTROL = "public, max-age=3600";
38 const FALLBACK_PROJECT_FAVICON_SVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="#6b728080" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" data-fallback="project-favicon"><path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-8l-2-2H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2Z"/></svg>`;
@@ -35,8 +40,8 @@ const OTLP_TRACES_PROXY_PATH = "/api/observability/v1/traces";
40 const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);
41
42 export const browserApiCorsLayer = HttpRouter.cors({
38 - allowedMethods: ["GET", "POST", "OPTIONS"],
39 - allowedHeaders: ["authorization", "b3", "traceparent", "content-type"],
43 + allowedMethods: [...browserApiCorsAllowedMethods],
44 + allowedHeaders: [...browserApiCorsAllowedHeaders],
45 maxAge: 600,
46 });
47
@@ -69,7 +74,10 @@ export const serverEnvironmentRouteLayer = HttpRouter.add(
74 const descriptor = yield* Effect.service(ServerEnvironment).pipe(
75 Effect.flatMap((serverEnvironment) => serverEnvironment.getDescriptor),
76 );
72 - return HttpServerResponse.jsonUnsafe(descriptor, { status: 200 });
77 + return HttpServerResponse.jsonUnsafe(descriptor, {
78 + status: 200,
79 + headers: browserApiCorsHeaders,
80 + });
81 }),
82 );
83
apps/server/src/httpCors.ts new
+13
@@ -0,0 +1,13 @@
1 +export const browserApiCorsAllowedMethods = ["GET", "POST", "OPTIONS"] as const;
2 +export const browserApiCorsAllowedHeaders = [
3 + "authorization",
4 + "b3",
5 + "traceparent",
6 + "content-type",
7 +] as const;
8 +
9 +export const browserApiCorsHeaders = {
10 + "access-control-allow-origin": "*",
11 + "access-control-allow-methods": browserApiCorsAllowedMethods.join(", "),
12 + "access-control-allow-headers": browserApiCorsAllowedHeaders.join(", "),
13 +} as const;
apps/server/src/server.test.ts
+105 -16
@@ -802,7 +802,12 @@ const bootstrapBrowserSession = (
802 };
803 });
804
805 -const bootstrapBearerSession = (credential = defaultDesktopBootstrapToken) =>
805 +const bootstrapBearerSession = (
806 + credential = defaultDesktopBootstrapToken,
807 + options?: {
808 + readonly headers?: Record<string, string>;
809 + },
810 +) =>
811 Effect.gen(function* () {
812 const bootstrapUrl = yield* getHttpServerUrl("/api/auth/bootstrap/bearer");
813 const response = yield* Effect.promise(() =>
@@ -810,6 +815,7 @@ const bootstrapBearerSession = (credential = defaultDesktopBootstrapToken) =>
815 method: "POST",
816 headers: {
817 "content-type": "application/json",
818 + ...options?.headers,
819 },
820 body: JSON.stringify({
821 credential,
@@ -885,6 +891,22 @@ const splitHeaderTokens = (value: string | null) =>
891 .filter((token) => token.length > 0)
892 .toSorted();
893
894 +const assertBrowserApiCorsHeaders = (headers: Headers) => {
895 + assert.equal(headers.get("access-control-allow-origin"), "*");
896 + assert.deepEqual(splitHeaderTokens(headers.get("access-control-allow-methods")), [
897 + "GET",
898 + "OPTIONS",
899 + "POST",
900 + ]);
901 + assert.deepEqual(splitHeaderTokens(headers.get("access-control-allow-headers")), [
902 + "authorization",
903 + "b3",
904 + "content-type",
905 + "traceparent",
906 + ]);
907 +};
908 +const crossOriginClientOrigin = "http://remote-client.test:3773";
909 +
910 const getWsServerUrl = (
911 pathname = "",
912 options?: { authenticated?: boolean; credential?: string },
@@ -1006,6 +1028,28 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
1028 }).pipe(Effect.provide(NodeHttpServer.layerTest)),
1029 );
1030
1031 + it.effect("includes CORS headers on public environment descriptor responses", () =>
1032 + Effect.gen(function* () {
1033 + yield* buildAppUnderTest();
1034 +
1035 + const url = yield* getHttpServerUrl("/.well-known/t3/environment");
1036 + const response = yield* Effect.promise(() =>
1037 + fetch(url, {
1038 + headers: {
1039 + origin: crossOriginClientOrigin,
1040 + },
1041 + }),
1042 + );
1043 + const body = (yield* Effect.promise(() =>
1044 + response.json(),
1045 + )) as typeof testEnvironmentDescriptor;
1046 +
1047 + assert.equal(response.status, 200);
1048 + assertBrowserApiCorsHeaders(response.headers);
1049 + assert.deepEqual(body, testEnvironmentDescriptor);
1050 + }).pipe(Effect.provide(NodeHttpServer.layerTest)),
1051 + );
1052 +
1053 it.effect("reports unauthenticated session state without requiring auth", () =>
1054 Effect.gen(function* () {
1055 yield* buildAppUnderTest();
@@ -1129,6 +1173,62 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
1173 }).pipe(Effect.provide(NodeHttpServer.layerTest)),
1174 );
1175
1176 + it.effect("includes CORS headers on remote auth success responses", () =>
1177 + Effect.gen(function* () {
1178 + yield* buildAppUnderTest();
1179 +
1180 + const origin = crossOriginClientOrigin;
1181 + const { response: bootstrapResponse, body: bootstrapBody } = yield* bootstrapBearerSession(
1182 + defaultDesktopBootstrapToken,
1183 + {
1184 + headers: { origin },
1185 + },
1186 + );
1187 +
1188 + assert.equal(bootstrapResponse.status, 200);
1189 + assertBrowserApiCorsHeaders(bootstrapResponse.headers);
1190 + assert.equal(bootstrapBody.authenticated, true);
1191 + assert.equal(typeof bootstrapBody.sessionToken, "string");
1192 +
1193 + const sessionUrl = yield* getHttpServerUrl("/api/auth/session");
1194 + const sessionResponse = yield* Effect.promise(() =>
1195 + fetch(sessionUrl, {
1196 + headers: {
1197 + authorization: `Bearer ${bootstrapBody.sessionToken ?? ""}`,
1198 + origin,
1199 + },
1200 + }),
1201 + );
1202 + const sessionBody = (yield* Effect.promise(() => sessionResponse.json())) as {
1203 + readonly authenticated: boolean;
1204 + readonly sessionMethod?: string;
1205 + };
1206 +
1207 + assert.equal(sessionResponse.status, 200);
1208 + assertBrowserApiCorsHeaders(sessionResponse.headers);
1209 + assert.equal(sessionBody.authenticated, true);
1210 + assert.equal(sessionBody.sessionMethod, "bearer-session-token");
1211 +
1212 + const wsTokenUrl = yield* getHttpServerUrl("/api/auth/ws-token");
1213 + const wsTokenResponse = yield* Effect.promise(() =>
1214 + fetch(wsTokenUrl, {
1215 + method: "POST",
1216 + headers: {
1217 + authorization: `Bearer ${bootstrapBody.sessionToken ?? ""}`,
1218 + origin,
1219 + },
1220 + }),
1221 + );
1222 + const wsTokenBody = (yield* Effect.promise(() => wsTokenResponse.json())) as {
1223 + readonly token: string;
1224 + };
1225 +
1226 + assert.equal(wsTokenResponse.status, 200);
1227 + assertBrowserApiCorsHeaders(wsTokenResponse.headers);
1228 + assert.equal(typeof wsTokenBody.token, "string");
1229 + }).pipe(Effect.provide(NodeHttpServer.layerTest)),
1230 + );
1231 +
1232 it.effect(
1233 "responds to remote auth websocket-token preflight requests with authorization CORS headers",
1234 () =>
@@ -1140,7 +1240,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
1240 fetch(wsTokenUrl, {
1241 method: "OPTIONS",
1242 headers: {
1143 - origin: "http://192.168.86.35:3773",
1243 + origin: crossOriginClientOrigin,
1244 "access-control-request-method": "POST",
1245 "access-control-request-headers": "authorization",
1246 },
@@ -1148,18 +1248,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
1248 );
1249
1250 assert.equal(response.status, 204);
1151 - assert.equal(response.headers.get("access-control-allow-origin"), "*");
1152 - assert.deepEqual(splitHeaderTokens(response.headers.get("access-control-allow-methods")), [
1153 - "GET",
1154 - "OPTIONS",
1155 - "POST",
1156 - ]);
1157 - assert.deepEqual(splitHeaderTokens(response.headers.get("access-control-allow-headers")), [
1158 - "authorization",
1159 - "b3",
1160 - "content-type",
1161 - "traceparent",
1162 - ]);
1251 + assertBrowserApiCorsHeaders(response.headers);
1252 }).pipe(Effect.provide(NodeHttpServer.layerTest)),
1253 );
1254
@@ -1172,7 +1261,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
1261 fetch(wsTokenUrl, {
1262 method: "POST",
1263 headers: {
1175 - origin: "http://192.168.86.35:3773",
1264 + origin: crossOriginClientOrigin,
1265 },
1266 }),
1267 );
@@ -1181,7 +1270,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
1270 };
1271
1272 assert.equal(response.status, 401);
1184 - assert.equal(response.headers.get("access-control-allow-origin"), "*");
1273 + assertBrowserApiCorsHeaders(response.headers);
1274 assert.equal(body.error, "Authentication required.");
1275 }).pipe(Effect.provide(NodeHttpServer.layerTest)),
1276 );