From 55bc3f4eca4b4a7ab474cddff3b1c2ef698116e0 Mon Sep 17 00:00:00 2001 From: Daniel Abromeit aka Abro Date: Sat, 20 Nov 2021 10:52:54 +0100 Subject: [PATCH] fix: Add catch/reject for invalid redirect URLs HTTP responses with invalid URLs in the location header field resulted in an uncatchable error. (`TypeError [ERR_INVALID_URL]: Invalid URL`) Real-world example: `Location: http://` --- src/index.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 38c076465..3f4d83f52 100644 --- a/src/index.js +++ b/src/index.js @@ -128,10 +128,18 @@ export default async function fetch(url, options_) { if (isRedirect(response_.statusCode)) { // HTTP fetch step 5.2 const location = headers.get('Location'); - + // HTTP fetch step 5.3 - const locationURL = location === null ? null : new URL(location, request.url); - + let locationURL; + + try{ + locationURL = location === null ? null : new URL(location, request.url); + } catch { + reject(new FetchError(`invalid redirect url: ${location} from: ${request.url}`, 'invalid-redirect')); + finalize(); + return; + } + // HTTP fetch step 5.5 switch (request.redirect) { case 'error':