fix(fetch): throw on unexpected end of file brotli requests (#18223)

https://github.com/microsoft/playwright/issues/18190
This commit is contained in:
Max Schmitt 2022-10-24 12:51:45 -07:00 committed by GitHub
parent 3460f01b47
commit be67189a54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -360,6 +360,7 @@ export abstract class APIRequestContext extends SdkObject {
if (e) if (e)
reject(new Error(`failed to decompress '${encoding}' encoding: ${e}`)); reject(new Error(`failed to decompress '${encoding}' encoding: ${e}`));
}); });
body.on('error', e => reject(new Error(`failed to decompress '${encoding}' encoding: ${e}`)));
} else { } else {
body.on('error', reject); body.on('error', reject);
} }

View File

@ -758,6 +758,29 @@ it('should respect timeout after redirects', async function({ context, server })
expect(error.message).toContain(`Request timed out after 100ms`); expect(error.message).toContain(`Request timed out after 100ms`);
}); });
it('should not hang on a brotli encoded Range request', async ({ context, server }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/18190' });
it.skip(+process.versions.node.split('.')[0] < 18);
const encodedRequestPayload = zlib.brotliCompressSync(Buffer.from('A'));
server.setRoute('/brotli', (req, res) => {
res.writeHead(206, {
'Content-Type': 'text/plain',
'content-length': 1,
'Content-Encoding': 'br',
'content-range': `bytes 0-2/${encodedRequestPayload.byteLength}`,
'Accept-Ranges': 'bytes',
});
res.write(encodedRequestPayload.slice(0, 2));
});
await expect(context.request.get(server.PREFIX + '/brotli', {
headers: {
range: 'bytes=0-2',
},
})).rejects.toThrow(`failed to decompress 'br' encoding: Error: unexpected end of file`);
});
it('should dispose', async function({ context, server }) { it('should dispose', async function({ context, server }) {
const response = await context.request.get(server.PREFIX + '/simple.json'); const response = await context.request.get(server.PREFIX + '/simple.json');
expect(await response.json()).toEqual({ foo: 'bar' }); expect(await response.json()).toEqual({ foo: 'bar' });