Skip to content

Commit 4b6981b

Browse files
authored
Merge pull request #1956 from codidact/0valt/1059/tag-wiki-fix
Fix incorrect handling of rendered tag wikis
2 parents 6651c4d + 375fbe6 commit 4b6981b

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

app/helpers/tags_helper.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ def category_sort_tags(tags, required_ids, topic_ids, moderator_ids)
1515
end
1616
end
1717

18+
# Renders wiki for a given tag
19+
# @param tag [Tag] tag to render the wiki for
20+
# @return [ActiveSupport::SafeBuffer] rendered usage wiki
21+
def rendered_wiki(tag)
22+
sanitize(tag.wiki, scrubber: scrubber)
23+
end
24+
1825
##
1926
# Generate a list of classes to be applied to a tag.
2027
# @param tag [Tag]
@@ -38,4 +45,14 @@ def post_ids_for_tags(tag_ids)
3845
sql = "SELECT post_id FROM posts_tags WHERE tag_id IN #{ApplicationRecord.sanitize_sql_in(tag_ids)}"
3946
ActiveRecord::Base.connection.execute(sql).to_a.flatten
4047
end
48+
49+
class TagWikiScrubber < PostsHelper::PostScrubber
50+
def allowed_node?(node)
51+
super && !node.matches?("a[href=''], p:only-child:empty")
52+
end
53+
end
54+
55+
def scrubber
56+
TagsHelper::TagWikiScrubber.new
57+
end
4158
end

app/views/tags/show.html.erb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<% content_for :title, "Posts tagged #{@tag.name}" %>
22

3+
<%
4+
wiki = rendered_wiki(@tag)
5+
%>
6+
37
<h1 class="has-margin-0 has-margin-top-4">
48
Posts tagged <span class="<%= tag_classes(@tag, @category) %> is-large wrap-anywhere"><%= @tag.name %></span>
59
<% if at_least_moderator? %>
@@ -60,17 +64,17 @@
6064
<% end %>
6165
</div>
6266
<div class="widget--body">
63-
<% if @tag.wiki.present? %>
64-
<% if @tag.wiki.length < 600 %>
65-
<%= raw(sanitize(@tag.wiki, scrubber: scrubber)) %>
67+
<% if wiki.present? %>
68+
<% if wiki.length < 600 %>
69+
<%= wiki %>
6670
<% else %>
6771
<details>
6872
<summary>Tag Wiki</summary>
69-
<%= raw(sanitize(@tag.wiki, scrubber: scrubber)) %>
73+
<%= wiki %>
7074
</details>
7175
<% end %>
7276
<% end %>
73-
<% unless @tag.wiki.present? %>
77+
<% unless wiki.present? %>
7478
<p class="has-font-size-caption has-margin-0">
7579
<em>
7680
This tag doesn't have a detailed wiki yet.

test/fixtures/tags.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,17 @@ base:
4444
name: base
4545
community: sample
4646
tag_set: main
47+
48+
with_vacuous_wiki:
49+
name: vacuous-wiki
50+
community: sample
51+
tag_set: main
52+
wiki: '<p><a href=""></a></p>'
53+
wiki_markdown: '[]()'
54+
55+
with_wiki:
56+
name: with-wiki
57+
community: sample
58+
tag_set: main
59+
wiki: <p>This tag has a wiki</p>
60+
wiki_markdown: This tag has a wiki

test/helpers/tags_helper_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'test_helper'
2+
3+
class TagsHelperTest < ActionView::TestCase
4+
test 'rendered_wiki should correctly sanitize content' do
5+
vacuous_wiki_tag = tags(:with_vacuous_wiki)
6+
vacuous_wiki = rendered_wiki(vacuous_wiki_tag)
7+
assert vacuous_wiki.blank?
8+
9+
normal_wiki_tag = tags(:with_wiki)
10+
normal_wiki = rendered_wiki(normal_wiki_tag)
11+
assert_equal normal_wiki_tag.wiki, normal_wiki
12+
end
13+
end

0 commit comments

Comments
 (0)