-
Notifications
You must be signed in to change notification settings - Fork 6
fix(cli): keep OAuth query parameters when opening the browser on Windows #117
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
Closed
Closed
Changes from all commits
Commits
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,5 @@ | ||
| --- | ||
| '@pymodel/pythinker-code': patch | ||
| --- | ||
|
|
||
| Open the browser on Windows through `rundll32` instead of `cmd /c start`. `cmd` cut every URL at the first `&`, so OAuth logins reached the provider with only the first query parameter and failed with an invalid authorize request. |
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 |
|---|---|---|
| @@ -1,11 +1,30 @@ | ||
| import { execFile } from 'node:child_process'; | ||
|
|
||
| export interface OpenUrlCommand { | ||
| readonly command: string; | ||
| readonly args: readonly string[]; | ||
| } | ||
|
|
||
| /** | ||
| * Windows uses `rundll32` rather than `cmd /c start` because `cmd` re-parses | ||
| * its arguments and cuts a URL at the first `&`, which strips every OAuth | ||
| * query parameter after `client_id`. | ||
| */ | ||
| export function openUrlCommandFor( | ||
| url: string, | ||
| platform: NodeJS.Platform = process.platform, | ||
| ): OpenUrlCommand { | ||
| switch (platform) { | ||
| case 'darwin': | ||
| return { command: 'open', args: [url] }; | ||
| case 'win32': | ||
| return { command: 'rundll32', args: ['url.dll,FileProtocolHandler', url] }; | ||
| default: | ||
| return { command: 'xdg-open', args: [url] }; | ||
| } | ||
| } | ||
|
|
||
| export function openUrl(url: string): void { | ||
| const command: [string, string[]] = | ||
| process.platform === 'darwin' | ||
| ? ['open', [url]] | ||
| : process.platform === 'win32' | ||
| ? ['cmd', ['/c', 'start', '', url]] | ||
| : ['xdg-open', [url]]; | ||
| execFile(command[0], command[1], () => {}); | ||
| const { command, args } = openUrlCommandFor(url); | ||
| execFile(command, [...args], () => {}); | ||
| } |
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,28 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { openUrlCommandFor } from '#/utils/open-url'; | ||
|
|
||
| const AUTHORIZE_URL = | ||
| 'https://auth.openai.com/oauth/authorize?client_id=app_test&response_type=code&state=abc'; | ||
|
|
||
| describe('openUrlCommandFor', () => { | ||
| it('keeps every query parameter on Windows', () => { | ||
| const { command, args } = openUrlCommandFor(AUTHORIZE_URL, 'win32'); | ||
| expect(command).toBe('rundll32'); | ||
| // `cmd /c start` cuts the URL at the first `&`, so the launcher must not | ||
| // hand the URL to a command interpreter. | ||
| expect(command).not.toBe('cmd'); | ||
| expect(args.at(-1)).toBe(AUTHORIZE_URL); | ||
| }); | ||
|
|
||
| it('uses the platform launcher elsewhere', () => { | ||
| expect(openUrlCommandFor(AUTHORIZE_URL, 'darwin')).toEqual({ | ||
| command: 'open', | ||
| args: [AUTHORIZE_URL], | ||
| }); | ||
| expect(openUrlCommandFor(AUTHORIZE_URL, 'linux')).toEqual({ | ||
| command: 'xdg-open', | ||
| args: [AUTHORIZE_URL], | ||
| }); | ||
| }); | ||
| }); | ||
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Assert the complete Windows command contract.
The test passes if
args[0]is noturl.dll,FileProtocolHandler. In that case,rundll32can fail even though the URL remains intact. Assert the complete command object.Proposed test update
As per path instructions, tests must be able to fail.
📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Path instructions