Skip to content

Commit

Permalink
Update resource validation logic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bsweeney committed Feb 7, 2024
1 parent 8a8a1eb commit 8ffcc41
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Svg/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ protected function before($attributes)
{
$surface = $this->getSurface();

$style = new DefaultStyle();
$style = new DefaultStyle($this);
$style->inherit($this);
$style->fromAttributes($attributes);

Expand Down
27 changes: 17 additions & 10 deletions src/Svg/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Style
const TYPE_ANGLE = 4;
const TYPE_NUMBER = 5;

private $_document;
private $_parentStyle;

public $color;
Expand All @@ -43,6 +44,12 @@ class Style
public $fontStyle = 'normal';
public $textAnchor = 'start';

public function __construct($document = null) {
if ($document !== null) {
$this->_document = $document;
}
}

protected function getStyleMap()
{
return array(
Expand Down Expand Up @@ -139,16 +146,6 @@ public function fromStyleSheets(AbstractTag $tag, $attributes) {
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"]);
}
}
}

Expand Down Expand Up @@ -185,6 +182,16 @@ protected function fillStyles($styles)
$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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Svg/Tag/AbstractTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function getStyle()
* @return Style
*/
protected function makeStyle($attributes) {
$style = new Style();
$style = new Style($this->document);
$style->inherit($this);
$style->fromStyleSheets($this, $attributes);
$style->fromAttributes($attributes);
Expand Down
10 changes: 8 additions & 2 deletions src/Svg/Tag/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,14 @@ public function start($attributes)

$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")