Skip to content

Commit bd65275

Browse files
committed
Backport security fix from PHPOffice#4119 to v1
1 parent 15a42d0 commit bd65275

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2525

2626
### Fixed
2727

28-
- Nothing
28+
- Backported security fix from PHPOffice/PhpSpreadsheet#4119 to v1
2929

3030

3131
## 1.28.0 - 2023-02-25

src/PhpSpreadsheet/Reader/Security/XmlScanner.php

+18-5
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,12 @@ private static function forceString($arg): string
113113
*/
114114
private function toUtf8($xml)
115115
{
116-
$pattern = '/encoding="(.*?)"/';
117-
$result = preg_match($pattern, $xml, $matches);
118-
$charset = strtoupper($result ? $matches[1] : 'UTF-8');
116+
$charset = $this->findCharSet($xml);
119117

120118
if ($charset !== 'UTF-8') {
121119
$xml = self::forceString(mb_convert_encoding($xml, 'UTF-8', $charset));
122120

123-
$result = preg_match($pattern, $xml, $matches);
124-
$charset = strtoupper($result ? $matches[1] : 'UTF-8');
121+
$charset = $this->findCharSet($xml);
125122
if ($charset !== 'UTF-8') {
126123
throw new Reader\Exception('Suspicious Double-encoded XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
127124
}
@@ -169,4 +166,20 @@ public function scanFile($filestream)
169166
{
170167
return $this->scan(file_get_contents($filestream));
171168
}
169+
170+
private function findCharSet(string $xml): string
171+
{
172+
$patterns = [
173+
'/encoding="([^"]*]?)"/',
174+
"/encoding='([^']*?)'/",
175+
];
176+
177+
foreach ($patterns as $pattern) {
178+
if (preg_match($pattern, $xml, $matches)) {
179+
return strtoupper($matches[1]);
180+
}
181+
}
182+
183+
return 'UTF-8';
184+
}
172185
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding='UTF-7' standalone="yes"?>
2+
+ADw-+ACE-DOCTYPE+ACA-foo+ACA-+AFs-+ADw-+ACE-ENTITY+ACA-toreplace+ACA-+ACI-xxe+AF8-test+ACI-+AD4-+ACA-+AF0-+AD4-+AAo-+ADw-sst+ACA-xmlns+AD0-+ACI-http://schemas.openxmlformats.org/spreadsheetml/2006/main+ACI-+ACA-count+AD0-+ACI-2+ACI-+ACA-uniqueCount+AD0-+ACI-1+ACI-+AD4-+ADw-si+AD4-+ADw-t+AD4-+ACY-toreplace+ADs-+ADw-/t+AD4-+ADw-/si+AD4-+ADw-/sst+AD4-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
2+
<root>
3+
test: Valid
4+
</root>

0 commit comments

Comments
 (0)