Skip to content

fix(oauth): harden generated account ids against collisions - #1513

Merged
Wibias merged 3 commits into
lidge-jun:devfrom
Wibias:fix/oauth-account-id-collision-hardening
Aug 12, 2026
Merged

fix(oauth): harden generated account ids against collisions#1513
Wibias merged 3 commits into
lidge-jun:devfrom
Wibias:fix/oauth-account-id-collision-hardening

Conversation

@Wibias

@Wibias Wibias commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-up security hardening for the OAuth multi-account store.

newAccountId() historically kept only 8 hex characters of SHA-256, giving generated account slots a 32-bit collision space. Two distinct identities could therefore receive the same persisted account id. Because activeAccountId and per-account mutations address slots by that id, a collision can make lookups or updates resolve the wrong account.

This PR:

  • expands newly-derived OAuth account ids from 32 bits to 128 bits (slice(0, 32))
  • preserves existing persisted account ids exactly as stored, so there is no auth-store migration or forced relogin
  • keeps legacy single-credential normalization deterministic across repeated loads
  • adds a regression using two concrete identities whose SHA-256 digests share the historical da1e26d2 32-bit prefix
  • verifies the colliding identities now receive distinct slots and the active credential resolves correctly
  • verifies an existing persisted 8-character account id remains unchanged after credential rotation

Security rationale

This is pre-existing hardening, separate from #1505. #1505's reasoning replay boundary also includes credential generation, so the old 32-bit slot id did not by itself cross that replay boundary. The wider issue is the OAuth store itself: duplicate generated slot ids can alias account selection and account-scoped mutations.

128 bits keeps the identifier deterministic where legacy normalization requires that property while making accidental or practical collision search infeasible for this use case.

Scope

Only OAuth account-id derivation and focused regression coverage are changed. Existing stored ids remain backwards-compatible.

Summary by CodeRabbit

  • Bug Fixes

    • Reduced the risk of OAuth account-ID collisions by using longer, more unique identifiers for newly created accounts.
    • Improved credential lookup when multiple accounts could previously share the same shortened identifier.
    • Preserved existing persisted account IDs, ensuring previously stored credentials remain compatible and can still be updated.
  • Tests

    • Added coverage for collision handling, account selection, and backward compatibility with existing OAuth account data.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

OAuth account IDs now use 32 hexadecimal SHA-256 characters for new and normalized IDs. Existing persisted 32-bit IDs remain unchanged. Regression tests cover historical collisions and legacy credential updates.

Changes

OAuth account ID handling

Layer / File(s) Summary
Account ID derivation and compatibility
src/oauth/store.ts, tests/oauth-account-id-collision.test.ts
newAccountId now derives 32-character IDs from SHA-256. Tests verify distinct account slots for historical hash collisions and preserve updates to persisted 32-bit IDs.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: lidge-jun, ingwannu

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preventing collisions in generated OAuth account IDs.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Aug 12, 2026
@Wibias
Wibias marked this pull request as ready for review August 12, 2026 05:30

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/oauth-account-id-collision.test.ts`:
- Around line 69-99: Add a focused test near the existing account-ID collision
tests that persists a raw Anthropic credential without an accounts array, calls
getAccountSet("anthropic") twice, and asserts both derived account IDs are
identical and exactly 32 characters long. Cover the legacy normalization path in
normalizeAccountSet while preserving the existing test setup and cleanup
conventions.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 3c26674e-9c62-4737-b232-2e9123c27753

📥 Commits

Reviewing files that changed from the base of the PR and between d667367 and 76ad058.

📒 Files selected for processing (2)
  • src/oauth/store.ts
  • tests/oauth-account-id-collision.test.ts

Comment on lines +69 to +99
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");
});

Copy link
Copy Markdown
Contributor

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 normalizeAccountSet lines 327-331 in src/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, calls getAccountSet("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
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/oauth-account-id-collision.test.ts` around lines 69 - 99, Add a focused
test near the existing account-ID collision tests that persists a raw Anthropic
credential without an accounts array, calls getAccountSet("anthropic") twice,
and asserts both derived account IDs are identical and exactly 32 characters
long. Cover the legacy normalization path in normalizeAccountSet while
preserving the existing test setup and cleanup conventions.

Source: Path instructions

@Wibias
Wibias merged commit ff06437 into lidge-jun:dev Aug 12, 2026
46 of 48 checks passed

Wibias commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator Author

CI note: the initial 25 GUI failures are pre-existing on the PR's exact base dev@d667367528afa2c604d19af2fc8538b965b88cf1, not introduced by this OAuth change. The base push run 31564782346 failed gates with the same 25 test names (743 pass / 25 fail), while all four backend test shards passed. #1513 changes only src/oauth/store.ts plus the focused OAuth collision regression and has no gui/ imports of that store. I reran only the failed gates job on exact head 76ad058cf9b409a6ff9bfe5ad778175f477d32d6; attempt 2 passed GUI tests, typecheck, privacy scan, and the rest of the gate. Cross-platform CI and React Doctor are now green on the exact head.

@Wibias
Wibias deleted the fix/oauth-account-id-collision-hardening branch August 12, 2026 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant