Skip to content

Commit fbd9a63

Browse files
committed
Backport Security Patch
1 parent 2a1ece2 commit fbd9a63

File tree

19 files changed

+278
-107
lines changed

19 files changed

+278
-107
lines changed

.github/workflows/main.yml

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
- '8.1'
1515
- '8.2'
1616
- '8.3'
17+
- '8.4'
1718

1819
include:
1920
- php-version: 'nightly'
@@ -75,14 +76,32 @@ jobs:
7576
- name: Setup PHP, with composer and extensions
7677
uses: shivammathur/setup-php@v2
7778
with:
78-
php-version: 8.1
79+
php-version: 8.3
7980
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
8081
coverage: none
8182

8283
# This is non-ideal because it only checks for the last commit of the PR, not all of them, but better than nothing
8384
- name: Check PHPDoc types
8485
run: ./bin/check-phpdoc-types
8586

87+
find-polyfill:
88+
runs-on: ubuntu-latest
89+
steps:
90+
- name: Checkout
91+
uses: actions/checkout@v4
92+
with:
93+
fetch-depth: 2
94+
95+
- name: Setup PHP, with composer and extensions
96+
uses: shivammathur/setup-php@v2
97+
with:
98+
php-version: 8.3
99+
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
100+
coverage: none
101+
102+
- name: Find code that might require polyfill
103+
run: php ./bin/findpolyfill.php
104+
86105
php-cs-fixer:
87106
runs-on: ubuntu-latest
88107
steps:
@@ -92,7 +111,7 @@ jobs:
92111
- name: Setup PHP, with composer and extensions
93112
uses: shivammathur/setup-php@v2
94113
with:
95-
php-version: 8.1
114+
php-version: 8.3
96115
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
97116
coverage: none
98117
tools: cs2pr
@@ -123,7 +142,7 @@ jobs:
123142
- name: Setup PHP, with composer and extensions
124143
uses: shivammathur/setup-php@v2
125144
with:
126-
php-version: 8.1
145+
php-version: 8.3
127146
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
128147
coverage: none
129148
tools: cs2pr
@@ -154,7 +173,7 @@ jobs:
154173
- name: Setup PHP, with composer and extensions
155174
uses: shivammathur/setup-php@v2
156175
with:
157-
php-version: 8.1
176+
php-version: 8.3
158177
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
159178
coverage: none
160179
tools: cs2pr
@@ -185,7 +204,7 @@ jobs:
185204
- name: Setup PHP, with composer and extensions
186205
uses: shivammathur/setup-php@v2
187206
with:
188-
php-version: 8.1
207+
php-version: 8.3
189208
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
190209
coverage: none
191210
tools: cs2pr
@@ -218,7 +237,7 @@ jobs:
218237
- name: Setup PHP, with composer and extensions
219238
uses: shivammathur/setup-php@v2
220239
with:
221-
php-version: 8.1
240+
php-version: 8.3
222241
extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib
223242
coverage: pcov
224243

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ 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.2
8+
## 2024-11-10 - 2.1.2
9+
10+
### Fixed
11+
12+
- Backported security patches.
13+
- Write ignoredErrors Tag Before Drawings. Backport of [PR #4212](https://github.com/PHPOffice/PhpSpreadsheet/pull/4212) intended for 3.4.0.
14+
- Changes to ROUNDDOWN/ROUNDUP/TRUNC. Backport of [PR #4214](https://github.com/PHPOffice/PhpSpreadsheet/pull/4214) intended for 3.4.0.
915

1016
### Added
1117

12-
- Method to Test Whether Csv Will Be Affected by Php9 (backport of PR #4189 intended for 3.4.0)
18+
- Method to Test Whether Csv Will Be Affected by Php9. Backport of [PR #4189](https://github.com/PHPOffice/PhpSpreadsheet/pull/4189) intended for 3.4.0.
1319

1420
## 2024-09-29 2.1.1
1521

bin/findpolyfill.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
function findPolyfill(string $directory): int
5+
{
6+
// See issue #4215 - code which should have erred in unit test
7+
// succeeded because a dev package required polyfills.
8+
$retCode = 0;
9+
$polyfill81 = 'MYSQLI_REFRESH_REPLICA\\b'
10+
. '|fdiv[(]'
11+
. '|array_is_list[(]'
12+
. '|enum_exists[(]';
13+
$polyfill = '/\\b(?:'
14+
. $polyfill81
15+
. '})/';
16+
17+
$it = new RecursiveIteratorIterator(
18+
new RecursiveDirectoryIterator($directory, FilesystemIterator::UNIX_PATHS)
19+
);
20+
21+
foreach ($it as $file) {
22+
if ($file->getExtension() === 'php') {
23+
$fullname = $it->getPath() . '/' . $it->getBaseName();
24+
$contents = file_get_contents($fullname);
25+
if ($contents === false) {
26+
echo "failed to read $fullname\n";
27+
++$retCode;
28+
} elseif (preg_match_all($polyfill, $contents, $matches)) {
29+
var_dump($fullname, $matches);
30+
++$retCode;
31+
}
32+
}
33+
}
34+
35+
return $retCode;
36+
}
37+
38+
// Don't care if tests use polyfill
39+
$errors = findPolyfill(__DIR__ . '/../src') + findPolyfill(__DIR__ . '/../samples');
40+
if ($errors !== 0) {
41+
echo "Found $errors files that might require polyfills\n";
42+
exit(1);
43+
}
44+
echo "No polyfills needed\n";

src/PhpSpreadsheet/Calculation/MathTrig/Round.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
66
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
77
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
8+
// following added in Php8.4
9+
use RoundingMode;
810

911
class Round
1012
{
@@ -67,31 +69,28 @@ public static function up($number, $digits): array|string|float
6769
return 0.0;
6870
}
6971

70-
$digitsPlus1 = $digits + 1;
71-
if ($number < 0.0) {
72-
if ($digitsPlus1 < 0) {
73-
return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN);
74-
}
75-
$result = sprintf("%.{$digitsPlus1}F", $number - 0.5 * 0.1 ** $digits);
76-
77-
return round((float) $result, $digits, PHP_ROUND_HALF_DOWN);
72+
if (PHP_VERSION_ID >= 80400) {
73+
return round(
74+
(float) (string) $number,
75+
$digits,
76+
RoundingMode::AwayFromZero //* @phpstan-ignore-line
77+
);
7878
}
7979

80-
if ($digitsPlus1 < 0) {
81-
return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN);
80+
if ($number < 0.0) {
81+
return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN);
8282
}
83-
$result = sprintf("%.{$digitsPlus1}F", $number + 0.5 * 0.1 ** $digits);
8483

85-
return round((float) $result, $digits, PHP_ROUND_HALF_DOWN);
84+
return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN);
8685
}
8786

8887
/**
8988
* ROUNDDOWN.
9089
*
9190
* Rounds a number down to a specified number of decimal places
9291
*
93-
* @param array|float $number Number to round, or can be an array of numbers
94-
* @param array|int $digits Number of digits to which you want to round $number, or can be an array of numbers
92+
* @param null|array|float|string $number Number to round, or can be an array of numbers
93+
* @param array|float|int|string $digits Number of digits to which you want to round $number, or can be an array of numbers
9594
*
9695
* @return array|float|string Rounded Number, or a string containing an error
9796
* If an array of numbers is passed as the argument, then the returned result will also be an array
@@ -114,23 +113,19 @@ public static function down($number, $digits): array|string|float
114113
return 0.0;
115114
}
116115

117-
$digitsPlus1 = $digits + 1;
118-
if ($number < 0.0) {
119-
if ($digitsPlus1 < 0) {
120-
return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP);
121-
}
122-
$result = sprintf("%.{$digitsPlus1}F", $number + 0.5 * 0.1 ** $digits);
123-
124-
return round((float) $result, $digits, PHP_ROUND_HALF_UP);
116+
if (PHP_VERSION_ID >= 80400) {
117+
return round(
118+
(float) (string) $number,
119+
$digits,
120+
RoundingMode::TowardsZero //* @phpstan-ignore-line
121+
);
125122
}
126123

127-
if ($digitsPlus1 < 0) {
128-
return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP);
124+
if ($number < 0.0) {
125+
return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP);
129126
}
130127

131-
$result = sprintf("%.{$digitsPlus1}F", $number - 0.5 * 0.1 ** $digits);
132-
133-
return round((float) $result, $digits, PHP_ROUND_HALF_UP);
128+
return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP);
134129
}
135130

136131
/**

src/PhpSpreadsheet/Calculation/MathTrig/Trunc.php

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
44

55
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6-
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
76

87
class Trunc
98
{
@@ -19,47 +18,19 @@ class Trunc
1918
* (or possibly that value minus 1).
2019
* Excel is unlikely to do any better.
2120
*
22-
* @param array|float $value Or can be an array of values
23-
* @param array|int $digits Or can be an array of values
21+
* @param null|array|float|string $value Or can be an array of values
22+
* @param array|float|int|string $digits Or can be an array of values
2423
*
2524
* @return array|float|string Truncated value, or a string containing an error
2625
* If an array of numbers is passed as an argument, then the returned result will also be an array
2726
* with the same dimensions
2827
*/
29-
public static function evaluate(array|float|string|null $value = 0, array|int|string $digits = 0): array|float|string
28+
public static function evaluate(array|float|string|null $value = 0, array|float|int|string $digits = 0): array|float|string
3029
{
3130
if (is_array($value) || is_array($digits)) {
3231
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $digits);
3332
}
3433

35-
try {
36-
$value = Helpers::validateNumericNullBool($value);
37-
$digits = Helpers::validateNumericNullSubstitution($digits, null);
38-
} catch (Exception $e) {
39-
return $e->getMessage();
40-
}
41-
42-
if ($value == 0) {
43-
return $value;
44-
}
45-
46-
if ($value >= 0) {
47-
$minusSign = '';
48-
} else {
49-
$minusSign = '-';
50-
$value = -$value;
51-
}
52-
$digits = (int) floor($digits);
53-
if ($digits < 0) {
54-
$result = (float) (substr(sprintf('%.0F', $value), 0, $digits) . str_repeat('0', -$digits));
55-
56-
return ($minusSign === '') ? $result : -$result;
57-
}
58-
$decimals = (floor($value) == (int) $value) ? (PHP_FLOAT_DIG - strlen((string) (int) $value)) : $digits;
59-
$resultString = ($decimals < 0) ? sprintf('%F', $value) : sprintf('%.' . $decimals . 'F', $value);
60-
$regExp = '/([.]\\d{' . $digits . '})\\d+$/';
61-
$result = $minusSign . (preg_replace($regExp, '$1', $resultString) ?? $resultString);
62-
63-
return (float) $result;
34+
return Round::down($value, $digits);
6435
}
6536
}

