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: |
<% datatable.related_tables.each do |table| %>
- - <%=link_to raw(table.title) , datatable_path(table) %>
+ - <%=link_to sanitized_title(table.title), datatable_path(table) %>
<% end %>
|
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('N
2O at 5°C &
more')
+
+ expect(heading.inner_html).to include('N
2O 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 = 'CO
2 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 'CO
2 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