fix(server): consolidate browser credential handling (#18509)
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
abc80c80a7
commit
48595c787c
6 files changed
+176
-18
No files matched your search
@@ -624,4 +624,88 @@ it.layer(NodeServices.layer)("EnvironmentAuth.layer", (it) => {
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("accepts cookie sessions on socket upgrades only from the app's own origins", () =>
|
||||
Effect.gen(function* () {
|
||||
const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;
|
||||
const sessions = yield* SessionStore.SessionStore;
|
||||
const pairing = yield* serverAuth.issuePairingCredential();
|
||||
const exchanged = yield* serverAuth.createBrowserSession(pairing.credential, requestMetadata);
|
||||
const upgrade = (headers: Record<string, string>) =>
|
||||
serverAuth.authenticateWebSocketUpgrade({
|
||||
url: "/ws",
|
||||
cookies: { [sessions.cookieName]: exchanged.sessionToken },
|
||||
headers: { host: "192.168.1.50:3773", ...headers },
|
||||
} as never);
|
||||
|
||||
// Same host, a proxy-reported host, a trusted app origin, or no browser at all.
|
||||
yield* upgrade({ origin: "http://192.168.1.50:3773" });
|
||||
yield* upgrade({
|
||||
origin: "https://box.tail1234.ts.net",
|
||||
"x-forwarded-host": "box.tail1234.ts.net",
|
||||
});
|
||||
yield* upgrade({ origin: "t3code://app" });
|
||||
yield* upgrade({ origin: "http://localhost:5733" });
|
||||
yield* upgrade({});
|
||||
|
||||
// Another port on the same machine is the same site but not the app.
|
||||
const otherPort = yield* Effect.flip(upgrade({ origin: "http://192.168.1.50:8080" }));
|
||||
expect(otherPort._tag).toBe("ServerAuthInvalidCredentialError");
|
||||
const opaque = yield* Effect.flip(upgrade({ origin: "null" }));
|
||||
expect(opaque._tag).toBe("ServerAuthInvalidCredentialError");
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
layerEnvironmentAuth({
|
||||
mode: "web",
|
||||
host: "192.168.1.50",
|
||||
devUrl: new URL("http://localhost:5733"),
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("accepts cookie sessions on HTTP requests only from the app's own origins", () =>
|
||||
Effect.gen(function* () {
|
||||
const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;
|
||||
const sessions = yield* SessionStore.SessionStore;
|
||||
const pairing = yield* serverAuth.issuePairingCredential();
|
||||
const exchanged = yield* serverAuth.createBrowserSession(pairing.credential, requestMetadata);
|
||||
const post = (headers: Record<string, string>) =>
|
||||
serverAuth.authenticateHttpRequest({
|
||||
cookies: { [sessions.cookieName]: exchanged.sessionToken },
|
||||
headers: { host: "localhost:13773", ...headers },
|
||||
} as never);
|
||||
|
||||
// The dev proxy rewrites Host, so a LAN page relies on the browser's own verdict.
|
||||
yield* post({ origin: "http://192.168.1.50:5733", "sec-fetch-site": "same-origin" });
|
||||
yield* post({ origin: "http://localhost:13773" });
|
||||
|
||||
const otherPort = yield* Effect.flip(
|
||||
post({ origin: "http://localhost:3000", "sec-fetch-site": "same-site" }),
|
||||
);
|
||||
expect(otherPort._tag).toBe("ServerAuthInvalidCredentialError");
|
||||
const state = yield* serverAuth.getSessionState({
|
||||
cookies: { [sessions.cookieName]: exchanged.sessionToken },
|
||||
headers: { host: "localhost:13773", origin: "http://localhost:3000" },
|
||||
} as never);
|
||||
expect(state.authenticated).toBe(false);
|
||||
}).pipe(Effect.provide(layerEnvironmentAuth({ mode: "web" }))),
|
||||
);
|
||||
|
||||
it.effect("keeps bearer socket upgrades independent of the page origin", () =>
|
||||
Effect.gen(function* () {
|
||||
const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;
|
||||
const bearer = yield* serverAuth.issueSession();
|
||||
const verified = yield* serverAuth.authenticateWebSocketUpgrade({
|
||||
url: "/ws",
|
||||
cookies: {},
|
||||
headers: {
|
||||
host: "192.168.1.50:3773",
|
||||
origin: "http://192.168.1.50:8080",
|
||||
authorization: `Bearer ${bearer.token}`,
|
||||
},
|
||||
} as never);
|
||||
expect(verified.sessionId).toBe(bearer.sessionId);
|
||||
}).pipe(Effect.provide(layerEnvironmentAuth({ mode: "web", host: "192.168.1.50" }))),
|
||||
);
|
||||
});
|
||||
@@ -44,6 +44,7 @@ import * as ServerSecretStore from "./ServerSecretStore.ts";
|
||||
import * as SessionStore from "./SessionStore.ts";
|
||||
import { REUSABLE_DEV_SESSION_EXPIRES_AT, resolveReusableDevAuth } from "./ReusableDevAuth.ts";
|
||||
import { verifyRequestDpopProof } from "./dpop.ts";
|
||||
import { DESKTOP_RENDERER_ORIGINS } from "../httpCors.ts";
|
||||
import * as SqlitePersistence from "../persistence/Sqlite.ts";
|
||||
|
||||
const DEFAULT_SESSION_SUBJECT = "cli-issued-session";
|
||||
@@ -687,6 +688,47 @@ export function selectRequestCredential(
|
||||
return undefined;
|
||||
}
|
||||
|
||||
type RequestCredentialSource = NonNullable<ReturnType<typeof selectRequestCredential>>["source"];
|
||||
|
||||
/** Whether a credential came from a browser cookie rather than an Authorization header. */
|
||||
export const isBrowserCookieCredential = (source: RequestCredentialSource | "dev-cookie") =>
|
||||
source === "cookie" || source === "legacy-cookie" || source === "dev-cookie";
|
||||
|
||||
function firstHeaderValue(value: string | undefined): string | undefined {
|
||||
const first = value?.split(",")[0]?.trim().toLowerCase();
|
||||
return first && first.length > 0 ? first : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a cookie-carrying request comes from a page allowed to use it.
|
||||
* Browsers send `Origin` on WebSocket upgrades, cross-origin requests, and
|
||||
* non-GET requests, so an absent header means a same-origin read or a
|
||||
* non-browser client. Otherwise the request must be same-origin by the
|
||||
* browser's own `Sec-Fetch-Site` (which survives the dev proxy rewriting
|
||||
* `Host`), come from this server's own host (directly or behind a proxy that
|
||||
* reports `X-Forwarded-Host`), or come from one of the trusted app origins.
|
||||
*/
|
||||
function isBrowserOriginAllowed(
|
||||
request: HttpServerRequest.HttpServerRequest,
|
||||
trustedOrigins: ReadonlySet<string>,
|
||||
): boolean {
|
||||
const origin = request.headers.origin;
|
||||
if (origin === undefined) return true;
|
||||
if (request.headers["sec-fetch-site"] === "same-origin") return true;
|
||||
if (trustedOrigins.has(origin)) return true;
|
||||
let originHost: string;
|
||||
try {
|
||||
originHost = new URL(origin).host.toLowerCase();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
if (originHost === "") return false;
|
||||
return (
|
||||
originHost === firstHeaderValue(request.headers.host) ||
|
||||
originHost === firstHeaderValue(request.headers["x-forwarded-host"])
|
||||
);
|
||||
}
|
||||
|
||||
/** @public Service construction is part of the canonical Effect module API. */
|
||||
export const make = Effect.gen(function* () {
|
||||
const policy = yield* EnvironmentAuthPolicy.EnvironmentAuthPolicy;
|
||||
@@ -726,25 +768,49 @@ export const make = Effect.gen(function* () {
|
||||
})),
|
||||
);
|
||||
|
||||
const authenticateRequest = (
|
||||
request: HttpServerRequest.HttpServerRequest,
|
||||
): Effect.Effect<AuthenticatedSession, ServerAuthCredentialError | ServerAuthInternalError> => {
|
||||
const resolveRequestCredential = (request: HttpServerRequest.HttpServerRequest) => {
|
||||
const selectedCredential = selectRequestCredential(
|
||||
request,
|
||||
sessions.cookieName,
|
||||
sessions.legacyCookieName,
|
||||
);
|
||||
const dpopToken = parseDpopToken(request);
|
||||
const hasAuthorization = request.headers.authorization !== undefined;
|
||||
const devCookieToken = devAuth ? request.cookies[devAuth.cookieName] : undefined;
|
||||
const credential =
|
||||
return (
|
||||
selectedCredential ??
|
||||
(!hasAuthorization && devCookieToken !== undefined
|
||||
? { token: devCookieToken, source: "dev-cookie" as const }
|
||||
: undefined);
|
||||
: undefined)
|
||||
);
|
||||
};
|
||||
|
||||
// Pages the server or its dev setup serves itself. Other origins on the same
|
||||
// site (another localhost port, a LAN IP's other ports) still get the cookie,
|
||||
// so cookie sessions must not authenticate their requests or sockets.
|
||||
const trustedBrowserOrigins = new Set<string>([
|
||||
...(config.devUrl ? [config.devUrl.origin] : []),
|
||||
...DESKTOP_RENDERER_ORIGINS,
|
||||
...config.devAllowedOrigins,
|
||||
]);
|
||||
|
||||
const authenticateRequest = (
|
||||
request: HttpServerRequest.HttpServerRequest,
|
||||
): Effect.Effect<AuthenticatedSession, ServerAuthCredentialError | ServerAuthInternalError> => {
|
||||
const dpopToken = parseDpopToken(request);
|
||||
const credential = resolveRequestCredential(request);
|
||||
if (!credential?.token) {
|
||||
return Effect.fail(new ServerAuthMissingCredentialError({}));
|
||||
}
|
||||
if (
|
||||
isBrowserCookieCredential(credential.source) &&
|
||||
!isBrowserOriginAllowed(request, trustedBrowserOrigins)
|
||||
) {
|
||||
return Effect.fail(
|
||||
new ServerAuthInvalidCredentialError({
|
||||
diagnostic: "Browser sessions are only accepted from the app's own origin.",
|
||||
}),
|
||||
);
|
||||
}
|
||||
return authenticateToken(credential.token).pipe(
|
||||
Effect.flatMap((session) => {
|
||||
if (session.proofKeyThumbprint) {
|
||||
@@ -1248,14 +1314,13 @@ export const make = Effect.gen(function* () {
|
||||
const authenticateBrowserSession: EnvironmentAuth["Service"]["authenticateBrowserSession"] = (
|
||||
request,
|
||||
) => {
|
||||
const token =
|
||||
request.cookies[sessions.cookieName] ??
|
||||
(sessions.legacyCookieName ? request.cookies[sessions.legacyCookieName] : undefined) ??
|
||||
(devAuth ? request.cookies[devAuth.cookieName] : undefined);
|
||||
if (!token) return Effect.fail(new ServerAuthMissingCredentialError({}));
|
||||
return authenticateToken(token).pipe(
|
||||
const credential = resolveRequestCredential(request);
|
||||
if (!credential || !isBrowserCookieCredential(credential.source)) {
|
||||
return Effect.fail(new ServerAuthMissingCredentialError({}));
|
||||
}
|
||||
return authenticateRequest(request).pipe(
|
||||
Effect.filterOrFail(
|
||||
(session) => session.method === "browser-session-cookie" && !session.proofKeyThumbprint,
|
||||
(session) => session.method === "browser-session-cookie",
|
||||
() => new ServerAuthInvalidCredentialError({ diagnostic: "Not a browser session." }),
|
||||
),
|
||||
Effect.withSpan("EnvironmentAuth.authenticateBrowserSession"),
|
||||
|
||||
@@ -309,8 +309,8 @@ export const layer = HttpApiBuilder.group(
|
||||
const result = yield* serverAuth.createBrowserSession(
|
||||
args.payload.credential,
|
||||
deriveAuthClientMetadata({ request }),
|
||||
previousCredential?.source === "cookie" ||
|
||||
previousCredential?.source === "legacy-cookie"
|
||||
previousCredential &&
|
||||
EnvironmentAuth.isBrowserCookieCredential(previousCredential.source)
|
||||
? previousCredential.token
|
||||
: undefined,
|
||||
);
|
||||
|
||||
@@ -48,11 +48,14 @@ import {
|
||||
} from "./auth/http.ts";
|
||||
import * as ServerEnvironment from "./environment/ServerEnvironment.ts";
|
||||
import { WEBHOOK_ROUTE_PREFIX } from "./scheduledTasks/ScheduledTaskService.ts";
|
||||
import { browserApiCorsAllowedHeaders, browserApiCorsAllowedMethods } from "./httpCors.ts";
|
||||
import {
|
||||
browserApiCorsAllowedHeaders,
|
||||
browserApiCorsAllowedMethods,
|
||||
DESKTOP_RENDERER_ORIGINS,
|
||||
} from "./httpCors.ts";
|
||||
|
||||
const OTLP_TRACES_PROXY_PATH = "/api/observability/v1/traces";
|
||||
const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);
|
||||
const DESKTOP_RENDERER_ORIGINS = ["t3code://app", "t3code-dev://app"];
|
||||
const SVG_CONTENT_SECURITY_POLICY = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
|
||||
// HTML previews are agent output, not the app. The sandbox gives the document an
|
||||
// opaque origin: scripts run, but same-origin cookies, storage, and API calls are
|
||||
|
||||
@@ -9,3 +9,6 @@ export const browserApiCorsAllowedHeaders = [
|
||||
"dpop",
|
||||
ORCHESTRATION_PROTOCOL_HEADER,
|
||||
] as const;
|
||||
|
||||
/** Origins of the Electron renderer, packaged and dev. */
|
||||
export const DESKTOP_RENDERER_ORIGINS = ["t3code://app", "t3code-dev://app"] as const;
|
||||
@@ -13,12 +13,15 @@ export const DEV_PROXIED_PATH_PREFIXES = ["/api", "/oauth", "/.well-known", "/ws
|
||||
/**
|
||||
* Prefixes the proxy must forward with the browser's own Host. MCP OAuth
|
||||
* derives its issuer and resource URLs from the request, and a client
|
||||
* rejects metadata naming a different origin than the one it fetched.
|
||||
* rejects metadata naming a different origin than the one it fetched. The
|
||||
* server accepts a cookie session on `/ws` only when the page's Origin matches
|
||||
* the Host it sees, so whatever origin the page was opened on keeps working.
|
||||
*/
|
||||
export const DEV_PROXIED_ORIGIN_PRESERVING_PREFIXES: ReadonlySet<string> = new Set([
|
||||
"/oauth",
|
||||
"/.well-known",
|
||||
"/mcp",
|
||||
"/ws",
|
||||
]);
|
||||
|
||||
export function isDevProxiedPath(pathname: string): boolean {
|
||||
|
||||
Reference in new issue
Block a user