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

[5.x] Allow opting out of svg tag sanitization #10020

Merged
merged 1 commit into from
May 8, 2024
Merged
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
14 changes: 13 additions & 1 deletion src/Tags/Svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Svg extends Tags
{
use Concerns\RendersAttributes;

private static $shouldSanitize = true;

public function wildcard($src)
{
$this->params['src'] = $src;
Expand Down Expand Up @@ -95,7 +97,7 @@ private function setTitleAndDesc($svg)

private function sanitize($svg)
{
if ($this->params->bool('sanitize', true) === false) {
if ($this->params->bool('sanitize', static::$shouldSanitize) === false) {
return $svg;
}

Expand Down Expand Up @@ -126,4 +128,14 @@ private function setAllowedTags(DOMSanitizer $sanitizer)
$sanitizer->setAllowedTags($allowed);
$sanitizer->setDisallowedTags($disallowed);
}

public static function disableSanitization()
{
static::$shouldSanitize = false;
}

public static function enableSanitization()
{
static::$shouldSanitize = true;
}
}
27 changes: 27 additions & 0 deletions tests/Tags/SvgTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Support\Facades\File;
use Statamic\Facades\Parse;
use Statamic\Tags\Svg;
use Stringy\StaticStringy;
use Tests\TestCase;

class SvgTagTest extends TestCase
Expand Down Expand Up @@ -80,6 +82,31 @@ public function sanitizing_doesnt_remove_an_xml_tag()
$this->assertEquals($svg, $this->tag('{{ svg src="xmltag" }}'));
}

/** @test */
public function sanitization_can_be_disabled()
{
$rawSvg = StaticStringy::collapseWhitespace(<<<'SVG'
<svg>
<path onload="loadxss" onclick="clickxss" />
<script>alert("xss")</script>
<foreignObject/>
<mesh/>
</svg>
SVG);
File::put(resource_path('xss.svg'), $rawSvg);

Svg::disableSanitization();

$this->assertEquals($rawSvg, $this->tag('{{ svg src="xss" }}'));

// If it's globally disabled, you can still opt into it per-tag.
$this->assertEquals('<svg><path/></svg>', $this->tag('{{ svg src="xss" sanitize="true" }}'));

Svg::enableSanitization();

$this->assertEquals('<svg><path/></svg>', $this->tag('{{ svg src="xss" }}'));
}

/** @test */
public function fails_gracefully_when_src_is_empty()
{
Expand Down
Loading