Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

ChatGPT sign-in: keep saved tokens out of the browser URL; refuse another account's renewal

Rach Pradhan7933caeb7b36
diff --git a/docs/adr/0221-chatgpt-new-sign-in.md b/docs/adr/0221-chatgpt-new-sign-in.md
index f0e278c6..303415c7 100644
--- a/docs/adr/0221-chatgpt-new-sign-in.md
+++ b/docs/adr/0221-chatgpt-new-sign-in.md
@@ -99,7 +99,8 @@ process rather than restarting anything.
flat-rate, and its request shape differs from the metered API's.
- **`graff login chatgpt-new`** (or `chat-gpt-new`). The first sign-in registers with
`client_id=dynamic_agent_client` and `agent_name_hint=Graff`; later sign-ins
- reuse the issued client id with `id_token_hint` and `login_hint`. PKCE S256,
+ reuse the issued client id with `login_hint` (never `id_token_hint`: the
+ URL reaches the browser opener's argv, so it carries no saved token). PKCE S256,
an OIDC nonce, `resource=https://api.openai.com/v1`, a loopback callback on
`127.0.0.1` (port 1455, then 1456–1459), and one `ext_agent_host_id`
(`urn:uuid:`, version 4) per machine.
diff --git a/src/oauth_chatgpt.zig b/src/oauth_chatgpt.zig
index e15ddcff..76a5784a 100644
--- a/src/oauth_chatgpt.zig
+++ b/src/oauth_chatgpt.zig
@@ -263,7 +263,6 @@ pub const Attempt = struct {
nonce: []const u8,
challenge: []const u8,
host_id: []const u8,
- id_token_hint: []const u8 = "",
login_hint: []const u8 = "",
};
@@ -277,7 +276,10 @@ pub fn authorizeUrl(arena: Allocator, a: Attempt) ![]const u8 {
});
// The name hint belongs only to a first registration.
if (std.mem.eql(u8, a.client_id, dynamic_client)) try pairs.append(arena, .{ "agent_name_hint", agent_name });
- if (a.id_token_hint.len > 0) try pairs.append(arena, .{ "id_token_hint", a.id_token_hint });
+ // No id_token_hint: the browser opener gets this URL as a process
+ // argument any local process can read, so it carries no saved token.
+ // The issued client id and the email hint keep a returning sign-in
+ // consent-free, and finish() checks the account still matches.
if (a.login_hint.len > 0) try pairs.append(arena, .{ "login_hint", a.login_hint });
return std.fmt.allocPrint(arena, "{s}?{s}", .{ authorize_url, try form(arena, pairs.items) });
}
@@ -394,15 +396,10 @@ pub fn login(io: Io, gpa: Allocator, arena: Allocator, home: []const u8) !void {
.nonce = randomToken(io, arena, 24),
.challenge = helpers.b64url(arena, &digest),
.host_id = try hostId(io, arena, home),
- .id_token_hint = if (returning) saved.?.id_token else "",
.login_hint = if (returning) saved.?.email else "",
};
const url = try authorizeUrl(arena, attempt);
- // With an id_token_hint the URL carries a credential: never print it.
- if (attempt.id_token_hint.len == 0)
- try out.print("\nSign in with ChatGPT (your browser should open it):\n\n{s}\n\n", .{url})
- else
- try out.writeAll("\nOpening your browser to sign in with ChatGPT…\n");
+ try out.print("\nSign in with ChatGPT (your browser should open it):\n\n{s}\n\n", .{url});
try out.print("waiting for the sign-in on {s} …\n", .{attempt.redirect});
try out.flush();
helpers.openBrowser(io, url);
@@ -482,7 +479,14 @@ fn refreshed(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, r: Reco
next.earliest_refresh_at = util.intFieldObj(tok, "earliest_refresh_at", 0);
if (strFieldObj(tok, "scope")) |s| next.scopes = splitScopes(arena, s);
if (strFieldObj(tok, "id_token")) |t| {
- if (jwt.verify(io, gpa, arena, t, r.client_id, null, now_s)) |_| {
+ if (jwt.verify(io, gpa, arena, t, r.client_id, null, now_s)) |claims| {
+ // Tokens for another account must never run under this sign-in
+ // (OpenAI's SIWC DevKit refuses them as account_mismatch). The
+ // grant already rotated, so this ends the session: sign in again.
+ if (r.subject.len > 0 and !std.mem.eql(u8, claims.sub, r.subject)) {
+ recordRefreshFailure(io, arena, home, r, "account_mismatch", "the renewed sign-in belongs to a different ChatGPT account");
+ return null;
+ }
next.id_token = t;
} else |_| {}
}
diff --git a/src/oauth_chatgpt_tests.zig b/src/oauth_chatgpt_tests.zig
index fffc428e..d15a7ae2 100644
--- a/src/oauth_chatgpt_tests.zig
+++ b/src/oauth_chatgpt_tests.zig
@@ -44,11 +44,12 @@ test "ChatGPT sign-in: a returning sign-in reuses the issued app id with account
.nonce = "no",
.challenge = "ch",
.host_id = host,
- .id_token_hint = "a.b.c",
.login_hint = "[email protected]",
});
- for ([_][]const u8{ "client_id=oaiapp_x&", "id_token_hint=a.b.c", "login_hint=you%40example.com", "127.0.0.1%3A1456" }) |part|
+ for ([_][]const u8{ "client_id=oaiapp_x&", "login_hint=you%40example.com", "127.0.0.1%3A1456" }) |part|
try std.testing.expect(std.mem.indexOf(u8, url, part) != null);
+ // A saved token never rides in the URL: openers expose it in argv.
+ try std.testing.expect(std.mem.indexOf(u8, url, "id_token_hint") == null);
// The name hint is for first registrations only.
try std.testing.expect(std.mem.indexOf(u8, url, "agent_name_hint") == null);
}