Skip to content

fix(auth): validate Copilot token cache belongs to requesting account - #22352

Closed
xinhuagu wants to merge 2 commits into
openclaw:mainfrom
xinhuagu:fix/copilot-token-cache-per-account
Closed

xinhuagu wants to merge 2 commits into
openclaw:mainfrom
xinhuagu:fix/copilot-token-cache-per-account

Conversation

@xinhuagu

@xinhuagu xinhuagu commented Feb 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem

resolveCopilotApiToken() uses a single shared cache file (github-copilot.token.json) for all Copilot auth profiles. When multiple GitHub accounts are configured, a cached API token from account A is returned for requests meant for account B. If account A is rate-limited, account B's requests fail too — even though account B has available quota (#22264).

The cache check only validates expiry (isTokenUsable), never whether the cached token actually belongs to the requesting account.

Fix

Store a SHA-256 hash of the ghu_ token alongside the cached API token. On cache read, compare the hash against the current request's githubToken. A mismatch triggers a fresh token exchange instead of returning the wrong account's token.

This is backward-compatible: existing cache files without githubTokenHash simply miss the cache on first read and get refreshed with the hash included.

Changes

  • src/providers/github-copilot-token.ts: Add githubTokenHash to CachedCopilotToken, validate on read, persist on write
  • src/providers/github-copilot-token.test.ts: Add test for cross-account cache rejection, update existing cache test to include hash

Closes #22264

Greptile Summary

Fixes a multi-account Copilot auth bug where a single shared cache file (github-copilot.token.json) could serve a stale token from account A to requests intended for account B, causing cross-account rate-limit bleed (#22264).

  • Adds a SHA-256 hash of the ghu_ GitHub token to the cached payload (githubTokenHash field in CachedCopilotToken)
  • On cache read, validates the hash matches the requesting account's token before returning a cached result; a mismatch triggers a fresh token exchange
  • Backward-compatible: existing cache files without githubTokenHash simply miss on first read and get refreshed with the hash included
  • Adds three well-scoped test cases covering cache hit (hash match), cache miss (hash mismatch / cross-account), and backward compatibility (missing hash field)

Confidence Score: 5/5

  • This PR is safe to merge — it's a targeted, backward-compatible bug fix with good test coverage.
  • The change is minimal and well-scoped: a single additional field on the cache type, one hash comparison added to the cache check, and one hash write on cache save. The backward-compat path (missing hash causes cache miss) is correct by construction since undefined !== string. The SHA-256 hashing avoids storing raw tokens. All three callers in the codebase pass githubToken to the function already, so no caller changes are needed. Tests cover the key scenarios.
  • No files require special attention

Last reviewed commit: d1d2a37

@greptile-apps greptile-apps 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.

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

const account2Hash = crypto.createHash("sha256").update("ghu_account2").digest("hex");
expect(saved.githubTokenHash).toBe(account2Hash);
});

it("fetches and stores token when cache is missing", async () => {

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.

Consider adding explicit test for backward compatibility: cache exists with valid token but no githubTokenHash field should trigger fresh fetch and save hash

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/providers/github-copilot-token.test.ts
Line: 88

Comment:
Consider adding explicit test for backward compatibility: cache exists with valid token but no `githubTokenHash` field should trigger fresh fetch and save hash

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

@xinhuagu

Copy link
Copy Markdown
Contributor Author

@greptileai review

@xinhuagu
xinhuagu force-pushed the fix/copilot-token-cache-per-account branch from d1d2a37 to 6f64e2e Compare February 23, 2026 22:12
@xinhuagu

Copy link
Copy Markdown
Contributor Author

Addressed ✅ Added explicit backward-compat coverage in src/providers/github-copilot-token.test.ts: when cache has a valid token but no githubTokenHash (pre-upgrade cache), resolver re-fetches and persists the hash. Re-ran the token test locally; green.

@xinhuagu
xinhuagu force-pushed the fix/copilot-token-cache-per-account branch from 6f64e2e to 9ab20b7 Compare February 28, 2026 19:20
@xinhuagu

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main to keep this PR current (scope unchanged).

@xinhuagu
xinhuagu force-pushed the fix/copilot-token-cache-per-account branch from 9ab20b7 to 98e3530 Compare February 28, 2026 21:22
xinhuagu added 2 commits March 5, 2026 19:32
The shared cache file github-copilot.token.json stores a single API
token without recording which GitHub account produced it. When multiple
Copilot auth profiles are configured, a cached token from account A is
returned for requests intended for account B, breaking multi-account
failover.

Store a SHA-256 hash of the ghu_ token alongside the cached API token
and compare it on cache read. A mismatch triggers a fresh token
exchange, ensuring each account always gets its own Copilot API token.

Closes openclaw#22264
@xinhuagu
xinhuagu force-pushed the fix/copilot-token-cache-per-account branch from 98e3530 to a3b0c6c Compare March 5, 2026 18:32
@xinhuagu

xinhuagu commented Mar 7, 2026

Copy link
Copy Markdown
Contributor Author

Closing to make room — will revisit later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Shared Copilot API token cache causes wrong-account token to be used with multiple GitHub Copilot profiles

2 participants