Skip to content

Commit 9254fe5

Browse files
Send credentials for cookie-authenticated snapshot fetches
Primary/local environments with no bearer or DPoP credential authenticate the browser via a session cookie, which a cross-origin fetch does not send by default — so the thread/shell snapshot requests 401'd against a primary env on a different origin. Add withEnvironmentCredentials (shared in environmentHttpAuth) which opts those requests into credentials: "include"; bearer/DPoP connections carry their credential in a header and are unaffected. Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent 9bc191b commit 9254fe5

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

‎packages/client-runtime/src/state/environmentHttpAuth.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Effect from "effect/Effect";
22
import * as Option from "effect/Option";
3-
import type { HttpMethod } from "effect/unstable/http";
3+
import { FetchHttpClient, type HttpMethod } from "effect/unstable/http";
44

55
import type { PreparedHttpAuthorization } from "../connection/model.ts";
66
import type { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
@@ -11,6 +11,22 @@ export interface EnvironmentHttpAuthHeaders {
1111
readonly dpop?: string;
1212
}
1313

14+
/**
15+
* Primary/local environments with no bearer or DPoP credential authenticate the
16+
* browser via a session cookie. A cross-origin `fetch` does not send cookies by
17+
* default, so those requests must opt into credentialed mode; bearer/DPoP
18+
* connections carry their credential in a header and need no cookies. Applied
19+
* per-request via `FetchHttpClient.RequestInit`, which the fetch client reads
20+
* from the fiber context at request time.
21+
*/
22+
export const withEnvironmentCredentials = <A, E, R>(
23+
authorization: PreparedHttpAuthorization | null,
24+
request: Effect.Effect<A, E, R>,
25+
): Effect.Effect<A, E, R> =>
26+
authorization === null
27+
? request.pipe(Effect.provideService(FetchHttpClient.RequestInit, { credentials: "include" }))
28+
: request;
29+
1430
/**
1531
* Build the authorization headers for an authenticated environment HTTP
1632
* request, matching the credential the connection was prepared with:

‎packages/client-runtime/src/state/shellSnapshotHttp.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { PreparedConnection } from "../connection/model.ts";
1010
import { environmentEndpointUrl } from "../environment/endpoint.ts";
1111
import { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
1212
import { executeEnvironmentHttpRequest, makeEnvironmentHttpApiClient } from "../rpc/http.ts";
13-
import { buildEnvironmentAuthHeaders } from "./environmentHttpAuth.ts";
13+
import { buildEnvironmentAuthHeaders, withEnvironmentCredentials } from "./environmentHttpAuth.ts";
1414

1515
// Bounded so a pathologically slow endpoint cannot block the (cheaper) socket
1616
// fallback for long. The cached shell renders while this runs.
@@ -40,7 +40,10 @@ export const fetchEnvironmentShellSnapshot = Effect.fn(
4040
return yield* executeEnvironmentHttpRequest(
4141
requestUrl,
4242
input.timeoutMs ?? DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS,
43-
client.orchestration.shellSnapshot({ headers }),
43+
withEnvironmentCredentials(
44+
input.prepared.httpAuthorization,
45+
client.orchestration.shellSnapshot({ headers }),
46+
),
4447
);
4548
});
4649

‎packages/client-runtime/src/state/threadSnapshotHttp.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
makeEnvironmentHttpApiClient,
1515
type RemoteEnvironmentRequestError,
1616
} from "../rpc/http.ts";
17-
import { buildEnvironmentAuthHeaders } from "./environmentHttpAuth.ts";
17+
import { buildEnvironmentAuthHeaders, withEnvironmentCredentials } from "./environmentHttpAuth.ts";
1818

1919
// Bounded so a pathologically slow endpoint cannot block the (cheaper) socket
2020
// fallback for long. The cached thread renders while this runs, so the wait only
@@ -48,10 +48,13 @@ export const fetchEnvironmentThreadSnapshot = Effect.fn(
4848
return yield* executeEnvironmentHttpRequest(
4949
requestUrl,
5050
input.timeoutMs ?? DEFAULT_THREAD_SNAPSHOT_TIMEOUT_MS,
51-
client.orchestration.threadSnapshot({
52-
params: { threadId: input.threadId },
53-
headers,
54-
}),
51+
withEnvironmentCredentials(
52+
input.prepared.httpAuthorization,
53+
client.orchestration.threadSnapshot({
54+
params: { threadId: input.threadId },
55+
headers,
56+
}),
57+
),
5558
);
5659
});
5760

0 commit comments

Comments
 (0)