Skip to content

Commit 2bcbdae

Browse files
authored
fix(server): ChatGPT sign-in keeps listening after a stray callback request (#34533)
Co-authored-by: maria-rcks <[email protected]>
1 parent 76b3e33 commit 2bcbdae

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

‎apps/server/src/provider/CodexChatGptAuth.test.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ const makeHarnessFor = Effect.fnUntraced(function* (
401401
setIdentityFailure: (value: "issuer" | "audience" | "signature") => {
402402
identityFailure = value;
403403
},
404-
mismatchCallbackState: () => {
405-
mismatchedState = true;
404+
mismatchCallbackState: (value = true) => {
405+
mismatchedState = value;
406406
},
407407
setCallbackClientId: (value: string) => {
408408
callbackClientId = value;
@@ -871,16 +871,24 @@ it.effect(
871871
),
872872
);
873873

874-
it.effect("rejects mismatched callback state before any token exchange or credential write", () =>
874+
it.effect("rejects mismatched callback state and keeps waiting for the real redirect", () =>
875875
provision(
876876
Effect.gen(function* () {
877877
const h = yield* makeHarness;
878878
h.mismatchCallbackState();
879879
yield* h.signIn;
880-
assert.include((yield* h.phase("failed")).message!, "could not be verified");
880+
assert.include(h.callbackResponses[0]!.body, "does not belong to the active sign-in");
881881
assert.strictEqual(h.exchanges.length, 0);
882882
assert.deepEqual(h.storedRecords(), []);
883883
assert.isTrue(Option.isNone(yield* h.auth.read));
884+
h.mismatchCallbackState(false);
885+
const waiting = yield* h.phase("waiting");
886+
const redirectUri = new URL(waiting.authorizationUrl!).searchParams.get("redirect_uri")!;
887+
const unparsable = yield* Effect.promise(() => fetch(`${new URL(redirectUri).origin}//`));
888+
assert.strictEqual(unparsable.status, 404);
889+
yield* h.finishCallback(waiting);
890+
yield* h.phase("succeeded");
891+
assert.strictEqual(h.exchanges.length, 1);
884892
}),
885893
),
886894
);

‎apps/server/src/provider/CodexChatGptAuth.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,18 +549,29 @@ export const makeCodexChatGptAuth = Effect.fn("makeCodexChatGptAuth")(function*
549549
Effect.tryPromise({
550550
try: () =>
551551
new Promise<NodeHttp.Server>((resolve, reject) => {
552+
const callbackBase = "http://127.0.0.1/auth/callback";
552553
const server = NodeHttp.createServer((request, response) => {
553-
const url = new URL(request.url ?? "/", "http://127.0.0.1");
554-
if (request.method !== "GET" || url.pathname !== "/auth/callback") {
554+
const url = URL.parse(request.url ?? "/", callbackBase);
555+
if (request.method !== "GET" || url?.pathname !== "/auth/callback") {
555556
response.writeHead(404).end();
556557
return;
557558
}
558559
if (used) {
559560
response.writeHead(410).end("Sign-in is no longer active.");
560561
return;
561562
}
563+
// Reject a stray request without ending the sign-in, so the real redirect still lands.
564+
let returned: URL;
565+
try {
566+
returned = codexCallbackUrl(url.toString(), callbackBase, state);
567+
} catch {
568+
response
569+
.writeHead(400)
570+
.end("This response does not belong to the active sign-in.");
571+
return;
572+
}
562573
used = true;
563-
callback.resolve({ url, response });
574+
callback.resolve({ url: returned, response });
564575
});
565576
server.once("error", reject);
566577
server.listen(0, "127.0.0.1", () => resolve(server));

0 commit comments

Comments
 (0)