Skip to content

Commit b515b6b

Browse files
authored
handle host detector with custom error handler and open_basedir (#1454)
if open_basedir is configured such that files that it tries to open are denied with a php warning, and a custom error handler is installed which converts warnings to exceptions, an unhandled exception occurs. add a test for this, suppress error handling around the code that can trigger this.
1 parent f73bcfa commit b515b6b

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

src/SDK/Resource/Detectors/Host.php

+22-6
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,32 @@ private function getMachineId(): ?string
4747
};
4848
}
4949

50+
/**
51+
* @phan-suppress PhanTypeMismatchArgumentInternal
52+
*/
53+
private function readFile(string $file): string|false
54+
{
55+
set_error_handler(static fn () => true);
56+
57+
try {
58+
$contents = file_get_contents($file);
59+
60+
return $contents !== false ? trim($contents) : false;
61+
} finally {
62+
restore_error_handler();
63+
}
64+
}
65+
5066
private function getLinuxId(): ?string
5167
{
5268
$paths = [self::PATH_ETC_MACHINEID, self::PATH_VAR_LIB_DBUS_MACHINEID];
5369

5470
foreach ($paths as $path) {
5571
$file = $this->dir . $path;
56-
if (is_file($file) && is_readable($file)) {
57-
$contents = file_get_contents($file);
5872

59-
return $contents !== false ? trim($contents) : null;
73+
$contents = $this->readFile($file);
74+
if ($contents !== false) {
75+
return $contents;
6076
}
6177
}
6278

@@ -66,10 +82,10 @@ private function getLinuxId(): ?string
6682
private function getBsdId(): ?string
6783
{
6884
$file = $this->dir . self::PATH_ETC_HOSTID;
69-
if (is_file($file) && is_readable($file)) {
70-
$contents = file_get_contents($file);
7185

72-
return $contents !== false ? trim($contents) : null;
86+
$contents = $this->readFile($file);
87+
if ($contents !== false) {
88+
return $contents;
7389
}
7490

7591
$out = exec('which kenv && kenv -q smbios.system.uuid');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Host detector with custom error handler and open_basedir
3+
--DESCRIPTION--
4+
An error handler is installed which converts PHP warnings to exceptions, and open_basedir
5+
is configured such that /etc/machine-id and friends cannot be read
6+
--SKIPIF--
7+
<?php if (!in_array(PHP_OS_FAMILY, ['Linux', 'BSD'])) die('skip requires Linux or BSD'); ?>
8+
--INI--
9+
open_basedir=${PWD}
10+
--FILE--
11+
<?php
12+
use OpenTelemetry\SDK\Resource\Detectors\Host;
13+
14+
require_once 'vendor/autoload.php';
15+
16+
function warningToException($errno, $errstr, $errfile, $errline)
17+
{
18+
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
19+
}
20+
set_error_handler('warningToException');
21+
22+
$detector = new Host();
23+
$resource = $detector->getResource();
24+
var_dump($resource->getAttributes()->toArray());
25+
?>
26+
--EXPECTF--
27+
array(2) {
28+
["host.name"]=>
29+
string(%d) "%s"
30+
["host.arch"]=>
31+
string(%d) "%s"
32+
}

0 commit comments

Comments
 (0)