Skip to content

Commit

Permalink
Do not allow non-image data as logo
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Dec 22, 2019
1 parent 9a37888 commit 92dd148
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Endroid\QrCode\Writer;

use Endroid\QrCode\Exception\GenerateImageException;
use Endroid\QrCode\Exception\InvalidLogoException;
use Endroid\QrCode\Exception\MissingExtensionException;
use Endroid\QrCode\QrCodeInterface;
Expand All @@ -29,6 +30,10 @@ protected function getMimeType(string $path): string
throw new InvalidLogoException('Could not determine mime type');
}

if (!preg_match('#^image/#', $mimeType)) {
throw new GenerateImageException('Logo path is not an image');
}

// Passing mime type image/svg results in invisible images
if ('image/svg' === $mimeType) {
return 'image/svg+xml';
Expand Down
12 changes: 12 additions & 0 deletions tests/QrCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Endroid\QrCode\Tests;

use Endroid\QrCode\Exception\GenerateImageException;
use Endroid\QrCode\Factory\QrCodeFactory;
use Endroid\QrCode\QrCode;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -184,4 +185,15 @@ public function testData(): void
$this->assertArrayHasKey('margin_left', $data);
$this->assertArrayHasKey('margin_right', $data);
}

public function testNonImageData(): void
{
$qrCode = new QrCode('QR Code');
$qrCode->setLogoPath(__DIR__.'/QrCodeTest.php');
$qrCode->setLogoSize(200, 200);
$qrCode->setWriterByExtension('svg');

$this->expectException(GenerateImageException::class);
$qrCode->writeString();
}
}

0 comments on commit 92dd148

Please sign in to comment.