Skip to content

Cross-Site Scripting警告の解消 #1740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ def page_lang(lang)
lang.empty? ? 'ja' : lang
end

def sanitize_content(content)
sanitize(content,
tags: ActionView::Base.sanitized_allowed_tags + ['center'],
attributes: ActionView::Base.sanitized_allowed_attributes + ['id']
)
end

def safe_dojo_url(dojo)
return '#' if dojo.url.blank?

begin
uri = URI.parse(dojo.url)
uri.scheme&.match?(/\Ahttps?\z/) ? dojo.url : '#'
rescue URI::InvalidURIError
'#'
end
end

# 'inline_' プレフィックスがついたflashメッセージをビュー内で表示するヘルパー
# inline_alert → alert, inline_warning → warning のように変換してBootstrapのCSSクラスを適用
def render_inline_flash_messages
Expand Down
2 changes: 1 addition & 1 deletion app/models/dojo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def active?
def active_at?(date)
created_at <= date && (inactivated_at.nil? || inactivated_at > date)
end

# 再活性化メソッド
def reactivate!
if inactivated_at.present?
Expand Down
4 changes: 1 addition & 3 deletions app/views/docs/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

<% if request.path.start_with? '/docs' %>
Expand All @@ -22,5 +22,3 @@
<%= render 'shared/social_buttons' %>
</section>
</div>


2 changes: 1 addition & 1 deletion app/views/podcasts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

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

<%= raw Rinku.auto_link(@content) %>
<%= sanitize_content(Rinku.auto_link(@content)) %>
</div>
</section>

Expand Down
14 changes: 7 additions & 7 deletions app/views/shared/_dojo.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<li class="dojo" id="<%= dojo.name %>">
<li class="dojo" id="<%= html_escape(dojo.name) %>">
<header>
<%= link_to lazy_image_tag(dojo.logo, alt: "CoderDojo #{dojo.name}", class: 'dojo-picture'), dojo.url,
target: "_blank", rel: "external noopener" %>
<%= link_to lazy_image_tag(dojo.logo, alt: html_escape("CoderDojo #{dojo.name}"), class: 'dojo-picture'), safe_dojo_url(dojo),
target: "_blank" %>
<span class="dojo-name">
<%= link_to "#{dojo.name} (#{dojo.prefecture.name})", dojo.url, target: "_blank", rel: "external noopener" %>
<%= link_to html_escape("#{dojo.name} (#{dojo.prefecture.name})"), safe_dojo_url(dojo), target: "_blank" %>
<% if not dojo.counter == 1 %>
<span class="dojo-counter"
data-original-title="道場数"
Expand All @@ -15,7 +15,7 @@

<ul class="tags">
<% dojo.tags.first(5).each do |tag| %>
<li><%= tag %></li>
<li><%= html_escape(tag) %></li>
<% end %>

<% if dojo.tags.length > 5 %>
Expand All @@ -26,12 +26,12 @@
<div class="tooltip-arrow"></div>
<div class="tooltip-inner"></div>
</div>'
title="<%= dojo.tags[5..].join(', ') %>">...</li>
title="<%= html_escape(dojo.tags[5..].join(', ')) %>">...</li>
<% end %>
</ul>

<p class="dojo-description">
<%= dojo.description %>
<%= html_escape(dojo.description) %>
<% if dojo.is_private %>
<%= link_to 'Private', doc_path('private-dojo'), class: 'dojo-private' %>
<% end %>
Expand Down
181 changes: 0 additions & 181 deletions config/brakeman.ignore

This file was deleted.

3 changes: 2 additions & 1 deletion spec/requests/docs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
get doc_path(param)
doc = Document.new(param)
expected = Kramdown::Document.new(doc.content, input: 'GFM').to_html
expected = ApplicationController.helpers.sanitize_content(expected)
expect(response.body).to include(expected.strip)
end

Expand All @@ -23,4 +24,4 @@
expect(response.status).to eq 302
end
end
end
end