Repository navigation
fix(providers): admit credential endpoints and bound responses #5336
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
+604
−72
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aa0b515
fix(providers): admit credentialed endpoints and bound provider respo…
thymikee 8fcdee4
fix(providers): thread host env into credential admission, large Test…
thymikee d8dec8b
fix(providers): keep credential admission off the eager closure, fix …
thymikee 18d1335
refactor(provider-revyl): extract response body read from sendOnce
thymikee 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
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,49 @@ | ||
| import { expect, test, vi } from 'vitest'; | ||
| import { readBoundedResponse, readBoundedResponseText, refuseRedirect } from './response-body.ts'; | ||
|
|
||
| function streamed(chunks: Uint8Array[], cancel = vi.fn(), close = true): Response { | ||
| return new Response( | ||
| new ReadableStream({ | ||
| start(controller) { | ||
| for (const chunk of chunks) controller.enqueue(chunk); | ||
| if (close) controller.close(); | ||
| }, | ||
| cancel, | ||
| }), | ||
| { status: 502 }, | ||
| ); | ||
| } | ||
|
|
||
| test('a response without a body reads as empty bytes', async () => { | ||
| expect(await readBoundedResponse(new Response(null, { status: 204 }), 8)).toEqual( | ||
| new Uint8Array(), | ||
| ); | ||
| }); | ||
|
|
||
| test('a body at the limit is accepted and one byte more fails typed with the status', async () => { | ||
| expect((await readBoundedResponse(streamed([new Uint8Array(8)]), 8)).byteLength).toBe(8); | ||
| const cancel = vi.fn(); | ||
| await expect( | ||
| readBoundedResponse(streamed([new Uint8Array(9)], cancel, false), 8), | ||
| ).rejects.toMatchObject({ | ||
| code: 'COMMAND_FAILED', | ||
| details: { reason: 'provider_response_too_large', limitBytes: 8, status: 502 }, | ||
| }); | ||
| expect(cancel).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('text decoding joins a character split across chunks', async () => { | ||
| const bytes = new TextEncoder().encode('é'); | ||
| const text = await readBoundedResponseText(streamed([bytes.slice(0, 1), bytes.slice(1)]), 8); | ||
| expect(text).toBe('é'); | ||
| }); | ||
|
|
||
| test.each([300, 301, 307, 399])('refuseRedirect refuses status %i', async (status) => { | ||
| await expect(refuseRedirect(new Response(null, { status }))).rejects.toMatchObject({ | ||
| details: { reason: 'provider_redirect_refused', status }, | ||
| }); | ||
| }); | ||
|
|
||
| test.each([204, 400])('refuseRedirect passes status %i', async (status) => { | ||
| await expect(refuseRedirect(new Response(null, { status }))).resolves.toBeUndefined(); | ||
| }); |
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,61 @@ | ||
| import { AppError, type ReasonTable } from '@agent-device/kernel/errors'; | ||
|
|
||
| const PROVIDER_RESPONSE_REASONS = { | ||
| // A provider response body is larger than the byte budget its caller allows. | ||
| tooLarge: { reason: 'provider_response_too_large' }, | ||
| // A provider answered a credentialed request with a redirect, which the sender never follows. | ||
| redirectRefused: { reason: 'provider_redirect_refused' }, | ||
| } as const satisfies ReasonTable<'provider response'>; | ||
|
|
||
| const PROVIDER_TEXT_BODY_LIMIT_BYTES = 1024 * 1024; | ||
| export const PROVIDER_LARGE_BODY_LIMIT_BYTES = 32 * 1024 * 1024; | ||
| export const PROVIDER_METADATA_BODY_LIMIT_BYTES = 256 * 1024; | ||
|
|
||
| /** Reads a response body and cancels the stream once it passes `maxBytes`. */ | ||
| export async function readBoundedResponse( | ||
| response: Response, | ||
| maxBytes: number = PROVIDER_TEXT_BODY_LIMIT_BYTES, | ||
| ): Promise<Uint8Array> { | ||
| const reader = response.body?.getReader(); | ||
| if (!reader) return new Uint8Array(); | ||
| const chunks: Uint8Array[] = []; | ||
| let bytes = 0; | ||
| try { | ||
| for (;;) { | ||
| const { done, value } = await reader.read(); | ||
| if (done) return Buffer.concat(chunks, bytes); | ||
| bytes += value.byteLength; | ||
| if (bytes > maxBytes) { | ||
| throw new AppError('COMMAND_FAILED', 'Provider response exceeds the size limit.', { | ||
| reason: PROVIDER_RESPONSE_REASONS.tooLarge.reason, | ||
| limitBytes: maxBytes, | ||
| status: response.status, | ||
| }); | ||
| } | ||
|
thymikee marked this conversation as resolved.
Resolved
|
||
| chunks.push(value); | ||
| } | ||
| } finally { | ||
| await reader.cancel().catch(() => {}); | ||
| } | ||
| } | ||
|
|
||
| export async function readBoundedResponseText( | ||
| response: Response, | ||
| maxBytes?: number, | ||
| ): Promise<string> { | ||
| return new TextDecoder().decode(await readBoundedResponse(response, maxBytes)); | ||
| } | ||
|
|
||
| /** A credentialed request sets `redirect: 'manual'`; any 3xx answer is refused instead of followed. */ | ||
| export async function refuseRedirect(response: Response): Promise<void> { | ||
| if (response.status < 300 || response.status > 399) return; | ||
| await response.body?.cancel().catch(() => {}); | ||
| throw new AppError( | ||
| 'COMMAND_FAILED', | ||
| 'Provider answered a credentialed request with a redirect.', | ||
| { | ||
| reason: PROVIDER_RESPONSE_REASONS.redirectRefused.reason, | ||
| status: response.status, | ||
| }, | ||
| ); | ||
| } | ||
|
thymikee marked this conversation as resolved.
Resolved
|
||
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
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.