Skip to content

Commit

Permalink
Leave whitespaced entities intact
Browse files Browse the repository at this point in the history
  • Loading branch information
ryrych committed Sep 9, 2016
1 parent 045d2d5 commit ddca209
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/sanitize/rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def config
@_config ||= {
:elements => ::ActionView::Base.sanitized_allowed_tags.to_a,
:attributes => { :all => ::ActionView::Base.sanitized_allowed_attributes.to_a },
:protocols => { :all => ::ActionView::Base.sanitized_allowed_protocols.to_a }
:protocols => { :all => ::ActionView::Base.sanitized_allowed_protocols.to_a },
:entities_whitelist => {}
}
end
else
Expand Down Expand Up @@ -64,7 +65,7 @@ def callback_for(options) #:nodoc:
point = (options[:on] || 'save').to_s

unless %w( save create ).include?(point)
raise ArgumentError, "Invalid callback point #{point}, valid ones are :save and :create"
raise ArgumentError, "Invalid callback point #{point}, valid ones are :save and :create"
end

"before_#{point}".intern
Expand All @@ -76,8 +77,16 @@ def method_for(fields) #:nodoc:

private

def decode_whitelistested_entities(string)
@_config[:entities_whitelist].each do |entity, decoded_value|
string.gsub!(entity.to_s, decoded_value.to_s)
end
string
end

def cleaned_fragment(string)
cleaner.fragment(string)
sanitized_string = cleaner.fragment(string)
decode_whitelistested_entities(sanitized_string) unless @_config[:entities_whitelist].empty?
end
end
end
7 changes: 7 additions & 0 deletions test/sanitize_rails_engine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def test_clean_not_producing_malicious_html_entities
assert_equal string, "<script>hello & world</script>"
end

def test_clean_not_making_explicit_html_entities
string = %Q|<script>hello & world</script>|
@engine.configure(entities_whitelist: { '&amp;': '&' })
@engine.clean! string
assert_equal string, "hello & world"
end

def test_clean_making_html_entities
string = %Q|<script>hello & world</script>|
@engine.clean! string
Expand Down

0 comments on commit ddca209

Please sign in to comment.