osem-fcy/spec/helpers/format_helper_spec.rb
Andrew Kvalheim 80d7ac545c Set nofollow on links in Markdown content
To disincentivize spamdexing, links in user-generated content should be
disavowed by annotation with `rel="nofollow"` attributes:

  - https://en.wikipedia.org/wiki/Nofollow

Automated spam has already targeted OSEM in the wild:

  - https://github.com/SeaGL/organization/issues/274

Ideally link annotation would be performed during Markdown rendering or
a single sanitization pass, but this is currently an unresolved issue:

  - https://github.com/vmg/redcarpet/issues/720
2023-03-03 17:16:53 -08:00

33 lines
962 B
Ruby

# frozen_string_literal: true
require 'spec_helper'
describe FormatHelper, type: :helper do
describe 'markdown' do
it 'should return empty string for nil' do
expect(markdown(nil)).to eq ''
end
it "doesn't render links with unsafe URI schemes" do
expect(markdown('[a](javascript:b)')).to eq "<p>[a](javascript:b)</p>\n"
end
it 'should return HTML for header markdown' do
expect(markdown('# this is my header')).to eq "<h1>this is my header</h1>\n"
end
it 'escapes input HTML' do
expect(markdown('<em>*a*</em>')).to eq "<p>&lt;em&gt;<em>a</em>&lt;/em&gt;</p>\n"
end
it 'removes unallowed elements' do
expect(markdown('<em>*<style>a</style>*</em>', false)).to eq "<p><em><em>a</em></em></p>\n"
end
it 'sets nofollow on links' do
expect(markdown('[a](https://example.com/)'))
.to eq "<p><a href=\"https://example.com/\" rel=\"nofollow\">a</a></p>\n"
end
end
end