Skip to content

Commit 04e8b6e

Browse files
committed
Backport Html Writer Security Patches
1 parent 9b9a55c commit 04e8b6e

File tree

8 files changed

+76
-5
lines changed

8 files changed

+76
-5
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com)
66
and this project adheres to [Semantic Versioning](https://semver.org).
77

8-
# TBD - 2.1.6
8+
# 2024-12-26 - 2.1.6
99

1010
### Deprecated
1111

@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1515

1616
- More context options may be needed for http(s) image. Backport of [PR #4276](https://github.com/PHPOffice/PhpSpreadsheet/pull/4276)
1717
- Backported security patches for Samples.
18+
- Backported security patches for Html Writer.
1819

1920
## 2024-12-08 - 2.1.5
2021

src/PhpSpreadsheet/Writer/Html.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,12 @@ public function generateHTMLHeader(bool $includeStyles = false): string
386386
} else {
387387
$propertyValue = (string) $propertyValue;
388388
}
389-
$html .= self::generateMeta($propertyValue, "custom.$propertyQualifier.$customProperty");
389+
$html .= self::generateMeta($propertyValue, htmlspecialchars("custom.$propertyQualifier.$customProperty"));
390390
}
391391
}
392392

393393
if (!empty($properties->getHyperlinkBase())) {
394-
$html .= ' <base href="' . $properties->getHyperlinkBase() . '" />' . PHP_EOL;
394+
$html .= ' <base href="' . htmlspecialchars($properties->getHyperlinkBase()) . '" />' . PHP_EOL;
395395
}
396396

397397
$html .= $includeStyles ? $this->generateStyles(true) : $this->generatePageDeclarations(true);
@@ -1494,8 +1494,9 @@ private function generateRow(Worksheet $worksheet, array $values, int $row, stri
14941494
// Hyperlink?
14951495
if ($worksheet->hyperlinkExists($coordinate) && !$worksheet->getHyperlink($coordinate)->isInternal()) {
14961496
$url = $worksheet->getHyperlink($coordinate)->getUrl();
1497-
$urldecode = strtolower(html_entity_decode(trim($url), encoding: 'UTF-8'));
1498-
$parseScheme = preg_match('/^(\\w+):/', $urldecode, $matches);
1497+
$urlDecode1 = html_entity_decode($url, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
1498+
$urlTrim = preg_replace('/^\\s+/u', '', $urlDecode1) ?? $urlDecode1;
1499+
$parseScheme = preg_match('/^([\\w\\s]+):/u', strtolower($urlTrim), $matches);
14991500
if ($parseScheme === 1 && !in_array($matches[1], ['http', 'https', 'file', 'ftp', 's3'], true)) {
15001501
$cellData = htmlspecialchars($url, Settings::htmlEntityFlags());
15011502
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
8+
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class BadCustomPropertyTest extends TestCase
12+
{
13+
public function testBadCustomProperty(): void
14+
{
15+
$reader = new XlsxReader();
16+
$infile = 'tests/data/Reader/XLSX/sec-q229.dontuse';
17+
$spreadsheet = $reader->load($infile);
18+
$writer = new HtmlWriter($spreadsheet);
19+
$html = $writer->generateHtmlAll();
20+
self::assertStringContainsString('<meta name="custom.string.custom_property&quot;&gt;&lt;img src=1 onerror=alert()&gt;" content="test" />', $html);
21+
$spreadsheet->disconnectWorksheets();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
8+
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class BadHyperlinkBaseTest extends TestCase
12+
{
13+
public function testBadHyperlinkBase(): void
14+
{
15+
$reader = new XlsxReader();
16+
$infile = 'tests/data/Reader/XLSX/sec-p66w.dontuse';
17+
$spreadsheet = $reader->load($infile);
18+
$writer = new HtmlWriter($spreadsheet);
19+
$html = $writer->generateHtmlAll();
20+
self::assertStringContainsString('<base href="&quot;&gt;&lt;img src=1 onerror=alert()&gt;" />', $html);
21+
$spreadsheet->disconnectWorksheets();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
8+
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class BadHyperlinkTest extends TestCase
12+
{
13+
public function testBadHyperlink(): void
14+
{
15+
$reader = new XlsxReader();
16+
$infile = 'tests/data/Reader/XLSX/sec-j47r.dontuse';
17+
$spreadsheet = $reader->load($infile);
18+
$writer = new HtmlWriter($spreadsheet);
19+
$html = $writer->generateHtmlAll();
20+
self::assertStringContainsString("<td class=\"column0 style1 f\">jav\tascript:alert()</td>", $html);
21+
$spreadsheet->disconnectWorksheets();
22+
}
23+
}
8.68 KB
Binary file not shown.
8.11 KB
Binary file not shown.
8.73 KB
Binary file not shown.

0 commit comments

Comments
 (0)