Skip to content
Open
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/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
require 'commonmarker'

module ApplicationHelper
# Inline formatting a title or citation may carry (N<sub>2</sub>O, <i>Zea mays</i>); 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?

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

# 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
17 changes: 5 additions & 12 deletions app/helpers/people_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,11 @@ def committee_affiliations(committee, role)
affiliations.flatten.uniq # .sort {|a,b| a.seniority <=> b.seniority }
end

# <li>Committee: Name, Name</li>, 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 = "<li>#{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}</li>"
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
6 changes: 3 additions & 3 deletions app/views/citations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
:crumbs => [{:name => controller_name.upcase, :url => url_for(:controller => controller_name, :action => 'index')},
{:name => @citation.short_author_string.upcase}] %>
<h2>
<%= raw @citation.formatted(long: true) %>
<%= sanitized_title(@citation.formatted(long: true)) %>
<% if @citation.doi.present? %>
<span>DOI: <%= link_to @citation.doi, normalize_doi(@citation.doi) %></span>
<% end %>
Expand All @@ -37,8 +37,8 @@
<% end %>
<p>
<%= markdown_to_html @citation.abstract %></p>
<% unless @citation.publisher_url.try(:empty?) %>
<p><%= link_to "vist the Publishers website", @citation.publisher_url %></p>
<% if (publisher_url = web_url(@citation.publisher_url)) %>
<p><%= link_to "vist the Publishers website", publisher_url %></p>
<% end %>

<% if @citation.data_url.present? %>
Expand Down
22 changes: 11 additions & 11 deletions app/views/people/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
<h2><%= role.name %></h2>
<% if role.id == 23 then %>
<ul>
<%=raw show_committee('Science Council',role) %>
<%=raw show_committee('Education Committee',role) %>
<%=raw show_committee('Communication Committee', role) %>
<%=raw show_committee('Social and Economic Science',role) %>
<%# <%=raw show_committee('Publications', role) %>
<%=raw show_committee('Climate', role) %>
<%# <%=raw show_committee('US ILTER',role) %>
<%=raw show_committee('Graduate Student Committee',role) %>
<%# <%=raw show_committee('Network Information System Advisory Committee',role) %>
<%=raw show_committee('Information Management Committee',role) %>
<%=raw show_committee('Diversity Committee',role) %>
<%= show_committee('Science Council',role) %>
<%= show_committee('Education Committee',role) %>
<%= show_committee('Communication Committee', role) %>
<%= show_committee('Social and Economic Science',role) %>
<%# <%= show_committee('Publications', role) %>
<%= show_committee('Climate', role) %>
<%# <%= show_committee('US ILTER',role) %>
<%= show_committee('Graduate Student Committee',role) %>
<%# <%= show_committee('Network Information System Advisory Committee',role) %>
<%= show_committee('Information Management Committee',role) %>
<%= show_committee('Diversity Committee',role) %>
</ul>
<% 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}
Expand Down
12 changes: 12 additions & 0 deletions spec/helpers/citations_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,<script>', '//example.com', 'example.com']
(urls + ['', ' ', nil]).each { |url| expect(helper.web_url(url)).to be_nil }
end
end

describe '#normalize_doi' do
it 'prefixes https:// when the value starts with doi.org/' do
expect(helper.normalize_doi('doi.org/10.1234/foo')).to eq('https://doi.org/10.1234/foo')
Expand Down
33 changes: 33 additions & 0 deletions spec/helpers/people_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,37 @@
expect(helper.contact_link(unmodified_email)).to eq(fixed_email)
end
end

describe '#show_committee' do
let(:role) { FactoryBot.create(:role) }

def join(committee, name, left_on: nil)
person = FactoryBot.create(:person, sur_name: name, given_name: 'Ada')
FactoryBot.create(:affiliation, person:, role:, title: committee, left_on:)
person
end

it 'lists the current members of the committee as links to their pages' do
first = join('Science Council', 'Smith')
second = join('Science Council', 'Jones')
join('Science Council', 'Gone', left_on: Date.new(2020, 1, 1))
join('Climate', 'Elsewhere')

html = helper.show_committee('Science Council', role)

expect(html).to be_html_safe
expect(html).to start_with('<li>Science Council: ')
expect(html).to include(%(href="#{person_path(first)}"), %(href="#{person_path(second)}"))
expect(html).not_to include('Gone', 'Elsewhere')
end

it 'escapes names and the committee, which the view no longer marks as safe' do
join('R&D <b>board</b>', 'O<script>alert(1)</script>Brien')

html = helper.show_committee('R&D <b>board</b>', role)

expect(html).to include('R&amp;D &lt;b&gt;board&lt;/b&gt;:', '&lt;script&gt;alert(1)&lt;/script&gt;')
expect(html).not_to include('<script>', '<b>board')
end
end
end
55 changes: 55 additions & 0 deletions spec/requests/citation_people_markup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require 'rails_helper'

# Citation pages and the people directory print text from records: simple formatting in a citation stays,
# and anything that could run in the page, or a link that is not a web address, is dropped.
RSpec.describe 'Markup from citation and people records', type: :request do
before { Website.create!(name: 'lter', url: 'lter.kbs.msu.edu') }

describe 'a citation page' do
def citation(attributes = {})
FactoryBot.create(:article_citation, state: 'published', pub_year: 2020, **attributes)
end

it 'keeps simple formatting in the citation and removes scripts' do
get citation_path(citation(title: 'Soil N<sub>2</sub>O <script>alert(1)</script> <i onclick="x()">flux</i>'))

heading = response.parsed_body.at('h2')
expect(heading.inner_html).to include('N<sub>2</sub>O', '<i>flux</i>')
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: '</script><script>alert(1)</script>'))

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 '</script><script>alert(1)</script>'
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: 'O<script>alert(1)</script>Brien', 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('<script>alert(1)</script>')
end
end
end
Loading