Skip to content

Commit 46071f3

Browse files
committed
Update sanitization of <progress> attrs to mimic browsers' impl
1 parent bf8168a commit 46071f3

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

library/HTMLPurifier/AttrTransform/Progress.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
/**
44
* Post-transform performing validations for <progress> elements ensuring
55
* that if value is present, it is within a valid range (0..1) or (0..max)
6+
*
7+
* Implementation is based on sanitization performed by browsers (compared
8+
* against Chrome 68 and Firefox 61).
69
*/
710
class HTMLPurifier_AttrTransform_Progress extends HTMLPurifier_AttrTransform
811
{
@@ -14,12 +17,19 @@ class HTMLPurifier_AttrTransform_Progress extends HTMLPurifier_AttrTransform
1417
*/
1518
public function transform($attr, $config, $context)
1619
{
20+
$max = isset($attr['max']) ? (float) $attr['max'] : 1;
21+
22+
if ($max <= 0) {
23+
$this->confiscateAttr($attr, 'max');
24+
}
25+
1726
if (isset($attr['value'])) {
18-
$max = isset($attr['max']) ? (float) $attr['max'] : 1;
1927
$value = (float) $attr['value'];
2028

21-
if ($value < 0 || $value > $max) {
29+
if ($value < 0) {
2230
$this->confiscateAttr($attr, 'value');
31+
} elseif ($value > $max) {
32+
$attr['value'] = isset($attr['max']) ? $attr['max'] : 1;
2333
}
2434
}
2535

tests/HTMLPurifier/AttrTransform/ProgressTest.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,55 @@ public function setUp()
2424
$this->progress = new HTMLPurifier_AttrTransform_Progress();
2525
}
2626

27-
protected function assertTransform($expected, array $input)
27+
public function transformInput()
2828
{
29-
$this->assertEquals($expected, $this->progress->transform($input, $this->config, $this->context));
29+
return array(
30+
array(
31+
array(),
32+
array(),
33+
),
34+
array(
35+
array('value' => 0),
36+
array('value' => 0),
37+
),
38+
array(
39+
array('value' => 1),
40+
array('value' => 1),
41+
),
42+
array(
43+
array('value' => 10),
44+
array('value' => 1),
45+
),
46+
array(
47+
array('value' => '.1'),
48+
array('value' => '.1'),
49+
),
50+
array(
51+
array('value' => -1),
52+
array(),
53+
),
54+
array(
55+
array('value' => 10, 'max' => 10),
56+
array('value' => 10, 'max' => 10),
57+
),
58+
array(
59+
array('value' => 100, 'max' => 10),
60+
array('value' => 10, 'max' => 10),
61+
),
62+
array(
63+
array('max' => 0),
64+
array(),
65+
),
66+
);
3067
}
3168

32-
public function testTransform()
69+
/**
70+
* @param array $input
71+
* @param array $expected
72+
* @dataProvider transformInput
73+
*/
74+
public function testTransform($input, $expected)
3375
{
34-
$this->assertTransform(array(), array());
35-
$this->assertTransform(array('value' => 0), array('value' => 0));
36-
$this->assertTransform(array('value' => 1), array('value' => 1));
37-
$this->assertTransform(array(), array('value' => 10));
38-
$this->assertTransform(array(), array('value' => -1));
39-
40-
$this->assertTransform(array('value' => 10, 'max' => 10), array('value' => 10, 'max' => 10));
41-
$this->assertTransform(array('max' => 10), array('value' => 100, 'max' => 10));
76+
$this->assertEquals($expected, $this->progress->transform($input, $this->config, $this->context));
4277
}
4378
}

tests/HTMLPurifier/HTML5DefinitionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,18 @@ public function progressInput()
330330
// The value of the 'value' attribute must be less than or
331331
// equal to one when the max attribute is absent.
332332
'<progress value="10"></progress>',
333+
'<progress value="1"></progress>',
334+
),
335+
array(
336+
'<progress value="-1"></progress>',
337+
'<progress></progress>',
338+
),
339+
array(
340+
'<progress value=".5" max=".25"></progress>',
341+
'<progress value=".25" max=".25"></progress>',
342+
),
343+
array(
344+
'<progress max="0"></progress>',
333345
'<progress></progress>',
334346
),
335347
array(

0 commit comments

Comments
 (0)