diff --git a/app/helpers/datatables_helper.rb b/app/helpers/datatables_helper.rb index 9ec3c968..289a2c49 100644 --- a/app/helpers/datatables_helper.rb +++ b/app/helpers/datatables_helper.rb @@ -2,6 +2,14 @@ # datatable view helpers module DatatablesHelper + # Inline formatting a title may carry (N2O, m2); 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 diff --git a/app/views/datatables/show.html.erb b/app/views/datatables/show.html.erb index 25bb7951..2284b73c 100644 --- a/app/views/datatables/show.html.erb +++ b/app/views/datatables/show.html.erb @@ -17,7 +17,7 @@ <% end %>
-

<%=raw datatable.title %> — <%= raw datatable.study.name %>

+

<%= sanitized_title(datatable.title) %> — <%= sanitized_title(datatable.study.name) %>

Data in the KBS <%= @subdomain_request.upcase %> core database may not be published without written permission of the lead investigator or project director Full terms of use @@ -87,7 +87,7 @@ Related Tables:

diff --git a/spec/requests/datatable_title_escaping_spec.rb b/spec/requests/datatable_title_escaping_spec.rb new file mode 100644 index 00000000..61d8bb18 --- /dev/null +++ b/spec/requests/datatable_title_escaping_spec.rb @@ -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('N2O at 5°C & more') + + expect(heading.inner_html).to include('N2O at 5°C & more') + end + + it "sanitizes the study's name too" do + study = FactoryBot.create(:study, name: 'Soil 2 study') + get datatable_path(FactoryBot.create(:datatable, study:)) + + heading = response.parsed_body.at('h1') + expect(heading.inner_html).to include('Soil 2', 'study') + 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 = 'CO2 flux' + 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 'CO2 alert(1)flux' + end + + it 'removes scripts, event handlers and links' do + heading = heading_for('Soil carbon link') + + 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