@@ -88,6 +88,21 @@ def test_auto_link_should_sanitize_input_when_sanitize_option_is_not_false
8888 assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>} , auto_link ( "#{ link_raw } #{ malicious_script } " )
8989 assert auto_link ( "#{ link_raw } #{ malicious_script } " ) . html_safe?
9090 end
91+
92+ def test_auto_link_should_sanitize_input_with_sanitize_options
93+ link_raw = %{http://www.rubyonrails.com?id=1&num=2}
94+ malicious_script = '<script>alert("malicious!")</script>'
95+ text_with_attributes = %{<a href="http://ruby-lang-org" target="_blank" data-malicious="inject">Ruby</a>}
96+
97+ text_result = %{<a class="big" href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a><a href="http://ruby-lang-org" target="_blank">Ruby</a>}
98+ assert_equal text_result , auto_link ( "#{ link_raw } #{ malicious_script } #{ text_with_attributes } " ,
99+ :sanitize_options => { :attributes => [ "target" , "href" ] } ,
100+ :html => { :class => 'big' } )
101+
102+ assert auto_link ( "#{ link_raw } #{ malicious_script } #{ text_with_attributes } " ,
103+ :sanitize_options => { :attributes => [ "target" , "href" ] } ,
104+ :html => { :class => 'big' } ) . html_safe?
105+ end
91106
92107 def test_auto_link_should_not_sanitize_input_when_sanitize_option_is_false
93108 link_raw = %{http://www.rubyonrails.com?id=1&num=2}
@@ -117,11 +132,13 @@ def test_auto_link_already_linked
117132 linked3 = %('<a href="http://www.example.com" rel="nofollow">www.example.com</a>')
118133 linked4 = %('<a href="http://www.example.com"><b>www.example.com</b></a>')
119134 linked5 = %('<a href="#close">close</a> <a href="http://www.example.com"><b>www.example.com</b></a>')
135+ linked6 = %('<a href="#close">close</a> <a href="http://www.example.com" target="_blank" data-ruby="ror"><b>www.example.com</b></a>')
120136 assert_equal linked1 , auto_link ( linked1 )
121137 assert_equal linked2 , auto_link ( linked2 )
122138 assert_equal linked3 , auto_link ( linked3 , :sanitize => false )
123139 assert_equal linked4 , auto_link ( linked4 )
124140 assert_equal linked5 , auto_link ( linked5 )
141+ assert_equal linked6 , auto_link ( linked6 , :sanitize_options => { :attributes => [ "href" , "target" , "data-ruby" ] } )
125142
126143 linked_email = %Q(<a href="mailto:[email protected] ">Mail me</a>) 127144 assert_equal linked_email , auto_link ( linked_email )
0 commit comments