From bcc42a130d4eb77b10b78b349d7c82583befcb2b Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Tue, 11 May 2021 11:21:03 -0400 Subject: [PATCH] Sanitize tag urls (#13720) * Sanitize tag URLs For tags with non-ASCII names, we were generating busted URLs, which would then cause errors. This commit fixes that particular error by generating usable URLs. * Use URL generator that will sanitize tag names * Remove path prefix when using a URL generator --- app/lib/url.rb | 2 +- app/views/admin/tags/edit.html.erb | 2 +- app/views/articles/_single_story.html.erb | 2 +- app/views/dashboards/following_tags.html.erb | 2 +- app/views/tags/_liquid.html.erb | 2 +- app/views/tags/edit.html.erb | 2 +- app/views/tags/index.html.erb | 2 +- spec/lib/url_spec.rb | 7 +++++++ 8 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/lib/url.rb b/app/lib/url.rb index f8d21106c..4d00db689 100644 --- a/app/lib/url.rb +++ b/app/lib/url.rb @@ -46,7 +46,7 @@ module URL # # @param tag [Tag] the tag to create the URL for def self.tag(tag, page = 1) - url(["/t/#{tag.name}", ("/page/#{page}" if page > 1)].join) + url(["/t/#{CGI.escape(tag.name)}", ("/page/#{page}" if page > 1)].join) end # Creates a user URL diff --git a/app/views/admin/tags/edit.html.erb b/app/views/admin/tags/edit.html.erb index 2f0eb7fa3..082d2aa25 100644 --- a/app/views/admin/tags/edit.html.erb +++ b/app/views/admin/tags/edit.html.erb @@ -5,7 +5,7 @@
- View + View
diff --git a/app/views/articles/_single_story.html.erb b/app/views/articles/_single_story.html.erb index cf5569bb3..be8dcdc50 100644 --- a/app/views/articles/_single_story.html.erb +++ b/app/views/articles/_single_story.html.erb @@ -59,7 +59,7 @@
<% flare_tag = FlareTag.new(story, @tag).tag %> <% if flare_tag %> - #<%= flare_tag.name %> + #<%= flare_tag.name %> <% end %> <% tags_to_display = story.cached_tag_list_array if flare_tag diff --git a/app/views/dashboards/following_tags.html.erb b/app/views/dashboards/following_tags.html.erb index cb9086f51..11bf1bae7 100644 --- a/app/views/dashboards/following_tags.html.erb +++ b/app/views/dashboards/following_tags.html.erb @@ -30,7 +30,7 @@ <% color = Color::CompareHex.new([tag.bg_color_hex || "#0000000", tag.text_color_hex || "#ffffff"]).brightness(0.8) %>

- + #<%= tag.name %> <% if follow.explicit_points < 0 %> diff --git a/app/views/tags/_liquid.html.erb b/app/views/tags/_liquid.html.erb index 1999cc821..7a3736208 100644 --- a/app/views/tags/_liquid.html.erb +++ b/app/views/tags/_liquid.html.erb @@ -7,7 +7,7 @@ }
-

#<%= tag.name %> <%= follow_btn %>

+

#<%= tag.name %> <%= follow_btn %>

<%= tag.short_summary %>
diff --git a/app/views/tags/edit.html.erb b/app/views/tags/edit.html.erb index 514c6984f..9b1ad729e 100644 --- a/app/views/tags/edit.html.erb +++ b/app/views/tags/edit.html.erb @@ -21,7 +21,7 @@

Editing: - <%= @tag.name %> + <%= @tag.name %>

#<%= @tag.name %> is the name of the tag used in Markdown. diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb index 86547cdcc..e04b83c8b 100644 --- a/app/views/tags/index.html.erb +++ b/app/views/tags/index.html.erb @@ -29,7 +29,7 @@ <% color = Color::CompareHex.new([tag.bg_color_hex || "#0000000", tag.text_color_hex || "#ffffff"]).brightness(0.8) %>

- + #<%= tag.name %>

diff --git a/spec/lib/url_spec.rb b/spec/lib/url_spec.rb index 797d0b329..d67ca75d7 100644 --- a/spec/lib/url_spec.rb +++ b/spec/lib/url_spec.rb @@ -94,6 +94,13 @@ RSpec.describe URL, type: :lib do it "returns the correct URL for a tag" do expect(described_class.tag(tag, 2)).to eq("https://dev.to/t/#{tag.name}/page/2") end + + it "returns the correct URL for a tag with a non-ASCII name" do + # Due to validations we can't trivially create a tag with a non-ASCII name + # so we just create something that quacks like one. + tag = instance_double("Tag", name: "histórico") + expect(described_class.tag(tag)).to eq("https://dev.to/t/hist%C3%B3rico") + end end describe ".deep_link" do