-
Notifications
You must be signed in to change notification settings - Fork 281
[Feature] Relative url patching on contract app extensions #8362
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
EliasJRH
wants to merge
5
commits into
main
Choose a base branch
from
relative_lifecycle_url
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
5 commits
Select commit
Hold shift + click to select a range
42d5d5f
Create helper file for relative app extension config urls
EliasJRH 9e822eb
Add relative url patching to contract based extensions
EliasJRH 1cd2030
Replace flow action relative url resolver with new generic function
EliasJRH 48b0a09
Add changelog entry
EliasJRH ab1ee78
Add optional guard for
EliasJRH 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 @@ | ||
| --- | ||
| '@shopify/cli': minor | ||
| --- | ||
|
|
||
| Allow Flow trigger lifecycle callback `url`s to be relative to the app's `application_url` |
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
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
135 changes: 135 additions & 0 deletions
135
packages/app/src/cli/models/extensions/specifications/validation/app_relative_urls.test.ts
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,135 @@ | ||
| import {patchAppRelativeUrls, resolveAppRelativeUrl} from './app_relative_urls.js' | ||
| import {describe, expect, test} from 'vitest' | ||
|
|
||
| const LIFECYCLE_CALLBACK = 'flow_trigger_lifecycle_callback' | ||
|
|
||
| describe('resolveAppRelativeUrl', () => { | ||
| const resolve = (url: string, appUrl: string | undefined) => resolveAppRelativeUrl('Test module', 'url', url, appUrl) | ||
|
|
||
| test('returns absolute URLs unchanged', () => { | ||
| expect(resolve('https://my-prod-host.example.com/api/execute', 'https://my-app.example.com')).toBe( | ||
| 'https://my-prod-host.example.com/api/execute', | ||
| ) | ||
| }) | ||
|
|
||
| test('accepts absolute HTTPS URLs regardless of scheme casing', () => { | ||
| expect(resolve('HTTPS://my-prod-host.example.com/api/execute', 'https://my-app.example.com')).toBe( | ||
| 'HTTPS://my-prod-host.example.com/api/execute', | ||
| ) | ||
| }) | ||
|
|
||
| test('prepends the app URL to relative URLs', () => { | ||
| expect(resolve('/api/execute', 'https://my-app.example.com/')).toBe('https://my-app.example.com/api/execute') | ||
| }) | ||
|
|
||
| test('throws when a relative URL cannot be resolved without an app URL', () => { | ||
| expect(() => resolve('/api/execute', undefined)).toThrow( | ||
| 'Test module url is a relative URL, but no application_url is configured. Set application_url in your app configuration or use an absolute HTTPS URL.', | ||
| ) | ||
| }) | ||
|
|
||
| test('throws when an absolute URL is not HTTPS', () => { | ||
| expect(() => resolve('http://my-prod-host.example.com/api/execute', undefined)).toThrow( | ||
| 'Test module url must resolve to an HTTPS URL. Set application_url to an HTTPS URL or use an absolute HTTPS URL.', | ||
| ) | ||
| }) | ||
|
|
||
| test('throws when the URL is empty', () => { | ||
| expect(() => resolve('', 'https://my-app.example.com')).toThrow( | ||
| 'Test module url must resolve to an HTTPS URL. Set application_url to an HTTPS URL or use an absolute HTTPS URL.', | ||
| ) | ||
| }) | ||
|
|
||
| test('throws when a relative URL resolves against a non-HTTPS app URL', () => { | ||
| expect(() => resolve('/api/execute', 'http://my-app.example.com')).toThrow( | ||
| 'Test module url must resolve to an HTTPS URL. Set application_url to an HTTPS URL or use an absolute HTTPS URL.', | ||
| ) | ||
| }) | ||
|
|
||
| test('throws on a protocol relative url', () => { | ||
| expect(() => resolve('//evil.example.com/api', 'https://my-app.example.com')).toThrow( | ||
| 'Test module url is invalid: a URL relative to the app URL must start with a single slash.', | ||
| ) | ||
| }) | ||
|
|
||
| test('throws on a url containing control characters', () => { | ||
| expect(() => resolve('/api\nX-Injected: 1', 'https://my-app.example.com')).toThrow( | ||
| 'Test module url is invalid: a URL must not contain control characters such as newlines or tabs.', | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('patchAppRelativeUrls', () => { | ||
| const patch = (config: object, appUrl: string | undefined, identifier = LIFECYCLE_CALLBACK) => { | ||
| patchAppRelativeUrls(identifier, config, appUrl) | ||
| return config | ||
| } | ||
|
|
||
| test('prepends the app URL to a relative url', () => { | ||
| // When | ||
| const got = patch({name: 'Auction lifecycle', url: '/api/flow/lifecycle'}, 'https://my-app.example.com') | ||
|
|
||
| // Then | ||
| expect(got).toEqual({name: 'Auction lifecycle', url: 'https://my-app.example.com/api/flow/lifecycle'}) | ||
| }) | ||
|
|
||
| test('removes a trailing slash from the app URL', () => { | ||
| // When | ||
| const got = patch({url: '/api/flow/lifecycle'}, 'https://my-app.example.com/') | ||
|
|
||
| // Then | ||
| expect(got).toEqual({url: 'https://my-app.example.com/api/flow/lifecycle'}) | ||
| }) | ||
|
|
||
| test('leaves an absolute url untouched', () => { | ||
| // When | ||
| const got = patch({url: 'https://my-prod-host.example.com/api/flow/lifecycle'}, 'https://my-app.example.com') | ||
|
|
||
| // Then | ||
| expect(got).toEqual({url: 'https://my-prod-host.example.com/api/flow/lifecycle'}) | ||
| }) | ||
|
|
||
| test('leaves a module with no relative URL fields untouched', () => { | ||
| // When | ||
| const got = patch({url: '/api/something'}, 'https://my-app.example.com', 'some_other_contract_module') | ||
|
|
||
| // Then | ||
| expect(got).toEqual({url: '/api/something'}) | ||
| }) | ||
|
|
||
| test('throws when there is no app URL to resolve against', () => { | ||
| // When/Then | ||
| expect(() => patch({url: '/api/flow/lifecycle'}, undefined)).toThrow( | ||
| 'Flow trigger lifecycle callback url is a relative URL, but no application_url is configured. Set application_url in your app configuration or use an absolute HTTPS URL.', | ||
| ) | ||
| }) | ||
|
|
||
| test('throws when the app URL is not HTTPS', () => { | ||
| // When/Then | ||
| expect(() => patch({url: '/api/flow/lifecycle'}, 'http://my-app.example.com')).toThrow( | ||
| 'Flow trigger lifecycle callback url must resolve to an HTTPS URL.', | ||
| ) | ||
| }) | ||
|
|
||
| test('throws on a protocol relative url', () => { | ||
| // When/Then | ||
| expect(() => patch({url: '//example.com/api'}, 'https://my-app.example.com')).toThrow( | ||
| 'a URL relative to the app URL must start with a single slash', | ||
| ) | ||
| }) | ||
|
|
||
| test('throws on a url containing control characters', () => { | ||
| // When/Then | ||
| expect(() => patch({url: '/api/flow/lifecycle\nmalicious-header: value'}, 'https://my-app.example.com')).toThrow( | ||
| 'a URL must not contain control characters', | ||
| ) | ||
| }) | ||
|
|
||
| test('resolves against the dev tunnel URL', () => { | ||
| // When | ||
| const got = patch({url: '/api/flow/lifecycle'}, 'https://my-tunnel.example.com') | ||
|
|
||
| // Then | ||
| expect(got).toEqual({url: 'https://my-tunnel.example.com/api/flow/lifecycle'}) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.