Skip to content
Merged
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
2 changes: 1 addition & 1 deletion app/views/leaves/_edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<%= link_to leafable_path(leaf), class: "toc__thumbnail", data: { turbo_frame: "_top" } do %>
<%= leaf.section.body if leaf.section? %>
<%= leaf.leafable.body.to_html if leaf.page? %>
<%= sanitize_content(leaf.leafable.body.to_html) if leaf.page? %>
<%= image_tag leaf.leafable.image.variant(:large) if leaf.picture&.image&.attached? %>
<% end %>

Expand Down
4 changes: 2 additions & 2 deletions app/views/pages/edits/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<% end %>
</header>

<%= @edit.page.body.to_html %>
<%= sanitize_content(@edit.page.body.to_html) %>
<% end %>
</section>

Expand All @@ -62,6 +62,6 @@
<% end %>
</header>

<%= @leaf.page.body.to_html %>
<%= sanitize_content(@leaf.page.body.to_html) %>
</section>
</div>
21 changes: 21 additions & 0 deletions test/controllers/pages/edits_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,25 @@ class Pages::EditsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
assert_select "p", /such a great handbook/
end

test "show sanitizes dangerous content in previous version" do
leaf = books(:handbook).press Page.new(body: %(<img src=x onerror="alert(1)">)), title: "XSS Test"
leaf.edit leafable_params: { body: "Clean content now" }

get page_edit_url(leaf, leaf.edits.last)

assert_response :success
assert_match '<img src="x">', response.body
assert_no_match(/onerror/, response.body)
end

test "show sanitizes dangerous content in current version" do
leaves(:welcome_page).edit leafable_params: { body: %(<img src=x onerror="alert(1)">) }

get page_edit_url(leaves(:welcome_page), leaves(:welcome_page).edits.last)

assert_response :success
assert_match '<img src="x">', response.body
assert_no_match(/onerror/, response.body)
end
end
Loading