From ad269597d6b95ccdf0552e24d17218f8f4ca20d3 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Thu, 3 Feb 2022 22:48:11 +0100 Subject: [PATCH] #208 improved GPG open_basedir --- .../app/libraries/snappymail/pgp/gpg.php | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/snappymail/pgp/gpg.php b/snappymail/v/0.0.0/app/libraries/snappymail/pgp/gpg.php index 9a98a0245..546bfe74c 100644 --- a/snappymail/v/0.0.0/app/libraries/snappymail/pgp/gpg.php +++ b/snappymail/v/0.0.0/app/libraries/snappymail/pgp/gpg.php @@ -1381,22 +1381,32 @@ class GPG private static function findBinary($name) : ?string { - if (!\ini_get('open_basedir')) { - $binary = \trim(`which $name`); - if ($binary && \is_executable($binary)) { - return $binary; - } - $locations = [ - '/sw/bin/', - '/usr/bin/', - '/usr/local/bin/', - '/opt/local/bin/', - '/run/current-system/sw/bin/' - ]; - foreach ($locations as $location) { - if (\is_executable($location . $name)) { - return $location . $name; + $binary = \trim(`which $name`); + if ($binary && \is_executable($binary)) { + return $binary; + } + $locations = [ + '/sw/bin/', + '/usr/bin/', + '/usr/local/bin/', + '/opt/local/bin/', + '/run/current-system/sw/bin/' + ]; + $open_basedir = \ini_get('open_basedir'); + if ($open_basedir) { + $open_basedir = \explode(PATH_SEPARATOR, $open_basedir); + $locations = \array_filter($locations, function($path) use ($open_basedir) { + foreach ($open_basedir as $dir) { + if (\str_starts_with($path, $open_basedir)) { + return true; + } } + return false; + }); + } + foreach ($locations as $location) { + if (\is_executable($location . $name)) { + return $location . $name; } } return null;