Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to delete remote references in svg sanitizer #14492

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/config/GeneralConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2557,6 +2557,24 @@ class GeneralConfig extends BaseConfig
*/
public bool $sanitizeSvgUploads = true;

/**
* @var bool Whether Craft should sanitize uploaded SVG files and delete any remote references.
*
* This should definitely be enabled if you are accepting SVG uploads from untrusted sources.
*
* ::: code
* ```php Static Config
* ->sanitizeSvgRemoteRefs(true)
* ```
* ```shell Environment Override
* CRAFT_SANITIZE_SVG_REMOTE_REFS=true
* ```
* :::
*
* @group Security
*/
public bool $sanitizeSvgRemoteRefs = false;

/**
* @var string A private, random, cryptographically-secure key that is used for hashing and encrypting data in [[\craft\services\Security]].
*
Expand Down Expand Up @@ -5963,6 +5981,27 @@ public function sanitizeSvgUploads(bool $value = true): self
return $this;
}

/**
* Whether Craft should sanitize uploaded SVG files and delete any remote references.
*
* This should definitely be enabled if you are accepting SVG uploads from untrusted sources.
*
* ```php
* ->sanitizeSvgRemoteRefs(true)
* ```
*
* @group Security
* @param bool $value
* @return self
* @see $sanitizeSvgRemoteRefs
* @since 4.9.0
*/
public function sanitizeSvgRemoteRefs(bool $value = false): self
{
$this->sanitizeSvgRemoteRefs = $value;
return $this;
}

/**
* A private, random, cryptographically-secure key that is used for hashing and encrypting data in [[\craft\services\Security]].
*
Expand Down
1 change: 1 addition & 0 deletions src/services/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ public function cleanImage(string $filePath): void
}

$sanitizer = new Sanitizer();
$sanitizer->removeRemoteReferences(Craft::$app->getConfig()->getGeneral()->sanitizeSvgRemoteRefs);
$sanitizer->setAllowedAttrs(new SvgAllowedAttributes());
$svgContents = file_get_contents($filePath);
$svgContents = $sanitizer->sanitize($svgContents);
Expand Down