Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle disabled shell_exec #598

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,23 @@ public function getThermalZones(): array {
return [];
}

/**
* Execute a command with shell_exec.
*
* The command will be escaped with escapeshellcmd.
*
* @throws RuntimeException if shell_exec is unavailable, the command failed or an empty response.
*/
protected function executeCommand(string $command): string {
$output = @shell_exec(escapeshellcmd($command));
if ($output === null || $output === '' || $output === false) {
if (function_exists('shell_exec') === false) {
throw new RuntimeException('shell_exec unavailable');
}

$output = shell_exec(escapeshellcmd($command));
if ($output === false || $output === null || $output === '') {
throw new RuntimeException('No output for command: "' . $command . '"');
}

return $output;
}

Expand Down
37 changes: 29 additions & 8 deletions lib/OperatingSystems/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ public function getCpuName(): string {
}

public function getTime(): string {
return (string)shell_exec('date');
try {
return $this->executeCommand('date');
} catch (RuntimeException $e) {
return '';
}
}

public function getUptime(): int {
Expand All @@ -139,12 +143,17 @@ public function getUptime(): int {
}

public function getNetworkInfo(): array {
$result = [];
$result['hostname'] = \gethostname();
$dns = shell_exec('cat /etc/resolv.conf |grep -i \'^nameserver\'|head -n1|cut -d \' \' -f2');
$result['dns'] = $dns;
$gw = shell_exec('ip route | awk \'/default/ { print $3 }\'');
$result['gateway'] = $gw;
$result = [
'hostname' => \gethostname(),
'dns' => '',
'gateway' => '',
];

if (function_exists('shell_exec')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the reason we can't use executeCommand() here is because of the inherent use of escapeshellcmd(). Maybe we add an optional parameter to `executeCommand() that specifies whether to escape or not (that defaults true). We could then use consistently (elsewhere too I think) w/o boilerplate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, escapeshellcmd does not allow a | b because it escapes the pipe.

I'm not a fan of those piped commands here in serverinfo because of the bad readability. Parsing the command output with PHP is easier to read and test.

I went for the function_exists approach to easily backport it without changing to much.

I have a few ideas in mind for follow-ups

  • Move executeCommand to a trait
  • Drop dns (because that's much more complicated today than reading /etc/resolv.conf)

$result['dns'] = shell_exec('cat /etc/resolv.conf |grep -i \'^nameserver\'|head -n1|cut -d \' \' -f2');
$result['gateway'] = shell_exec('ip route | awk \'/default/ { print $3 }\'');
}

return $result;
}

Expand Down Expand Up @@ -261,11 +270,23 @@ protected function readContent(string $filename): string {
return trim($data);
}

/**
* Execute a command with shell_exec.
*
* The command will be escaped with escapeshellcmd.
*
* @throws RuntimeException if shell_exec is unavailable, the command failed or an empty response.
*/
protected function executeCommand(string $command): string {
$output = @shell_exec(escapeshellcmd($command));
if (function_exists('shell_exec') === false) {
throw new RuntimeException('shell_exec unavailable');
}

$output = shell_exec(escapeshellcmd($command));
if ($output === false || $output === null || $output === '') {
throw new RuntimeException('No output for command: "' . $command . '"');
}

return $output;
}

Expand Down
Loading