Skip to content

Commit ca623eb

Browse files
realpascalbotjetrealpascalbotjet
authored andcommitted
Add allowed attributes and tags config option
Adds an 'allowed' key to the anti_xss config that calls removeEvilAttributes() and removeEvilHtmlTags() on the AntiXSS instance, allowing users to whitelist specific attributes (e.g. 'style') or tags without overriding the middleware. Fixes #13
1 parent 4f47ad4 commit ca623eb

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,16 @@ As of version 1.6.0, you may provide additional configuration for the `voku/anti
120120
],
121121

122122
'replacement' => '*redacted*',
123+
124+
'allowed' => [
125+
'attributes' => ['style'],
126+
'tags' => ['iframe'],
127+
],
123128
]
124129
```
125130

131+
The `evil` key adds *additional* attributes and tags to the evil list. The `allowed` key does the opposite — it removes attributes and tags from the default evil list, allowing them through the sanitizer. For example, if you need to allow `style` attributes or `iframe` tags in your content, add them to the `allowed` arrays.
132+
126133
## Changelog
127134

128135
Please see [CHANGELOG](CHANGELOG.md) for more information about what has changed recently.

config/xss-protection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,10 @@
2828
],
2929

3030
'replacement' => null,
31+
32+
'allowed' => [
33+
'attributes' => null,
34+
'tags' => null,
35+
],
3136
],
3237
];

src/ServiceProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ public function packageBooted()
4545
}
4646
}
4747

48+
$allowed = config('xss-protection.anti_xss.allowed');
49+
50+
if ($allowed !== null) {
51+
if (! empty($allowed['attributes'])) {
52+
$antiXss->removeEvilAttributes($allowed['attributes']);
53+
}
54+
55+
if (! empty($allowed['tags'])) {
56+
$antiXss->removeEvilHtmlTags($allowed['tags']);
57+
}
58+
}
59+
4860
return $antiXss;
4961
});
5062
}

tests/MiddlewareTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,17 @@ class ExceptXssCleanInput extends XssCleanInput
217217
expect($request->input('allow'))->toBe('test<script>script</script>');
218218
expect($request->input('nested.allowed'))->toBe('test<script>script</script>');
219219
});
220+
221+
it('can allow inline style attributes via config', function () {
222+
$request = Request::createFromGlobals()->merge([
223+
'key' => '<div style="color: red">Hello</div>',
224+
]);
225+
226+
config(['xss-protection.anti_xss.allowed.attributes' => ['style']]);
227+
228+
/** @var XssCleanInput $middleware */
229+
$middleware = app(XssCleanInput::class);
230+
$middleware->handle($request, fn ($request) => $request);
231+
232+
expect($request->input('key'))->toBe('<div style="color: red">Hello</div>');
233+
});

0 commit comments

Comments
 (0)