2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Banzai::Filter::SanitizationFilter do
|
2015-12-23 02:04:40 +05:30
|
|
|
include FilterSpecHelper
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
it_behaves_like 'default whitelist'
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
describe 'custom whitelist' do
|
2020-01-01 13:55:28 +05:30
|
|
|
it_behaves_like 'XSS prevention'
|
|
|
|
it_behaves_like 'sanitize link'
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
it 'customizes the whitelist only once' do
|
|
|
|
instance = described_class.new('Foo')
|
2018-03-17 18:26:18 +05:30
|
|
|
control_count = instance.whitelist[:transformers].size
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
3.times { instance.whitelist }
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(instance.whitelist[:transformers].size).to eq control_count
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'customizes the whitelist only once for different instances' do
|
|
|
|
instance1 = described_class.new('Foo1')
|
|
|
|
instance2 = described_class.new('Foo2')
|
|
|
|
control_count = instance1.whitelist[:transformers].size
|
|
|
|
|
|
|
|
instance1.whitelist
|
|
|
|
instance2.whitelist
|
|
|
|
|
|
|
|
expect(instance1.whitelist[:transformers].size).to eq control_count
|
|
|
|
expect(instance2.whitelist[:transformers].size).to eq control_count
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'sanitizes `class` attribute from all elements' do
|
|
|
|
act = %q{<pre class="code highlight white c"><code><span class="k">def</span></code></pre>}
|
|
|
|
exp = %q{<pre><code><span class="k">def</span></code></pre>}
|
2015-12-23 02:04:40 +05:30
|
|
|
expect(filter(act).to_html).to eq exp
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sanitizes `class` attribute from non-highlight spans' do
|
|
|
|
act = %q{<span class="k">def</span>}
|
|
|
|
expect(filter(act).to_html).to eq %q{<span>def</span>}
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'allows `text-align` property in `style` attribute on table elements' do
|
|
|
|
html = <<~HTML
|
2015-12-23 02:04:40 +05:30
|
|
|
<table>
|
|
|
|
<tr><th style="text-align: center">Head</th></tr>
|
|
|
|
<tr><td style="text-align: right">Body</th></tr>
|
|
|
|
</table>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
doc = filter(html)
|
|
|
|
|
|
|
|
expect(doc.at_css('th')['style']).to eq 'text-align: center'
|
|
|
|
expect(doc.at_css('td')['style']).to eq 'text-align: right'
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'disallows other properties in `style` attribute on table elements' do
|
|
|
|
html = <<~HTML
|
|
|
|
<table>
|
|
|
|
<tr><th style="text-align: foo">Head</th></tr>
|
|
|
|
<tr><td style="position: fixed; height: 50px; width: 50px; background: red; z-index: 999; font-size: 36px; text-align: center">Body</th></tr>
|
|
|
|
</table>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
doc = filter(html)
|
|
|
|
|
|
|
|
expect(doc.at_css('th')['style']).to be_nil
|
|
|
|
expect(doc.at_css('td')['style']).to eq 'text-align: center'
|
|
|
|
end
|
|
|
|
|
2018-06-27 16:04:02 +05:30
|
|
|
it 'disallows `text-align` property in `style` attribute on other elements' do
|
|
|
|
html = <<~HTML
|
|
|
|
<div style="text-align: center">Text</div>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
doc = filter(html)
|
|
|
|
|
|
|
|
expect(doc.at_css('div')['style']).to be_nil
|
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
it 'allows `span` elements' do
|
|
|
|
exp = act = %q{<span>Hello</span>}
|
|
|
|
expect(filter(act).to_html).to eq exp
|
|
|
|
end
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
it 'allows `abbr` elements' do
|
|
|
|
exp = act = %q{<abbr title="HyperText Markup Language">HTML</abbr>}
|
|
|
|
expect(filter(act).to_html).to eq exp
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it 'disallows the `name` attribute globally, allows on `a`' do
|
2017-09-10 17:25:29 +05:30
|
|
|
html = <<~HTML
|
|
|
|
<img name="getElementById" src="">
|
|
|
|
<span name="foo" class="bar">Hi</span>
|
2018-03-17 18:26:18 +05:30
|
|
|
<a name="foo" class="bar">Bye</a>
|
2017-09-10 17:25:29 +05:30
|
|
|
HTML
|
|
|
|
|
|
|
|
doc = filter(html)
|
|
|
|
|
|
|
|
expect(doc.at_css('img')).not_to have_attribute('name')
|
|
|
|
expect(doc.at_css('span')).not_to have_attribute('name')
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(doc.at_css('a')).to have_attribute('name')
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'allows `summary` elements' do
|
|
|
|
exp = act = '<summary>summary line</summary>'
|
|
|
|
expect(filter(act).to_html).to eq exp
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows `details` elements' do
|
|
|
|
exp = act = '<details>long text goes here</details>'
|
|
|
|
expect(filter(act).to_html).to eq exp
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'allows `data-math-style` attribute on `code` and `pre` elements' do
|
|
|
|
html = <<-HTML
|
|
|
|
<pre class="code" data-math-style="inline">something</pre>
|
|
|
|
<code class="code" data-math-style="inline">something</code>
|
|
|
|
<div class="code" data-math-style="inline">something</div>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
output = <<-HTML
|
|
|
|
<pre data-math-style="inline">something</pre>
|
|
|
|
<code data-math-style="inline">something</code>
|
|
|
|
<div>something</div>
|
|
|
|
HTML
|
|
|
|
|
|
|
|
expect(filter(html).to_html).to eq(output)
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
it 'allows the `data-sourcepos` attribute globally' do
|
|
|
|
exp = %q{<p data-sourcepos="1:1-1:10">foo/bar.md</p>}
|
|
|
|
act = filter(exp)
|
|
|
|
|
|
|
|
expect(act.to_html).to eq exp
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'footnotes' do
|
|
|
|
it 'allows correct footnote id property on links' do
|
|
|
|
exp = %q{<a href="#fn1" id="fnref1">foo/bar.md</a>}
|
|
|
|
act = filter(exp)
|
|
|
|
|
|
|
|
expect(act.to_html).to eq exp
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows correct footnote id property on li element' do
|
|
|
|
exp = %q{<ol><li id="fn1">footnote</li></ol>}
|
|
|
|
act = filter(exp)
|
|
|
|
|
|
|
|
expect(act.to_html).to eq exp
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes invalid id for footnote links' do
|
|
|
|
exp = %q{<a href="#fn1">link</a>}
|
|
|
|
|
|
|
|
%w[fnrefx test xfnref1].each do |id|
|
|
|
|
act = filter(%Q{<a href="#fn1" id="#{id}">link</a>})
|
|
|
|
|
|
|
|
expect(act.to_html).to eq exp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes invalid id for footnote li' do
|
|
|
|
exp = %q{<ol><li>footnote</li></ol>}
|
|
|
|
|
|
|
|
%w[fnx test xfn1].each do |id|
|
|
|
|
act = filter(%Q{<ol><li id="#{id}">footnote</li></ol>})
|
|
|
|
|
|
|
|
expect(act.to_html).to eq exp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows footnotes numbered higher than 9' do
|
|
|
|
exp = %q{<a href="#fn15" id="fnref15">link</a><ol><li id="fn15">footnote</li></ol>}
|
|
|
|
act = filter(exp)
|
|
|
|
|
|
|
|
expect(act.to_html).to eq exp
|
|
|
|
end
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|