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
8 changes: 8 additions & 0 deletions app/helpers/datatables_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

# datatable view helpers
module DatatablesHelper
# Inline formatting a title may carry (N<sub>2</sub>O, m<sup>2</sup>); anything else is removed, and no
# tag keeps its attributes
TITLE_TAGS = %w[sub sup em i strong b].freeze

def sanitized_title(title)
sanitize(title.to_s, tags: TITLE_TAGS, attributes: [])
end

# Long HTML collapses to a plain-text excerpt that expands to the full text (no JavaScript)
def expandable_html(html, length: 800)
text = strip_tags(html).squish
Expand Down
4 changes: 2 additions & 2 deletions app/views/datatables/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</div>
<% end %>
<div class='page type-page hfeed'>
<h1> <%=raw datatable.title %> — <%= raw datatable.study.name %></h1>
<h1> <%= sanitized_title(datatable.title) %> — <%= sanitized_title(datatable.study.name) %></h1>

<div>
<p> <em>Data in the KBS <%= @subdomain_request.upcase %> core database may not be published without written permission of the lead investigator or project director <a href="https://lter.kbs.msu.edu/data/terms-of-use/">Full terms of use</a></em>
Expand Down Expand Up @@ -87,7 +87,7 @@
<td>Related Tables:</td>
<td><ul>
<% datatable.related_tables.each do |table| %>
<li><%=link_to raw(table.title) , datatable_path(table) %></li>
<li><%=link_to sanitized_title(table.title), datatable_path(table) %></li>
<% end %>
</ul></td>
</tr>
Expand Down
48 changes: 48 additions & 0 deletions spec/requests/datatable_title_escaping_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require 'rails_helper'

# A datatable's title, its study's name and the titles of its related tables may carry simple formatting;
# anything that could run in the page is removed.
RSpec.describe 'Datatable title on its page', type: :request do
before { Website.create!(name: 'lter', url: 'lter.kbs.msu.edu') }

def heading_for(title)
get datatable_path(FactoryBot.create(:datatable, title:))
response.parsed_body.at('h1')
end

it 'keeps simple formatting and entities' do
heading = heading_for('N<sub>2</sub>O at 5&deg;C & <em>more</em>')

expect(heading.inner_html).to include('N<sub>2</sub>O at 5°C &amp; <em>more</em>')
end

it "sanitizes the study's name too" do
study = FactoryBot.create(:study, name: 'Soil <sub>2</sub> <script>alert(1)</script><b onclick="x()">study</b>')
get datatable_path(FactoryBot.create(:datatable, study:))

heading = response.parsed_body.at('h1')
expect(heading.inner_html).to include('Soil <sub>2</sub>', '<b>study</b>')
expect(heading.css('script, [onclick]')).to be_empty
end

it 'sanitizes the titles of related tables, which stay links' do
datatable = FactoryBot.create(:datatable)
title = 'CO<sub>2</sub> <script>alert(1)</script><i onmouseover="x()">flux</i>'
related = FactoryBot.create(:datatable, dataset: datatable.dataset, study: datatable.study, title:)

get datatable_path(datatable)

link = response.parsed_body.at("a[href='#{datatable_path(related)}']")
expect(link.inner_html).to eq 'CO<sub>2</sub> alert(1)<i>flux</i>'
end

it 'removes scripts, event handlers and links' do
heading = heading_for('Soil <script>alert(1)</script><b onclick="x()">carbon</b> <a href="javascript:x()">link</a>')

expect(heading.css('script, a, [onclick]')).to be_empty
expect(heading.text).to include('Soil', 'carbon', 'link')
expect(heading.inner_html).not_to include('alert(1)>', 'onclick', 'javascript:')
end
end
Loading