-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean and validate uploaded SVG files (#318)
* Clean/validate svg uploads * Apply fixes from StyleCI * extend the singletons Co-authored-by: StyleCI Bot <bot@styleci.io>
- Loading branch information
1 parent
837e86a
commit d1f0e4b
Showing
7 changed files
with
208 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/upload. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* Copyright (c) Flagrow. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FoF\Upload\Extend; | ||
|
||
use Flarum\Extend\ExtenderInterface; | ||
use Flarum\Extension\Extension; | ||
use Illuminate\Contracts\Container\Container; | ||
|
||
class SvgSanitizer implements ExtenderInterface | ||
{ | ||
protected $allowedAttrs = []; | ||
|
||
protected $allowedTags = []; | ||
|
||
protected $removeAttrs = []; | ||
|
||
protected $removeTags = []; | ||
|
||
public function allowAttr(string $attr): self | ||
{ | ||
$this->allowedAttrs[] = $attr; | ||
|
||
return $this; | ||
} | ||
|
||
public function allowTag(string $tag): self | ||
{ | ||
$this->allowedTags[] = $tag; | ||
|
||
return $this; | ||
} | ||
|
||
public function removeAttr($attr): self | ||
{ | ||
$this->removeAttrs[] = $attr; | ||
|
||
return $this; | ||
} | ||
|
||
public function removeTag($tag): self | ||
{ | ||
$this->removeTags[] = $tag; | ||
|
||
return $this; | ||
} | ||
|
||
public function extend(Container $container, Extension $extension = null) | ||
{ | ||
$container->extend('fof.upload.sanitizer.svg_allowed_attrs', function ($items): array { | ||
return array_merge($items, $this->allowedAttrs); | ||
}); | ||
|
||
$container->extend('fof.upload.sanitizer.svg_disallowed_attrs', function ($items): array { | ||
return array_merge($items, $this->removeAttrs); | ||
}); | ||
|
||
$container->extend('fof.upload.sanitizer.svg_allowed_tags', function ($items): array { | ||
return array_merge($items, $this->allowedTags); | ||
}); | ||
|
||
$container->extend('fof.upload.sanitizer.svg_disallowed_tags', function ($items): array { | ||
return array_merge($items, $this->removeTags); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/upload. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* Copyright (c) Flagrow. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FoF\Upload\Providers; | ||
|
||
use enshrined\svgSanitize\Sanitizer; | ||
use Flarum\Foundation\AbstractServiceProvider; | ||
use FoF\Upload\Sanitizer\SvgAllowedAttrs; | ||
use FoF\Upload\Sanitizer\SvgAllowedTags; | ||
|
||
class SanitizerProvider extends AbstractServiceProvider | ||
{ | ||
public function register() | ||
{ | ||
$this->container->singleton(Sanitizer::class, function (): Sanitizer { | ||
$sanitizer = new Sanitizer(); | ||
$sanitizer->setAllowedAttrs(new SvgAllowedAttrs()); | ||
$sanitizer->setAllowedTags(new SvgAllowedTags()); | ||
$sanitizer->removeRemoteReferences(true); | ||
|
||
return $sanitizer; | ||
}); | ||
|
||
$this->container->singleton('fof.upload.sanitizer.svg_allowed_attrs', function (): array { | ||
return []; | ||
}); | ||
|
||
$this->container->singleton('fof.upload.sanitizer.svg_disallowed_attrs', function (): array { | ||
return []; | ||
}); | ||
|
||
$this->container->singleton('fof.upload.sanitizer.svg_allowed_tags', function (): array { | ||
return []; | ||
}); | ||
|
||
$this->container->singleton('fof.upload.sanitizer.svg_disallowed_tags', function (): array { | ||
return []; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/upload. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* Copyright (c) Flagrow. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FoF\Upload\Sanitizer; | ||
|
||
use enshrined\svgSanitize\data\AllowedAttributes; | ||
|
||
class SvgAllowedAttrs extends AllowedAttributes | ||
{ | ||
public static function getAttributes(): array | ||
{ | ||
return array_diff( | ||
array_merge(parent::getAttributes(), resolve('fof.upload.sanitizer.svg_allowed_attrs')), | ||
resolve('fof.upload.sanitizer.svg_disallowed_attrs') | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/upload. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* Copyright (c) Flagrow. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FoF\Upload\Sanitizer; | ||
|
||
use enshrined\svgSanitize\data\AllowedTags; | ||
|
||
class SvgAllowedTags extends AllowedTags | ||
{ | ||
public static function getTags(): array | ||
{ | ||
return array_diff( | ||
array_merge(parent::getTags(), resolve('fof.upload.sanitizer.svg_allowed_tags')), | ||
resolve('fof.upload.sanitizer.svg_disallowed_tags') | ||
); | ||
} | ||
} |