From 228cf4f6b53b30e29a8aaab957e84443ccfae959 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 7 Aug 2017 17:04:54 -0400 Subject: Limit `style` attribute on `th` and `td` elements to specific properties Previously we whitelisted the entire `style` attribute on `th` and `td` elements, in order to allow Markdown table alignment to work. But this opened us up to a potential exploit by allowing a malicious user to define properties besides `text-align` in the attribute. We now remove everything except `text-align: (center|left|right)`. --- spec/lib/banzai/filter/sanitization_filter_spec.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'spec') diff --git a/spec/lib/banzai/filter/sanitization_filter_spec.rb b/spec/lib/banzai/filter/sanitization_filter_spec.rb index 35a32a46eff..659b4460fc3 100644 --- a/spec/lib/banzai/filter/sanitization_filter_spec.rb +++ b/spec/lib/banzai/filter/sanitization_filter_spec.rb @@ -49,7 +49,7 @@ describe Banzai::Filter::SanitizationFilter do instance = described_class.new('Foo') 3.times { instance.whitelist } - expect(instance.whitelist[:transformers].size).to eq 4 + expect(instance.whitelist[:transformers].size).to eq 5 end it 'sanitizes `class` attribute from all elements' do @@ -63,8 +63,8 @@ describe Banzai::Filter::SanitizationFilter do expect(filter(act).to_html).to eq %q{def} end - it 'allows `style` attribute on table elements' do - html = <<-HTML.strip_heredoc + it 'allows `text-align` property in `style` attribute on table elements' do + html = <<~HTML @@ -77,6 +77,20 @@ describe Banzai::Filter::SanitizationFilter do expect(doc.at_css('td')['style']).to eq 'text-align: right' end + it 'disallows other properties in `style` attribute on table elements' do + html = <<~HTML +
Head
Body
+ + +
Head
Body
+ HTML + + doc = filter(html) + + expect(doc.at_css('th')['style']).to be_nil + expect(doc.at_css('td')['style']).to eq 'text-align: center' + end + it 'allows `span` elements' do exp = act = %q{Hello} expect(filter(act).to_html).to eq exp -- cgit v1.2.1