-
Notifications
You must be signed in to change notification settings - Fork 821
/
HTMLEditorSanitiser.php
384 lines (339 loc) · 15.2 KB
/
HTMLEditorSanitiser.php
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
namespace SilverStripe\Forms\HTMLEditor;
use DOMAttr;
use DOMElement;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\View\Parsers\HTMLValue;
use stdClass;
/**
* Sanitises an HTMLValue so it's contents are the elements and attributes that are whitelisted
* using the same configuration as TinyMCE
*
* See www.tinymce.com/wiki.php/configuration:valid_elements for details on the spec of TinyMCE's
* whitelist configuration
*/
class HTMLEditorSanitiser
{
use Configurable;
use Injectable;
/**
* rel attribute to add to link elements which have a target attribute (usually "_blank")
* this is to done to prevent reverse tabnabbing - see https://www.owasp.org/index.php/Reverse_Tabnabbing
* noopener includes the behaviour we want, though some browsers don't yet support it and rely
* upon using noreferrer instead - see https://caniuse.com/rel-noopener for current browser compatibility
* set this to null if you would like to disable this behaviour
* set this to an empty string if you would like to remove rel attributes that were previously set
*
* @var string
*/
private static $link_rel_value = 'noopener noreferrer';
/** @var stdClass - $element => $rule hash for whitelist element rules where the element name isn't a pattern */
protected $elements = [];
/** @var stdClass - Sequential list of whitelist element rules where the element name is a pattern */
protected $elementPatterns = [];
/** @var stdClass - The list of attributes that apply to all further whitelisted elements added */
protected $globalAttributes = [];
/**
* Construct a sanitiser from a given HTMLEditorConfig
*
* Note that we build data structures from the current state of HTMLEditorConfig - later changes to
* the passed instance won't cause this instance to update it's whitelist
*
* @param HTMLEditorConfig $config
*/
public function __construct(HTMLEditorConfig $config)
{
$valid = $config->getOption('valid_elements');
if ($valid) {
$this->addValidElements($valid);
}
$valid = $config->getOption('extended_valid_elements');
if ($valid) {
$this->addValidElements($valid);
}
}
/**
* Given a TinyMCE pattern (close to unix glob style), create a regex that does the match
*
* @param $str - The TinyMCE pattern
* @return string - The equivalent regex
*/
protected function patternToRegex($str)
{
return '/^' . preg_replace('/([?+*])/', '.$1', $str ?? '') . '$/';
}
/**
* Given a valid_elements string, parse out the actual element and attribute rules and add to the
* internal whitelist
*
* Logic based heavily on javascript version from tiny_mce_src.js
*
* @param string $validElements - The valid_elements or extended_valid_elements string to add to the whitelist
*/
protected function addValidElements($validElements)
{
$elementRuleRegExp = '/^([#+\-])?([^\[\/]+)(?:\/([^\[]+))?(?:\[([^\]]+)\])?$/';
$attrRuleRegExp = '/^([!\-])?(\w+::\w+|[^=:<]+)?(?:([=:<])(.*))?$/';
$hasPatternsRegExp = '/[*?+]/';
foreach (explode(',', $validElements ?? '') as $validElement) {
if (preg_match($elementRuleRegExp ?? '', $validElement ?? '', $matches)) {
$prefix = isset($matches[1]) ? $matches[1] : null;
$elementName = isset($matches[2]) ? $matches[2] : null;
$outputName = isset($matches[3]) ? $matches[3] : null;
$attrData = isset($matches[4]) ? $matches[4] : null;
// Create the new element
$element = new stdClass();
$element->attributes = [];
$element->attributePatterns = [];
$element->attributesRequired = [];
$element->attributesDefault = [];
$element->attributesForced = [];
foreach (['#' => 'paddEmpty', '-' => 'removeEmpty'] as $match => $means) {
$element->$means = ($prefix === $match);
}
// Copy attributes from global rule into current rule
if ($this->globalAttributes) {
$element->attributes = array_merge($element->attributes, $this->globalAttributes);
}
// Attributes defined
if ($attrData) {
foreach (explode('|', $attrData ?? '') as $attr) {
if (preg_match($attrRuleRegExp ?? '', $attr ?? '', $matches)) {
$attr = new stdClass();
$attrType = isset($matches[1]) ? $matches[1] : null;
$attrName = isset($matches[2]) ? str_replace('::', ':', $matches[2]) : null;
$prefix = isset($matches[3]) ? $matches[3] : null;
$value = isset($matches[4]) ? $matches[4] : null;
// Required
if ($attrType === '!') {
$element->attributesRequired[] = $attrName;
$attr->required = true;
} elseif ($attrType === '-') {
// Denied from global
unset($element->attributes[$attrName]);
continue;
}
// Default value
if ($prefix) {
if ($prefix === '=') { // Default value
$element->attributesDefault[$attrName] = $value;
$attr->defaultValue = $value;
} elseif ($prefix === ':') {
// Forced value
$element->attributesForced[$attrName] = $value;
$attr->forcedValue = $value;
} elseif ($prefix === '<') {
// Required values
$attr->validValues = explode('?', $value ?? '');
}
}
// Check for attribute patterns
if (preg_match($hasPatternsRegExp ?? '', $attrName ?? '')) {
$attr->pattern = $this->patternToRegex($attrName);
$element->attributePatterns[] = $attr;
} else {
$element->attributes[$attrName] = $attr;
}
}
}
}
// Global rule, store away these for later usage
if (!$this->globalAttributes && $elementName == '@') {
$this->globalAttributes = $element->attributes;
}
// Handle substitute elements such as b/strong
if ($outputName) {
$element->outputName = $elementName;
$this->elements[$outputName] = $element;
}
// Add pattern or exact element
if (preg_match($hasPatternsRegExp ?? '', $elementName ?? '')) {
$element->pattern = $this->patternToRegex($elementName);
$this->elementPatterns[] = $element;
} else {
$this->elements[$elementName] = $element;
}
}
}
}
/**
* Given an element tag, return the rule structure for that element
* @param string $tag The element tag
* @return stdClass The element rule
*/
protected function getRuleForElement($tag)
{
if (isset($this->elements[$tag])) {
return $this->elements[$tag];
}
foreach ($this->elementPatterns as $element) {
if (preg_match($element->pattern ?? '', $tag ?? '')) {
return $element;
}
}
return null;
}
/**
* Given an attribute name, return the rule structure for that attribute
*
* @param object $elementRule
* @param string $name The attribute name
* @return stdClass The attribute rule
*/
protected function getRuleForAttribute($elementRule, $name)
{
if (isset($elementRule->attributes[$name])) {
return $elementRule->attributes[$name];
}
foreach ($elementRule->attributePatterns as $attribute) {
if (preg_match($attribute->pattern ?? '', $name ?? '')) {
return $attribute;
}
}
return null;
}
/**
* Given a DOMElement and an element rule, check if that element passes the rule
* @param DOMElement $element The element to check
* @param stdClass $rule The rule to check against
* @return bool True if the element passes (and so can be kept), false if it fails (and so needs stripping)
*/
protected function elementMatchesRule($element, $rule = null)
{
// If the rule doesn't exist at all, the element isn't allowed
if (!$rule) {
return false;
}
// If the rule has attributes required, check them to see if this element has at least one
if ($rule->attributesRequired) {
$hasMatch = false;
foreach ($rule->attributesRequired as $attr) {
if ($element->getAttribute($attr)) {
$hasMatch = true;
break;
}
}
if (!$hasMatch) {
return false;
}
}
// If the rule says to remove empty elements, and this element is empty, remove it
if ($rule->removeEmpty && !$element->firstChild) {
return false;
}
// No further tests required, element passes
return true;
}
/**
* Given a DOMAttr and an attribute rule, check if that attribute passes the rule
* @param DOMAttr $attr - the attribute to check
* @param stdClass $rule - the rule to check against
* @return bool - true if the attribute passes (and so can be kept), false if it fails (and so needs stripping)
*/
protected function attributeMatchesRule($attr, $rule = null)
{
// If the rule doesn't exist at all, the attribute isn't allowed
if (!$rule) {
return false;
}
// If the rule has a set of valid values, check them to see if this attribute is one
if (isset($rule->validValues) && !in_array($attr->value, $rule->validValues ?? [])) {
return false;
}
// No further tests required, attribute passes
return true;
}
/**
* Given an SS_HTMLValue instance, will remove and elements and attributes that are
* not explicitly included in the whitelist passed to __construct on instance creation
*
* @param HTMLValue $html - The HTMLValue to remove any non-whitelisted elements & attributes from
*/
public function sanitise(HTMLValue $html)
{
$linkRelValue = $this->config()->get('link_rel_value');
$doc = $html->getDocument();
/** @var DOMElement $el */
foreach ($html->query('//body//*') as $el) {
$elementRule = $this->getRuleForElement($el->tagName);
// If this element isn't allowed, strip it
if (!$this->elementMatchesRule($el, $elementRule)) {
// If it's a script or style, we don't keep contents
if ($el->tagName === 'script' || $el->tagName === 'style') {
$el->parentNode->removeChild($el);
} else {
// Otherwise we replace this node with all it's children
// First, create a new fragment with all of $el's children moved into it
$frag = $doc->createDocumentFragment();
while ($el->firstChild) {
$frag->appendChild($el->firstChild);
}
// Then replace $el with the frags contents (which used to be it's children)
$el->parentNode->replaceChild($frag, $el);
}
} else {
// Otherwise tidy the element
// First, if we're supposed to pad & this element is empty, fix that
if ($elementRule->paddEmpty && !$el->firstChild) {
$el->nodeValue = ' ';
}
// Then filter out any non-whitelisted attributes
$children = $el->attributes;
$i = $children->length;
while ($i--) {
$attr = $children->item($i);
$attributeRule = $this->getRuleForAttribute($elementRule, $attr->name);
// If this attribute isn't allowed, strip it
if (!$this->attributeMatchesRule($attr, $attributeRule)) {
$el->removeAttributeNode($attr);
}
}
// Then enforce any default attributes
foreach ($elementRule->attributesDefault as $attr => $default) {
if (!$el->getAttribute($attr)) {
$el->setAttribute($attr, $default);
}
}
// And any forced attributes
foreach ($elementRule->attributesForced as $attr => $forced) {
$el->setAttribute($attr, $forced);
}
// Matches "javascript:" with any arbitrary linebreaks inbetween the characters.
$regex = '#^\s*(' . implode('\s*', str_split('javascript:')) . '|' . implode('\s*', str_split('data:text/html;')) . ')#i';
// Strip out javascript execution in href or src attributes.
foreach (['src', 'href', 'data'] as $dangerAttribute) {
if ($el->hasAttribute($dangerAttribute)) {
if (preg_match($regex, $el->getAttribute($dangerAttribute))) {
$el->removeAttribute($dangerAttribute);
}
}
}
}
if ($el->tagName === 'a' && $linkRelValue !== null) {
$this->addRelValue($el, $linkRelValue);
}
}
}
/**
* Adds rel="noopener noreferrer" to link elements with a target attribute
*
* @param DOMElement $el
* @param string|null $linkRelValue
*/
private function addRelValue(DOMElement $el, $linkRelValue)
{
// user has checked the checkbox 'open link in new window'
if ($el->getAttribute('target') && $el->getAttribute('rel') !== $linkRelValue) {
if ($linkRelValue !== '') {
$el->setAttribute('rel', $linkRelValue);
} else {
$el->removeAttribute('rel');
}
} elseif ($el->getAttribute('rel') === $linkRelValue && !$el->getAttribute('target')) {
// user previously checked 'open link in new window' and noopener was added,
// now user has unchecked the checkbox so we can remove noopener
$el->removeAttribute('rel');
}
}
}