Skip to content

Commit b291f58

Browse files
[HtmlSanitizer] Remove MastermindsParser and add $context arg to ParserInterface::parse()
1 parent ace7825 commit b291f58

File tree

10 files changed

+17
-102
lines changed

10 files changed

+17
-102
lines changed

UPGRADE-8.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ FrameworkBundle
171171
* Remove the `validation.cache` option
172172
* Remove `TranslationUpdateCommand` in favor of `TranslationExtractCommand`
173173

174+
HtmlSanitizer
175+
-------------
176+
177+
* Remove `MastermindsParser`; use `NativeParser` instead
178+
* Add argument `$context` to `ParserInterface::parse()`
179+
174180
HttpFoundation
175181
--------------
176182

src/Symfony/Component/HtmlSanitizer/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
8.0
5+
---
6+
7+
* Remove `MastermindsParser`; use `NativeParser` instead
8+
* Add argument `$context` to `ParserInterface::parse()`
9+
410
7.4
511
---
612

src/Symfony/Component/HtmlSanitizer/HtmlSanitizer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\HtmlSanitizer;
1313

14-
use Symfony\Component\HtmlSanitizer\Parser\MastermindsParser;
1514
use Symfony\Component\HtmlSanitizer\Parser\NativeParser;
1615
use Symfony\Component\HtmlSanitizer\Parser\ParserInterface;
1716
use Symfony\Component\HtmlSanitizer\Reference\W3CReference;
@@ -35,7 +34,7 @@ public function __construct(
3534
?ParserInterface $parser = null,
3635
) {
3736
$this->config = $config;
38-
$this->parser = $parser ?? (\PHP_VERSION_ID < 80400 ? new MastermindsParser() : new NativeParser());
37+
$this->parser = $parser ?? new NativeParser();
3938
}
4039

4140
public function sanitize(string $input): string

src/Symfony/Component/HtmlSanitizer/Parser/MastermindsParser.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/Symfony/Component/HtmlSanitizer/Parser/NativeParser.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616
*/
1717
final class NativeParser implements ParserInterface
1818
{
19-
public function __construct()
20-
{
21-
if (\PHP_VERSION_ID < 80400) {
22-
throw new \LogicException(self::class.' requires PHP 8.4 or higher.');
23-
}
24-
}
25-
2619
public function parse(string $html, string $context = 'body'): ?\Dom\Node
2720
{
2821
$document = @\Dom\HTMLDocument::createFromString(\sprintf('<!DOCTYPE html><%s>%s</%1$s>', $context, $html));

src/Symfony/Component/HtmlSanitizer/Parser/ParserInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ interface ParserInterface
2525
*
2626
* @param string $context The name of the context element in which the HTML is parsed
2727
*/
28-
public function parse(string $html/* , string $context = 'body' */): \Dom\Node|\DOMNode|null;
28+
public function parse(string $html, string $context = 'body'): \Dom\Node|\DOMNode|null;
2929
}

src/Symfony/Component/HtmlSanitizer/Tests/HtmlSanitizerAllTest.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,8 @@ public static function provideSanitizeHead()
6464
}
6565

6666
#[DataProvider('provideSanitizeBody')]
67-
public function testSanitizeBody(string $input, string $expected, ?string $legacyExpected = null)
67+
public function testSanitizeBody(string $input, string $expected)
6868
{
69-
if (\PHP_VERSION_ID < 80400) {
70-
$expected = $legacyExpected ?? $expected;
71-
}
7269
$this->assertSame($expected, $this->createSanitizer()->sanitize($input));
7370
}
7471

