# encoding: utf-8
require_relative 'common'
describe 'Sanitize::Transformers::CleanElement' do
make_my_diffs_pretty!
parallelize_me!
strings = {
:basic => {
:html => 'Lorem ipsum dolor sit
amet ',
:default => 'Lorem ipsum dolor sit amet .foo { color: #fff; } alert("hello world");',
:restricted => 'Lorem ipsum dolor sit amet .foo { color: #fff; } alert("hello world");',
:basic => 'Lorem ipsum dolor sit
amet .foo { color: #fff; } alert("hello world");',
:relaxed => 'Lorem ipsum dolor sit
amet alert("hello world");'
},
:malformed => {
:html => 'Lorem dolor sit
amet ',
:default => 'Lorem ipsum dolor sit amet <script>alert("hello world");',
:restricted => 'Lorem ipsum dolor sit amet <script>alert("hello world");',
:basic => 'Lorem ipsum dolor sit
amet <script>alert("hello world");',
:relaxed => 'Lorem ipsum dolor sit
amet <script>alert("hello world");'
}
}
protocols = {
'protocol-based JS injection: simple, no spaces' => {
:html => 'foo',
:default => 'foo',
:restricted => 'foo',
:basic => 'foo',
:relaxed => 'foo'
},
'protocol-based JS injection: simple, spaces before' => {
:html => 'foo',
:default => 'foo',
:restricted => 'foo',
:basic => 'foo',
:relaxed => 'foo'
},
'protocol-based JS injection: simple, spaces after' => {
:html => 'foo',
:default => 'foo',
:restricted => 'foo',
:basic => 'foo',
:relaxed => 'foo'
},
'protocol-based JS injection: simple, spaces before and after' => {
:html => 'foo',
:default => 'foo',
:restricted => 'foo',
:basic => 'foo',
:relaxed => 'foo'
},
'protocol-based JS injection: preceding colon' => {
:html => 'foo',
:default => 'foo',
:restricted => 'foo',
:basic => 'foo',
:relaxed => 'foo'
},
'protocol-based JS injection: UTF-8 encoding' => {
:html => 'foo',
:default => 'foo',
:restricted => 'foo',
:basic => 'foo',
:relaxed => 'foo'
},
'protocol-based JS injection: long UTF-8 encoding' => {
:html => 'foo',
:default => 'foo',
:restricted => 'foo',
:basic => 'foo',
:relaxed => 'foo'
},
'protocol-based JS injection: long UTF-8 encoding without semicolons' => {
:html => 'foo',
:default => 'foo',
:restricted => 'foo',
:basic => 'foo',
:relaxed => 'foo'
},
'protocol-based JS injection: hex encoding' => {
:html => 'foo',
:default => 'foo',
:restricted => 'foo',
:basic => 'foo',
:relaxed => 'foo'
},
'protocol-based JS injection: long hex encoding' => {
:html => 'foo',
:default => 'foo',
:restricted => 'foo',
:basic => 'foo',
:relaxed => 'foo'
},
'protocol-based JS injection: hex encoding without semicolons' => {
:html => 'foo',
:default => 'foo',
:restricted => 'foo',
:basic => 'foo',
:relaxed => 'foo'
},
'protocol-based JS injection: null char' => {
:html => "",
:default => '',
:restricted => '',
:basic => '',
:relaxed => ''
},
'protocol-based JS injection: invalid URL char' => {
:html => '',
:default => '',
:restricted => '',
:basic => '',
:relaxed => ''
},
'protocol-based JS injection: spaces and entities' => {
:html => '',
:default => '',
:restricted => '',
:basic => '',
:relaxed => ''
},
'protocol whitespace' => {
:html => '',
:default => '',
:restricted => '',
:basic => '',
:relaxed => ''
}
}
describe 'Default config' do
it 'should remove non-whitelisted elements, leaving safe contents behind' do
Sanitize.fragment('foo bar baz quux')
.must_equal 'foo bar baz quux'
Sanitize.fragment('')
.must_equal 'alert("<xss>");'
Sanitize.fragment('<')
.must_equal '< script <>> alert("");'
end
it 'should surround the contents of :whitespace_elements with space characters when removing the element' do
Sanitize.fragment('foo
bar
' Sanitize.fragment(input).must_equal ' bar ' Sanitize.fragment(input, { :elements => ['p'], :attributes => {:all => ['class']} }).must_equal input Sanitize.fragment(input, { :elements => ['p'], :attributes => {'div' => ['class']} }).must_equal 'bar
' Sanitize.fragment(input, { :elements => ['p'], :attributes => {'p' => ['title'], :all => ['class']} }).must_equal input end it "should not allow relative URLs when relative URLs aren't whitelisted" do input = 'Link' Sanitize.fragment(input, :elements => ['a'], :attributes => {'a' => ['href']}, :protocols => {'a' => {'href' => ['http']}} ).must_equal 'Link' end it 'should allow relative URLs containing colons when the colon is not in the first path segment' do input = 'Random Page' Sanitize.fragment(input, { :elements => ['a'], :attributes => {'a' => ['href']}, :protocols => {'a' => {'href' => [:relative]}} }).must_equal input end it 'should allow relative URLs containing colons when the colon is part of an anchor' do input = 'Footnote 1' Sanitize.fragment(input, { :elements => ['a'], :attributes => {'a' => ['href']}, :protocols => {'a' => {'href' => [:relative]}} }).must_equal input input = 'Footnote 1' Sanitize.fragment(input, { :elements => ['a'], :attributes => {'a' => ['href']}, :protocols => {'a' => {'href' => [:relative]}} }).must_equal input end it 'should remove the contents of filtered nodes when :remove_contents is true' do Sanitize.fragment('foo barfoo
').must_equal "\nfoo\n" s.fragment('foo
bar
').must_equal "\nfoo\n\nbar\n" s.fragment('foo