Skip to content

Commit c6e2346

Browse files
committed
security: raw() を sanitize_content() に置き換えて XSS 脆弱性を修正
- docs/show.html.erb と podcasts/show.html.erb で raw() を sanitize_content() ヘルパーに変更 - ApplicationHelper に sanitize_content() メソッドを追加し、HTML サニタイズ処理を共通化 - Rails デフォルトに加えて 'center' タグと 'id' 属性を許可してコンテンツを適切に表示 - docs_spec.rb のテスト期待値にも同じサニタイズ処理を適用
1 parent 1cd92fa commit c6e2346

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

app/helpers/application_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ 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+
5158
# 'inline_' プレフィックスがついたflashメッセージをビュー内で表示するヘルパー
5259
# inline_alert → alert, inline_warning → warning のように変換してBootstrapのCSSクラスを適用
5360
def render_inline_flash_messages

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

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)