Skip to content

Commit e312656

Browse files
committed
Restrict Internet Protocols for Linked Images
Do not allow use of Php propietary protocols to retrieve linked images. Restrict to http, https, ftp, file, and s3.
1 parent a917176 commit e312656

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

src/PhpSpreadsheet/Worksheet/Drawing.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public function setPath(string $path, bool $verifyFile = true, ?ZipArchive $zip
9797
if ($verifyFile && preg_match('~^data:image/[a-z]+;base64,~', $path) !== 1) {
9898
// Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979
9999
if (filter_var($path, FILTER_VALIDATE_URL)) {
100+
if (!preg_match('/^(http|https|file|ftp|s3):/', $path)) {
101+
throw new PhpSpreadsheetException('Invalid protocol for linked drawing');
102+
}
100103
$this->path = $path;
101104
// Implicit that it is a URL, rather store info than running check above on value in other places.
102105
$this->isUrl = true;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Html;
6+
7+
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
8+
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class HtmlImage2Test extends TestCase
12+
{
13+
public function testCanInsertImageGoodProtocol(): void
14+
{
15+
if (getenv('SKIP_URL_IMAGE_TEST') === '1') {
16+
self::markTestSkipped('Skipped due to setting of environment variable');
17+
}
18+
$imagePath = 'https://phpspreadsheet.readthedocs.io/en/latest/topics/images/01-03-filter-icon-1.png';
19+
$html = '<table>
20+
<tr>
21+
<td><img src="' . $imagePath . '" alt="test image voilà"></td>
22+
</tr>
23+
</table>';
24+
$filename = HtmlHelper::createHtml($html);
25+
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true);
26+
$firstSheet = $spreadsheet->getSheet(0);
27+
28+
/** @var Drawing $drawing */
29+
$drawing = $firstSheet->getDrawingCollection()[0];
30+
self::assertEquals($imagePath, $drawing->getPath());
31+
self::assertEquals('A1', $drawing->getCoordinates());
32+
}
33+
34+
public function testCannotInsertImageBadProtocol(): void
35+
{
36+
$this->expectException(SpreadsheetException::class);
37+
$this->expectExceptionMessage('Invalid protocol for linked drawing');
38+
$imagePath = 'httpx://phpspreadsheet.readthedocs.io/en/latest/topics/images/01-03-filter-icon-1.png';
39+
$html = '<table>
40+
<tr>
41+
<td><img src="' . $imagePath . '" alt="test image voilà"></td>
42+
</tr>
43+
</table>';
44+
$filename = HtmlHelper::createHtml($html);
45+
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true);
46+
}
47+
}

tests/PhpSpreadsheetTests/Reader/Xlsx/URLImageTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
66

7+
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
78
use PhpOffice\PhpSpreadsheet\IOFactory;
89
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
910
use PhpOffice\PhpSpreadsheetTests\Reader\Utility\File;
@@ -41,4 +42,14 @@ public function testURLImageSource(): void
4142
self::assertSame('png', $extension);
4243
}
4344
}
45+
46+
public function testURLImageSourceBadProtocol(): void
47+
{
48+
$filename = realpath(__DIR__ . '/../../../data/Reader/XLSX/urlImage.bad.xlsx');
49+
self::assertNotFalse($filename);
50+
$this->expectException(SpreadsheetException::class);
51+
$this->expectExceptionMessage('Invalid protocol for linked drawing');
52+
$reader = IOFactory::createReader('Xlsx');
53+
$reader->load($filename);
54+
}
4455
}
9.88 KB
Binary file not shown.

0 commit comments

Comments
 (0)