File tree 10 files changed +83
-103
lines changed
10 files changed +83
-103
lines changed Original file line number Diff line number Diff line change 25
25
"require" : {
26
26
"ext-dom" : " *" ,
27
27
"ext-libxml" : " *" ,
28
- "php" : " ^7.0 || ^8.0"
28
+ "php" : " ^7.0 || ^8.0" ,
29
+ "ezyang/htmlpurifier" : " ^4.16"
29
30
},
30
31
"require-dev" : {
31
32
"phpunit/phpunit" : " ^6.5 || ^8.5"
Original file line number Diff line number Diff line change 7
7
use enshrined \svgSanitize \data \TagInterface ;
8
8
use enshrined \svgSanitize \data \XPath ;
9
9
use enshrined \svgSanitize \ElementReference \Resolver ;
10
+ use HTMLPurifier ;
11
+ use HTMLPurifier_Config ;
10
12
11
13
/**
12
14
* Class Sanitizer
@@ -646,7 +648,9 @@ public function setUseNestingLimit($limit)
646
648
protected function cleanUnsafeNodes (\DOMNode $ currentElement ) {
647
649
// Replace CDATA node with encoded text node
648
650
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 );
650
654
$ currentElement ->parentNode ->replaceChild ($ textNode , $ currentElement );
651
655
// If the element doesn't have a tagname, remove it and continue with next iteration
652
656
} elseif (!$ currentElement instanceof \DOMElement && !$ currentElement instanceof \DOMText) {
Original file line number Diff line number Diff line change @@ -19,75 +19,9 @@ public static function getTags()
19
19
return array (
20
20
// HTML
21
21
'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 ' ,
51
22
'font ' ,
52
- 'footer ' ,
53
- 'form ' ,
54
- 'header ' ,
55
- 'hgroup ' ,
56
- 'html ' ,
57
23
'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 ' ,
83
24
'style ' ,
84
- 'summary ' ,
85
- 'template ' ,
86
- 'textarea ' ,
87
- 'time ' ,
88
- 'track ' ,
89
- 'video ' ,
90
- 'wbr ' ,
91
25
92
26
// SVG
93
27
'svg ' ,
@@ -158,37 +92,6 @@ public static function getTags()
158
92
'feTile ' ,
159
93
'feTurbulence ' ,
160
94
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
-
192
95
//text
193
96
'#text '
194
97
);
Original file line number Diff line number Diff line change @@ -323,4 +323,36 @@ public function cdataSectionIsSanitized()
323
323
324
324
self ::assertXmlStringEqualsXmlString ($ expected , $ cleanData );
325
325
}
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
+ }
326
358
}
You can’t perform that action at this time.
0 commit comments