Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(fetch): handle invalid redirect URL (#19890)
Max Schmittc339e1615b00
diff --git a/packages/playwright-core/src/server/fetch.ts b/packages/playwright-core/src/server/fetch.tsindex a58454005b..1bda7e2e44 100644--- a/packages/playwright-core/src/server/fetch.ts+++ b/packages/playwright-core/src/server/fetch.ts@@ -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();diff --git a/tests/library/browsercontext-fetch.spec.ts b/tests/library/browsercontext-fetch.spec.tsindex 0c2bdb25c3..b797f0a934 100644--- a/tests/library/browsercontext-fetch.spec.ts+++ b/tests/library/browsercontext-fetch.spec.ts@@ -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);