Seeking Review: Security of SVG Sanitizer #187476
-
Select Topic AreaQuestion BodyHi everyone, I’ve been working on improving the sanitizer in php-svg-optimizer and want to make sure it’s secure, especially when handling untrusted SVG input. The sanitizer currently focuses on removing unsafe elements, non-standard tags, and risky attributes, but I’d really appreciate a second set of eyes to verify that nothing is missed that could lead to XSS or other attacks. If you have experience with SVG security, sanitization, or PHP security best practices, I’d love your feedback. You can review the relevant code here. Example of how to use it: <?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use MathiasReker\PhpSvgOptimizer\Service\Facade\SvgOptimizerFacade;
try {
$svgOptimizer = SvgOptimizerFacade::fromFile('path/to/source.svg')
->withRules(
removeNonStandardAttributes: true,
removeNonStandardTags: true,
removeUnsafeElements: true,
)
->allowRisky()
->optimize()
->saveToFile('path/to/output.svg');
} catch (\Exception $exception) {
echo $exception->getMessage();
}Any suggestions for improvements, potential pitfalls, or security holes would be extremely valuable. Link til library: https://github.com/MathiasReker/php-svg-optimizer Link til rules: https://github.com/MathiasReker/php-svg-optimizer/tree/develop/src/Service/Rule Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
If this is meant to handle untrusted SVG, the bar is “no executable surface left”. Removing “unsafe” or “non-standard” elements is not sufficient. Key requirements:
|
Beta Was this translation helpful? Give feedback.
If this is meant to handle untrusted SVG, the bar is “no executable surface left”. Removing “unsafe” or “non-standard” elements is not sufficient.
Key requirements:
Whitelist, not blacklist
“Non-standard” ≠ unsafe. Many standard SVG features are executable.
Eliminate all script vectors
<script>,<foreignObject>,<iframe>,<object>,<embed>.on*attributes (onload, onclick, etc.).javascript:,vbscript:,data:text/html.href/xlink:hrefstrictly.Block external resource lo…