-
Notifications
You must be signed in to change notification settings - Fork 100
Fix OAuth exchange hardening and Gemini anyOf schema normalization #58
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
RovshanMuradov
wants to merge
4
commits into
jenslys:main
Choose a base branch
from
RovshanMuradov:fix/oauth-schema-guards
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
4dd0a70
fix: harden oauth callback parsing and exchange guards
RovshanMuradov 4d3fbd7
fix: normalize gemini function schemas for anyOf constraints
RovshanMuradov 32eeb02
fix: allow oauth exchange retry after transient failures
RovshanMuradov a561bf3
fix: preserve defs when normalizing anyOf schemas
RovshanMuradov 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { beforeEach, describe, expect, it, mock } from "bun:test"; | ||
|
|
||
| import { exchangeGeminiWithVerifier } from "./oauth"; | ||
|
|
||
| describe("exchangeGeminiWithVerifier", () => { | ||
| beforeEach(() => { | ||
| mock.restore(); | ||
| }); | ||
|
|
||
| it("returns a failure when code is not a string", async () => { | ||
| const result = await exchangeGeminiWithVerifier( | ||
| { code: "not-a-string" } as unknown as string, | ||
| "verifier", | ||
| ); | ||
|
|
||
| expect(result.type).toBe("failed"); | ||
| if (result.type === "failed") { | ||
| expect(result.error).toContain("Missing authorization code"); | ||
| } | ||
| }); | ||
|
|
||
| it("returns a failure when verifier is not a string", async () => { | ||
| const result = await exchangeGeminiWithVerifier( | ||
| "auth-code", | ||
| { verifier: "not-a-string" } as unknown as string, | ||
| ); | ||
|
|
||
| expect(result.type).toBe("failed"); | ||
| if (result.type === "failed") { | ||
| expect(result.error).toContain("Missing PKCE verifier"); | ||
| } | ||
| }); | ||
|
|
||
| it("allows retry after a failed token exchange", async () => { | ||
| const fetchMock = mock(async () => { | ||
| return new Response( | ||
| JSON.stringify({ error: "internal_error" }), | ||
| { status: 500, statusText: "Internal Server Error" }, | ||
| ); | ||
| }); | ||
| (globalThis as { fetch: typeof fetch }).fetch = fetchMock as unknown as typeof fetch; | ||
|
|
||
| const first = await exchangeGeminiWithVerifier("retry-code-1", "retry-verifier-1"); | ||
| const second = await exchangeGeminiWithVerifier("retry-code-1", "retry-verifier-1"); | ||
|
|
||
| expect(first.type).toBe("failed"); | ||
| expect(second.type).toBe("failed"); | ||
| expect(fetchMock.mock.calls.length).toBe(2); | ||
| }); | ||
|
|
||
| it("marks code consumed after successful exchange", async () => { | ||
| let callCount = 0; | ||
| const fetchMock = mock(async () => { | ||
| callCount += 1; | ||
| if (callCount === 1) { | ||
| return new Response( | ||
| JSON.stringify({ | ||
| access_token: "access-token", | ||
| expires_in: 3600, | ||
| refresh_token: "refresh-token", | ||
| }), | ||
| { status: 200 }, | ||
| ); | ||
| } | ||
|
|
||
| return new Response(JSON.stringify({ email: "user@example.com" }), { status: 200 }); | ||
| }); | ||
| (globalThis as { fetch: typeof fetch }).fetch = fetchMock as unknown as typeof fetch; | ||
|
|
||
| const first = await exchangeGeminiWithVerifier("success-code-1", "success-verifier-1"); | ||
| const second = await exchangeGeminiWithVerifier("success-code-1", "success-verifier-1"); | ||
|
|
||
| expect(first.type).toBe("success"); | ||
| expect(second.type).toBe("failed"); | ||
| if (second.type === "failed") { | ||
| expect(second.error).toContain("already submitted"); | ||
| } | ||
| expect(callCount).toBe(2); | ||
| }); | ||
| }); |
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,47 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
|
|
||
| import { | ||
| normalizeAuthorizationCode, | ||
| parseOAuthCallbackInput, | ||
| } from "./oauth-authorize"; | ||
|
|
||
| describe("oauth authorize helpers", () => { | ||
| it("parses full callback URLs", () => { | ||
| const parsed = parseOAuthCallbackInput( | ||
| "http://localhost:8085/oauth2callback?code=4%2Fabc123&state=state-1", | ||
| ); | ||
|
|
||
| expect(parsed.source).toBe("url"); | ||
| expect(parsed.code).toBe("4/abc123"); | ||
| expect(parsed.state).toBe("state-1"); | ||
| }); | ||
|
|
||
| it("parses query-style callback inputs", () => { | ||
| const parsed = parseOAuthCallbackInput("code=4%2Fabc123&state=state-2"); | ||
|
|
||
| expect(parsed.source).toBe("query"); | ||
| expect(parsed.code).toBe("4/abc123"); | ||
| expect(parsed.state).toBe("state-2"); | ||
| }); | ||
|
|
||
| it("falls back to raw code when no query markers are present", () => { | ||
| const parsed = parseOAuthCallbackInput("4/0AbCDef"); | ||
|
|
||
| expect(parsed.source).toBe("raw"); | ||
| expect(parsed.code).toBe("4/0AbCDef"); | ||
| }); | ||
|
|
||
| it("normalizes encoded authorization codes", () => { | ||
| const singleEncoded = normalizeAuthorizationCode("4%2Fabc"); | ||
| const doubleEncoded = normalizeAuthorizationCode("4%252Fabc"); | ||
|
|
||
| expect(singleEncoded).toBe("4/abc"); | ||
| expect(doubleEncoded).toBe("4/abc"); | ||
| }); | ||
|
|
||
| it("rejects malformed authorization codes", () => { | ||
| expect(normalizeAuthorizationCode(" ")).toBeUndefined(); | ||
| expect(normalizeAuthorizationCode("4/abc 123")).toBeUndefined(); | ||
| expect(normalizeAuthorizationCode("4/abc\n123")).toBeUndefined(); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.