-
Notifications
You must be signed in to change notification settings - Fork 77
fix(cloud_function): return full base64 state for v0.0.9+ compatibility #325
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
Open
bs-nubank
wants to merge
4
commits into
gemini-cli-extensions:main
Choose a base branch
from
bs-nubank:fix/oauth-state-mismatch-v0.0.9
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
84aeeb1
fix(cloud_function): return full base64 state for v0.0.9+ compatibility
bs-nubank e98d302
fix(auth): support both base64 JSON and raw hex state formats for CSR…
bs-nubank b410a1e
test(auth): add unit tests for OAuth state CSRF extraction
bs-nubank 4469af1
test(cloud_function): add unit tests for OAuth state passthrough fix
bs-nubank 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,94 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Tests for the OAuth cloud function — specifically the `state` passthrough | ||
| * fix introduced to support workspace-server v0.0.9+. | ||
| * | ||
| * The critical invariant: the cloud function must pass the original base64 | ||
| * state back to the client unchanged, so the client can decode it and extract | ||
| * the csrf field for CSRF validation. | ||
| */ | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Unit tests for the state passthrough logic (no HTTP server needed) | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('state parameter passthrough in handleCallback', () => { | ||
| /** | ||
| * Mirrors the state-handling block in handleCallback to test it in isolation. | ||
| * Returns the value that would be appended as the `state` query param on the | ||
| * redirect URL, or null if no state would be appended. | ||
| */ | ||
| function buildRedirectState(stateParam) { | ||
| if (!stateParam) return null; | ||
| if (stateParam.length > 4096) throw new Error('State parameter exceeds size limit of 4KB.'); | ||
|
|
||
| const payload = JSON.parse(Buffer.from(stateParam, 'base64').toString('utf8')); | ||
|
|
||
| if (payload && payload.manual === false && payload.uri) { | ||
| // The fix: return `stateParam` unchanged, NOT `payload.csrf` | ||
| return stateParam; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| const CSRF = 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'; | ||
|
|
||
| it('returns the full base64 state unchanged (v0.0.9+ client can decode it)', () => { | ||
| const payload = { uri: 'http://localhost:54321/oauth2callback', manual: false, csrf: CSRF }; | ||
| const state = Buffer.from(JSON.stringify(payload)).toString('base64'); | ||
|
|
||
| const result = buildRedirectState(state); | ||
|
|
||
| expect(result).toBe(state); | ||
|
|
||
| // Verify the client can decode csrf from the returned value | ||
| const decoded = JSON.parse(Buffer.from(result, 'base64').toString('utf8')); | ||
| expect(decoded.csrf).toBe(CSRF); | ||
| }); | ||
|
|
||
| it('returned state must NOT be just the raw hex csrf (old buggy behaviour)', () => { | ||
| const payload = { uri: 'http://localhost:54321/oauth2callback', manual: false, csrf: CSRF }; | ||
| const state = Buffer.from(JSON.stringify(payload)).toString('base64'); | ||
|
|
||
| const result = buildRedirectState(state); | ||
|
|
||
| // The old code returned `payload.csrf` — that must no longer happen | ||
| expect(result).not.toBe(CSRF); | ||
| }); | ||
|
|
||
| it('returns null when manual=true (manual flow, no redirect)', () => { | ||
| const payload = { manual: true, csrf: CSRF }; | ||
| const state = Buffer.from(JSON.stringify(payload)).toString('base64'); | ||
|
|
||
| expect(buildRedirectState(state)).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns null when state param is absent', () => { | ||
| expect(buildRedirectState(null)).toBeNull(); | ||
| }); | ||
|
|
||
| it('throws when state exceeds 4KB', () => { | ||
| const oversized = 'a'.repeat(4097); | ||
| expect(() => buildRedirectState(oversized)).toThrow('4KB'); | ||
| }); | ||
|
|
||
| it('preserves uri, manual and csrf through encode/decode roundtrip', () => { | ||
| const payload = { | ||
| uri: 'http://127.0.0.1:12345/oauth2callback', | ||
| manual: false, | ||
| csrf: CSRF, | ||
| }; | ||
| const state = Buffer.from(JSON.stringify(payload)).toString('base64'); | ||
| const returned = buildRedirectState(state); | ||
|
|
||
| const decoded = JSON.parse(Buffer.from(returned, 'base64').toString('utf8')); | ||
| expect(decoded.uri).toBe(payload.uri); | ||
| expect(decoded.manual).toBe(false); | ||
| expect(decoded.csrf).toBe(CSRF); | ||
| }); | ||
| }); |
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
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
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.
The change to return the full base64-encoded
statestring is incompatible with the current implementation of the callback validation inworkspace-server/src/auth/AuthManager.ts.In
AuthManager.ts(lines 356-361), the server expects thestateparameter to be the raw hex CSRF token and performs a direct comparison:If the cloud function is updated to return the base64-encoded JSON string, this comparison will fail because
returnedState(base64) will not matchcsrfToken(hex). This will break the authentication flow for all users of the current server implementation.To resolve this, the server-side logic in
AuthManager.tsmust be updated to decode the base64 state and extract thecsrffield before validation, as mentioned in the PR description. Please ensure that the server-side changes are included in this PR or a coordinated update.