-
Notifications
You must be signed in to change notification settings - Fork 176
Allow internal webhook call on the HTTP action #2212
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,11 @@ | ||
| export default { | ||
| displayName: 'blocks-http', | ||
| preset: '../../../jest.preset.js', | ||
| setupFiles: ['../../../jest.env.js'], | ||
| testEnvironment: 'node', | ||
| transform: { | ||
| '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
| }, | ||
| moduleFileExtensions: ['ts', 'js', 'html'], | ||
| coverageDirectory: '../../../coverage/packages/blocks/http', | ||
| }; |
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
51 changes: 51 additions & 0 deletions
51
packages/blocks/http/src/lib/common/webhook-url-validator.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,51 @@ | ||
| import { networkUtls, validateHost } from '@openops/server-shared'; | ||
|
|
||
| export async function validateAndRewritePublicWebhookUrl( | ||
| userUrl: string, | ||
| ): Promise<string> { | ||
| if (!userUrl) { | ||
| return userUrl; | ||
| } | ||
|
|
||
| try { | ||
| await validateHost(userUrl); | ||
| return userUrl; | ||
| } catch (error) { | ||
| const publicUrl = await networkUtls.getPublicUrl(); | ||
| const internalApiUrl = networkUtls.getInternalApiUrl(); | ||
|
|
||
| const publicUrlObj = new URL(publicUrl); | ||
| const internalUrlObj = new URL(internalApiUrl); | ||
| const userUrlObj = new URL(userUrl); | ||
|
|
||
| if (userUrlObj.origin !== publicUrlObj.origin) { | ||
| throw error; | ||
| } | ||
|
|
||
| const internalBasePath = internalUrlObj.pathname.replace(/\/$/, ''); | ||
| let relativePath = userUrlObj.pathname; | ||
|
|
||
| if ( | ||
| internalBasePath && | ||
| internalBasePath !== '/' && | ||
| relativePath.startsWith(internalBasePath) | ||
| ) { | ||
| relativePath = relativePath.slice(internalBasePath.length); | ||
| } | ||
|
|
||
| if (!relativePath.startsWith('/')) { | ||
| relativePath = `/${relativePath}`; | ||
| } | ||
|
|
||
| if (!/^\/v1\/webhooks\/[0-9A-Za-z]{21}\/sync$/.test(relativePath)) { | ||
| throw error; | ||
| } | ||
|
|
||
| const rewrittenPath = `${internalBasePath}${relativePath}`.replace( | ||
|
Check warning on line 44 in packages/blocks/http/src/lib/common/webhook-url-validator.ts
|
||
| /\/{2,}/g, | ||
| '/', | ||
| ); | ||
|
|
||
| return `${internalUrlObj.origin}${rewrittenPath}`; | ||
| } | ||
| } | ||
121 changes: 121 additions & 0 deletions
121
packages/blocks/http/test/webhook-url-validator.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,121 @@ | ||
| import { networkUtls, validateHost } from '@openops/server-shared'; | ||
| import { validateAndRewritePublicWebhookUrl } from '../src/lib/common/webhook-url-validator'; | ||
|
|
||
| jest.mock('@openops/server-shared', () => ({ | ||
| validateHost: jest.fn(), | ||
| networkUtls: { | ||
| getPublicUrl: jest.fn(), | ||
| getInternalApiUrl: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| describe('webhook-url-validator', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should return the original URL if it is empty', async () => { | ||
| const result = await validateAndRewritePublicWebhookUrl(''); | ||
| expect(result).toBe(''); | ||
| }); | ||
|
|
||
| it('should return the original URL if validateHost passes', async () => { | ||
| (validateHost as jest.Mock).mockResolvedValue(undefined); | ||
| const userUrl = 'https://example.com/webhook'; | ||
| const result = await validateAndRewritePublicWebhookUrl(userUrl); | ||
| expect(result).toBe(userUrl); | ||
| expect(validateHost).toHaveBeenCalledWith(userUrl); | ||
| }); | ||
|
|
||
| it('should rewrite the URL if it matches the public URL origin and valid webhook path', async () => { | ||
| const error = new Error('Host must not be an internal address'); | ||
| (validateHost as jest.Mock).mockRejectedValue(error); | ||
| (networkUtls.getPublicUrl as jest.Mock).mockResolvedValue( | ||
| 'https://public.openops.com', | ||
| ); | ||
| (networkUtls.getInternalApiUrl as jest.Mock).mockReturnValue( | ||
| 'http://internal-api:3000', | ||
| ); | ||
|
|
||
| const userUrl = | ||
| 'https://public.openops.com/v1/webhooks/123456789012345678901/sync'; | ||
| const result = await validateAndRewritePublicWebhookUrl(userUrl); | ||
|
|
||
| expect(result).toBe( | ||
| 'http://internal-api:3000/v1/webhooks/123456789012345678901/sync', | ||
| ); | ||
| }); | ||
|
|
||
| it('should rewrite the URL when public URL has a base path', async () => { | ||
| const error = new Error('Host must not be an internal address'); | ||
| (validateHost as jest.Mock).mockRejectedValue(error); | ||
| (networkUtls.getPublicUrl as jest.Mock).mockResolvedValue( | ||
| 'https://openops.com/', | ||
| ); | ||
| (networkUtls.getInternalApiUrl as jest.Mock).mockReturnValue( | ||
| 'http://internal-api:3000/api', | ||
| ); | ||
|
|
||
| const userUrl = | ||
| 'https://openops.com/api/v1/webhooks/123456789012345678901/sync'; | ||
| const result = await validateAndRewritePublicWebhookUrl(userUrl); | ||
|
|
||
| expect(result).toBe( | ||
| 'http://internal-api:3000/api/v1/webhooks/123456789012345678901/sync', | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw the original error if origin does not match public URL origin', async () => { | ||
| const error = new Error('Host must not be an internal address'); | ||
| (validateHost as jest.Mock).mockRejectedValue(error); | ||
| (networkUtls.getPublicUrl as jest.Mock).mockResolvedValue( | ||
| 'https://public.openops.com', | ||
| ); | ||
| (networkUtls.getInternalApiUrl as jest.Mock).mockReturnValue( | ||
| 'http://internal-api:3000', | ||
| ); | ||
|
|
||
| const userUrl = | ||
| 'https://other-domain.com/v1/webhooks/123456789012345678901/sync'; | ||
|
|
||
| await expect(validateAndRewritePublicWebhookUrl(userUrl)).rejects.toThrow( | ||
| error, | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw the original error if the path does not match the webhook pattern', async () => { | ||
| const error = new Error('Host must not be an internal address'); | ||
| (validateHost as jest.Mock).mockRejectedValue(error); | ||
| (networkUtls.getPublicUrl as jest.Mock).mockResolvedValue( | ||
| 'https://public.openops.com', | ||
| ); | ||
| (networkUtls.getInternalApiUrl as jest.Mock).mockReturnValue( | ||
| 'http://internal-api:3000', | ||
| ); | ||
|
|
||
| const userUrl = 'https://public.openops.com/v1/webhooks/invalid-id/sync'; | ||
|
|
||
| await expect(validateAndRewritePublicWebhookUrl(userUrl)).rejects.toThrow( | ||
| error, | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle multiple slashes correctly during rewrite', async () => { | ||
| const error = new Error('Host must not be an internal address'); | ||
| (validateHost as jest.Mock).mockRejectedValue(error); | ||
| (networkUtls.getPublicUrl as jest.Mock).mockResolvedValue( | ||
| 'https://public.openops.com/', | ||
| ); | ||
| (networkUtls.getInternalApiUrl as jest.Mock).mockReturnValue( | ||
| 'http://internal-api:3000/', | ||
| ); | ||
|
|
||
| const userUrl = | ||
| 'https://public.openops.com/v1/webhooks/123456789012345678901/sync'; | ||
| const result = await validateAndRewritePublicWebhookUrl(userUrl); | ||
|
|
||
| expect(result).toBe( | ||
| 'http://internal-api:3000/v1/webhooks/123456789012345678901/sync', | ||
| ); | ||
| }); | ||
| }); |
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,14 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "compilerOptions": { | ||
| "outDir": "../../../dist/out-tsc", | ||
| "module": "commonjs", | ||
| "types": ["jest", "node"] | ||
| }, | ||
| "include": [ | ||
| "jest.config.ts", | ||
| "src/**/*.test.ts", | ||
| "src/**/*.spec.ts", | ||
| "src/**/*.d.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
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
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.