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

[4.x] Ability to disable SVG sanitization on upload #9839

Merged
merged 4 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions config/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,17 @@

'additional_uploadable_extensions' => [],

/*
|--------------------------------------------------------------------------
| Disable SVG Sanitization
|--------------------------------------------------------------------------
|
| Statamic will automatically sanitize uploaded SVG files to avoid potential
| security issues. If you trust your users and need to disable this behaviour,
| you may do so here.
|
*/

'disable_svg_sanitization' => false,

];
2 changes: 1 addition & 1 deletion src/Assets/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function write($sourcePath, $destinationPath)
{
$stream = fopen($sourcePath, 'r');

if (Str::endsWith($destinationPath, '.svg')) {
if (! config('statamic.assets.disable_svg_sanitization', false) && Str::endsWith($destinationPath, '.svg')) {
$sanitizer = new DOMSanitizer(DOMSanitizer::SVG);
$stream = $sanitizer->sanitize($svg = stream_get_contents($stream), [
'remove-xml-tags' => ! Str::startsWith($svg, '<?xml'),
Expand Down
24 changes: 24 additions & 0 deletions tests/Assets/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,30 @@ public function it_sanitizes_svgs_on_upload()
$this->assertStringNotContainsString('</script>', $asset->contents());
}

/** @test */
public function it_does_not_sanitizes_svgs_on_upload_when_behaviour_is_disabled()
{
Event::fake();

config()->set('statamic.assets.disable_svg_sanitization', true);

$asset = (new Asset)->container($this->container)->path('path/to/asset.svg')->syncOriginal();

Facades\AssetContainer::shouldReceive('findByHandle')->with('test_container')->andReturn($this->container);
Storage::disk('test')->assertMissing('path/to/asset.svg');

$return = $asset->upload(UploadedFile::fake()->createWithContent('asset.svg', '<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" width="500" height="500"><script type="text/javascript">alert(`Bad stuff could go in here.`);</script></svg>'));

$this->assertEquals($asset, $return);
Storage::disk('test')->assertExists('path/to/asset.svg');
$this->assertEquals('path/to/asset.svg', $asset->path());

// Ensure the inline scripts were stripped out.
$this->assertStringContainsString('<script', $asset->contents());
$this->assertStringContainsString('Bad stuff could go in here.', $asset->contents());
$this->assertStringContainsString('</script>', $asset->contents());
}

public static function nonGlideableFileExtensionsProvider()
{
return [
Expand Down
Loading