-
Notifications
You must be signed in to change notification settings - Fork 288
[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
base: main
Are you sure you want to change the base?
Changes from all commits
42d5d5f
9e822eb
1cd2030
48b0a09
ab1ee78
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': minor | ||
| --- | ||
|
|
||
| Allow Flow trigger lifecycle callback `url`s to be relative to the app's `application_url` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import {ZodSchemaType, BaseConfigType, BaseSchema} from './schemas.js' | ||
| import {ExtensionInstance} from './extension-instance.js' | ||
| import {patchAppRelativeUrls} from './specifications/validation/app_relative_urls.js' | ||
| import {blocks} from '../../constants.js' | ||
| import {ClientSteps} from '../../services/build/client-steps.js' | ||
|
|
||
|
|
@@ -312,8 +313,19 @@ export function createContractBasedModuleSpecification<TConfiguration extends Ba | |
| uidStrategy: spec.uidStrategy, | ||
| transformRemoteToLocal: spec.transformRemoteToLocal, | ||
| devSessionWatchConfig: spec.devSessionWatchConfig, | ||
| deployConfig: async (config, directory) => { | ||
| // A contract based module has no local schema, so its configuration is deployed as authored. The exception is | ||
| // the app relative URL fields declared in app_relative_urls.ts: those are resolved against the dev tunnel here, | ||
| // and against the app's application_url in deployConfig below. | ||
| patchWithAppDevURLs: (config, urls) => { | ||
|
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. Having a secondary registry for a subset of extension types hidden inside of Have we considered making app-relative URL support an explicit capability of a specification instead of adding For example, |
||
| patchAppRelativeUrls(spec.identifier, config, urls.applicationUrl) | ||
| }, | ||
| deployConfig: async (config, directory, _apiKey, _moduleId, context) => { | ||
| const applicationUrl = context?.appConfiguration?.application_url | ||
| const appUrl = typeof applicationUrl === 'string' ? applicationUrl : undefined | ||
|
|
||
| // configWithoutFirstClassFields returns a fresh object, so patching it in place cannot affect the caller. | ||
| let parsedConfig = configWithoutFirstClassFields(config) | ||
| patchAppRelativeUrls(spec.identifier, parsedConfig, appUrl) | ||
| if (spec.appModuleFeatures().includes('localization')) { | ||
| const localization = await loadLocalesConfig(directory, spec.identifier) | ||
| parsedConfig = {...parsedConfig, localization} | ||
|
|
||
| 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'}) | ||
| }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.