Repository navigation
fix(ai-proxy): validate the Zendesk subdomain and refuse redirects on integration requests #2039
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
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
21 changes: 21 additions & 0 deletions
21
packages/ai-proxy/src/integrations/fetch-without-redirect.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,21 @@ | ||
| import { AIToolUnprocessableError } from '../errors'; | ||
|
|
||
| const MIN_REDIRECT_STATUS = 300; | ||
| const MAX_REDIRECT_STATUS = 399; | ||
|
|
||
| // The integration APIs never redirect, so a redirect is refused rather than followed to a URL | ||
| // that was never validated. | ||
| export default async function fetchWithoutRedirect( | ||
| url: string, | ||
| init: RequestInit, | ||
| ): Promise<Response> { | ||
| const response = await fetch(url, { ...init, redirect: 'manual' }); | ||
|
|
||
| if (response.status >= MIN_REDIRECT_STATUS && response.status <= MAX_REDIRECT_STATUS) { | ||
| throw new AIToolUnprocessableError( | ||
| `Refused to follow a redirect (HTTP ${response.status}) from ${new URL(url).origin}`, | ||
| ); | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
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
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
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
92 changes: 92 additions & 0 deletions
92
packages/ai-proxy/test/integrations/fetch-without-redirect.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,92 @@ | ||
| import type { AddressInfo } from 'net'; | ||
|
|
||
| import http from 'http'; | ||
|
|
||
| import { AIToolUnprocessableError } from '../../src/errors'; | ||
| import fetchWithoutRedirect from '../../src/integrations/fetch-without-redirect'; | ||
| import createExecuteQueryTool from '../../src/integrations/snowflake/tools/execute-query'; | ||
| import createGetTicketTool from '../../src/integrations/zendesk/tools/get-ticket'; | ||
|
|
||
| function listen(server: http.Server): Promise<string> { | ||
| return new Promise(resolve => { | ||
| server.listen(0, '127.0.0.1', () => { | ||
| resolve(`http://127.0.0.1:${(server.address() as AddressInfo).port}`); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| describe('fetchWithoutRedirect', () => { | ||
| const internalHits: string[] = []; | ||
| const internal = http.createServer((req, res) => { | ||
| internalHits.push(req.url); | ||
| res.writeHead(200, { 'content-type': 'application/json' }); | ||
| res.end(JSON.stringify({ secret: 'internal' })); | ||
| }); | ||
| let internalUrl: string; | ||
|
|
||
| let redirectStatus: number; | ||
| const redirecting = http.createServer((req, res) => { | ||
| res.writeHead(redirectStatus, { location: `${internalUrl}/latest/meta-data/` }); | ||
| res.end(); | ||
| }); | ||
| let redirectingUrl: string; | ||
|
|
||
| const plain = http.createServer((req, res) => { | ||
| res.writeHead(200, { 'content-type': 'application/json' }); | ||
| res.end(JSON.stringify({ ticket: { id: 1 } })); | ||
| }); | ||
| let plainUrl: string; | ||
|
|
||
| beforeAll(async () => { | ||
| [internalUrl, redirectingUrl, plainUrl] = await Promise.all( | ||
| [internal, redirecting, plain].map(listen), | ||
| ); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| [internal, redirecting, plain].forEach(server => server.close()); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| internalHits.length = 0; | ||
| }); | ||
|
|
||
| it('should return the response when the host does not redirect', async () => { | ||
| const response = await fetchWithoutRedirect(`${plainUrl}/tickets/1.json`, {}); | ||
|
|
||
| expect(await response.json()).toEqual({ ticket: { id: 1 } }); | ||
| }); | ||
|
|
||
| it.each([301, 302, 303, 307, 308])( | ||
| 'should refuse a %i redirect without requesting its target', | ||
| async status => { | ||
| redirectStatus = status; | ||
|
|
||
| await expect(fetchWithoutRedirect(`${redirectingUrl}/tickets/1.json`, {})).rejects.toThrow( | ||
| new AIToolUnprocessableError( | ||
| `Refused to follow a redirect (HTTP ${status}) from ${redirectingUrl}`, | ||
| ), | ||
| ); | ||
| expect(internalHits).toEqual([]); | ||
| }, | ||
| ); | ||
|
|
||
| it('should keep a Zendesk tool from returning what a redirect points to', async () => { | ||
| redirectStatus = 302; | ||
| const tool = createGetTicketTool({ Authorization: 'Basic abc' }, redirectingUrl); | ||
|
|
||
| await expect(tool.invoke({ ticket_id: 1 })).rejects.toThrow(AIToolUnprocessableError); | ||
| expect(internalHits).toEqual([]); | ||
| }); | ||
|
|
||
| it('should keep a Snowflake tool from replaying its query to what a redirect points to', async () => { | ||
| redirectStatus = 307; | ||
| const tool = createExecuteQueryTool({ Authorization: 'Bearer abc' }, redirectingUrl, { | ||
| accountIdentifier: 'acme', | ||
| programmaticAccessToken: 'token', | ||
| }); | ||
|
|
||
| await expect(tool.invoke({ statement: 'SELECT 1' })).rejects.toThrow(AIToolUnprocessableError); | ||
| expect(internalHits).toEqual([]); | ||
| }); | ||
| }); |
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
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.