File size: 10,828 Bytes
352fe2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
diff --git a/src/Sanitizer.php b/src/Sanitizer.php
index 58c8111..79a350a 100644
--- a/src/Sanitizer.php
+++ b/src/Sanitizer.php
@@ -214,13 +214,10 @@ public function sanitize($dirty)
         $this->elementReferenceResolver->collect();
         $elementsToRemove = $this->elementReferenceResolver->getElementsToRemove();
 
-        // Grab all the elements
-        $allElements = $this->xmlDocument->getElementsByTagName("*");
-
         // remove doctype after node elements have been analyzed
         $this->removeDoctype();
         // Start the cleaning proccess
-        $this->startClean($allElements, $elementsToRemove);
+        $this->startClean($this->xmlDocument->childNodes, $elementsToRemove);
 
         // Save cleaned XML to a variable
         if ($this->removeXMLTag) {
@@ -316,33 +313,63 @@ protected function startClean(\DOMNodeList $elements, array $elementsToRemove)
                 continue;
             }
 
-            // If the tag isn't in the whitelist, remove it and continue with next iteration
-            if (!in_array(strtolower($currentElement->tagName), $this->allowedTags)) {
-                $currentElement->parentNode->removeChild($currentElement);
-                $this->xmlIssues[] = array(
-                    'message' => 'Suspicious tag \'' . $currentElement->tagName . '\'',
-                    'line' => $currentElement->getLineNo(),
-                );
-                continue;
-            }
-
-            $this->cleanHrefs($currentElement);
-
-            $this->cleanXlinkHrefs($currentElement);
-
-            $this->cleanAttributesOnWhitelist($currentElement);
-
-            if (strtolower($currentElement->tagName) === 'use') {
-                if ($this->isUseTagDirty($currentElement)
-                    || $this->isUseTagExceedingThreshold($currentElement)
-                ) {
+            if ($currentElement instanceof \DOMElement) {
+                // If the tag isn't in the whitelist, remove it and continue with next iteration
+                if (!in_array(strtolower($currentElement->tagName), $this->allowedTags)) {
                     $currentElement->parentNode->removeChild($currentElement);
                     $this->xmlIssues[] = array(
-                        'message' => 'Suspicious \'' . $currentElement->tagName . '\'',
+                        'message' => 'Suspicious tag \'' . $currentElement->tagName . '\'',
                         'line' => $currentElement->getLineNo(),
                     );
                     continue;
                 }
+
+                $this->cleanHrefs( $currentElement );
+
+                $this->cleanXlinkHrefs( $currentElement );
+
+                $this->cleanAttributesOnWhitelist($currentElement);
+
+                if (strtolower($currentElement->tagName) === 'use') {
+                    if ($this->isUseTagDirty($currentElement)
+                        || $this->isUseTagExceedingThreshold($currentElement)
+                    ) {
+                        $currentElement->parentNode->removeChild($currentElement);
+                        $this->xmlIssues[] = array(
+                            'message' => 'Suspicious \'' . $currentElement->tagName . '\'',
+                            'line' => $currentElement->getLineNo(),
+                        );
+                        continue;
+                    }
+                }
+
+                // Strip out font elements that will break out of foreign content.
+                if (strtolower($currentElement->tagName) === 'font') {
+                    $breaksOutOfForeignContent = false;
+                    for ($x = $currentElement->attributes->length - 1; $x >= 0; $x--) {
+                        // get attribute name
+                        $attrName = $currentElement->attributes->item( $x )->name;
+
+                        if (in_array($attrName, ['face', 'color', 'size'])) {
+                            $breaksOutOfForeignContent = true;
+                        }
+                    }
+
+                    if ($breaksOutOfForeignContent) {
+                        $currentElement->parentNode->removeChild($currentElement);
+                        $this->xmlIssues[] = array(
+                            'message' => 'Suspicious tag \'' . $currentElement->tagName . '\'',
+                            'line' => $currentElement->getLineNo(),
+                        );
+                        continue;
+                    }
+                }
+            }
+
+            $this->cleanUnsafeNodes($currentElement);
+
+            if ($currentElement->hasChildNodes()) {
+                $this->startClean($currentElement->childNodes, $elementsToRemove);
             }
         }
     }
@@ -627,4 +654,50 @@ public function setUseNestingLimit($limit)
     {
         $this->useNestingLimit = (int) $limit;
     }
