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

Allow td and th elements without a style attribute #73

Merged
merged 2 commits into from
May 3, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/govspeak/html_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ def call(sanitize_context)

# Kramdown uses text-align to allow table cells to be aligned
# http://kramdown.gettalong.org/quickref.html#tables
unless node['style'].match(/^text-align:\s*(center|left|right)$/)
if invalid_style_attribute?(node['style'])
node.remove_attribute('style')
end
end

def invalid_style_attribute?(style)
style && !style.match(/^text-align:\s*(center|left|right)$/)
end
end

def initialize(dirty_html, options = {})
Expand Down
11 changes: 8 additions & 3 deletions test/html_sanitizer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ class HtmlSanitizerTest < Minitest::Test
assert_equal "", Govspeak::HtmlSanitizer.new(html).sanitize_without_images
end

test "allows table cells and table headings without a style attribute" do
html = "<th>thing</th><td>thing</td>"
assert_equal html, Govspeak::HtmlSanitizer.new(html).sanitize
end

test "allows valid text-align properties on the style attribute for table cells and table headings" do
["left", "right", "center"].each do |alignment|
html = "<td style=\"text-align: #{alignment}\">thing</td>"
html = "<th style=\"text-align: #{alignment}\">thing</th><td style=\"text-align: #{alignment}\">thing</td>"
assert_equal html, Govspeak::HtmlSanitizer.new(html).sanitize
end

Expand All @@ -57,8 +62,8 @@ class HtmlSanitizerTest < Minitest::Test
"background-image: url(javascript:alert('XSS'))",
"expression(alert('XSS'));"
].each do |style|
html = "<td style=\"#{style}\">thing</td>"
assert_equal '<td>thing</td>', Govspeak::HtmlSanitizer.new(html).sanitize
html = "<th style=\"#{style}\">thing</th><td style=\"#{style}\">thing</td>"
assert_equal '<th>thing</th><td>thing</td>', Govspeak::HtmlSanitizer.new(html).sanitize
end
end
end