@@ -87,7 +84,6 @@ public static function provideSanitizeBody()
8784
[
8885
'< Hello',
8986
'&lt; Hello',
90-
' Hello',
9187
],
9288
[
9389
'Lorem & Ipsum',
@@ -132,7 +128,6 @@ public static function provideSanitizeBody()
132128
[
133129
'<<a href="javascript:evil"/>a href="javascript:evil"/>',
134130
'&lt;<a>a href&#61;&#34;javascript:evil&#34;/&gt;</a>',
135-
'<a>a href&#61;&#34;javascript:evil&#34;/&gt;</a>',
136131
],
137132
[
138133
'<a href="javascript:alert(\'ok\')">Test</a>',
@@ -169,12 +164,10 @@ public static function provideSanitizeBody()
169164
[
170165
'<<img src="javascript:evil"/>iframe src="javascript:evil"/>',
171166
'&lt;<img />iframe src&#61;&#34;javascript:evil&#34;/&gt;',
172-
'<img />iframe src&#61;&#34;javascript:evil&#34;/&gt;',
173167
],
174168
[
175169
'<<img src="javascript:evil"/>img src="javascript:evil"/>',
176170
'&lt;<img />img src&#61;&#34;javascript:evil&#34;/&gt;',
177-
'<img />img src&#61;&#34;javascript:evil&#34;/&gt;',
178171
],
179172
[
180173
'<IMG SRC="javascript:alert(\'XSS\');">',
@@ -219,12 +212,10 @@ public static function provideSanitizeBody()
219212
[
220213
'<IMG SRC=&#0000106&#0000097&#0000118&#0000097&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116&#0000058&#0000097&#0000108&#0000101&#0000114&#0000116&#0000040&#0000039&#0000088&#0000083&#0000083&#0000039&#0000041>',
221214
'<img />',
222-
'<img src="&amp;#0000106&amp;#0000097&amp;#0000118&amp;#0000097&amp;#0000115&amp;#0000099&amp;#0000114&amp;#0000105&amp;#0000112&amp;#0000116&amp;#0000058&amp;#0000097&amp;#0000108&amp;#0000101&amp;#0000114&amp;#0000116&amp;#0000040&amp;#0000039&amp;#0000088&amp;#0000083&amp;#0000083&amp;#0000039&amp;#0000041" />',
223215
],
224216
[
225217
'<IMG SRC=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>',
226218
'<img />',
227-
'<img src="&amp;#x6A&amp;#x61&amp;#x76&amp;#x61&amp;#x73&amp;#x63&amp;#x72&amp;#x69&amp;#x70&amp;#x74&amp;#x3A&amp;#x61&amp;#x6C&amp;#x65&amp;#x72&amp;#x74&amp;#x28&amp;#x27&amp;#x58&amp;#x53&amp;#x53&amp;#x27&amp;#x29" />',
228219
],
229220
[
230221
'<IMG DYNSRC="javascript:alert(\'XSS\')">',
@@ -531,7 +522,6 @@ public static function provideSanitizeBody()
531522
[
532523
'<table>Lorem ipsum</table>',
533524
'Lorem ipsum<table></table>',
534-
'<table>Lorem ipsum</table>',
535525
],
536526
[
537527
'<ul>Lorem ipsum</ul>',
@@ -545,12 +535,8 @@ public static function provideSanitizeBody()
545535
}
546536

547537
#[DataProvider('provideSanitizeTable')]
548-
public function testSanitizeTable(string $input, string $expected, ?string $legacyExpected = null)
538+
public function testSanitizeTable(string $input, string $expected)
549539
{
550-
if (\PHP_VERSION_ID < 80400) {
551-
$expected = $legacyExpected ?? $expected;
552-
}
553-
554540
$this->assertSame($expected, $this->createSanitizer()->sanitizeFor('table', $input));
555541
}
556542

@@ -564,32 +550,26 @@ public static function provideSanitizeTable(): iterable
564550
[
565551
'<tbody>Lorem ipsum</tbody>',
566552
'<tbody></tbody>',
567-
'<tbody>Lorem ipsum</tbody>',
568553
],
569554
[
570555
'<td>Lorem ipsum</td>',
571556
'<tbody><tr><td>Lorem ipsum</td></tr></tbody>',
572-
'<td>Lorem ipsum</td>',
573557
],
574558
[
575559
'<tfoot>Lorem ipsum</tfoot>',
576560
'<tfoot></tfoot>',
577-
'<tfoot>Lorem ipsum</tfoot>',
578561
],
579562
[
580563
'<thead>Lorem ipsum</thead>',
581564
'<thead></thead>',
582-
'<thead>Lorem ipsum</thead>',
583565
],
584566
[
585567
'<th>Lorem ipsum</th>',
586568
'<tbody><tr><th>Lorem ipsum</th></tr></tbody>',
587-
'<th>Lorem ipsum</th>',
588569
],
589570
[
590571
'<tr>Lorem ipsum</tr>',
591572
'<tbody><tr></tr></tbody>',
592-
'<tr>Lorem ipsum</tr>',
593573
],
594574
];
595575
}

src/Symfony/Component/HtmlSanitizer/Tests/Parser/MastermindsParserTest.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Symfony/Component/HtmlSanitizer/Tests/Parser/NativeParserTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111

1212
namespace Symfony\Component\HtmlSanitizer\Tests\Parser;
1313

14-
use PHPUnit\Framework\Attributes\RequiresPhp;
1514
use PHPUnit\Framework\TestCase;
1615
use Symfony\Component\HtmlSanitizer\Parser\NativeParser;
1716

18-
#[RequiresPhp('8.4')]
1917
class NativeParserTest extends TestCase
2018
{
2119
public function testParseValid()

src/Symfony/Component/HtmlSanitizer/composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
"require": {
1919
"php": ">=8.4",
2020
"ext-dom": "*",
21-
"league/uri": "^6.5|^7.0",
22-
"masterminds/html5": "^2.7.2",
23-
"symfony/deprecation-contracts": "^2.5|^3"
21+
"league/uri": "^6.5|^7.0"
2422
},
2523
"autoload": {
2624
"psr-4": { "Symfony\\Component\\HtmlSanitizer\\": "" },

0 commit comments

Comments
 (0)