Make White List Instance Property Rather than Static

This commit is contained in:
oleibman
2025-12-21 22:31:45 -08:00
parent 2ce3ac1a90
commit 9bc3a73ba4
5 changed files with 34 additions and 27 deletions
@@ -2578,6 +2578,7 @@ class FunctionArray extends CalculationBase
'category' => Category::CATEGORY_WEB,
'functionCall' => [Web\Service::class, 'webService'],
'argumentCount' => '1',
'passCellReference' => true,
],
'WEEKDAY' => [
'category' => Category::CATEGORY_DATE_AND_TIME,
@@ -4,7 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\Web;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
use PhpOffice\PhpSpreadsheet\Settings;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
class Service
@@ -19,7 +19,7 @@ class Service
*
* @return string the output resulting from a call to the webservice
*/
public static function webService(mixed $url): string
public static function webService(mixed $url, ?Cell $cell = null): ?string
{
if (is_array($url)) {
$url = Functions::flattenSingleValue($url);
@@ -33,9 +33,10 @@ class Service
if ($scheme !== 'http' && $scheme !== 'https') {
return ExcelError::VALUE(); // Invalid protocol
}
$domainWhiteList = $cell?->getWorksheet()->getParent()?->getDomainWhiteList() ?? [];
$host = $parsed['host'] ?? '';
if (!in_array($host, Settings::getDomainWhiteList(), true)) {
return Functions::NOT_YET_IMPLEMENTED; // will be converted to oldCalculatedValue or null
if (!in_array($host, $domainWhiteList, true)) {
return ($cell === null) ? null : Functions::NOT_YET_IMPLEMENTED; // will be converted to oldCalculatedValue or null
}
// Get results from the webservice
-19
View File
@@ -155,23 +155,4 @@ class Settings
{
return self::$requestFactory;
}
/** @var string[] */
private static $domainWhiteList = [];
/**
* Currently used only by WEBSERVICE function.
*
* @param string[] $domainWhiteList
*/
public static function setDomainWhiteList(array $domainWhiteList): void
{
self::$domainWhiteList = $domainWhiteList;
}
/** @return string[] */
public static function getDomainWhiteList(): array
{
return self::$domainWhiteList;
}
}
+21
View File
@@ -1851,4 +1851,25 @@ class Spreadsheet implements JsonSerializable
Calculation::RETURN_ARRAY_AS_VALUE
);
}
/** @var string[] */
private $domainWhiteList = [];
/**
* Currently used only by WEBSERVICE function.
*
* @param string[] $domainWhiteList
*/
public function setDomainWhiteList(array $domainWhiteList): self
{
$this->domainWhiteList = $domainWhiteList;
return $this;
}
/** @return string[] */
public function getDomainWhiteList(): array
{
return $this->domainWhiteList;
}
}
@@ -4,8 +4,8 @@ declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Web;
use PhpOffice\PhpSpreadsheet\Calculation\Web\Service;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Settings;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
@@ -22,7 +22,6 @@ class WebServiceTest extends TestCase
protected function tearDown(): void
{
Settings::setDomainWhiteList([]);
if ($this->spreadsheet !== null) {
$this->spreadsheet->disconnectWorksheets();
$this->spreadsheet = null;
@@ -35,8 +34,8 @@ class WebServiceTest extends TestCase
if (str_starts_with($url, 'https') && getenv('SKIP_URL_IMAGE_TEST') === '1') {
self::markTestSkipped('Skipped due to setting of environment variable');
}
Settings::setDomainWhiteList(self::WHITELIST);
$this->spreadsheet = new Spreadsheet();
$this->spreadsheet->setDomainWhiteList(self::WHITELIST);
$sheet = $this->spreadsheet->getActiveSheet();
$sheet->getCell('Z1')->setValue('http://www.example.com');
$sheet->getCell('Z2')->setValue(2);
@@ -56,9 +55,9 @@ class WebServiceTest extends TestCase
public function testOldCalculated(): void
{
Settings::setDomainWhiteList(self::WHITELIST);
$reader = new XlsxReader();
$this->spreadsheet = $reader->load('tests/data/Reader/XLSX/fakewebservice.xlsx');
$this->spreadsheet->setDomainWhiteList(self::WHITELIST);
$sheet = $this->spreadsheet->getActiveSheet();
$a1Formula = $sheet->getCell('A1')->getValue();
self::assertSame(
@@ -82,5 +81,9 @@ class WebServiceTest extends TestCase
$sheet->getCell('A3')->getCalculatedValue(),
'oldCalculatedValue explicitly set above'
);
self::assertNull(
Service::webService('http://www.example.com'),
'no Spreadsheet so no whitelist'
);
}
}