ghsa-4qrp-27r3-66fj
Vulnerability from github
Published
2022-03-14 22:38
Modified
2022-03-18 15:22
Summary
Improper sanitize of SVG files during content upload ('Cross-site Scripting') in sylius/sylius
Details

Impact

There is a possibility to upload an SVG file containing XSS code in the admin panel. In order to perform an XSS attack, the file itself has to be opened in a new card (or loaded outside of the IMG tag). The problem applies both to the files opened on the admin panel and shop pages.

Patches

The issue is fixed in versions: 1.9.10, 1.10.11, 1.11.2, and above.

Workarounds

If there is a need to upload an SVG image type, on-upload sanitization has to be added. The way to achieve this is to require a library that will do the trick:

composer require enshrined/svg-sanitize

The second step is all about performing a file content sanitization before writing it to the filesystem. It can be done by overwriting the service:

```php <?php

declare(strict_types=1);

namespace App\Uploader;

use enshrined\svgSanitize\Sanitizer; use Gaufrette\Filesystem; use Sylius\Component\Core\Generator\ImagePathGeneratorInterface; use Sylius\Component\Core\Generator\UploadedImagePathGenerator; use Sylius\Component\Core\Model\ImageInterface; use Sylius\Component\Core\Uploader\ImageUploaderInterface; use Symfony\Component\HttpFoundation\File\File; use Webmozart\Assert\Assert;

final class ImageUploader implements ImageUploaderInterface { private const MIME_SVG_XML = 'image/svg+xml'; private const MIME_SVG = 'image/svg';

/** @var Filesystem */
protected $filesystem;

/** @var ImagePathGeneratorInterface */
protected $imagePathGenerator;

/** @var Sanitizer */
protected $sanitizer;

public function __construct(
    Filesystem $filesystem,
    ?ImagePathGeneratorInterface $imagePathGenerator = null
) {
    $this->filesystem = $filesystem;

    if ($imagePathGenerator === null) {
        @trigger_error(sprintf(
            'Not passing an $imagePathGenerator to %s constructor is deprecated since Sylius 1.6 and will be not possible in Sylius 2.0.', self::class
        ), \E_USER_DEPRECATED);
    }

    $this->imagePathGenerator = $imagePathGenerator ?? new UploadedImagePathGenerator();
    $this->sanitizer = new Sanitizer();
}

public function upload(ImageInterface $image): void
{
    if (!$image->hasFile()) {
        return;
    }

    /** @var File $file */
    $file = $image->getFile();

    Assert::isInstanceOf($file, File::class);

    $fileContent = $this->sanitizeContent(file_get_contents($file->getPathname()), $file->getMimeType());

    if (null !== $image->getPath() && $this->has($image->getPath())) {
        $this->remove($image->getPath());
    }

    do {
        $path = $this->imagePathGenerator->generate($image);
    } while ($this->isAdBlockingProne($path) || $this->filesystem->has($path));

    $image->setPath($path);

    $this->filesystem->write($image->getPath(), $fileContent);
}

public function remove(string $path): bool
{
    if ($this->filesystem->has($path)) {
        return $this->filesystem->delete($path);
    }

    return false;
}

protected function sanitizeContent(string $fileContent, string $mimeType): string
{
    if (self::MIME_SVG_XML === $mimeType || self::MIME_SVG === $mimeType) {
        $fileContent = $this->sanitizer->sanitize($fileContent);
    }

    return $fileContent;
}

private function has(string $path): bool
{
    return $this->filesystem->has($path);
}

/**
 * Will return true if the path is prone to be blocked by ad blockers
 */
private function isAdBlockingProne(string $path): bool
{
    return strpos($path, 'ad') !== false;
}

} ```

After that, register service in the container:

yaml services: sylius.image_uploader: class: App\Uploader\ImageUploader arguments: - '@gaufrette.sylius_image_filesystem' - '@Sylius\Component\Core\Generator\ImagePathGeneratorInterface'

For more information

If you have any questions or comments about this advisory: * Open an issue in Sylius issues * Email us at security@sylius.com

Show details on source website


