From 4e7929abb6db72b5a0a00e3db5ab8e58c5debf87 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Wed, 22 Nov 2023 15:19:40 +1000 Subject: [PATCH] FIX: Wrong argument error being thrown in UrlHelper (#24506) We were throwing ArgumentError in UrlHelper.normalised_encode, but it was incorrect -- we were passing ArgumentError.new 2 arguments which is not supported. Fix this and have a hint of which URL is causing the issue for debugging. --- lib/url_helper.rb | 4 +++- spec/lib/url_helper_spec.rb | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/url_helper.rb b/lib/url_helper.rb index e299a8e3232..4c333839ba6 100644 --- a/lib/url_helper.rb +++ b/lib/url_helper.rb @@ -69,7 +69,9 @@ class UrlHelper def self.normalized_encode(uri) url = uri.to_s - raise ArgumentError.new(:uri, "URL is too long") if url.length > MAX_URL_LENGTH + if url.length > MAX_URL_LENGTH + raise ArgumentError.new("URL starting with #{url[0..100]} is too long") + end # Ideally we will jump straight to `Addressable::URI.normalized_encode`. However, # that implementation has some edge-case issues like https://github.com/sporkmonger/addressable/issues/472. diff --git a/spec/lib/url_helper_spec.rb b/spec/lib/url_helper_spec.rb index 7bfad982594..81608561085 100644 --- a/spec/lib/url_helper_spec.rb +++ b/spec/lib/url_helper_spec.rb @@ -166,8 +166,10 @@ RSpec.describe UrlHelper do end it "raises error if too long" do - expect do UrlHelper.normalized_encode("https://#{"a" * 2_000}.com") end.to raise_error( + long_url = "https://#{"a" * 2_000}.com" + expect do UrlHelper.normalized_encode(long_url) end.to raise_error( ArgumentError, + "URL starting with #{long_url[0..100]} is too long", ) end end