34 use IsMimeTypeOrExtension;
55 private const SVG =
'svg';
142 ?
string $additional_message_script =
null,
143 ?
string $additional_message_base64 =
null,
144 ?
string $additional_message_foreign_object =
null,
145 ?
string $additional_message_elements =
null
148 $this->rejection_message_script = $additional_message_script ??
'contains script tags';
149 $this->rejection_message_base64 = $additional_message_base64 ??
'contains base64 encoded content';
150 $this->rejection_message_foreign_object = $additional_message_foreign_object ??
'contains foreign object';
151 $this->rejection_message_elements = $additional_message_elements ??
'contains not allowed or unknown elements or attributes';
156 return $this->isMimeTypeOrExtension(
159 [self::SVG_MIME_TYPE]
173 $dom = new \DOMDocument();
175 $dom->loadXML($raw_svg_content, LIBXML_NOWARNING | LIBXML_NOERROR);
176 }
catch (\Exception) {
179 $errors = libxml_get_errors();
180 if ($errors !== []) {
188 $raw_svg_content = (string) $stream;
189 if (
false === $raw_svg_content) {
207 $prohibited_attributes = (fn(
string $name):
bool => in_array(strtolower($name), $this->svg_event_lists,
true));
208 return $looper($dom, $prohibited_attributes) !==
false;
214 if (preg_match(self::REGEX_BASE64, $raw_svg_content)) {
220 if (preg_match(self::REGEX_SCRIPT, $raw_svg_content)) {
226 if (preg_match(self::REGEX_FOREIGN_OBJECT, $raw_svg_content)) {
236 return function (\DOMDocument $dom, \Closure $closure):
bool {
237 $attributes_looper =
function (\DOMNode $node, \Closure $closure) use (&$attributes_looper):
bool {
238 foreach ($node->attributes as $attribute) {
239 if ($closure($attribute->name)) {
240 $this->rejection_message .= sprintf(
241 $this->rejection_message_elements .
' (%s)',
247 foreach ($node->childNodes as $child) {
248 if ($child instanceof \DOMElement && !$attributes_looper($child, $closure)) {
254 foreach ($dom->getElementsByTagName(
"*") as $i => $element) {
255 if ($attributes_looper($element, $closure) ===
false) {
Class SVGBlacklistPreProcessor.
string $rejection_message
string $rejection_message_elements
checkStream(FileStream $stream)
string $rejection_message_base64
__construct(?string $rejection_message=null, ?string $additional_message_script=null, ?string $additional_message_base64=null, ?string $additional_message_foreign_object=null, ?string $additional_message_elements=null)
string $rejection_message_foreign_object
process(FileStream $stream, Metadata $metadata)
This method gets invoked by the file upload service to process the file with the help of the processo...
getDomDocument(string $raw_svg_content)
const REGEX_FOREIGN_OBJECT
hasContentScriptTag(string $raw_svg_content)
string $rejection_message_script
isSVG(Metadata $metadata)
The base interface for all filesystem streams.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...