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

SVG Sanitizer Enhanced - SA3 #4764

Merged
merged 1 commit into from
Mar 25, 2021
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
SVG Sanitizer Enhanced
  • Loading branch information
devansh-webkul committed Mar 25, 2021
commit 7bbf0c4bb565fc2601f031f9bbcdfa06e24dbd45
35 changes: 28 additions & 7 deletions packages/Webkul/Core/src/Traits/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,42 @@

trait Sanitizer
{
/**
* List of mime types which needs to check.
*/
public $mimeTypes = [
'image/svg',
'image/svg+xml'
];

/**
* Sanitize SVG file.
*
* @param string $path
* @return void
*/
public function sanitizeSVG($path)
public function sanitizeSVG($path, $mimeType)
{
/* sanitizer instance */
$sanitizer = new MainSanitizer();
if ($this->checkMimeType($mimeType)) {
/* sanitizer instance */
$sanitizer = new MainSanitizer();

/* grab svg file */
$dirtySVG = Storage::get($path);
/* grab svg file */
$dirtySVG = Storage::get($path);

/* save sanitized svg */
Storage::put($path, $sanitizer->sanitize($dirtySVG));
/* save sanitized svg */
Storage::put($path, $sanitizer->sanitize($dirtySVG));
}
}

/**
* Sanitize SVG file.
*
* @param string $path
* @return void
*/
public function checkMimeType($mimeType)
{
return in_array($mimeType, $this->mimeTypes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public function uploadSearchImage($data)
{
$path = request()->file('image')->store('product-search');

if ($data['image']->getMimeType() === 'image/svg') {
$this->sanitizeSVG($path);
}
$this->sanitizeSVG($path, $data['image']->getMimeType());

return Storage::url($path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ public function uploadAdvertisementImages($data, $index, $advertisement)
$saveImage[substr($imageId, 6, 1)] = $path = request()->file($file)->store($dir);
}

if ($image->getMimeType() === 'image/svg') {
$this->sanitizeSVG($path);
}
$this->sanitizeSVG($path, $image->getMimeType());
}
} else {
if (isset($advertisement[$index][$imageId]) && $advertisement[$index][$imageId] && !request()->hasFile($file)) {
Expand Down