From de14709d0b723c89df8154122752a8ca6b750190 Mon Sep 17 00:00:00 2001 From: Patrik Ragnarsson Date: Tue, 10 Nov 2026 19:02:31 +0000 Subject: [PATCH 1/2] Pin how '+' in query parameter names is normalized Addressable::URI#query_values keeps a literal '+' in a parameter name as a plus, so Extended#normalized writes it as '%2B'. The next Addressable release reads it as a space, which changes normalized_url and with it the urlhash. These specs fail when that happens. --- spec/lib/twingly/url/extended_spec.rb | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/spec/lib/twingly/url/extended_spec.rb b/spec/lib/twingly/url/extended_spec.rb index 42219a0..69658ec 100644 --- a/spec/lib/twingly/url/extended_spec.rb +++ b/spec/lib/twingly/url/extended_spec.rb @@ -79,6 +79,14 @@ it { is_expected.to eq(expected) } end + # The urlhash is calculated from this URL, changing how '+' is treated changes the urlhash + context "keeps a literal '+' in a query parameter name as a plus, but rewrites it as a space in the value" do + let(:url) { "https://example.com/?a+b=c+d" } + let(:expected) { "https://www.example.com/?a%2Bb=c%20d" } + + it { is_expected.to eq(expected) } + end + context "with addressable_normalize: true" do subject { described_class.parse(url).normalized(addressable_normalize: true).to_s } @@ -240,6 +248,22 @@ end end + describe "#query_values" do + subject { described_class.parse(url).query_values } + + context "keeps a literal '+' in a name, but reads it as a space in a value" do + let(:url) { "https://example.com/?a+b=c+d" } + + it { is_expected.to eq("a+b" => "c d") } + end + + context "reads '%2B' as a plus in both names and values" do + let(:url) { "https://example.com/?a%2Bb=c%2Bd" } + + it { is_expected.to eq("a+b" => "c+d") } + end + end + describe "#callable_url" do it "is the addressable-normalized URL without blacklisted parameters" do url = described_class.parse("https://Example.com/p%c3%a4ge;jsessionid=X?k=1&utm_source=a&k=2") @@ -316,6 +340,16 @@ legacy_urlhash: "14653629529287702089") end + it "keeps a literal '+' in a query parameter name as '%2B' in normalized_url, so the urlhash stays the same" do + url = "https://example.com/?a+b=c" + + expect(described_class.normalize_and_calculate_urlhash(url)).to have_attributes(url: url, + callable_url: url, + normalized_url: "//www.example.com/?a%2Bb=c", + urlhash: "12381321737887489877", + legacy_urlhash: "8901784836407991551") + end + it "calculates the legacy urlhash from the normalized URL with its scheme kept" do result = described_class.normalize_and_calculate_urlhash("https://example.com/blog") @@ -402,6 +436,19 @@ def callable_query(url) expect(Addressable::URI.unencode_component(callable)).to eq("tag=c++") end + it "rewrites a literal '+' in a name as '%2B' in normalized_url, so it decodes to a plus, but keeps it in callable_url" do + url = "https://example.com/?a+b=c" + + normalized = normalized_query(url) + callable = callable_query(url) + + expect(normalized).to eq("a%2Bb=c") + expect(callable).to eq("a+b=c") + + expect(Addressable::URI.unencode_component(normalized)).to eq("a+b=c") + expect(Addressable::URI.unencode_component(callable)).to eq("a+b=c") + end + it "percent-encodes raw reserved characters in values in normalized_url, but keeps them in callable_url" do url = "https://example.com/?path=https://x/y" From 55ce2f16ca01c501ea18cf0d1f6cd54f5ab7d4a9 Mon Sep 17 00:00:00 2001 From: Patrik Ragnarsson Date: Tue, 10 Nov 2026 23:54:21 +0000 Subject: [PATCH 2/2] Parse the query in Extended without Addressable Extended#normalized built the normalized query from Addressable::URI#query_values, so the urlhash followed however Addressable reads a query. The next Addressable release reads '+' in a parameter name as a space, which would change the urlhash of every URL with such a name. Extended#query_values now splits and decodes the query itself, the way Addressable up to 2.9.2 does: '+' is a plus in a name and a space in a value. The output is the same for every Addressable version. --- lib/twingly/url/extended.rb | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/twingly/url/extended.rb b/lib/twingly/url/extended.rb index 78072eb..ff88de1 100644 --- a/lib/twingly/url/extended.rb +++ b/lib/twingly/url/extended.rb @@ -78,8 +78,17 @@ def original_url_without_blacklisted_parameters url.to_s end + # Parsed here instead of with Addressable::URI#query_values, as the urlhash depends on the result + # and has to stay the same when Addressable changes how it reads a query. def query_values - addressable_uri.query_values + return if query.nil? + + query.split("&").each_with_object({}) do |pair, values| + next if pair.empty? + + name, value = pair.split("=", 2) + values[decode_query_name(name)] = decode_query_value(value) + end end def query @@ -105,13 +114,25 @@ def normalized_path end def normalized_query - without_blacklisted_query_parameters(addressable_uri.query_values) + without_blacklisted_query_parameters(query_values) end def normalize_addressable_url addressable_uri.normalize end + # A literal '+' in a name is a plus + def decode_query_name(name) + Addressable::URI.unencode_component(name) + end + + # A literal '+' in a value is a space + def decode_query_value(value) + return if value.nil? + + Addressable::URI.unencode_component(value.tr("+", " ")) + end + def without_blacklisted_query_parameters(query_values) return if query_values.nil? @@ -128,7 +149,7 @@ def without_blacklisted_query_string(query) kept = query.split("&").reject do |pair| next true if pair.empty? - name = Addressable::URI.unencode_component(pair.split("=", 2).first) + name = decode_query_name(pair.split("=", 2).first) BLACKLISTED_QUERY_PARAMETERS.include?(name) end