diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f0ff5e7c..d255b9ba 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -4,6 +4,14 @@ require 'commonmarker' module ApplicationHelper + # Inline formatting a title or citation may carry (N2O, Zea mays); 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 + def markdown_to_html(text) return nil if text.blank? diff --git a/app/helpers/citations_helper.rb b/app/helpers/citations_helper.rb index 401a8063..5a0689ff 100644 --- a/app/helpers/citations_helper.rb +++ b/app/helpers/citations_helper.rb @@ -12,6 +12,11 @@ def citation_link_label(citation, link) "#{link}: #{strip_tags(citation.title.to_s).squish}, #{citation.author_and_year.to_s.squish.delete_suffix('.')}" end + # A link taken from a citation's record: only web addresses, never javascript: or data: URLs + def web_url(url) + url.to_s.strip.presence&.then { |address| address if address.match?(%r{\Ahttps?://}i) } + end + def normalize_doi(doi) if doi.match?(%r{\Ahttps?://}) doi diff --git a/app/helpers/datatables_helper.rb b/app/helpers/datatables_helper.rb index 289a2c49..9ec3c968 100644 --- a/app/helpers/datatables_helper.rb +++ b/app/helpers/datatables_helper.rb @@ -2,14 +2,6 @@ # 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/helpers/people_helper.rb b/app/helpers/people_helper.rb index 13028466..2b4b0cc8 100644 --- a/app/helpers/people_helper.rb +++ b/app/helpers/people_helper.rb @@ -23,18 +23,11 @@ def committee_affiliations(committee, role) affiliations.flatten.uniq # .sort {|a,b| a.seniority <=> b.seniority } end + #
  • Committee: Name, Name
  • , each name linking to the person's page. Built as safe HTML, so the names + # are escaped and the view needs no raw. def show_committee(committee, role) - first_one = true - html = "
  • #{committee}: " - committee_people = committee_affiliations(committee, role).collect(&:person) - committee_people.each do |person| - if first_one - first_one = false - else - html += ', ' - end - html += link_to person.full_name.to_s, person_path(person) - end - "#{html}
  • " + people = committee_affiliations(committee, role).collect(&:person) + links = people.collect { |person| link_to(person.full_name.to_s, person_path(person)) } + tag.li(safe_join(["#{committee}: ", safe_join(links, ', ')])) end end diff --git a/app/views/citations/show.html.erb b/app/views/citations/show.html.erb index ad8fc1f5..9271223c 100644 --- a/app/views/citations/show.html.erb +++ b/app/views/citations/show.html.erb @@ -24,7 +24,7 @@ :crumbs => [{:name => controller_name.upcase, :url => url_for(:controller => controller_name, :action => 'index')}, {:name => @citation.short_author_string.upcase}] %>

    - <%= raw @citation.formatted(long: true) %> + <%= sanitized_title(@citation.formatted(long: true)) %> <% if @citation.doi.present? %> DOI: <%= link_to @citation.doi, normalize_doi(@citation.doi) %> <% end %> @@ -37,8 +37,8 @@ <% end %>

    <%= markdown_to_html @citation.abstract %>

    - <% unless @citation.publisher_url.try(:empty?) %> -

    <%= link_to "vist the Publishers website", @citation.publisher_url %>

    + <% if (publisher_url = web_url(@citation.publisher_url)) %> +

    <%= link_to "vist the Publishers website", publisher_url %>

    <% end %> <% if @citation.data_url.present? %> diff --git a/app/views/people/index.html.erb b/app/views/people/index.html.erb index f81e4a22..2f0a64f8 100644 --- a/app/views/people/index.html.erb +++ b/app/views/people/index.html.erb @@ -8,17 +8,17 @@

    <%= role.name %>

    <% if role.id == 23 then %> <% else %> <% people = role.people.uniq.sort {|a,b| a.affiliations.find_by_role_id(role).seniority.to_s + a.sur_name <=> b.affiliations.find_by_role_id(role).seniority.to_s + b.sur_name} diff --git a/spec/helpers/citations_helper_spec.rb b/spec/helpers/citations_helper_spec.rb index faa94ab5..a084c2f3 100644 --- a/spec/helpers/citations_helper_spec.rb +++ b/spec/helpers/citations_helper_spec.rb @@ -3,6 +3,18 @@ require 'rails_helper' RSpec.describe CitationsHelper, type: :helper do + describe '#web_url' do + it 'keeps web addresses' do + expect(helper.web_url('https://example.com/paper')).to eq 'https://example.com/paper' + expect(helper.web_url(' http://example.com ')).to eq 'http://example.com' + end + + it 'drops anything else, and blanks' do + urls = ['javascript:alert(1)', 'JaVaScRiPt:alert(1)', 'data:text/html,Brien') + + html = helper.show_committee('R&D board', role) + + expect(html).to include('R&D <b>board</b>:', '<script>alert(1)</script>') + expect(html).not_to include(' flux')) + + heading = response.parsed_body.at('h2') + expect(heading.inner_html).to include('N2O', 'flux') + expect(heading.css('script, [onclick]')).to be_empty + end + + it 'keeps a title from breaking out of the page data' do + get citation_path(citation(title: '')) + + scripts = response.parsed_body.css('script').reject { |script| script['type'] == 'application/ld+json' } + data = response.parsed_body.at('script[type="application/ld+json"]').text + expect(scripts.map(&:text).join).not_to include('alert(1)') + expect(JSON.parse(data)['headline']).to eq '' + end + + it 'links the publisher only when the address is a web address' do + get citation_path(citation(publisher_url: 'javascript:alert(1)')) + expect(response.parsed_body.css('a').map { |link| link['href'].to_s }).not_to include(/javascript:/i) + + get citation_path(citation(publisher_url: 'https://publisher.example.org/paper')) + expect(response.parsed_body.css('a[href="https://publisher.example.org/paper"]')).not_to be_empty + end + end + + describe 'the people directory' do + it 'lists committee members as links, with names escaped' do + role = FactoryBot.create(:role, id: 23, name: 'Committees', role_type: RoleType.create!(name: 'lter')) + person = FactoryBot.create(:person, sur_name: 'OBrien', given_name: 'Ada') + FactoryBot.create(:affiliation, person:, role:, title: 'Science Council') + + get people_path + + item = response.parsed_body.css('li').find { |li| li.text.start_with?('Science Council:') } + expect(item.at("a[href='#{person_path(person)}']")).to be_present + expect(item.css('script')).to be_empty + expect(item.text).to include('') + end + end +end