Skip to content

Commit

Permalink
Backport Security Patches for Samples
Browse files Browse the repository at this point in the history
  • Loading branch information
oleibman committed Dec 26, 2024
1 parent a78ceaa commit a50ebfe
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,4 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
with:
bodyFile: release-body.txt
makeLatest: false
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

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

## 1.29.6 - 2024-12-08

Expand Down
20 changes: 15 additions & 5 deletions samples/Calculations/Engineering/Convert-Online.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
}

$categories = ConvertUOM::getConversionCategories();
$defaultCategory = $_POST['category'] ?? $categories[0];
$units = [];
foreach ($categories as $category) {
$categoryUnits = ConvertUOM::getConversionCategoryUnitDetails($category)[$category];
Expand Down Expand Up @@ -48,7 +49,7 @@
<label for="fromUnit" class="col-sm-2 col-form-label">From Unit</label>
<div class="col-sm-10">
<select name="fromUnit" class="form-select">
<?php foreach ($units[$_POST['category']] as $fromUnitCode => $fromUnitName) {
<?php foreach ($units[$defaultCategory] as $fromUnitCode => $fromUnitName) {
echo "<option value=\"{$fromUnitCode}\" " . ((isset($_POST['fromUnit']) && $_POST['fromUnit'] === $fromUnitCode) ? 'selected' : '') . ">{$fromUnitName}</option>", PHP_EOL;
} ?>
</select>
Expand All @@ -58,7 +59,7 @@
<label for="toUnit" class="col-sm-2 col-form-label">To Unit</label>
<div class="col-sm-10">
<select name="toUnit" class="form-select">
<?php foreach ($units[$_POST['category']] as $toUnitCode => $toUnitName) {
<?php foreach ($units[$defaultCategory] as $toUnitCode => $toUnitName) {
echo "<option value=\"{$toUnitCode}\" " . ((isset($_POST['toUnit']) && $_POST['toUnit'] === $toUnitCode) ? 'selected' : '') . ">{$toUnitName}</option>", PHP_EOL;
} ?>
</select>
Expand All @@ -73,11 +74,20 @@

<?php
/** If the user has submitted the form, then we need to calculate the value and display the result */
if (isset($_POST['submit'])) {
if (isset($_POST['quantity'], $_POST['fromUnit'], $_POST['toUnit'])) {
$quantity = $_POST['quantity'];
$fromUnit = $_POST['fromUnit'];
$toUnit = $_POST['toUnit'];
$result = ConvertUOM::CONVERT($quantity, $fromUnit, $toUnit);
if (!is_numeric($quantity)) {
$helper->log('Quantity is not numeric');
} elseif (isset($units[$_POST['category']][$fromUnit], $units[$_POST['category']][$toUnit])) {
/** @var float|string */
$result = ConvertUOM::CONVERT($quantity, $fromUnit, $toUnit);

echo "{$quantity} {$units[$_POST['category']][$fromUnit]} is {$result} {$units[$_POST['category']][$toUnit]}", PHP_EOL;
$helper->log("{$quantity} {$units[$_POST['category']][$fromUnit]} is {$result} {$units[$_POST['category']][$toUnit]}");
} else {
$helper->log('Please enter quantity and select From Unit and To Unit');
}
} else {
$helper->log('Please enter quantity and select From Unit and To Unit');
}
8 changes: 6 additions & 2 deletions samples/Wizards/NumberFormat/Accounting.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
$helper->log('The Sample Number Value must be numeric');
} elseif (!is_numeric($_POST['decimals']) || strpos($_POST['decimals'], '.') !== false || (int) $_POST['decimals'] < 0) {
$helper->log('The Decimal Places value must be positive integer');
} elseif (!in_array($_POST['currency'], array_keys($currencies), true)) {
$helper->log('Unrecognized currency symbol');
} else {
try {
$wizard = new Wizard\Accounting($_POST['currency'], $_POST['decimals'], isset($_POST['thousands']), (bool) $_POST['position'], (bool) $_POST['spacing']);
Expand All @@ -93,12 +95,14 @@
$helper->log('<hr /><b>Code:</b><br />');
$helper->log('use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;');
$helper->log(
"\$mask = Wizard\\Accounting('{$_POST['currency']}', {$_POST['decimals']}, Wizard\\Number::" .
"\$wizard = new Wizard\\Accounting('{$_POST['currency']}', {$_POST['decimals']}, Wizard\\Number::" .
(isset($_POST['thousands']) ? 'WITH_THOUSANDS_SEPARATOR' : 'WITHOUT_THOUSANDS_SEPARATOR') .
', Wizard\Currency::' . (((bool) $_POST['position']) ? 'LEADING_SYMBOL' : 'TRAILING_SYMBOL') .
', Wizard\Currency::' . (((bool) $_POST['spacing']) ? 'SYMBOL_WITH_SPACING' : 'SYMBOL_WITHOUT_SPACING') .
');<br />'
');'
);
$helper->log('$mask = $wizard->format();');
$helper->log('<br />');
$helper->log('echo (string) $mask;');
$helper->log('<hr /><b>Mask:</b><br />');
$helper->log($mask . '<br />');
Expand Down
8 changes: 6 additions & 2 deletions samples/Wizards/NumberFormat/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
$helper->log('The Sample Number Value must be numeric');
} elseif (!is_numeric($_POST['decimals']) || strpos($_POST['decimals'], '.') !== false || (int) $_POST['decimals'] < 0) {
$helper->log('The Decimal Places value must be positive integer');
} elseif (!in_array($_POST['currency'], array_keys($currencies), true)) {
$helper->log('Unrecognized currency symbol');
} else {
try {
$wizard = new Wizard\Currency($_POST['currency'], $_POST['decimals'], isset($_POST['thousands']), (bool) $_POST['position'], (bool) $_POST['spacing']);
Expand All @@ -93,12 +95,14 @@
$helper->log('<hr /><b>Code:</b><br />');
$helper->log('use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;');
$helper->log(
"\$mask = Wizard\\Currency('{$_POST['currency']}', {$_POST['decimals']}, Wizard\\Number::" .
"\$wizard = new Wizard\\Currency('{$_POST['currency']}', {$_POST['decimals']}, Wizard\\Number::" .
(isset($_POST['thousands']) ? 'WITH_THOUSANDS_SEPARATOR' : 'WITHOUT_THOUSANDS_SEPARATOR') .
', Wizard\Currency::' . (((bool) $_POST['position']) ? 'LEADING_SYMBOL' : 'TRAILING_SYMBOL') .
', Wizard\Currency::' . (((bool) $_POST['spacing']) ? 'SYMBOL_WITH_SPACING' : 'SYMBOL_WITHOUT_SPACING') .
');<br />'
');'
);
$helper->log('$mask = $wizard->format();');
$helper->log('<br />');
$helper->log('echo (string) $mask;');
$helper->log('<hr /><b>Mask:</b><br />');
$helper->log($mask . '<br />');
Expand Down
6 changes: 3 additions & 3 deletions src/PhpSpreadsheet/Helper/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ class Downloader
public function __construct(string $folder, string $filename, ?string $filetype = null)
{
if ((is_dir($folder) === false) || (is_readable($folder) === false)) {
throw new Exception("Folder {$folder} is not accessable");
throw new Exception('Folder is not accessible');
}
$filepath = "{$folder}/{$filename}";
$this->filepath = (string) realpath($filepath);
$this->filename = basename($filepath);
if ((file_exists($this->filepath) === false) || (is_readable($this->filepath) === false)) {
throw new Exception("{$this->filename} not found, or cannot be read");
throw new Exception('File not found, or cannot be read');
}

$filetype ??= pathinfo($filename, PATHINFO_EXTENSION);
if (array_key_exists(strtolower($filetype), self::CONTENT_TYPES) === false) {
throw new Exception("Invalid filetype: {$filetype} cannot be downloaded");
throw new Exception('Invalid filetype: cannot be downloaded');
}
$this->filetype = strtolower($filetype);
}
Expand Down

0 comments on commit a50ebfe

Please sign in to comment.