File tree Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -25,15 +25,16 @@ class SanitizationFilter < Filter
2525 # of places we're using tables to contain formatted user content (like pull
2626 # request review comments).
2727 TABLE_ITEMS = Set . new ( %w( tr td th ) . freeze )
28- TABLE = 'table' . freeze
28+ TABLE = 'table' . freeze
29+ TABLE_SECTIONS = Set . new ( %w( thead tbody tfoot ) . freeze )
2930
3031 # The main sanitization whitelist. Only these elements and attributes are
3132 # allowed through by default.
3233 WHITELIST = {
3334 :elements => %w(
3435 h1 h2 h3 h4 h5 h6 h7 h8 br b i strong em a pre code img tt
35- div ins del sup sub p ol ul table blockquote dl dt dd
36- kbd q samp var hr ruby rt rp li tr td th
36+ div ins del sup sub p ol ul table thead tbody tfoot blockquote
37+ dl dt dd kbd q samp var hr ruby rt rp li tr td th
3738 ) ,
3839 :remove_contents => [ 'script' ] ,
3940 :attributes => {
@@ -75,7 +76,7 @@ class SanitizationFilter < Filter
7576 # Table child elements that are not contained by a <table> are removed.
7677 lambda { |env |
7778 name , node = env [ :node_name ] , env [ :node ]
78- if TABLE_ITEMS . include? ( name ) && !node . ancestors . any? { |n | n . name == TABLE }
79+ if ( TABLE_SECTIONS . include? ( name ) || TABLE_ITEMS . include? ( name ) ) && !node . ancestors . any? { |n | n . name == TABLE }
7980 node . replace ( node . children )
8081 end
8182 }
@@ -103,4 +104,4 @@ def whitelist
103104 end
104105 end
105106 end
106- end
107+ end
Original file line number Diff line number Diff line change @@ -49,4 +49,23 @@ def test_script_contents_are_removed
4949 orig = '<script>JavaScript!</script>'
5050 assert_equal "" , SanitizationFilter . call ( orig ) . to_s
5151 end
52+
53+ def test_table_rows_and_cells_removed_if_not_in_table
54+ orig = %(<tr><td>Foo</td></tr><td>Bar</td>)
55+ assert_equal 'FooBar' , SanitizationFilter . call ( orig ) . to_s
56+ end
57+
58+ def test_table_sections_removed_if_not_in_table
59+ orig = %(<thead><tr><td>Foo</td></tr></thead>)
60+ assert_equal 'Foo' , SanitizationFilter . call ( orig ) . to_s
61+ end
62+
63+ def test_table_sections_are_not_removed
64+ orig = %(<table>
65+ <thead><tr><th>Column 1</th></tr></thead>
66+ <tfoot><tr><td>Sum</td></tr></tfoot>
67+ <tbody><tr><td>1</td></tr></tbody>
68+ </table>)
69+ assert_equal orig , SanitizationFilter . call ( orig ) . to_s
70+ end
5271end
You can’t perform that action at this time.
0 commit comments