forked from symfony/html-sanitizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HtmlSanitizerConfig.php
507 lines (431 loc) · 14.1 KB
/
HtmlSanitizerConfig.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HtmlSanitizer;
use Symfony\Component\HtmlSanitizer\Reference\W3CReference;
use Symfony\Component\HtmlSanitizer\Visitor\AttributeSanitizer\AttributeSanitizerInterface;
/**
* @author Titouan Galopin <[email protected]>
*/
class HtmlSanitizerConfig
{
/**
* Elements that should be removed but their children should be retained.
*
* @var array<string, true>
*/
private array $blockedElements = [];
/**
* Elements that should be retained, with their allowed attributes.
*
* @var array<string, array<string, true>>
*/
private array $allowedElements = [];
/**
* Attributes that should always be added to certain elements.
*
* @var array<string, array<string, string>>
*/
private array $forcedAttributes = [];
/**
* Links schemes that should be retained, other being dropped.
*
* @var list<string>
*/
private array $allowedLinkSchemes = ['http', 'https', 'mailto', 'tel'];
/**
* Links hosts that should be retained (by default, all hosts are allowed).
*
* @var list<string>|null
*/
private ?array $allowedLinkHosts = null;
/**
* Should the sanitizer allow relative links (by default, they are dropped).
*/
private bool $allowRelativeLinks = false;
/**
* Image/Audio/Video schemes that should be retained, other being dropped.
*
* @var list<string>
*/
private array $allowedMediaSchemes = ['http', 'https', 'data'];
/**
* Image/Audio/Video hosts that should be retained (by default, all hosts are allowed).
*
* @var list<string>|null
*/
private ?array $allowedMediaHosts = null;
/**
* Should the sanitizer allow relative media URL (by default, they are dropped).
*/
private bool $allowRelativeMedias = false;
/**
* Should the URL in the sanitized document be transformed to HTTPS if they are using HTTP.
*/
private bool $forceHttpsUrls = false;
/**
* Sanitizers that should be applied to specific attributes in addition to standard sanitization.
*
* @var list<AttributeSanitizerInterface>
*/
private array $attributeSanitizers;
private int $maxInputLength = 20_000;
public function __construct()
{
$this->attributeSanitizers = [
new Visitor\AttributeSanitizer\UrlAttributeSanitizer(),
];
}
/**
* Allows all static elements and attributes from the W3C Sanitizer API standard.
*
* All scripts will be removed but the output may still contain other dangerous
* behaviors like CSS injection (click-jacking), CSS expressions, ...
*/
public function allowStaticElements(): static
{
$elements = array_merge(
array_keys(W3CReference::HEAD_ELEMENTS),
array_keys(W3CReference::BODY_ELEMENTS)
);
$clone = clone $this;
foreach ($elements as $element) {
$clone = $clone->allowElement($element, '*');
}
return $clone;
}
/**
* Allows "safe" elements and attributes.
*
* All scripts will be removed, as well as other dangerous behaviors like CSS injection.
*/
public function allowSafeElements(): static
{
$attributes = [];
foreach (W3CReference::ATTRIBUTES as $attribute => $isSafe) {
if ($isSafe) {
$attributes[] = $attribute;
}
}
$clone = clone $this;
foreach (W3CReference::HEAD_ELEMENTS as $element => $isSafe) {
if ($isSafe) {
$clone = $clone->allowElement($element, $attributes);
}
}
foreach (W3CReference::BODY_ELEMENTS as $element => $isSafe) {
if ($isSafe) {
$clone = $clone->allowElement($element, $attributes);
}
}
return $clone;
}
/**
* Allows only a given list of schemes to be used in links href attributes.
*
* All other schemes will be dropped.
*
* @param list<string> $allowLinkSchemes
*/
public function allowLinkSchemes(array $allowLinkSchemes): static
{
$clone = clone $this;
$clone->allowedLinkSchemes = $allowLinkSchemes;
return $clone;
}
/**
* Allows only a given list of hosts to be used in links href attributes.
*
* All other hosts will be dropped. By default all hosts are allowed
* ($allowedLinkHosts = null).
*
* @param list<string>|null $allowLinkHosts
*/
public function allowLinkHosts(?array $allowLinkHosts): static
{
$clone = clone $this;
$clone->allowedLinkHosts = $allowLinkHosts;
return $clone;
}
/**
* Allows relative URLs to be used in links href attributes.
*/
public function allowRelativeLinks(bool $allowRelativeLinks = true): static
{
$clone = clone $this;
$clone->allowRelativeLinks = $allowRelativeLinks;
return $clone;
}
/**
* Allows only a given list of schemes to be used in media source attributes (img, audio, video, ...).
*
* All other schemes will be dropped.
*
* @param list<string> $allowMediaSchemes
*/
public function allowMediaSchemes(array $allowMediaSchemes): static
{
$clone = clone $this;
$clone->allowedMediaSchemes = $allowMediaSchemes;
return $clone;
}
/**
* Allows only a given list of hosts to be used in media source attributes (img, audio, video, ...).
*
* All other hosts will be dropped. By default all hosts are allowed
* ($allowMediaHosts = null).
*
* @param list<string>|null $allowMediaHosts
*/
public function allowMediaHosts(?array $allowMediaHosts): static
{
$clone = clone $this;
$clone->allowedMediaHosts = $allowMediaHosts;
return $clone;
}
/**
* Allows relative URLs to be used in media source attributes (img, audio, video, ...).
*/
public function allowRelativeMedias(bool $allowRelativeMedias = true): static
{
$clone = clone $this;
$clone->allowRelativeMedias = $allowRelativeMedias;
return $clone;
}
/**
* Transforms URLs using the HTTP scheme to use the HTTPS scheme instead.
*/
public function forceHttpsUrls(bool $forceHttpsUrls = true): static
{
$clone = clone $this;
$clone->forceHttpsUrls = $forceHttpsUrls;
return $clone;
}
/**
* Configures the given element as allowed.
*
* Allowed elements are elements the sanitizer should retain from the input.
*
* A list of allowed attributes for this element can be passed as a second argument.
* Passing "*" will allow all standard attributes on this element. By default, no
* attributes are allowed on the element.
*
* @param list<string>|string $allowedAttributes
*/
public function allowElement(string $element, array|string $allowedAttributes = []): static
{
$clone = clone $this;
// Unblock the element is necessary
unset($clone->blockedElements[$element]);
$clone->allowedElements[$element] = [];
$attrs = ('*' === $allowedAttributes) ? array_keys(W3CReference::ATTRIBUTES) : (array) $allowedAttributes;
foreach ($attrs as $allowedAttr) {
$clone->allowedElements[$element][$allowedAttr] = true;
}
return $clone;
}
/**
* Configures the given element as blocked.
*
* Blocked elements are elements the sanitizer should remove from the input, but retain
* their children.
*/
public function blockElement(string $element): static
{
$clone = clone $this;
// Disallow the element is necessary
unset($clone->allowedElements[$element]);
$clone->blockedElements[$element] = true;
return $clone;
}
/**
* Configures the given element as dropped.
*
* Dropped elements are elements the sanitizer should remove from the input, including
* their children.
*
* Note: when using an empty configuration, all unknown elements are dropped
* automatically. This method let you drop elements that were allowed earlier
* in the configuration.
*/
public function dropElement(string $element): static
{
$clone = clone $this;
unset($clone->allowedElements[$element], $clone->blockedElements[$element]);
return $clone;
}
/**
* Configures the given attribute as allowed.
*
* Allowed attributes are attributes the sanitizer should retain from the input.
*
* A list of allowed elements for this attribute can be passed as a second argument.
* Passing "*" will allow all currently allowed elements to use this attribute.
*
* @param list<string>|string $allowedElements
*/
public function allowAttribute(string $attribute, array|string $allowedElements): static
{
$clone = clone $this;
$allowedElements = ('*' === $allowedElements) ? array_keys($clone->allowedElements) : (array) $allowedElements;
// For each configured element ...
foreach ($clone->allowedElements as $element => $attrs) {
if (\in_array($element, $allowedElements, true)) {
// ... if the attribute should be allowed, add it
$clone->allowedElements[$element][$attribute] = true;
} else {
// ... if the attribute should not be allowed, remove it
unset($clone->allowedElements[$element][$attribute]);
}
}
return $clone;
}
/**
* Configures the given attribute as dropped.
*
* Dropped attributes are attributes the sanitizer should remove from the input.
*
* A list of elements on which to drop this attribute can be passed as a second argument.
* Passing "*" will drop this attribute from all currently allowed elements.
*
* Note: when using an empty configuration, all unknown attributes are dropped
* automatically. This method let you drop attributes that were allowed earlier
* in the configuration.
*
* @param list<string>|string $droppedElements
*/
public function dropAttribute(string $attribute, array|string $droppedElements): static
{
$clone = clone $this;
$droppedElements = ('*' === $droppedElements) ? array_keys($clone->allowedElements) : (array) $droppedElements;
foreach ($droppedElements as $element) {
if (isset($clone->allowedElements[$element][$attribute])) {
unset($clone->allowedElements[$element][$attribute]);
}
}
return $clone;
}
/**
* Forcefully set the value of a given attribute on a given element.
*
* The attribute will be created on the nodes if it didn't exist.
*/
public function forceAttribute(string $element, string $attribute, string $value): static
{
$clone = clone $this;
$clone->forcedAttributes[$element][$attribute] = $value;
return $clone;
}
/**
* Registers a custom attribute sanitizer.
*/
public function withAttributeSanitizer(AttributeSanitizerInterface $sanitizer): static
{
$clone = clone $this;
$clone->attributeSanitizers[] = $sanitizer;
return $clone;
}
/**
* Unregisters a custom attribute sanitizer.
*/
public function withoutAttributeSanitizer(AttributeSanitizerInterface $sanitizer): static
{
$clone = clone $this;
$clone->attributeSanitizers = array_values(array_filter(
$this->attributeSanitizers,
static fn ($current) => $current !== $sanitizer
));
return $clone;
}
/**
* @param int $maxInputLength The maximum length of the input string in bytes
* -1 means no limit
*/
public function withMaxInputLength(int $maxInputLength): static
{
if ($maxInputLength < -1) {
throw new \InvalidArgumentException(sprintf('The maximum input length must be greater than -1, "%d" given.', $maxInputLength));
}
$clone = clone $this;
$clone->maxInputLength = $maxInputLength;
return $clone;
}
public function getMaxInputLength(): int
{
return $this->maxInputLength;
}
/**
* @return array<string, array<string, true>>
*/
public function getAllowedElements(): array
{
return $this->allowedElements;
}
/**
* @return array<string, true>
*/
public function getBlockedElements(): array
{
return $this->blockedElements;
}
/**
* @return array<string, array<string, string>>
*/
public function getForcedAttributes(): array
{
return $this->forcedAttributes;
}
/**
* @return list<string>
*/
public function getAllowedLinkSchemes(): array
{
return $this->allowedLinkSchemes;
}
/**
* @return list<string>|null
*/
public function getAllowedLinkHosts(): ?array
{
return $this->allowedLinkHosts;
}
public function getAllowRelativeLinks(): bool
{
return $this->allowRelativeLinks;
}
/**
* @return list<string>
*/
public function getAllowedMediaSchemes(): array
{
return $this->allowedMediaSchemes;
}
/**
* @return list<string>|null
*/
public function getAllowedMediaHosts(): ?array
{
return $this->allowedMediaHosts;
}
public function getAllowRelativeMedias(): bool
{
return $this->allowRelativeMedias;
}
public function getForceHttpsUrls(): bool
{
return $this->forceHttpsUrls;
}
/**
* @return list<AttributeSanitizerInterface>
*/
public function getAttributeSanitizers(): array
{
return $this->attributeSanitizers;
}
}