Skip to content

Commit 79d82d4

Browse files
authored
Merge pull request #1740 from coderdojo-japan/fix/brakeman-xss-warnings
Cross-Site Scripting警告の解消
2 parents 2bc0d97 + 70c8b5f commit 79d82d4

File tree

7 files changed

+30
-194
lines changed

7 files changed

+30
-194
lines changed

app/helpers/application_helper.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ def page_lang(lang)
4848
lang.empty? ? 'ja' : lang
4949
end
5050

51+
def sanitize_content(content)
52+
sanitize(content,
53+
tags: ActionView::Base.sanitized_allowed_tags + ['center'],
54+
attributes: ActionView::Base.sanitized_allowed_attributes + ['id']
55+
)
56+
end
57+
58+
def safe_dojo_url(dojo)
59+
return '#' if dojo.url.blank?
60+
61+
begin
62+
uri = URI.parse(dojo.url)
63+
uri.scheme&.match?(/\Ahttps?\z/) ? dojo.url : '#'
64+
rescue URI::InvalidURIError
65+
'#'
66+
end
67+
end
68+
5169
# 'inline_' プレフィックスがついたflashメッセージをビュー内で表示するヘルパー
5270
# inline_alert → alert, inline_warning → warning のように変換してBootstrapのCSSクラスを適用
5371
def render_inline_flash_messages

app/models/dojo.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def active?
9494
def active_at?(date)
9595
created_at <= date && (inactivated_at.nil? || inactivated_at > date)
9696
end
97-
97+
9898
# 再活性化メソッド
9999
def reactivate!
100100
if inactivated_at.present?

app/views/docs/show.html.erb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<div class='container' style='line-height: 1.9em;'>
1111
<section class='doc' style='padding: 50px 0px 100px 0px;'>
12-
<%= raw @content %>
12+
<%= sanitize_content(@content) %>
1313
</section>
1414

1515
<% if request.path.start_with? '/docs' %>
@@ -22,5 +22,3 @@
2222
<%= render 'shared/social_buttons' %>
2323
</section>
2424
</div>
25-
26-

app/views/podcasts/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
<iframe class="lazyload" src="https://anchor.fm/coderdojo-japan/embed/episodes/<%= @episode.permalink %>" width="100%" scrolling="no" frameborder="yes" style='margin-bottom: 30px;'></iframe>
3838

39-
<%= raw Rinku.auto_link(@content) %>
39+
<%= sanitize_content(Rinku.auto_link(@content)) %>
4040
</div>
4141
</section>
4242

app/views/shared/_dojo.html.erb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<li class="dojo" id="<%= dojo.name %>">
1+
<li class="dojo" id="<%= html_escape(dojo.name) %>">
22
<header>
3-
<%= link_to lazy_image_tag(dojo.logo, alt: "CoderDojo #{dojo.name}", class: 'dojo-picture'), dojo.url,
4-
target: "_blank", rel: "external noopener" %>
3+
<%= link_to lazy_image_tag(dojo.logo, alt: html_escape("CoderDojo #{dojo.name}"), class: 'dojo-picture'), safe_dojo_url(dojo),
4+
target: "_blank" %>
55
<span class="dojo-name">
6-
<%= link_to "#{dojo.name} (#{dojo.prefecture.name})", dojo.url, target: "_blank", rel: "external noopener" %>
6+
<%= link_to html_escape("#{dojo.name} (#{dojo.prefecture.name})"), safe_dojo_url(dojo), target: "_blank" %>
77
<% if not dojo.counter == 1 %>
88
<span class="dojo-counter"
99
data-original-title="道場数"
@@ -15,7 +15,7 @@
1515

1616
<ul class="tags">
1717
<% dojo.tags.first(5).each do |tag| %>
18-
<li><%= tag %></li>
18+
<li><%= html_escape(tag) %></li>
1919
<% end %>
2020

2121
<% if dojo.tags.length > 5 %>
@@ -26,12 +26,12 @@
2626
<div class="tooltip-arrow"></div>
2727
<div class="tooltip-inner"></div>
2828
</div>'
29-
title="<%= dojo.tags[5..].join(', ') %>">...</li>
29+
title="<%= html_escape(dojo.tags[5..].join(', ')) %>">...</li>
3030
<% end %>
3131
</ul>
3232

3333
<p class="dojo-description">
34-
<%= dojo.description %>
34+
<%= html_escape(dojo.description) %>
3535
<% if dojo.is_private %>
3636
<%= link_to 'Private', doc_path('private-dojo'), class: 'dojo-private' %>
3737
<% end %>

config/brakeman.ignore

Lines changed: 0 additions & 181 deletions
This file was deleted.

spec/requests/docs_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
get doc_path(param)
1515
doc = Document.new(param)
1616
expected = Kramdown::Document.new(doc.content, input: 'GFM').to_html
17+
expected = ApplicationController.helpers.sanitize_content(expected)
1718
expect(response.body).to include(expected.strip)
1819
end
1920

@@ -23,4 +24,4 @@
2324
expect(response.status).to eq 302
2425
end
2526
end
26-
end
27+
end

0 commit comments

Comments
 (0)