123 lines
6.2 KiB
Ruby
123 lines
6.2 KiB
Ruby
require "test_helper"
|
|
|
|
class HtmlHelperTest < ActionView::TestCase
|
|
test "convert URLs into anchor tags" do
|
|
assert_equal_html \
|
|
%(<p>Check this: <a href="https://example.com" rel="noopener noreferrer">https://example.com</a></p>),
|
|
format_html("<p>Check this: https://example.com</p>")
|
|
assert_equal_html \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a></p>),
|
|
format_html("<p>Check this: https://example.com/</p>")
|
|
end
|
|
|
|
test "convert multiple URLs in the same string" do
|
|
assert_equal_html \
|
|
%(Visit <a href="https://foo.com/" rel="noopener noreferrer">https://foo.com/</a>. Also see <a href="https://bar.com/" rel="noopener noreferrer">https://bar.com/</a>!),
|
|
format_html("Visit https://foo.com/. Also see https://bar.com/!")
|
|
end
|
|
|
|
test "don't include punctuation in URL autolinking" do
|
|
assert_equal_html \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>!</p>),
|
|
format_html("<p>Check this: https://example.com/!</p>")
|
|
assert_equal_html \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>.</p>),
|
|
format_html("<p>Check this: https://example.com/.</p>")
|
|
assert_equal_html \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>?</p>),
|
|
format_html("<p>Check this: https://example.com/?</p>")
|
|
assert_equal_html \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>,</p>),
|
|
format_html("<p>Check this: https://example.com/,</p>")
|
|
assert_equal_html \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>:</p>),
|
|
format_html("<p>Check this: https://example.com/:</p>")
|
|
assert_equal_html \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>;</p>),
|
|
format_html("<p>Check this: https://example.com/;</p>")
|
|
assert_equal_html \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>"</p>),
|
|
format_html("<p>Check this: https://example.com/\"</p>")
|
|
assert_equal_html \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>'</p>),
|
|
format_html("<p>Check this: https://example.com/'</p>")
|
|
|
|
# trailing entities that decode to punctuation
|
|
# use assert_equal and not assert_equal_html to make sure we're getting entities correct
|
|
assert_equal \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a><</p>),
|
|
format_html("<p>Check this: https://example.com/<</p>")
|
|
assert_equal \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>></p>),
|
|
format_html("<p>Check this: https://example.com/></p>")
|
|
assert_equal \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>"</p>),
|
|
format_html("<p>Check this: https://example.com/"</p>")
|
|
|
|
# multiple punctuation characters including entities
|
|
assert_equal_html \
|
|
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>!?;</p>),
|
|
format_html("<p>Check this: https://example.com/!?;</p>")
|
|
assert_equal_html \
|
|
%(<img src="<a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>">),
|
|
format_html(%(<img src="https://example.com/">))
|
|
assert_equal_html \
|
|
%(<img src="<a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>"!>),
|
|
format_html(%(<img src="https://example.com/"!>))
|
|
end
|
|
|
|
test "make sure the linked content is properly sanitized" do
|
|
# https://hackerone.com/reports/3481093
|
|
result = format_html(%(https://google.com/\">test</a><input></input>))
|
|
assert_no_match(/<input>/i, result, "should not create an input element")
|
|
|
|
result = format_html(%(https://google.com/\"><script>alert('xss')</script>))
|
|
assert_no_match(/<script>/i, result, "should not create a script element")
|
|
end
|
|
|
|
test "handle URLs with query parameters" do
|
|
# use assert_equal and not assert_equal_html to make sure we're getting entities correct
|
|
assert_equal \
|
|
%(<p>Check this: <a href="https://example.com/a?b=c&d=e" rel="noopener noreferrer">https://example.com/a?b=c&d=e</a></p>),
|
|
format_html("<p>Check this: https://example.com/a?b=c&d=e</p>")
|
|
|
|
assert_equal \
|
|
%(<p>Check this: <a href="https://example.com/a?b=c&d=e" rel="noopener noreferrer">https://example.com/a?b=c&d=e</a></p>),
|
|
format_html("<p>Check this: https://example.com/a?b=c&d=e</p>")
|
|
end
|
|
|
|
test "respect existing links" do
|
|
assert_equal_html \
|
|
%(<p>Check this: <a href="https://example.com">https://example.com</a></p>),
|
|
format_html("<p>Check this: <a href=\"https://example.com\">https://example.com</a></p>")
|
|
end
|
|
|
|
test "convert email addresses into mailto links" do
|
|
assert_equal_html \
|
|
%(<p>Contact us at <a href="mailto:support@example.com" rel="noopener noreferrer">support@example.com</a></p>),
|
|
format_html("<p>Contact us at support@example.com</p>")
|
|
end
|
|
|
|
test "respect existing linked emails" do
|
|
assert_equal_html \
|
|
%(<p>Contact us at <a href="mailto:support@example.com">support@example.com</a></p>),
|
|
format_html(%(<p>Contact us at <a href="mailto:support@example.com">support@example.com</a></p>))
|
|
end
|
|
|
|
test "don't autolink content in excluded elements" do
|
|
%w[ figcaption pre code ].each do |element|
|
|
assert_equal_html \
|
|
"<#{element}>Check this: https://example.com</#{element}>",
|
|
format_html("<#{element}>Check this: https://example.com</#{element}>")
|
|
end
|
|
end
|
|
|
|
test "preserve escaped HTML containing URLs" do
|
|
input = 'before text <img src="https://example.com/image.png"> after text'
|
|
output = format_html(input)
|
|
|
|
assert_no_match(/<img/, output, "should not create an img element")
|
|
assert_includes output, "<img"
|
|
end
|
|
end
|