-
Notifications
You must be signed in to change notification settings - Fork 274
Encode device authorization request parameters #8270
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@shopify/cli-kit': patch | ||
| --- | ||
|
|
||
| Encode device authorization request parameters safely. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,11 +63,48 @@ describe('requestDeviceAuthorization', () => { | |
| expect(shopifyFetch).toBeCalledWith('https://fqdn.com/oauth/device_authorization', { | ||
| method: 'POST', | ||
| headers: {'Content-type': 'application/x-www-form-urlencoded'}, | ||
| body: 'client_id=clientId&scope=scope1 scope2', | ||
| body: 'client_id=clientId&scope=scope1+scope2', | ||
| }) | ||
| expect(got).toEqual(dataExpected) | ||
| }) | ||
|
|
||
| test('encodes special characters in authorization scopes', async () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we testing the behavior of URLSearchParams? I feel like we could get away with much simpler testing here... |
||
| // Given | ||
| const response = new Response(JSON.stringify(data)) | ||
| vi.mocked(shopifyFetch).mockResolvedValue(response) | ||
| vi.mocked(identityFqdn).mockResolvedValue('fqdn.com') | ||
| vi.mocked(clientId).mockReturnValue('clientId') | ||
| const scopes = ['scope&name', 'scope=value', 'scope%value'] | ||
|
|
||
| // When | ||
| await requestDeviceAuthorization(scopes) | ||
|
|
||
| // Then | ||
| expect(shopifyFetch).toHaveBeenCalledWith('https://fqdn.com/oauth/device_authorization', { | ||
| method: 'POST', | ||
| headers: {'Content-type': 'application/x-www-form-urlencoded'}, | ||
| body: 'client_id=clientId&scope=scope%26name+scope%3Dvalue+scope%25value', | ||
| }) | ||
| }) | ||
|
|
||
| test('omits empty authorization scope values', async () => { | ||
| // Given | ||
| const response = new Response(JSON.stringify(data)) | ||
| vi.mocked(shopifyFetch).mockResolvedValue(response) | ||
| vi.mocked(identityFqdn).mockResolvedValue('fqdn.com') | ||
| vi.mocked(clientId).mockReturnValue('clientId') | ||
|
|
||
| // When | ||
| await requestDeviceAuthorization([]) | ||
|
|
||
| // Then | ||
| expect(shopifyFetch).toHaveBeenCalledWith('https://fqdn.com/oauth/device_authorization', { | ||
| method: 'POST', | ||
| headers: {'Content-type': 'application/x-www-form-urlencoded'}, | ||
| body: 'client_id=clientId', | ||
| }) | ||
| }) | ||
|
|
||
| test('opens the browser directly in an interactive terminal', async () => { | ||
| // Given | ||
| const outputInfo = vi.spyOn(output, 'outputInfo') | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,10 +152,8 @@ export async function pollForDeviceAuthorization(code: string, interval = 5): Pr | |
| } | ||
|
|
||
| function convertRequestToParams(queryParams: {client_id: string; scope: string}): string { | ||
| return Object.entries(queryParams) | ||
| .map(([key, value]) => value && `${key}=${value}`) | ||
| .filter((hasValue) => Boolean(hasValue)) | ||
| .join('&') | ||
| // URLSearchParams applies form encoding so scope values cannot change the request structure. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need a lot of justification for the use of URLSearchParams over a hand-rolled conversion function 😆 |
||
| return new URLSearchParams(Object.entries(queryParams).filter(([, value]) => Boolean(value))).toString() | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.