diff --git a/src/SDK/Resource/Detectors/Host.php b/src/SDK/Resource/Detectors/Host.php index 3a353cc67..73c7afa86 100644 --- a/src/SDK/Resource/Detectors/Host.php +++ b/src/SDK/Resource/Detectors/Host.php @@ -47,16 +47,32 @@ private function getMachineId(): ?string }; } + /** + * @phan-suppress PhanTypeMismatchArgumentInternal + */ + private function readFile(string $file): string|false + { + set_error_handler(static fn () => true); + + try { + $contents = file_get_contents($file); + + return $contents !== false ? trim($contents) : false; + } finally { + restore_error_handler(); + } + } + private function getLinuxId(): ?string { $paths = [self::PATH_ETC_MACHINEID, self::PATH_VAR_LIB_DBUS_MACHINEID]; foreach ($paths as $path) { $file = $this->dir . $path; - if (is_file($file) && is_readable($file)) { - $contents = file_get_contents($file); - return $contents !== false ? trim($contents) : null; + $contents = $this->readFile($file); + if ($contents !== false) { + return $contents; } } @@ -66,10 +82,10 @@ private function getLinuxId(): ?string private function getBsdId(): ?string { $file = $this->dir . self::PATH_ETC_HOSTID; - if (is_file($file) && is_readable($file)) { - $contents = file_get_contents($file); - return $contents !== false ? trim($contents) : null; + $contents = $this->readFile($file); + if ($contents !== false) { + return $contents; } $out = exec('which kenv && kenv -q smbios.system.uuid'); diff --git a/tests/Integration/SDK/Resource/Detectors/test_host_detector_with_open_basedir.phpt b/tests/Integration/SDK/Resource/Detectors/test_host_detector_with_open_basedir.phpt new file mode 100644 index 000000000..ef8dbc4ef --- /dev/null +++ b/tests/Integration/SDK/Resource/Detectors/test_host_detector_with_open_basedir.phpt @@ -0,0 +1,32 @@ +--TEST-- +Host detector with custom error handler and open_basedir +--DESCRIPTION-- +An error handler is installed which converts PHP warnings to exceptions, and open_basedir +is configured such that /etc/machine-id and friends cannot be read +--SKIPIF-- + +--INI-- +open_basedir=${PWD} +--FILE-- +getResource(); +var_dump($resource->getAttributes()->toArray()); +?> +--EXPECTF-- +array(2) { + ["host.name"]=> + string(%d) "%s" + ["host.arch"]=> + string(%d) "%s" +}