Skip to content

Commit c81d5ed

Browse files
authored
Merge pull request #148 from incentify-platform/add-null-support
Allow explicit non string types through without being cast to empty strings
2 parents 8e0b3d8 + 9f7d0cf commit c81d5ed

File tree

4 files changed

+90
-8
lines changed

4 files changed

+90
-8
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ Config file `config/purifier.php` should like this
7070
```php
7171

7272
return [
73-
'encoding' => 'UTF-8',
74-
'finalize' => true,
75-
'cachePath' => storage_path('app/purifier'),
76-
'cacheFileMode' => 0755,
73+
'encoding' => 'UTF-8',
74+
'finalize' => true,
75+
'ignoreNonStrings' => false,
76+
'cachePath' => storage_path('app/purifier'),
77+
'cacheFileMode' => 0755,
7778
'settings' => [
7879
'default' => [
7980
'HTML.Doctype' => 'HTML 4.01 Transitional',

config/purifier.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
*/
1818

1919
return [
20-
'encoding' => 'UTF-8',
21-
'finalize' => true,
22-
'cachePath' => storage_path('app/purifier'),
23-
'cacheFileMode' => 0755,
20+
'encoding' => 'UTF-8',
21+
'finalize' => true,
22+
'ignoreNonStrings' => false,
23+
'cachePath' => storage_path('app/purifier'),
24+
'cacheFileMode' => 0755,
2425
'settings' => [
2526
'default' => [
2627
'HTML.Doctype' => 'HTML 4.01 Transitional',

src/Purifier.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ public function clean($dirty, $config = null, \Closure $postCreateConfigHook = n
273273
}
274274
}
275275

276+
//If $dirty is not an explicit string, bypass purification assuming configuration allows this
277+
$ignoreNonStrings = $this->config->get('purifier.ignoreNonStrings', false);
278+
$stringTest = is_string($dirty);
279+
if($stringTest === false && $ignoreNonStrings === true) {
280+
return $dirty;
281+
}
282+
276283
return $this->purifier->purify($dirty, $configObject);
277284
}
278285

tests/PurifierTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,79 @@ public function testCleaningWithCustomConfigAndPostCreateHook()
111111
$this->assertSame('<p><a href="https://example.com">https://example.com</a></p>', $pureHtml);
112112
}
113113

114+
public function testCleaningNullPassThru() {
115+
$testConfig = require __DIR__.'/../config/purifier.php';
116+
$configRepo = new Repository(['purifier'=>$testConfig]);
117+
118+
//$purifier = $this->app->make('purifier');
119+
$purifier = new Purifier(new Filesystem(), $configRepo);
120+
121+
//test default config value is expected
122+
$this->assertEquals(false, $configRepo->get('purifier.ignoreNonStrings'));
123+
124+
//Test default behavior is unchanged without nullPassThru Config value of true
125+
$html = null;
126+
$pureHtml = $purifier->clean($html);
127+
$this->assertEquals('', $pureHtml);
128+
$html = false;
129+
$pureHtml = $purifier->clean($html);
130+
$this->assertEquals('', $pureHtml);
131+
132+
$html = [
133+
'good'=>'<span id="some-id">This is my H1 title',
134+
'bad'=>'<script>alert(\'XSS\');</script>',
135+
'empty'=>null,
136+
'bool'=>false,
137+
'bool2'=>true,
138+
'float'=>4.321,
139+
];
140+
$expectedHtml = [
141+
'good'=>'<p><span>This is my H1 title</span></p>',
142+
'bad'=>'',
143+
'empty'=>'',
144+
'bool'=>'',
145+
'bool2'=>'<p>1</p>',
146+
'float'=>'<p>4.321</p>'
147+
];
148+
$pureHtml = $purifier->clean($html);
149+
$this->assertEquals($expectedHtml, $pureHtml);
150+
151+
152+
//Test behavior as expected with nullPassThru Config value of true
153+
$configRepo->set('purifier.ignoreNonStrings', true);
154+
$purifier = new Purifier(new Filesystem(), $configRepo);
155+
$this->assertEquals(true, $configRepo->get('purifier.ignoreNonStrings'));
156+
157+
$html = null;
158+
$pureHtml = $purifier->clean($html);
159+
$this->assertEquals(null, $pureHtml);
160+
161+
$html = false;
162+
$pureHtml = $purifier->clean($html);
163+
$this->assertEquals(false, $pureHtml);
164+
165+
$html = [
166+
'good'=>'<span id="some-id">This is my H1 title',
167+
'bad'=>'<script>alert(\'XSS\');</script>',
168+
'empty'=>null,
169+
'emptyStr'=>'',
170+
'bool'=>false,
171+
'bool2'=>true,
172+
'float'=>4.321,
173+
];
174+
$expectedHtml = [
175+
'good'=>'<p><span>This is my H1 title</span></p>',
176+
'bad'=>'',
177+
'empty'=>null,
178+
'emptyStr'=>'',
179+
'bool'=>false,
180+
'bool2'=>true,
181+
'float'=>4.321,
182+
];
183+
$pureHtml = $purifier->clean($html);
184+
$this->assertEquals($expectedHtml, $pureHtml);
185+
}
186+
114187
public function testCustomDefinitions()
115188
{
116189
/** @var HTMLPurifier $purifier */

0 commit comments

Comments
 (0)