Skip to content

Commit cce18bc

Browse files
authoredMar 20, 2023
Merge pull request from GHSA-xrqq-wqh4-5hg2
Fix/advisory 2
2 parents 2e3d2ec + dee1f56 commit cce18bc

10 files changed

+83
-103
lines changed
 

‎composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"require": {
2626
"ext-dom": "*",
2727
"ext-libxml": "*",
28-
"php": "^7.0 || ^8.0"
28+
"php": "^7.0 || ^8.0",
29+
"ezyang/htmlpurifier": "^4.16"
2930
},
3031
"require-dev": {
3132
"phpunit/phpunit": "^6.5 || ^8.5"

‎src/Sanitizer.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use enshrined\svgSanitize\data\TagInterface;
88
use enshrined\svgSanitize\data\XPath;
99
use enshrined\svgSanitize\ElementReference\Resolver;
10+
use HTMLPurifier;
11+
use HTMLPurifier_Config;
1012

1113
/**
1214
* Class Sanitizer
@@ -646,7 +648,9 @@ public function setUseNestingLimit($limit)
646648
protected function cleanUnsafeNodes(\DOMNode $currentElement) {
647649
// Replace CDATA node with encoded text node
648650
if ($currentElement instanceof \DOMCdataSection) {
649-
$textNode = $currentElement->ownerDocument->createTextNode($currentElement->nodeValue);
651+
$purifier = new HTMLPurifier(HTMLPurifier_Config::createDefault());
652+
$clean_html = $purifier->purify($currentElement->nodeValue);
653+
$textNode = $currentElement->ownerDocument->createTextNode($clean_html);
650654
$currentElement->parentNode->replaceChild($textNode, $currentElement);
651655
// If the element doesn't have a tagname, remove it and continue with next iteration
652656
} elseif (!$currentElement instanceof \DOMElement && !$currentElement instanceof \DOMText) {

‎src/data/AllowedTags.php

-97
Original file line numberDiff line numberDiff line change
@@ -19,75 +19,9 @@ public static function getTags()
1919
return array (
2020
// HTML
2121
'a',
22-
'abbr',
23-
'acronym',
24-
'address',
25-
'area',
26-
'article',
27-
'aside',
28-
'audio',
29-
'bdi',
30-
'bdo',
31-
'blink',
32-
'button',
33-
'canvas',
34-
'caption',
35-
'cite',
36-
'col',
37-
'colgroup',
38-
'content',
39-
'data',
40-
'datalist',
41-
'decorator',
42-
'del',
43-
'details',
44-
'dfn',
45-
'dir',
46-
'div',
47-
'element',
48-
'fieldset',
49-
'figcaption',
50-
'figure',
5122
'font',
52-
'footer',
53-
'form',
54-
'header',
55-
'hgroup',
56-
'html',
5723
'image',
58-
'input',
59-
'ins',
60-
'kbd',
61-
'label',
62-
'legend',
63-
'li',
64-
'main',
65-
'map',
66-
'mark',
67-
'marquee',
68-
'meter',
69-
'nav',
70-
'optgroup',
71-
'option',
72-
'output',
73-
'progress',
74-
'q',
75-
'rp',
76-
'rt',
77-
'samp',
78-
'section',
79-
'select',
80-
'shadow',
81-
'source',
82-
'spacer',
8324
'style',
84-
'summary',
85-
'template',
86-
'textarea',
87-
'time',
88-
'track',
89-
'video',
90-
'wbr',
9125

9226
// SVG
9327
'svg',
@@ -158,37 +92,6 @@ public static function getTags()
15892
'feTile',
15993
'feTurbulence',
16094

161-
//MathML
162-
'math',
163-
'menclose',
164-
'merror',
165-
'mfenced',
166-
'mfrac',
167-
'mglyph',
168-
'mi',
169-
'mlabeledtr',
170-
'mmuliscripts',
171-
'mn',
172-
'mo',
173-
'mover',
174-
'mpadded',
175-
'mphantom',
176-
'mroot',
177-
'mrow',
178-
'ms',
179-
'mpspace',
180-
'msqrt',
181-
'mystyle',
182-
'msub',
183-
'msup',
184-
'msubsup',
185-
'mtable',
186-
'mtd',
187-
'mtext',
188-
'mtr',
189-
'munder',
190-
'munderover',
191-
19295
//text
19396
'#text'
19497
);

‎tests/SanitizerTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,36 @@ public function cdataSectionIsSanitized()
323323

324324
self::assertXmlStringEqualsXmlString($expected, $cleanData);
325325
}
326+
327+
/**
328+
* @test
329+
*/
330+
public function cdataBackgroundSectionIsSanitized()
331+
{
332+
$dataDirectory = __DIR__ . '/data';
333+
$initialData = file_get_contents($dataDirectory . '/cdataTwoTest.svg');
334+
$expected = file_get_contents($dataDirectory . '/cdataTwoClean.svg');
335+
336+
$sanitizer = new Sanitizer();
337+
$sanitizer->minify(false);
338+
$cleanData = $sanitizer->sanitize($initialData);
339+
340+
self::assertXmlStringEqualsXmlString($expected, $cleanData);
341+
}
342+
343+
/**
344+
* @test
345+
*/
346+
public function formDataisSanitized()
347+
{
348+
$dataDirectory = __DIR__ . '/data';
349+
$initialData = file_get_contents($dataDirectory . '/formDataTest.svg');
350+
$expected = file_get_contents($dataDirectory . '/formDataClean.svg');
351+
352+
$sanitizer = new Sanitizer();
353+
$sanitizer->minify(false);
354+
$cleanData = $sanitizer->sanitize($initialData);
355+
356+
self::assertXmlStringEqualsXmlString($expected, $cleanData);
357+
}
326358
}

‎tests/data/cdataClean.svg

+4-3
Loading

‎tests/data/cdataTwoClean.svg

+7
Loading

‎tests/data/cdataTwoTest.svg

+10
Loading

‎tests/data/formDataClean.svg

+9
Loading

‎tests/data/formDataTest.svg

+12
Loading

‎tests/data/htmlClean.svg

+2-1
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.