Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle sanitize_options in simple_format helper #48355

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions actionview/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
* `simple_format` helper now handles a `:sanitize_options` - any extra options you want appending to the sanitize.

Before:
```ruby
simple_format("<a target=\"_blank\" href=\"http://example.com\">Continue</a>")
# => "<p><a href=\"http://example.com\">Continue</a></p>"
```

After:
```ruby
simple_format("<a target=\"_blank\" href=\"http://example.com\">Continue</a>", {}, { sanitize_options: { attributes: %w[target href] } })
# => "<p><a target=\"_blank\" href=\"http://example.com\">Continue</a></p>"
```

*Andrei Andriichuk*

* Add support for HTML5 standards-compliant sanitizers, and default to `Rails::HTML5::Sanitizer`
in the Rails 7.1 configuration if it is supported.

Expand Down
6 changes: 5 additions & 1 deletion actionview/lib/action_view/helpers/text_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ def word_wrap(text, line_width: 80, break_sequence: "\n")
#
# ==== Options
# * <tt>:sanitize</tt> - If +false+, does not sanitize +text+.
# * <tt>:sanitize_options</tt> - Any extra options you want appended to the sanitize.
# * <tt>:wrapper_tag</tt> - String representing the wrapper tag, defaults to <tt>"p"</tt>
#
# ==== Examples
Expand All @@ -315,10 +316,13 @@ def word_wrap(text, line_width: 80, break_sequence: "\n")
#
# simple_format("<blink>Blinkable!</blink> It's true.", {}, sanitize: false)
# # => "<p><blink>Blinkable!</blink> It's true.</p>"
#
# simple_format("<a target=\"_blank\" href=\"http://example.com\">Continue</a>", {}, { sanitize_options: { attributes: %w[target href] } })
# # => "<p><a target=\"_blank\" href=\"http://example.com\">Continue</a></p>"
def simple_format(text, html_options = {}, options = {})
wrapper_tag = options.fetch(:wrapper_tag, :p)

text = sanitize(text) if options.fetch(:sanitize, true)
text = sanitize(text, options.fetch(:sanitize_options, {})) if options.fetch(:sanitize, true)
paragraphs = split_paragraphs(text)

if paragraphs.empty?
Expand Down
9 changes: 9 additions & 0 deletions actionview/test/template/text_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ def test_simple_format_should_sanitize_input_when_sanitize_option_is_true
simple_format("<b> test with unsafe string </b><script>code!</script>", {}, { sanitize: true })
end

def test_simple_format_should_sanitize_input_when_sanitize_options_is_specified
assert_equal "<p><a target=\"_blank\" href=\"http://example.com\">Continue</a></p>",
simple_format("<a target=\"_blank\" href=\"http://example.com\">Continue</a>", {}, { sanitize_options: { attributes: %w[target href] } })
end

def test_simple_format_should_sanitize_input_when_sanitize_options_is_not_specified
assert_equal "<p><a href=\"http://example.com\">Continue</a></p>", simple_format("<a target=\"_blank\" href=\"http://example.com\">Continue</a>")
end

def test_simple_format_should_not_sanitize_input_when_sanitize_option_is_false
assert_equal "<p><b> test with unsafe string </b><script>code!</script></p>", simple_format("<b> test with unsafe string </b><script>code!</script>", {}, { sanitize: false })
end
Expand Down