Fix remote pairing CORS responses (#2594)

This commit is contained in:
Ben Vargas authored and GitHub committed 2026-05-08 23:52:47 +00:00
1 parent 34ec8a86fb
commit e0f3abd1a4
4 files changed
+141 -22

No files matched your search

+12 -3
View File
@@ -14,6 +14,7 @@ import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstab
import { AuthError, ServerAuth } from "./Services/ServerAuth.ts";
import { SessionCredentialService } from "./Services/SessionCredentialService.ts";
import { deriveAuthClientMetadata } from "./utils.ts";
import { browserApiCorsHeaders } from "../httpCors.ts";
export const respondToAuthError = (error: AuthError) =>
Effect.gen(function* () {
@@ -27,7 +28,7 @@ export const respondToAuthError = (error: AuthError) =>
{
error: error.message,
},
{ status: error.status ?? 500 },
{ status: error.status ?? 500, headers: browserApiCorsHeaders },
);
});
@@ -38,7 +39,10 @@ export const authSessionRouteLayer = HttpRouter.add(
const request = yield* HttpServerRequest.HttpServerRequest;
const serverAuth = yield* ServerAuth;
const session = yield* serverAuth.getSessionState(request);
return HttpServerResponse.jsonUnsafe(session, { status: 200 });
return HttpServerResponse.jsonUnsafe(session, {
status: 200,
headers: browserApiCorsHeaders,
});
}),
);
@@ -81,7 +85,10 @@ export const authBootstrapRouteLayer = HttpRouter.add(
deriveAuthClientMetadata({ request }),
);
return yield* HttpServerResponse.jsonUnsafe(result.response, { status: 200 }).pipe(
return yield* HttpServerResponse.jsonUnsafe(result.response, {
status: 200,
headers: browserApiCorsHeaders,
}).pipe(
HttpServerResponse.setCookie(sessions.cookieName, result.sessionToken, {
expires: DateTime.toDate(result.response.expiresAt),
httpOnly: true,
@@ -114,6 +121,7 @@ export const authBearerBootstrapRouteLayer = HttpRouter.add(
);
return HttpServerResponse.jsonUnsafe(result satisfies AuthBearerBootstrapResult, {
status: 200,
headers: browserApiCorsHeaders,
});
}).pipe(Effect.catchTag("AuthError", (error) => respondToAuthError(error))),
);
@@ -128,6 +136,7 @@ export const authWebSocketTokenRouteLayer = HttpRouter.add(
const result = yield* serverAuth.issueWebSocketToken(session);
return HttpServerResponse.jsonUnsafe(result satisfies AuthWebSocketTokenResult, {
status: 200,
headers: browserApiCorsHeaders,
});
}).pipe(Effect.catchTag("AuthError", (error) => respondToAuthError(error))),
);
+11 -3
View File
@@ -28,6 +28,11 @@ import { ProjectFaviconResolver } from "./project/Services/ProjectFaviconResolve
import { ServerAuth } from "./auth/Services/ServerAuth.ts";
import { respondToAuthError } from "./auth/http.ts";
import { ServerEnvironment } from "./environment/Services/ServerEnvironment.ts";
import {
browserApiCorsAllowedHeaders,
browserApiCorsAllowedMethods,
browserApiCorsHeaders,
} from "./httpCors.ts";
const PROJECT_FAVICON_CACHE_CONTROL = "public, max-age=3600";
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";
const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);
export const browserApiCorsLayer = HttpRouter.cors({
allowedMethods: ["GET", "POST", "OPTIONS"],
allowedHeaders: ["authorization", "b3", "traceparent", "content-type"],
allowedMethods: [...browserApiCorsAllowedMethods],
allowedHeaders: [...browserApiCorsAllowedHeaders],
maxAge: 600,
});
@@ -69,7 +74,10 @@ export const serverEnvironmentRouteLayer = HttpRouter.add(
const descriptor = yield* Effect.service(ServerEnvironment).pipe(
Effect.flatMap((serverEnvironment) => serverEnvironment.getDescriptor),
);
return HttpServerResponse.jsonUnsafe(descriptor, { status: 200 });
return HttpServerResponse.jsonUnsafe(descriptor, {
status: 200,
headers: browserApiCorsHeaders,
});
}),
);
+13
View File
@@ -0,0 +1,13 @@
export const browserApiCorsAllowedMethods = ["GET", "POST", "OPTIONS"] as const;
export const browserApiCorsAllowedHeaders = [
"authorization",
"b3",
"traceparent",
"content-type",
] as const;
export const browserApiCorsHeaders = {
"access-control-allow-origin": "*",
"access-control-allow-methods": browserApiCorsAllowedMethods.join(", "),
"access-control-allow-headers": browserApiCorsAllowedHeaders.join(", "),
} as const;
+105 -16
View File
@@ -802,7 +802,12 @@ const bootstrapBrowserSession = (
};
});
const bootstrapBearerSession = (credential = defaultDesktopBootstrapToken) =>
const bootstrapBearerSession = (
credential = defaultDesktopBootstrapToken,
options?: {
readonly headers?: Record<string, string>;
},
) =>
Effect.gen(function* () {
const bootstrapUrl = yield* getHttpServerUrl("/api/auth/bootstrap/bearer");
const response = yield* Effect.promise(() =>
@@ -810,6 +815,7 @@ const bootstrapBearerSession = (credential = defaultDesktopBootstrapToken) =>
method: "POST",
headers: {
"content-type": "application/json",
...options?.headers,
},
body: JSON.stringify({
credential,
@@ -885,6 +891,22 @@ const splitHeaderTokens = (value: string | null) =>
.filter((token) => token.length > 0)
.toSorted();
const assertBrowserApiCorsHeaders = (headers: Headers) => {
assert.equal(headers.get("access-control-allow-origin"), "*");
assert.deepEqual(splitHeaderTokens(headers.get("access-control-allow-methods")), [
"GET",
"OPTIONS",
"POST",
]);
assert.deepEqual(splitHeaderTokens(headers.get("access-control-allow-headers")), [
"authorization",
"b3",
"content-type",
"traceparent",
]);
};
const crossOriginClientOrigin = "http://remote-client.test:3773";
const getWsServerUrl = (
pathname = "",
options?: { authenticated?: boolean; credential?: string },
@@ -1006,6 +1028,28 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("includes CORS headers on public environment descriptor responses", () =>
Effect.gen(function* () {
yield* buildAppUnderTest();
const url = yield* getHttpServerUrl("/.well-known/t3/environment");
const response = yield* Effect.promise(() =>
fetch(url, {
headers: {
origin: crossOriginClientOrigin,
},
}),
);
const body = (yield* Effect.promise(() =>
response.json(),
)) as typeof testEnvironmentDescriptor;
assert.equal(response.status, 200);
assertBrowserApiCorsHeaders(response.headers);
assert.deepEqual(body, testEnvironmentDescriptor);
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("reports unauthenticated session state without requiring auth", () =>
Effect.gen(function* () {
yield* buildAppUnderTest();
@@ -1129,6 +1173,62 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect("includes CORS headers on remote auth success responses", () =>
Effect.gen(function* () {
yield* buildAppUnderTest();
const origin = crossOriginClientOrigin;
const { response: bootstrapResponse, body: bootstrapBody } = yield* bootstrapBearerSession(
defaultDesktopBootstrapToken,
{
headers: { origin },
},
);
assert.equal(bootstrapResponse.status, 200);
assertBrowserApiCorsHeaders(bootstrapResponse.headers);
assert.equal(bootstrapBody.authenticated, true);
assert.equal(typeof bootstrapBody.sessionToken, "string");
const sessionUrl = yield* getHttpServerUrl("/api/auth/session");
const sessionResponse = yield* Effect.promise(() =>
fetch(sessionUrl, {
headers: {
authorization: `Bearer ${bootstrapBody.sessionToken ?? ""}`,
origin,
},
}),
);
const sessionBody = (yield* Effect.promise(() => sessionResponse.json())) as {
readonly authenticated: boolean;
readonly sessionMethod?: string;
};
assert.equal(sessionResponse.status, 200);
assertBrowserApiCorsHeaders(sessionResponse.headers);
assert.equal(sessionBody.authenticated, true);
assert.equal(sessionBody.sessionMethod, "bearer-session-token");
const wsTokenUrl = yield* getHttpServerUrl("/api/auth/ws-token");
const wsTokenResponse = yield* Effect.promise(() =>
fetch(wsTokenUrl, {
method: "POST",
headers: {
authorization: `Bearer ${bootstrapBody.sessionToken ?? ""}`,
origin,
},
}),
);
const wsTokenBody = (yield* Effect.promise(() => wsTokenResponse.json())) as {
readonly token: string;
};
assert.equal(wsTokenResponse.status, 200);
assertBrowserApiCorsHeaders(wsTokenResponse.headers);
assert.equal(typeof wsTokenBody.token, "string");
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
it.effect(
"responds to remote auth websocket-token preflight requests with authorization CORS headers",
() =>
@@ -1140,7 +1240,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
fetch(wsTokenUrl, {
method: "OPTIONS",
headers: {
origin: "http://192.168.86.35:3773",
origin: crossOriginClientOrigin,
"access-control-request-method": "POST",
"access-control-request-headers": "authorization",
},
@@ -1148,18 +1248,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
);
assert.equal(response.status, 204);
assert.equal(response.headers.get("access-control-allow-origin"), "*");
assert.deepEqual(splitHeaderTokens(response.headers.get("access-control-allow-methods")), [
"GET",
"OPTIONS",
"POST",
]);
assert.deepEqual(splitHeaderTokens(response.headers.get("access-control-allow-headers")), [
"authorization",
"b3",
"content-type",
"traceparent",
]);
assertBrowserApiCorsHeaders(response.headers);
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);
@@ -1172,7 +1261,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
fetch(wsTokenUrl, {
method: "POST",
headers: {
origin: "http://192.168.86.35:3773",
origin: crossOriginClientOrigin,
},
}),
);
@@ -1181,7 +1270,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
};
assert.equal(response.status, 401);
assert.equal(response.headers.get("access-control-allow-origin"), "*");
assertBrowserApiCorsHeaders(response.headers);
assert.equal(body.error, "Authentication required.");
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
);