-
Notifications
You must be signed in to change notification settings - Fork 699
fix(oauth): harden generated account ids against collisions #1513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Wibias
merged 3 commits into
lidge-jun:dev
from
Wibias:fix/oauth-account-id-collision-hardening
Aug 12, 2026
+106
−3
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { afterEach, beforeEach, describe, expect, test } from "bun:test"; | ||
| import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { | ||
| resetHardenedStateForTests, | ||
| setIcaclsRunnerForTests, | ||
| } from "../src/lib/windows-secret-acl"; | ||
| import { | ||
| getAccountSet, | ||
| getCredential, | ||
| saveCredential, | ||
| } from "../src/oauth/store"; | ||
|
|
||
| const TEST_DIR = join(import.meta.dir, ".tmp-oauth-account-id-collision-test"); | ||
| let previousOpencodexHome: string | undefined; | ||
|
|
||
| const COLLIDING_ACCOUNT_A = "account-collision-16138"; | ||
| const COLLIDING_ACCOUNT_B = "account-collision-28806"; | ||
|
|
||
| describe("OAuth account id collision hardening", () => { | ||
| beforeEach(() => { | ||
| previousOpencodexHome = process.env.OPENCODEX_HOME; | ||
| if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); | ||
| mkdirSync(TEST_DIR, { recursive: true }); | ||
| process.env.OPENCODEX_HOME = TEST_DIR; | ||
| resetHardenedStateForTests(); | ||
| setIcaclsRunnerForTests(() => ({ | ||
| success: true, | ||
| exitCode: 0, | ||
| timedOut: false, | ||
| stdout: "", | ||
| })); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| setIcaclsRunnerForTests(null); | ||
| resetHardenedStateForTests(); | ||
| if (previousOpencodexHome === undefined) delete process.env.OPENCODEX_HOME; | ||
| else process.env.OPENCODEX_HOME = previousOpencodexHome; | ||
| if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true }); | ||
| }); | ||
|
|
||
| test("distinct identities that collide on the historical 32-bit prefix get distinct slots", async () => { | ||
| // sha256(account-collision-16138) and sha256(account-collision-28806) both | ||
| // start with da1e26d2. The historical 8-hex id therefore aliased these | ||
| // two accounts and made active-account lookup return the wrong credential. | ||
| await saveCredential("anthropic", { | ||
| access: "access-a", | ||
| refresh: "refresh-a", | ||
| expires: Date.now() + 3600_000, | ||
| accountId: COLLIDING_ACCOUNT_A, | ||
| }); | ||
| await saveCredential("anthropic", { | ||
| access: "access-b", | ||
| refresh: "refresh-b", | ||
| expires: Date.now() + 3600_000, | ||
| accountId: COLLIDING_ACCOUNT_B, | ||
| }); | ||
|
|
||
| const set = getAccountSet("anthropic"); | ||
| expect(set).not.toBeNull(); | ||
| expect(set!.accounts).toHaveLength(2); | ||
| expect(new Set(set!.accounts.map(account => account.id)).size).toBe(2); | ||
| expect(set!.accounts.every(account => account.id.length === 32)).toBe(true); | ||
| expect(getCredential("anthropic")?.accountId).toBe(COLLIDING_ACCOUNT_B); | ||
| expect(getCredential("anthropic")?.access).toBe("access-b"); | ||
| }); | ||
|
|
||
| test("existing persisted 32-bit account ids remain valid and are not rewritten", async () => { | ||
| const authPath = join(TEST_DIR, "auth.json"); | ||
| writeFileSync(authPath, JSON.stringify({ | ||
| anthropic: { | ||
| activeAccountId: "deadbeef", | ||
| accounts: [{ | ||
| id: "deadbeef", | ||
| credential: { | ||
| access: "old-access", | ||
| refresh: "old-refresh", | ||
| expires: Date.now() + 3600_000, | ||
| accountId: "existing-account", | ||
| }, | ||
| }], | ||
| }, | ||
| })); | ||
|
|
||
| expect(getAccountSet("anthropic")?.activeAccountId).toBe("deadbeef"); | ||
|
|
||
| await saveCredential("anthropic", { | ||
| access: "rotated-access", | ||
| refresh: "rotated-refresh", | ||
| expires: Date.now() + 7200_000, | ||
| accountId: "existing-account", | ||
| }); | ||
|
|
||
| const set = getAccountSet("anthropic"); | ||
| expect(set?.activeAccountId).toBe("deadbeef"); | ||
| expect(set?.accounts[0]?.id).toBe("deadbeef"); | ||
| expect(getCredential("anthropic")?.access).toBe("rotated-access"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win
Cover the legacy single-credential normalization path.
Lines 71-84 persist an account set. This bypasses
normalizeAccountSetlines 327-331 insrc/oauth/store.ts, where a raw legacy credential derives its ID on every load.Add a test that persists a raw provider credential without
accounts, callsgetAccountSet("anthropic")twice, and asserts that both derived IDs are equal and 32 characters long. This verifies the deterministic legacy-normalization contract described by this PR.As per path instructions, “A behavior change in src/ should come with a focused regression test near the existing tests for that subsystem.”
🤖 Prompt for AI Agents
Source: Path instructions