{
  "affected": [
    {
      "package": {
        "ecosystem": "Packagist",
        "name": "sylius/sylius"
      },
      "ranges": [
        {
          "events": [
            {
              "introduced": "0"
            },
            {
              "fixed": "1.9.10"
            }
          ],
          "type": "ECOSYSTEM"
        }
      ]
    },
    {
      "package": {
        "ecosystem": "Packagist",
        "name": "sylius/sylius"
      },
      "ranges": [
        {
          "events": [
            {
              "introduced": "1.10.0"
            },
            {
              "fixed": "1.10.11"
            }
          ],
          "type": "ECOSYSTEM"
        }
      ]
    },
    {
      "package": {
        "ecosystem": "Packagist",
        "name": "sylius/sylius"
      },
      "ranges": [
        {
          "events": [
            {
              "introduced": "1.11.0"
            },
            {
              "fixed": "1.11.2"
            }
          ],
          "type": "ECOSYSTEM"
        }
      ]
    }
  ],
  "aliases": [
    "CVE-2022-24749"
  ],
  "database_specific": {
    "cwe_ids": [
      "CWE-434",
      "CWE-79",
      "CWE-80"
    ],
    "github_reviewed": true,
    "github_reviewed_at": "2022-03-14T22:38:14Z",
    "nvd_published_at": "2022-03-14T22:15:00Z",
    "severity": "MODERATE"
  },
  "details": "### Impact\nThere is a possibility to upload an SVG file containing XSS code in the admin panel. In order to perform an XSS attack, the file itself has to be opened in a new card (or loaded outside of the IMG tag). The problem applies both to the files opened on the admin panel and shop pages.\n\n### Patches\nThe issue is fixed in versions: 1.9.10, 1.10.11, 1.11.2, and above.\n\n### Workarounds\nIf there is a need to upload an SVG image type, on-upload sanitization has to be added. The way to achieve this is to require a library that will do the trick:\n\n```\ncomposer require enshrined/svg-sanitize\n```\n\nThe second step is all about performing a file content sanitization before writing it to the filesystem. It can be done by overwriting the service:\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Uploader;\n\nuse enshrined\\svgSanitize\\Sanitizer;\nuse Gaufrette\\Filesystem;\nuse Sylius\\Component\\Core\\Generator\\ImagePathGeneratorInterface;\nuse Sylius\\Component\\Core\\Generator\\UploadedImagePathGenerator;\nuse Sylius\\Component\\Core\\Model\\ImageInterface;\nuse Sylius\\Component\\Core\\Uploader\\ImageUploaderInterface;\nuse Symfony\\Component\\HttpFoundation\\File\\File;\nuse Webmozart\\Assert\\Assert;\n\nfinal class ImageUploader implements ImageUploaderInterface\n{\n    private const MIME_SVG_XML = \u0027image/svg+xml\u0027;\n    private const MIME_SVG = \u0027image/svg\u0027;\n\n    /** @var Filesystem */\n    protected $filesystem;\n\n    /** @var ImagePathGeneratorInterface */\n    protected $imagePathGenerator;\n\n    /** @var Sanitizer */\n    protected $sanitizer;\n\n    public function __construct(\n        Filesystem $filesystem,\n        ?ImagePathGeneratorInterface $imagePathGenerator = null\n    ) {\n        $this-\u003efilesystem = $filesystem;\n\n        if ($imagePathGenerator === null) {\n            @trigger_error(sprintf(\n                \u0027Not passing an $imagePathGenerator to %s constructor is deprecated since Sylius 1.6 and will be not possible in Sylius 2.0.\u0027, self::class\n            ), \\E_USER_DEPRECATED);\n        }\n\n        $this-\u003eimagePathGenerator = $imagePathGenerator ?? new UploadedImagePathGenerator();\n        $this-\u003esanitizer = new Sanitizer();\n    }\n\n    public function upload(ImageInterface $image): void\n    {\n        if (!$image-\u003ehasFile()) {\n            return;\n        }\n\n        /** @var File $file */\n        $file = $image-\u003egetFile();\n\n        Assert::isInstanceOf($file, File::class);\n\n        $fileContent = $this-\u003esanitizeContent(file_get_contents($file-\u003egetPathname()), $file-\u003egetMimeType());\n\n        if (null !== $image-\u003egetPath() \u0026\u0026 $this-\u003ehas($image-\u003egetPath())) {\n            $this-\u003eremove($image-\u003egetPath());\n        }\n\n        do {\n            $path = $this-\u003eimagePathGenerator-\u003egenerate($image);\n        } while ($this-\u003eisAdBlockingProne($path) || $this-\u003efilesystem-\u003ehas($path));\n\n        $image-\u003esetPath($path);\n\n        $this-\u003efilesystem-\u003ewrite($image-\u003egetPath(), $fileContent);\n    }\n\n    public function remove(string $path): bool\n    {\n        if ($this-\u003efilesystem-\u003ehas($path)) {\n            return $this-\u003efilesystem-\u003edelete($path);\n        }\n\n        return false;\n    }\n\n    protected function sanitizeContent(string $fileContent, string $mimeType): string\n    {\n        if (self::MIME_SVG_XML === $mimeType || self::MIME_SVG === $mimeType) {\n            $fileContent = $this-\u003esanitizer-\u003esanitize($fileContent);\n        }\n\n        return $fileContent;\n    }\n\n    private function has(string $path): bool\n    {\n        return $this-\u003efilesystem-\u003ehas($path);\n    }\n\n    /**\n     * Will return true if the path is prone to be blocked by ad blockers\n     */\n    private function isAdBlockingProne(string $path): bool\n    {\n        return strpos($path, \u0027ad\u0027) !== false;\n    }\n}\n```\n\nAfter that, register service in the container:\n\n```yaml\nservices:\n    sylius.image_uploader:\n        class: App\\Uploader\\ImageUploader\n        arguments:\n            - \u0027@gaufrette.sylius_image_filesystem\u0027\n            - \u0027@Sylius\\Component\\Core\\Generator\\ImagePathGeneratorInterface\u0027\n```\n\n### For more information\nIf you have any questions or comments about this advisory:\n* Open an issue in [Sylius issues](https://github.com/Sylius/Sylius/issues)\n* Email us at security@sylius.com\n",
  "id": "GHSA-4qrp-27r3-66fj",
  "modified": "2022-03-18T15:22:18Z",
  "published": "2022-03-14T22:38:14Z",
  "references": [
    {
      "type": "WEB",
      "url": "https://github.com/Sylius/Sylius/security/advisories/GHSA-4qrp-27r3-66fj"
    },
    {
      "type": "ADVISORY",
      "url": "https://nvd.nist.gov/vuln/detail/CVE-2022-24749"
    },
    {
      "type": "PACKAGE",
      "url": "https://github.com/Sylius/Sylius"
    },
    {
      "type": "WEB",
      "url": "https://github.com/Sylius/Sylius/releases/tag/v1.10.11"
    },
    {
      "type": "WEB",
      "url": "https://github.com/Sylius/Sylius/releases/tag/v1.11.2"
    },
    {
      "type": "WEB",
      "url": "https://github.com/Sylius/Sylius/releases/tag/v1.9.10"
    }
  ],
  "schema_version": "1.4.0",
  "severity": [
    {
      "score": "CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N",
      "type": "CVSS_V3"
    }
  ],
  "summary": "Improper sanitize of SVG files during content upload (\u0027Cross-site Scripting\u0027) in sylius/sylius"
}


Log in or create an account to share your comment.




Tags
Taxonomy of the tags.


Loading…

Loading…

Loading…

Sightings

Author Source Type Date

Nomenclature

  • Seen: The vulnerability was mentioned, discussed, or seen somewhere by the user.
  • Confirmed: The vulnerability is confirmed from an analyst perspective.
  • Exploited: This vulnerability was exploited and seen by the user reporting the sighting.
  • Patched: This vulnerability was successfully patched by the user reporting the sighting.
  • Not exploited: This vulnerability was not exploited or seen by the user reporting the sighting.
  • Not confirmed: The user expresses doubt about the veracity of the vulnerability.
  • Not patched: This vulnerability was not successfully patched by the user reporting the sighting.


Loading…