Skip to content

Commit e56f520

Browse files
committed
fix(shared): accept a pasted ChatGPT callback URL that is missing http://
1 parent 5307c23 commit e56f520

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎packages/shared/src/codexAuthHandoff.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
codexAuthorizationRequest,
55
codexAuthDeliveryUrl,
66
codexAuthHandoffUrl,
7+
codexCallbackUrl,
78
readCodexAuthDelivery,
89
readCodexAuthHandoff,
910
} from "./codexAuthHandoff.ts";
@@ -77,3 +78,19 @@ describe("Codex desktop handoff", () => {
7778
expect(() => codexAuthDeliveryUrl(input, callbackUrl + "&state=another")).toThrow();
7879
});
7980
});
81+
82+
describe("pasted Codex callback", () => {
83+
const { redirectUri, state } = codexAuthorizationRequest(input.authorizationUrl);
84+
const copied = callbackUrl.slice("http://".length);
85+
86+
it("accepts a callback copied without its scheme", () => {
87+
expect(codexCallbackUrl(copied, redirectUri, state).toString()).toBe(callbackUrl);
88+
});
89+
it("still rejects a scheme-less callback for another address or sign-in", () => {
90+
expect(() =>
91+
codexCallbackUrl(copied.replace("127.0.0.1", "localhost"), redirectUri, state),
92+
).toThrow();
93+
expect(() => codexCallbackUrl(copied.replace("54213", "54214"), redirectUri, state)).toThrow();
94+
expect(() => codexCallbackUrl(`${copied}&state=another`, redirectUri, state)).toThrow();
95+
});
96+
});

‎packages/shared/src/codexAuthHandoff.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ export function codexAuthorizationRequest(value: string) {
5656
}
5757

5858
export function codexCallbackUrl(value: string, redirectUri: string, state: string) {
59-
const callback = new URL(value);
6059
const expected = new URL(redirectUri);
60+
// Browsers can hide `http://` in the address bar, so a pasted callback may lack it.
61+
const callback = new URL(
62+
/^[a-z][a-z\d+.-]*:\/\//iu.test(value) ? value : `${expected.protocol}//${value}`,
63+
);
6164
const states = callback.searchParams.getAll("state");
6265
const codes = callback.searchParams.getAll("code");
6366
const errors = callback.searchParams.getAll("error");

0 commit comments

Comments
 (0)