From ea5354fc85f298897f64082a3bfcfc70bbb7df3d Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 7 Mar 2024 17:32:05 -0800 Subject: [PATCH] handle invalid URL in Location header for fetch() (#9305) --- src/bun.js/webcore/response.zig | 1 + src/http.zig | 4 ++++ src/output.zig | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig index 917cdae722..a5c4f36af7 100644 --- a/src/bun.js/webcore/response.zig +++ b/src/bun.js/webcore/response.zig @@ -1228,6 +1228,7 @@ pub const Fetch = struct { error.FailedToOpenSocket => bun.String.static("Was there a typo in the url or port?"), error.TooManyRedirects => bun.String.static("The response redirected too many times. For more information, pass `verbose: true` in the second argument to fetch()"), error.ConnectionRefused => bun.String.static("Unable to connect. Is the computer able to access the url?"), + error.RedirectURLInvalid => bun.String.static("Redirect URL in Location header is invalid."), error.UNABLE_TO_GET_ISSUER_CERT => bun.String.static("unable to get issuer certificate"), error.UNABLE_TO_GET_CRL => bun.String.static("unable to get certificate CRL"), diff --git a/src/http.zig b/src/http.zig index 644c2ed391..79a1f987e5 100644 --- a/src/http.zig +++ b/src/http.zig @@ -3568,6 +3568,10 @@ pub fn handleResponseMetadata( const normalized_url = JSC.URL.hrefFromString(bun.String.fromBytes(string_builder.allocatedSlice())); defer normalized_url.deref(); + if (normalized_url.tag == .Dead) { + // URL__getHref failed, dont pass dead tagged string to toOwnedSlice. + return error.RedirectURLInvalid; + } const normalized_url_str = try normalized_url.toOwnedSlice(bun.default_allocator); const new_url = URL.parse(normalized_url_str); diff --git a/src/output.zig b/src/output.zig index ea239ca223..1fab443bb6 100644 --- a/src/output.zig +++ b/src/output.zig @@ -772,7 +772,7 @@ pub inline fn err(error_name: anytype, comptime fmt: []const u8, args: anytype) } // TODO: convert zig errors to errno for better searchability? - if (errors.len == 1) break :display_name .{ comptime @errorName(errors[0]), true }; + if (errors.len == 1) break :display_name .{ errors[0].name, true }; } break :display_name .{ @errorName(error_name), false };