# frozen_string_literal: true
RSpec.shared_examples 'default allowlist' do
it 'sanitizes tags that are not allowed' do
act = %q{ and }
exp = 'no inputs and no blinks'
expect(filter(act).to_html).to eq exp
end
it 'sanitizes tag attributes' do
act = %q{Text}
exp = %q{Text}
expect(filter(act).to_html).to eq exp
end
it 'sanitizes javascript in attributes' do
act = %q(Text)
exp = 'Text'
expect(filter(act).to_html).to eq exp
end
it 'sanitizes mixed-cased javascript in attributes' do
act = %q(Text)
exp = 'Text'
expect(filter(act).to_html).to eq exp
end
it 'allows whitelisted HTML tags from the user' do
exp = act = "
\n- Term
\n- Definition
\n
"
expect(filter(act).to_html).to eq exp
end
it 'sanitizes `class` attribute on any element' do
act = %q{Strong}
expect(filter(act).to_html).to eq %q{Strong}
end
it 'sanitizes `id` attribute on any element' do
act = %q{Emphasis}
expect(filter(act).to_html).to eq %q{Emphasis}
end
end
RSpec.shared_examples 'XSS prevention' do
# Adapted from the Sanitize test suite: http://git.io/vczrM
protocols = {
'protocol-based JS injection: simple, no spaces' => {
input: 'foo',
output: 'foo'
},
'protocol-based JS injection: simple, spaces before' => {
input: 'foo',
output: 'foo'
},
'protocol-based JS injection: simple, spaces after' => {
input: 'foo',
output: 'foo'
},
'protocol-based JS injection: simple, spaces before and after' => {
input: 'foo',
output: 'foo'
},
'protocol-based JS injection: preceding colon' => {
input: 'foo',
output: 'foo'
},
'protocol-based JS injection: UTF-8 encoding' => {
input: 'foo',
output: 'foo'
},
'protocol-based JS injection: long UTF-8 encoding' => {
input: 'foo',
output: 'foo'
},
'protocol-based JS injection: long UTF-8 encoding without semicolons' => {
input: 'foo',
output: 'foo'
},
'protocol-based JS injection: hex encoding' => {
input: 'foo',
output: 'foo'
},
'protocol-based JS injection: long hex encoding' => {
input: 'foo',
output: 'foo'
},
'protocol-based JS injection: hex encoding without semicolons' => {
input: 'foo',
output: 'foo'
},
'protocol-based JS injection: null char' => {
input: "foo",
output: ''
},
'protocol-based JS injection: invalid URL char' => {
input: '
',
output: '
'
},
'protocol-based JS injection: Unicode' => {
input: %Q(foo),
output: 'foo'
},
'protocol-based JS injection: spaces and entities' => {
input: 'foo',
output: 'foo'
},
'protocol whitespace' => {
input: '',
output: ''
}
}
protocols.each do |name, data|
it "disallows #{name}" do
doc = filter(data[:input])
expect(doc.to_html).to eq data[:output]
end
end
it 'disallows data links' do
input = 'XSS'
output = filter(input)
expect(output.to_html).to eq 'XSS'
end
it 'disallows vbscript links' do
input = 'XSS'
output = filter(input)
expect(output.to_html).to eq 'XSS'
end
end
RSpec.shared_examples 'sanitize link' do
it 'removes `rel` attribute from `a` elements' do
act = %q{Link}
exp = %q{Link}
expect(filter(act).to_html).to eq exp
end
it 'disallows invalid URIs' do
expect(Addressable::URI).to receive(:parse).with('foo://example.com')
.and_raise(Addressable::URI::InvalidURIError)
input = 'Foo'
output = filter(input)
expect(output.to_html).to eq 'Foo'
end
it 'allows non-standard anchor schemes' do
exp = %q{IRC}
act = filter(exp)
expect(act.to_html).to eq exp
end
it 'allows relative links' do
exp = %q{foo/bar.md}
act = filter(exp)
expect(act.to_html).to eq exp
end
end