Skip to content

Commit 922ca9a

Browse files
committed
Merge pull request tenderlove#1 from xuanxu/master
return sanitized strings
2 parents e665a25 + 589762b commit 922ca9a

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

Gemfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
source 'http://rubygems.org'
22

3-
gem 'rails', :path => '/Users/aaron/git/rails'
4-
gem 'arel', :path => '/Users/aaron/git/arel'
5-
gem 'rack', :path => '/Users/aaron/git/rack'
3+
gem 'rails'
4+
gem 'arel'
5+
gem 'rack'
66
gem 'hoe'
77
gem 'minitest'
88

lib/rails_autolink.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ module TextHelper
1414
# will limit what should be linked. You can add HTML attributes to the links using
1515
# <tt>:html</tt>. Possible values for <tt>:link</tt> are <tt>:all</tt> (default),
1616
# <tt>:email_addresses</tt>, and <tt>:urls</tt>. If a block is given, each URL and
17-
# e-mail address is yielded and the result is used as the link text.
17+
# e-mail address is yielded and the result is used as the link text. By default the
18+
# text given is sanitized, you can override this behaviour setting the
19+
# <tt>:sanitize</tt> option to false.
1820
#
1921
# ==== Examples
2022
# auto_link("Go to http://www.rubyonrails.org and say hello to [email protected]")
@@ -48,15 +50,15 @@ module TextHelper
4850
# # => "Welcome to my new blog at <a href=\"http://www.myblog.com/\" target=\"_blank\">http://www.myblog.com</a>.
4951
# Please e-mail me at <a href=\"mailto:[email protected]\">[email protected]</a>."
5052
def auto_link(text, *args, &block)#link = :all, html = {}, &block)
51-
return '' if text.blank?
53+
return ''.html_safe if text.blank?
5254

5355
options = args.size == 2 ? {} : args.extract_options! # this is necessary because the old auto_link API has a Hash as its last parameter
5456
unless args.empty?
5557
options[:link] = args[0] || :all
5658
options[:html] = args[1] || {}
5759
end
5860
options.reverse_merge!(:link => :all, :html => {})
59-
61+
text = sanitize(text) unless options[:sanitize] == false
6062
case options[:link].to_sym
6163
when :all then auto_link_email_addresses(auto_link_urls(text, options[:html], options, &block), options[:html], &block)
6264
when :email_addresses then auto_link_email_addresses(text, options[:html], &block)

test/test_rails_autolink.rb

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,17 @@ def test_auto_link_with_block_with_html
8484

8585
def test_auto_link_should_sanitize_input_when_sanitize_option_is_not_false
8686
link_raw = %{http://www.rubyonrails.com?id=1&num=2}
87-
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)
87+
malicious_script = '<script>alert("malicious!")</script>'
88+
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}")
89+
assert auto_link("#{link_raw}#{malicious_script}").html_safe?
8890
end
8991

9092
def test_auto_link_should_not_sanitize_input_when_sanitize_option_is_false
9193
link_raw = %{http://www.rubyonrails.com?id=1&num=2}
92-
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, :sanitize => false)
94+
malicious_script = '<script>alert("malicious!")</script>'
95+
96+
assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a><script>alert("malicious!")</script>}, auto_link("#{link_raw}#{malicious_script}", :sanitize => false)
97+
assert !auto_link("#{link_raw}#{malicious_script}", :sanitize => false).html_safe?
9398
end
9499

95100
def test_auto_link_other_protocols
@@ -114,7 +119,7 @@ def test_auto_link_already_linked
114119
linked5 = %('<a href="#close">close</a> <a href="http://www.example.com"><b>www.example.com</b></a>')
115120
assert_equal linked1, auto_link(linked1)
116121
assert_equal linked2, auto_link(linked2)
117-
assert_equal linked3, auto_link(linked3)
122+
assert_equal linked3, auto_link(linked3, :sanitize => false)
118123
assert_equal linked4, auto_link(linked4)
119124
assert_equal linked5, auto_link(linked5)
120125

@@ -130,14 +135,25 @@ def test_auto_link_at_eol
130135
assert_equal %(<p><a href="#{url1}">#{url1}</a><br /><a href="#{url2}">#{url2}</a><br /></p>), auto_link("<p>#{url1}<br />#{url2}<br /></p>")
131136
end
132137

133-
def test_auto_link_should_not_be_html_safe
134-
email_raw = '[email protected]'
135-
link_raw = 'http://www.rubyonrails.org'
136-
137-
assert !auto_link(nil).html_safe?, 'should not be html safe'
138-
assert !auto_link('').html_safe?, 'should not be html safe'
139-
assert !auto_link("#{link_raw} #{link_raw} #{link_raw}").html_safe?, 'should not be html safe'
140-
assert !auto_link("hello #{email_raw}").html_safe?, 'should not be html safe'
138+
def test_auto_link_should_be_html_safe
139+
email_raw = '[email protected]'
140+
link_raw = 'http://www.rubyonrails.org'
141+
malicious_script = '<script>alert("malicious!")</script>'
142+
143+
assert auto_link(nil).html_safe?, 'should be html safe'
144+
assert auto_link('').html_safe?, 'should be html safe'
145+
assert auto_link("#{link_raw} #{link_raw} #{link_raw}").html_safe?, 'should be html safe'
146+
assert auto_link("hello #{email_raw}").html_safe?, 'should be html safe'
147+
assert auto_link("hello #{email_raw} #{malicious_script}").html_safe?, 'should be html safe'
148+
end
149+
150+
def test_auto_link_should_not_be_html_safe_when_sanitize_option_false
151+
email_raw = '[email protected]'
152+
link_raw = 'http://www.rubyonrails.org'
153+
154+
assert !auto_link("hello", :sanitize => false).html_safe?, 'should not be html safe'
155+
assert !auto_link("#{link_raw} #{link_raw} #{link_raw}", :sanitize => false).html_safe?, 'should not be html safe'
156+
assert !auto_link("hello #{email_raw}", :sanitize => false).html_safe?, 'should not be html safe'
141157
end
142158

143159
def test_auto_link_email_address

0 commit comments

Comments
 (0)