From 1157a44d9732a76567df1bc5348d98ea55e04ad2 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 12:31:11 +0000 Subject: [PATCH 1/4] Escape the datatable title in the page heading The heading printed the title with raw, so markup in a title ran in the page. The page's tag and breadcrumb already escape it. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7 --- app/views/datatables/show.html.erb | 2 +- spec/requests/datatable_title_escaping_spec.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 spec/requests/datatable_title_escaping_spec.rb diff --git a/app/views/datatables/show.html.erb b/app/views/datatables/show.html.erb index 25bb7951..85fd099f 100644 --- a/app/views/datatables/show.html.erb +++ b/app/views/datatables/show.html.erb @@ -17,7 +17,7 @@ </div> <% end %> <div class='page type-page hfeed'> - <h1> <%=raw datatable.title %> — <%= raw datatable.study.name %></h1> + <h1> <%= datatable.title %> — <%= raw 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> diff --git a/spec/requests/datatable_title_escaping_spec.rb b/spec/requests/datatable_title_escaping_spec.rb new file mode 100644 index 00000000..5c324355 --- /dev/null +++ b/spec/requests/datatable_title_escaping_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'rails_helper' + +# A datatable's title is plain text: markup in it is shown, not run. +RSpec.describe 'Datatable title on its page', type: :request do + before { Website.create!(name: 'lter', url: 'lter.kbs.msu.edu') } + + it 'escapes the title in the heading' do + datatable = FactoryBot.create(:datatable, title: 'Soil <script>alert(1)</script> & water') + + get datatable_path(datatable) + + heading = response.parsed_body.at('h1').inner_html + expect(heading).to include('Soil <script>alert(1)</script> & water') + expect(response.parsed_body.css('h1 script')).to be_empty + end +end From 407582d1d6844b0e0500f6e99222cefb90b6d93d Mon Sep 17 00:00:00 2001 From: Claude <noreply@anthropic.com> Date: Tue, 3 Nov 2026 12:32:09 +0000 Subject: [PATCH 2/4] Sanitize the datatable title instead of escaping it Titles can carry simple formatting (N<sub>2</sub>O), so rather than showing the tags as text, the heading keeps sub, sup, em, i, strong and b without attributes and removes everything else, including scripts, links and event handlers. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7 --- app/helpers/datatables_helper.rb | 8 +++++++ app/views/datatables/show.html.erb | 2 +- .../requests/datatable_title_escaping_spec.rb | 23 +++++++++++++------ 3 files changed, 25 insertions(+), 8 deletions(-) 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 (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 diff --git a/app/views/datatables/show.html.erb b/app/views/datatables/show.html.erb index 85fd099f..4fbe137f 100644 --- a/app/views/datatables/show.html.erb +++ b/app/views/datatables/show.html.erb @@ -17,7 +17,7 @@ </div> <% end %> <div class='page type-page hfeed'> - <h1> <%= datatable.title %> — <%= raw datatable.study.name %></h1> + <h1> <%= sanitized_title(datatable.title) %> — <%= raw 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> diff --git a/spec/requests/datatable_title_escaping_spec.rb b/spec/requests/datatable_title_escaping_spec.rb index 5c324355..ddbd7ae6 100644 --- a/spec/requests/datatable_title_escaping_spec.rb +++ b/spec/requests/datatable_title_escaping_spec.rb @@ -2,17 +2,26 @@ require 'rails_helper' -# A datatable's title is plain text: markup in it is shown, not run. +# A datatable's title 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') } - it 'escapes the title in the heading' do - datatable = FactoryBot.create(:datatable, title: 'Soil <script>alert(1)</script> & water') + 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°C & <em>more</em>') + + expect(heading.inner_html).to include('N<sub>2</sub>O at 5°C & <em>more</em>') + end - get datatable_path(datatable) + 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>') - heading = response.parsed_body.at('h1').inner_html - expect(heading).to include('Soil <script>alert(1)</script> & water') - expect(response.parsed_body.css('h1 script')).to be_empty + 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 From 46ca7eb2165671702291dc68569a52dab5744a96 Mon Sep 17 00:00:00 2001 From: Claude <noreply@anthropic.com> Date: Tue, 3 Nov 2026 12:33:30 +0000 Subject: [PATCH 3/4] Sanitize the study name in the datatable heading too It sits next to the title in the same heading and was still printed with raw. It gets the same rules: simple inline formatting kept, everything that could run removed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7 --- app/views/datatables/show.html.erb | 2 +- spec/requests/datatable_title_escaping_spec.rb | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/views/datatables/show.html.erb b/app/views/datatables/show.html.erb index 4fbe137f..20e6dd9f 100644 --- a/app/views/datatables/show.html.erb +++ b/app/views/datatables/show.html.erb @@ -17,7 +17,7 @@ </div> <% end %> <div class='page type-page hfeed'> - <h1> <%= sanitized_title(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> diff --git a/spec/requests/datatable_title_escaping_spec.rb b/spec/requests/datatable_title_escaping_spec.rb index ddbd7ae6..7d161c6f 100644 --- a/spec/requests/datatable_title_escaping_spec.rb +++ b/spec/requests/datatable_title_escaping_spec.rb @@ -2,7 +2,8 @@ require 'rails_helper' -# A datatable's title may carry simple formatting; anything that could run in the page is removed. +# A datatable's title and its study's name 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') } @@ -17,6 +18,15 @@ def heading_for(title) expect(heading.inner_html).to include('N<sub>2</sub>O at 5°C & <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 '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>') From 35db02de73abf53787a000b911e137060a9872e9 Mon Sep 17 00:00:00 2001 From: Claude <noreply@anthropic.com> Date: Tue, 3 Nov 2026 12:34:23 +0000 Subject: [PATCH 4/4] Sanitize the titles of related tables on the datatable page They were printed with raw inside their links. They get the same rules as the heading: simple inline formatting kept, everything that could run removed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7 --- app/views/datatables/show.html.erb | 2 +- spec/requests/datatable_title_escaping_spec.rb | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/views/datatables/show.html.erb b/app/views/datatables/show.html.erb index 20e6dd9f..2284b73c 100644 --- a/app/views/datatables/show.html.erb +++ b/app/views/datatables/show.html.erb @@ -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> diff --git a/spec/requests/datatable_title_escaping_spec.rb b/spec/requests/datatable_title_escaping_spec.rb index 7d161c6f..61d8bb18 100644 --- a/spec/requests/datatable_title_escaping_spec.rb +++ b/spec/requests/datatable_title_escaping_spec.rb @@ -2,8 +2,8 @@ require 'rails_helper' -# A datatable's title and its study's name may carry simple formatting; anything that could run in the page -# is removed. +# 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') } @@ -27,6 +27,17 @@ def heading_for(title) 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>')