Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/common/http/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export class FetchBackend implements HttpBackend {
Number.isFinite(contentLengthValue) &&
contentLengthValue > this.maxResponseSize
) {
await response.body.cancel();
throwBodyTooLargeError(this.maxResponseSize);
}

Expand Down
23 changes: 21 additions & 2 deletions packages/common/http/test/fetch_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,19 @@ describe('FetchBackend', () => {
expect(infiniteStreamFactory.cancelCount).toBe(1);
});

it('cancels an unread response stream when the declared size exceeds the limit', async () => {
infiniteStreamFactory.declaredContentLength = 2049;

const req = new HttpRequest('GET', '/test', {responseType: 'text'});
const events = await trackEvents(backend.handle(req));
const error = events[1] as HttpErrorResponse;

expect(events.length).toBe(2);
expect(error instanceof HttpErrorResponse).toBeTrue();
expect(error.error.code).toBe(RuntimeErrorCode.FETCH_RESPONSE_BODY_TOO_LARGE);
expect(infiniteStreamFactory.cancelCount).toBe(1);
});

it('allows disabling the size limit via dependency injection', async () => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
Expand Down Expand Up @@ -854,6 +867,7 @@ class MockFetchRequest {

class InfiniteStreamFetchFactory extends FetchFactory {
public cancelCount = 0;
public declaredContentLength?: number;

override fetch = async (_input: RequestInfo | URL, _init?: RequestInit): Promise<Response> => {
const stream = new ReadableStream<Uint8Array>({
Expand All @@ -868,7 +882,12 @@ class InfiniteStreamFetchFactory extends FetchFactory {
return new Response(stream, {
status: HttpStatusCode.Ok,
statusText: 'OK',
headers: {'Content-Type': 'text/plain'},
headers: {
'Content-Type': 'text/plain',
...(this.declaredContentLength === undefined
? {}
: {'Content-Length': `${this.declaredContentLength}`}),
},
});
};
}
Expand All @@ -885,7 +904,7 @@ class FiniteChunkFetchFactory extends FetchFactory {
return new Response(stream, {
status: HttpStatusCode.Ok,
statusText: 'OK',
headers: {'Content-Type': 'text/plain'},
headers: {'Content-Type': 'text/plain', 'Content-Length': '2'},
});
};
}
Expand Down