Skip to content

Commit 1565bfe

Browse files
committed
fix: allow CSS properties to have quoted string values
This was broken in v2.9.0 by bf13d48 Fixes #202
1 parent cc5ce1c commit 1565bfe

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

CHANGELOG.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
# Changelog
22

3-
### 2.9.0 / 2021-01-14
3+
### next / unreleased
4+
5+
### Bug fixes
6+
7+
* Fix a regression in v2.9.0 which inappropriately removed CSS properties with quoted string values. [[#202](https://github.com/flavorjones/loofah/issues/202)]
8+
9+
10+
## 2.9.0 / 2021-01-14
11+
12+
### Features
413

514
* Handle CSS functions in a CSS shorthand property (like `background`). [[#199](https://github.com/flavorjones/loofah/issues/199), [#200](https://github.com/flavorjones/loofah/issues/200)]
615

716

8-
### 2.8.0 / 2020-11-25
17+
## 2.8.0 / 2020-11-25
18+
19+
### Features
920

1021
* Allow CSS properties `order`, `flex-direction`, `flex-grow`, `flex-wrap`, `flex-shrink`, `flex-flow`, `flex-basis`, `flex`, `justify-content`, `align-self`, `align-items`, and `align-content`. [[#197](https://github.com/flavorjones/loofah/issues/197)] (Thanks, [@miguelperez](https://github.com/miguelperez)!)
1122

lib/loofah/html5/scrub.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Scrub
99
CSS_KEYWORDISH = /\A(#[0-9a-fA-F]+|rgb\(\d+%?,\d*%?,?\d*%?\)?|-?\d{0,3}\.?\d{0,10}(ch|cm|r?em|ex|in|lh|mm|pc|pt|px|Q|vmax|vmin|vw|vh|%|,|\))?)\z/
1010
CRASS_SEMICOLON = { node: :semicolon, raw: ";" }
1111
CSS_IMPORTANT = '!important'
12+
CSS_PROPERTY_STRING_WITHOUT_EMBEDDED_QUOTES = /\A(["'])?[^"']+\1\z/
1213

1314
class << self
1415
def allowed_element?(element_name)
@@ -92,7 +93,11 @@ def scrub_css(style)
9293
when :whitespace
9394
nil
9495
when :string
95-
nil
96+
if child[:raw] =~ CSS_PROPERTY_STRING_WITHOUT_EMBEDDED_QUOTES
97+
Crass::Parser.stringify(child)
98+
else
99+
nil
100+
end
96101
when :function
97102
if SafeList::ALLOWED_CSS_FUNCTIONS.include?(child[:name].downcase)
98103
Crass::Parser.stringify(child)

test/html5/test_scrub_css.rb

+29
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,33 @@ class UnitHTML5Scrub < Loofah::TestCase
2929
Loofah::HTML5::Scrub.scrub_css("background: linear-gradient(transparent 50%, #ffff66 50%);")
3030
end
3131
end
32+
33+
describe "property string values" do
34+
it "allows hypenated values" do
35+
text = %q(font-family:'AvenirNext-Regular';)
36+
assert_equal(text, Loofah::HTML5::Scrub.scrub_css(text))
37+
38+
text = %q(font-family:"AvenirNext-Regular";)
39+
assert_equal(text, Loofah::HTML5::Scrub.scrub_css(text))
40+
end
41+
42+
it "allows embedded spaces in values" do
43+
text = %q(font-family:'Avenir Next';)
44+
assert_equal(text, Loofah::HTML5::Scrub.scrub_css(text))
45+
46+
text = %q(font-family:"Avenir Next";)
47+
assert_equal(text, Loofah::HTML5::Scrub.scrub_css(text))
48+
end
49+
50+
it "does not allow values with embedded or irregular quotes" do
51+
assert_empty(Loofah::HTML5::Scrub.scrub_css(%q(font-family:'AvenirNext"-Regular';)))
52+
assert_empty(Loofah::HTML5::Scrub.scrub_css(%q(font-family:"AvenirNext'-Regular";)))
53+
54+
assert_empty(Loofah::HTML5::Scrub.scrub_css(%q(font-family:'AvenirNext-Regular;)))
55+
assert_empty(Loofah::HTML5::Scrub.scrub_css(%q(font-family:'AvenirNext-Regular";)))
56+
57+
assert_empty(Loofah::HTML5::Scrub.scrub_css(%q(font-family:"AvenirNext-Regular;)))
58+
assert_empty(Loofah::HTML5::Scrub.scrub_css(%q(font-family:"AvenirNext-Regular';)))
59+
end
60+
end
3261
end

test/integration/test_ad_hoc.rb

+20
Original file line numberDiff line numberDiff line change
@@ -260,5 +260,25 @@ def test_dont_remove_whitespace_between_tags
260260
end
261261
end
262262
end
263+
264+
it "handles property string values" do
265+
input = <<~EOF
266+
<span style="font-size: 36px; font-family: 'AvenirNext-Regular';">variation 1a</span>
267+
<span style="font-size: 36px; font-family: AvenirNext-Regular;">variation 1b</span>
268+
<span style="font-size: 36px; font-family: 'Avenir Next';">variation 2a</span>
269+
<span style="font-size: 36px; font-family: Avenir Next;">variation 2b</span>
270+
EOF
271+
272+
expected = <<~EOF
273+
<span style="font-size:36px;font-family:'AvenirNext-Regular';">variation 1a</span>
274+
<span style="font-size:36px;font-family:AvenirNext-Regular;">variation 1b</span>
275+
<span style="font-size:36px;font-family:'Avenir Next';">variation 2a</span>
276+
<span style="font-size:36px;font-family:Avenir Next;">variation 2b</span>
277+
EOF
278+
279+
actual = Loofah.scrub_fragment(input, :escape)
280+
281+
assert_equal(expected, actual.to_html)
282+
end
263283
end
264284
end

0 commit comments

Comments
 (0)