CVE-2024-25117: Insufficient Information (NVD-CWE-noinfo)
value: php-svg-lib is a scalable vector graphics (SVG) file parsing/rendering library. Prior to version 0.5.2, php-svg-lib fails to validate that font-family doesn't contain a PHAR url, which might leads to RCE on PHP < 8.0, and doesn't validate if external references are allowed. This might leads to bypass of restrictions or RCE on projects that are using it, if they do not strictly revalidate the fontName that is passed by php-svg-lib. The `Style::fromAttributes(`), or the `Style::parseCssStyle()` should check the content of the `font-family` and prevents it to use a PHAR url, to avoid passing an invalid and dangerous `fontName` value to other libraries. The same check as done in the `Style::fromStyleSheets` might be reused. Libraries using this library as a dependency might be vulnerable to some bypass of restrictions, or even remote code execution, if they do not double check the value of the `fontName` that is passed by php-svg-lib. Version 0.5.2 contains a fix for this issue.
CWE description: There is insufficient information about the issue to classify it; details are unkown or unspecified.
Published: 2024-02-21T17:15Z
Repository: https://github.com/dompdf/php-svg-lib
Language: PHP
Files changed: src/Svg/Document.php, src/Svg/Style.php, src/Svg/Tag/AbstractTag.php, src/Svg/Tag/Image.php
Commit date: 2024-02-07 07:01:19 -0500
Commit message: Update resource validation logic

The previous logic did not validate the font-family when set by attribute. To accommodate style validation across all sources the Style class now accepts the Document during construction so that it has access to the allowExternalReferences property regardless of style source.

Diff: diff --git a/src/Svg/Document.php b/src/Svg/Document.php
index 309875b..990cfde 100644
--- a/src/Svg/Document.php
+++ b/src/Svg/Document.php
@@ -200,11 +200,11 @@ class Document extends AbstractTag
 
     protected function before($attributes)
     {
         $surface = $this->getSurface();
 
-        $style = new DefaultStyle();
+        $style = new DefaultStyle($this);
         $style->inherit($this);
         $style->fromAttributes($attributes);
 
         $this->setStyle($style);
 
diff --git a/src/Svg/Style.php b/src/Svg/Style.php
index 514f546..9fac469 100644
--- a/src/Svg/Style.php
+++ b/src/Svg/Style.php
@@ -16,10 +16,11 @@ class Style
     const TYPE_LENGTH = 2;
     const TYPE_NAME = 3;
     const TYPE_ANGLE = 4;
     const TYPE_NUMBER = 5;
 
+    private $_document;
     private $_parentStyle;
 
     public $color;
     public $opacity;
     public $display;
@@ -41,10 +42,16 @@ class Style
     public $fontSize = 12;
     public $fontWeight = 'normal';
     public $fontStyle = 'normal';
     public $textAnchor = 'start';
 
+    public function __construct($document = null) {
+        if ($document !== null) {
+            $this->_document = $document;
+        }
+    }
+
     protected function getStyleMap()
     {
         return array(
             'color'             => array('color', self::TYPE_COLOR),
             'opacity'           => array('opacity', self::TYPE_NUMBER),
@@ -137,20 +144,10 @@ class Style
                         }
 
                         break;
                     }
                 }
-
-                if (
-                    \array_key_exists("font-family", $styles)
-                    && (
-                        \strtolower(\substr($this->href, 0, 7)) === "phar://"
-                        || ($this->document->allowExternalReferences === false && \strtolower(\substr($this->href, 0, 5)) !== "data:")
-                    )
-                ) {
-                    unset($style["font-family"]);
-                }
             }
         }
 
         $this->fillStyles($styles);
     }
@@ -183,10 +180,20 @@ class Style
 
                     default:
                         $value = $styles[$from];
                 }
 
+                if ($from === "font-family") {
+                    $scheme = \strtolower(parse_url($value, PHP_URL_SCHEME) ?: "");
+                    if (
+                        $scheme === "phar" || \strtolower(\substr($value, 0, 7)) === "phar://"
+                        || ($this->_document !== null && $this->_document->allowExternalReferences === false && $scheme !== "data")
+                    ) {
+                        continue;
+                    }
+                }
+
                 if ($value !== null) {
                     $this->$to = $value;
                 }
             }
         }
diff --git a/src/Svg/Tag/AbstractTag.php b/src/Svg/Tag/AbstractTag.php
index 9fa6793..e368aab 100644
--- a/src/Svg/Tag/AbstractTag.php
+++ b/src/Svg/Tag/AbstractTag.php
@@ -117,11 +117,11 @@ abstract class AbstractTag
      * @param array $attributes
      *
      * @return Style
      */
     protected function makeStyle($attributes) {
-        $style = new Style();
+        $style = new Style($this->document);
         $style->inherit($this);
         $style->fromStyleSheets($this, $attributes);
         $style->fromAttributes($attributes);
 
         return $style;
diff --git a/src/Svg/Tag/Image.php b/src/Svg/Tag/Image.php
index 8cbfccd..1689954 100644
--- a/src/Svg/Tag/Image.php
+++ b/src/Svg/Tag/Image.php
@@ -56,12 +56,18 @@ class Image extends AbstractTag
             $this->href = $attributes['href'];
         }
 
         $this->document->getSurface()->transform(1, 0, 0, -1, 0, $height);
 
-        if (\strtolower(\substr($this->href, 0, 7)) === "phar://" || ($this->document->allowExternalReferences === false && \strtolower(\substr($this->href, 0, 5) !== "data:"))) {
-            return;
+        if ($from === "font-family") {
+            $scheme = \strtolower(parse_url($this->href, PHP_URL_SCHEME) ?: "");
+            if (
+                $scheme === "phar" || \strtolower(\substr($this->href, 0, 7)) === "phar://"
+                || ($this->document->allowExternalReferences === false && $scheme !== "data")
+            ) {
+                return;
+            }
         }
 
         $this->document->getSurface()->drawImage($this->href, $this->x, $this->y, $this->width, $this->height);
     }
 

Vulnerable code:         $style = new DefaultStyle();

                if (
                    \array_key_exists("font-family", $styles)
                    && (
                        \strtolower(\substr($this->href, 0, 7)) === "phar://"
                        || ($this->document->allowExternalReferences === false && \strtolower(\substr($this->href, 0, 5)) !== "data:")
                    )
                ) {
                    unset($style["font-family"]);
                }
        $style = new Style();
        if (\strtolower(\substr($this->href, 0, 7)) === "phar://" || ($this->document->allowExternalReferences === false && \strtolower(\substr($this->href, 0, 5) !== "data:"))) {
            return;
Fixed code:         $style = new DefaultStyle($this);
    private $_document;
    public function __construct($document = null) {
        if ($document !== null) {
            $this->_document = $document;
        }
    }

                if ($from === "font-family") {
                    $scheme = \strtolower(parse_url($value, PHP_URL_SCHEME) ?: "");
                    if (
                        $scheme === "phar" || \strtolower(\substr($value, 0, 7)) === "phar://"
                        || ($this->_document !== null && $this->_document->allowExternalReferences === false && $scheme !== "data")
                    ) {
                        continue;
                    }
                }

        $style = new Style($this->document);
        if ($from === "font-family") {
            $scheme = \strtolower(parse_url($this->href, PHP_URL_SCHEME) ?: "");
            if (
                $scheme === "phar" || \strtolower(\substr($this->href, 0, 7)) === "phar://"
                || ($this->document->allowExternalReferences === false && $scheme !== "data")
            ) {
                return;
            }