Skip to content

Commit 78f2ccf

Browse files
committed
Add logging feature to the sanitizer
1 parent 45ec054 commit 78f2ccf

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

src/Sanitizer.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use HtmlSanitizer\Extension\Table\TableExtension;
2121
use HtmlSanitizer\Parser\MastermindsParser;
2222
use HtmlSanitizer\Parser\ParserInterface;
23+
use Psr\Log\LoggerInterface;
2324

2425
/**
2526
* @author Titouan Galopin <galopintitouan@gmail.com>
@@ -43,11 +44,17 @@ class Sanitizer implements SanitizerInterface
4344
*/
4445
private $parser;
4546

46-
public function __construct(DomVisitorInterface $domVisitor, int $maxInputLength, ParserInterface $parser = null)
47+
/**
48+
* @var LoggerInterface|null
49+
*/
50+
private $logger;
51+
52+
public function __construct(DomVisitorInterface $domVisitor, int $maxInputLength, ParserInterface $parser = null, LoggerInterface $logger = null)
4753
{
4854
$this->domVisitor = $domVisitor;
4955
$this->maxInputLength = $maxInputLength;
5056
$this->parser = $parser ?: new MastermindsParser();
57+
$this->logger = $logger;
5158
}
5259

5360
/**
@@ -72,6 +79,19 @@ public static function create(array $config): SanitizerInterface
7279
}
7380

7481
public function sanitize(string $html): string
82+
{
83+
$sanitized = $this->doSanitize($html);
84+
85+
if ($this->logger) {
86+
$this->logger->debug('Sanitized given input to "{output}".', [
87+
'output' => mb_substr($sanitized, 0, 50).(mb_strlen($sanitized) > 50 ? '...' : ''),
88+
]);
89+
}
90+
91+
return $sanitized;
92+
}
93+
94+
private function doSanitize(string $html): string
7595
{
7696
// Prevent DOS attack induced by extremely long HTML strings
7797
if (mb_strlen($html) > $this->maxInputLength) {

src/SanitizerBuilder.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
namespace HtmlSanitizer;
1313

1414
use HtmlSanitizer\Extension\ExtensionInterface;
15+
use HtmlSanitizer\Parser\ParserInterface;
1516
use HtmlSanitizer\Visitor\ScriptNodeVisitor;
1617
use HtmlSanitizer\Visitor\StyleNodeVisitor;
18+
use Psr\Log\LoggerInterface;
1719

1820
/**
1921
* @author Titouan Galopin <galopintitouan@gmail.com>
@@ -27,11 +29,31 @@ class SanitizerBuilder implements SanitizerBuilderInterface
2729
*/
2830
private $extensions = [];
2931

32+
/**
33+
* @var ParserInterface|null
34+
*/
35+
private $parser;
36+
37+
/**
38+
* @var LoggerInterface|null
39+
*/
40+
private $logger;
41+
3042
public function registerExtension(ExtensionInterface $extension)
3143
{
3244
$this->extensions[$extension->getName()] = $extension;
3345
}
3446

47+
public function setParser(?ParserInterface $parser)
48+
{
49+
$this->parser = $parser;
50+
}
51+
52+
public function setLogger(?LoggerInterface $logger)
53+
{
54+
$this->logger = $logger;
55+
}
56+
3557
public function build(array $config): SanitizerInterface
3658
{
3759
$nodeVisitors = [];
@@ -54,6 +76,6 @@ public function build(array $config): SanitizerInterface
5476
$nodeVisitors['script'] = new ScriptNodeVisitor();
5577
$nodeVisitors['style'] = new StyleNodeVisitor();
5678

57-
return new Sanitizer(new DomVisitor($nodeVisitors), $config['max_input_length'] ?? 20000);
79+
return new Sanitizer(new DomVisitor($nodeVisitors), $config['max_input_length'] ?? 20000, $this->parser, $this->logger);
5880
}
5981
}

tests/LoggedSanitizerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the HTML sanitizer project.
5+
*
6+
* (c) Titouan Galopin <galopintitouan@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\HtmlSanitizer;
13+
14+
use HtmlSanitizer\SanitizerBuilder;
15+
use PHPUnit\Framework\TestCase;
16+
use Psr\Log\LoggerInterface;
17+
18+
class LoggedSanitizerTest extends TestCase
19+
{
20+
public function testLoggedSanitizer()
21+
{
22+
$logger = $this->createMock(LoggerInterface::class);
23+
$logger->expects($this->once())
24+
->method('debug')
25+
->with('Sanitized given input to "{output}".', ['output' => 'Hello']);
26+
27+
$builder = new SanitizerBuilder();
28+
$builder->setLogger($logger);
29+
$builder->build([])->sanitize('<div>Hello</div>');
30+
}
31+
}

0 commit comments

Comments
 (0)