+
+    /**
+     * Determines whether a node is safe or not.
+     *
+     * @param \DOMNode $node
+     * @return bool
+     */
+    protected function isNodeSafe(\DOMNode $node) {
+        $safeNodes = [
+            '#text',
+        ];
+
+        if (!in_array($node->nodeName, $safeNodes, true)) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Remove nodes that are either invalid or malformed.
+     *
+     * @param \DOMNode $currentElement The current element.
+     */
+    protected function cleanUnsafeNodes(\DOMNode $currentElement) {
+        // If the element doesn't have a tagname, remove it and continue with next iteration
+        if (!property_exists($currentElement, 'tagName')) {
+            if (!$this->isNodeSafe($currentElement)) {
+                $currentElement->parentNode->removeChild($currentElement);
+                $this->xmlIssues[] = array(
+                    'message' => 'Suspicious node \'' . $currentElement->nodeName . '\'',
+                    'line' => $currentElement->getLineNo(),
+                );
+
+                return;
+            }
+        }
+
+        if ( $currentElement->childNodes && $currentElement->childNodes->length > 0 ) {
+            for ($j = $currentElement->childNodes->length - 1; $j >= 0; $j--) {
+                /** @var \DOMElement $childElement */
+                $childElement = $currentElement->childNodes->item($j);
+                $this->cleanUnsafeNodes($childElement);
+            }
+        }
+    }
 }
diff --git a/src/data/AllowedTags.php b/src/data/AllowedTags.php
index 5b0f3de..e8d1795 100644
--- a/src/data/AllowedTags.php
+++ b/src/data/AllowedTags.php
@@ -29,56 +29,35 @@ public static function getTags()
             'article',
             'aside',
             'audio',
-            'b',
             'bdi',
             'bdo',
-            'big',
             'blink',
-            'blockquote',
-            'body',
-            'br',
             'button',
             'canvas',
             'caption',
-            'center',
             'cite',
-            'code',
             'col',
             'colgroup',
             'content',
             'data',
             'datalist',
-            'dd',
             'decorator',
             'del',
             'details',
             'dfn',
             'dir',
             'div',
-            'dl',
-            'dt',
             'element',
-            'em',
             'fieldset',
             'figcaption',
             'figure',
             'font',
             'footer',
             'form',
-            'h1',
-            'h2',
-            'h3',
-            'h4',
-            'h5',
-            'h6',
-            'head',
             'header',
             'hgroup',
-            'hr',
             'html',
-            'i',
             'image',
-            'img',
             'input',
             'ins',
             'kbd',
@@ -89,52 +68,27 @@ public static function getTags()
             'map',
             'mark',
             'marquee',
-            'menu',
-            'menuitem',
             'meter',
             'nav',
-            'nobr',
-            'ol',
             'optgroup',
             'option',
             'output',
-            'p',
-            'pre',
             'progress',
             'q',
             'rp',
             'rt',
-            'ruby',
-            's',
             'samp',
             'section',
             'select',
             'shadow',
-            'small',
             'source',
             'spacer',
-            'span',
-            'strike',
-            'strong',
             'style',
-            'sub',
             'summary',
-            'sup',
-            'table',
-            'tbody',
-            'td',
             'template',
             'textarea',
-            'tfoot',
-            'th',
-            'thead',
             'time',
-            'tr',
             'track',
-            'tt',
-            'u',
-            'ul',
-            'var',
             'video',
             'wbr',
 
diff --git a/tests/SanitizerTest.php b/tests/SanitizerTest.php
index c5536b8..8014269 100644
--- a/tests/SanitizerTest.php
+++ b/tests/SanitizerTest.php
@@ -294,4 +294,17 @@ public function testLargeUseDOSattacksAreNullified()
 
         self::assertXmlStringEqualsXmlString($expected, $cleanData);
     }
+
+	public function testInvalidNodesAreHandled()
+	{
+		$dataDirectory = __DIR__ . '/data';
+		$initialData = file_get_contents($dataDirectory . '/htmlTest.svg');
+		$expected = file_get_contents($dataDirectory . '/htmlClean.svg');
+
+		$sanitizer = new Sanitizer();
+		$sanitizer->minify(false);
+		$cleanData = $sanitizer->sanitize($initialData);
+
+		self::assertXmlStringEqualsXmlString($expected, $cleanData);
+	}
 }
diff --git a/tests/data/entityClean.svg b/tests/data/entityClean.svg
index a7c49b4..01af421 100644
--- a/tests/data/entityClean.svg
+++ b/tests/data/entityClean.svg
@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <svg xmlns="http://www.w3.org/2000/svg">
-  <text x="0" y="20" font-size="20">&lab;</text>
+  <text x="0" y="20" font-size="20"></text>
 </svg>
diff --git a/tests/data/htmlClean.svg b/tests/data/htmlClean.svg
new file mode 100644
index 0000000..e11c03b
--- /dev/null
+++ b/tests/data/htmlClean.svg
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="-1 -1 2 2"></svg>
diff --git a/tests/data/htmlTest.svg b/tests/data/htmlTest.svg
new file mode 100644
index 0000000..12bad58
--- /dev/null
+++ b/tests/data/htmlTest.svg
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="-1 -1 2 2">
+    <!--><img src onerror=alert(1)><!-->
+    <?x ><img src onerror=alert(1)><?x?>
+    <p/><![CDATA[ ><img src onerror=alert(1)> ]]>
+    <font face=""/><![CDATA[ ><img src onerror=alert(1)> ]]>
+</svg>
\ No newline at end of file