fix(fetch): handle invalid redirect URL (#19890)

Fixes https://github.com/microsoft/playwright/issues/19879.

This part is then similar to how node-fetch is doing it:
55a4870ae5/src/index.js (L152-L159)

node-fetch also throws as of today with this URL. Before in Python it
was stalling, because the error was written to stdout and on Windows the
stdout wasn't working. On Node.js it ended up in an unhandled exception.
This commit is contained in:
Max Schmitt 2023-01-06 19:22:17 +01:00 committed by GitHub
parent 6d64edc090
commit c339e1615b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -319,7 +319,14 @@ export abstract class APIRequestContext extends SdkObject {
// HTTP-redirect fetch step 4: If locationURL is null, then return response.
if (response.headers.location) {
const locationURL = new URL(response.headers.location, url);
let locationURL;
try {
locationURL = new URL(response.headers.location, url);
} catch (error) {
reject(new Error(`uri requested responds with an invalid redirect URL: ${response.headers.location}`));
request.destroy();
return;
}
notifyRequestFinished();
fulfill(this._sendRequest(progress, locationURL, redirectOptions, postData));
request.destroy();

View File

@ -751,6 +751,22 @@ it('should respect timeout after redirects', async function({ context, server })
expect(error.message).toContain(`Request timed out after 100ms`);
});
it('should throw on a redirect with an invalid URL', async ({ context, server }) => {
server.setRedirect('/redirect', '/test');
server.setRoute('/test', (req, res) => {
// Node.js prevents us from responding with an invalid header, therefore we manually write the response.
const conn = res.connection;
conn.write('HTTP/1.1 302\r\n');
conn.write('Location: https://здравствуйте/\r\n');
conn.write('\r\n');
conn.uncork();
conn.end();
});
console.log(server.PREFIX + '/test');
const error = await context.request.get(server.PREFIX + '/redirect').catch(e => e);
expect(error.message).toContain('apiRequestContext.get: uri requested responds with an invalid redirect URL');
});
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);