src/PhpSpreadsheet/Reader/Security/XmlScanner.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class XmlScanner
88
{
9+
private const ENCODING_PATTERN = '/encoding\\s*=\\s*(["\'])(.+?)\\1/s';
10+
private const ENCODING_UTF7 = '/encoding\\s*=\\s*(["\'])UTF-7\\1/si';
11+
912
private string $pattern;
1013

1114
/** @var ?callable */
@@ -36,29 +39,41 @@ private static function forceString(mixed $arg): string
3639
private function toUtf8(string $xml): string
3740
{
3841
$charset = $this->findCharSet($xml);
42+
$foundUtf7 = $charset === 'UTF-7';
3943
if ($charset !== 'UTF-8') {
44+
$testStart = '/^.{0,4}\\s*<?xml/s';
45+
$startWithXml1 = preg_match($testStart, $xml);
4046
$xml = self::forceString(mb_convert_encoding($xml, 'UTF-8', $charset));
41-
42-
$charset = $this->findCharSet($xml);
43-
if ($charset !== 'UTF-8') {
44-
throw new Reader\Exception('Suspicious Double-encoded XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
47+
if ($startWithXml1 === 1 && preg_match($testStart, $xml) !== 1) {
48+
throw new Reader\Exception('Double encoding not permitted');
4549
}
50+
$foundUtf7 = $foundUtf7 || (preg_match(self::ENCODING_UTF7, $xml) === 1);
51+
$xml = preg_replace(self::ENCODING_PATTERN, '', $xml) ?? $xml;
52+
} else {
53+
$foundUtf7 = $foundUtf7 || (preg_match(self::ENCODING_UTF7, $xml) === 1);
54+
}
55+
if ($foundUtf7) {
56+
throw new Reader\Exception('UTF-7 encoding not permitted');
57+
}
58+
if (substr($xml, 0, Reader\Csv::UTF8_BOM_LEN) === Reader\Csv::UTF8_BOM) {
59+
$xml = substr($xml, Reader\Csv::UTF8_BOM_LEN);
4660
}
4761

4862
return $xml;
4963
}
5064

5165
private function findCharSet(string $xml): string
5266
{
53-
$patterns = [
54-
'/encoding\\s*=\\s*"([^"]*]?)"/',
55-
"/encoding\\s*=\\s*'([^']*?)'/",
56-
];
57-
58-
foreach ($patterns as $pattern) {
59-
if (preg_match($pattern, $xml, $matches)) {
60-
return strtoupper($matches[1]);
61-
}
67+
if (substr($xml, 0, 4) === "\x4c\x6f\xa7\x94") {
68+
throw new Reader\Exception('EBCDIC encoding not permitted');
69+
}
70+
$encoding = Reader\Csv::guessEncodingBom('', $xml);
71+
if ($encoding !== '') {
72+
return $encoding;
73+
}
74+
$xml = str_replace("\0", '', $xml);
75+
if (preg_match(self::ENCODING_PATTERN, $xml, $matches)) {
76+
return strtoupper($matches[2]);
6277
}
6378

6479
return 'UTF-8';
@@ -71,13 +86,16 @@ private function findCharSet(string $xml): string
7186
*/
7287
public function scan($xml): string
7388
{
89+
// Don't rely purely on libxml_disable_entity_loader()
90+
$pattern = '/\\0*' . implode('\\0*', str_split($this->pattern)) . '\\0*/';
91+
7492
$xml = "$xml";
93+
if (preg_match($pattern, $xml)) {
94+
throw new Reader\Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
95+
}
7596

7697
$xml = $this->toUtf8($xml);
7798

78-
// Don't rely purely on libxml_disable_entity_loader()
79-
$pattern = '/\\0?' . implode('\\0?', str_split($this->pattern)) . '\\0?/';
80-
8199
if (preg_match($pattern, $xml)) {
82100
throw new Reader\Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
83101
}
@@ -90,7 +108,7 @@ public function scan($xml): string
90108
}
91109

92110
/**
93-
* Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks.
111+
* Scan the XML for use of <!ENTITY to prevent XXE/XEE attacks.
94112
*/
95113
public function scanFile(string $filestream): string
96114
{

0 commit comments

Comments
 (0)