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

Issue 2927: create a method for checking if filename is a directory and also checking open_basedir #21923

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 2 additions & 2 deletions CRM/Admin/Page/APIExplorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function run() {
$paths = self::uniquePaths();
foreach ($paths as $path) {
$dir = \CRM_Utils_File::addTrailingSlash($path) . 'api' . DIRECTORY_SEPARATOR . 'v3' . DIRECTORY_SEPARATOR . 'examples';
if (is_dir($dir)) {
if (\CRM_Utils_File::isDir($dir)) {
foreach (scandir($dir) as $item) {
if ($item && strpos($item, '.') === FALSE && array_search($item, $examples) === FALSE) {
$examples[] = $item;
Expand Down Expand Up @@ -96,7 +96,7 @@ public static function getExampleFile() {
$paths = self::uniquePaths();
foreach ($paths as $path) {
$dir = \CRM_Utils_File::addTrailingSlash($path) . 'api' . DIRECTORY_SEPARATOR . 'v3' . DIRECTORY_SEPARATOR . 'examples' . DIRECTORY_SEPARATOR . $_GET['entity'];
if (is_dir($dir)) {
if (\CRM_Utils_File::isDir($dir)) {
foreach (scandir($dir) as $item) {
$item = str_replace('.ex.php', '', $item);
if ($item && strpos($item, '.') === FALSE) {
Expand Down
41 changes: 41 additions & 0 deletions CRM/Utils/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1100,4 +1100,45 @@ public static function getExtensionFromPath($path) {
return pathinfo($path, PATHINFO_EXTENSION);
}

/**
* Check that filename is a directory.
* @param string $fileName
* @return bool
*/
public static function isDir($fileName = '') {
// If not within open_basedir do not consider it a directory.
if (!CRM_Utils_File::isFileInOpenbasedir($fileName)) {
return FALSE;
}
return is_dir($fileName);
}

/**
* Check that path is within open_basedir restriction.
* @param string $filePath the absolute path
* @return bool
*/
private static function isFileInOpenbasedir($filePath = '') {
// Use the appropriate comparison function.
$compareFunc = 'strncmp';
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$compareFunc = 'strncasecmp';
}
// Ensure file path is within open_basedir restrictions.
$openBasedir = ini_get('open_basedir');
if (!empty($openBasedir)) {
$baseDirs = explode(PATH_SEPARATOR, $openBasedir);
$withinBasedir = FALSE;
foreach ($baseDirs as $baseDir) {
if ($compareFunc($filePath, $baseDir, strlen($baseDir)) === 0) {
$withinBasedir = TRUE;
herbdool marked this conversation as resolved.
Show resolved Hide resolved
}
}
if (!$withinBasedir) {
return FALSE;
}
}
return TRUE;
}

}
6 changes: 3 additions & 3 deletions Civi/API/Provider/MagicFunctionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getEntityNames($version) {
foreach ($include_dirs as $include_dir) {
$api_dir = implode(DIRECTORY_SEPARATOR,
[$include_dir, 'api', 'v' . $version]);
if (!is_dir($api_dir)) {
if (!\CRM_Utils_File::isDir($api_dir)) {
continue;
}
$iterator = new \DirectoryIterator($api_dir);
Expand All @@ -119,7 +119,7 @@ public function getEntityNames($version) {

// Check for entities with standalone action files (eg "api/v3/MyEntity/MyAction.php").
$action_dir = $api_dir . DIRECTORY_SEPARATOR . $file;
if (preg_match('/^[A-Z][A-Za-z0-9]*$/', $file) && is_dir($action_dir)) {
if (preg_match('/^[A-Z][A-Za-z0-9]*$/', $file) && \CRM_Utils_File::isDir($action_dir)) {
if (count(glob("$action_dir/[A-Z]*.php")) > 0) {
$entities[] = $file;
}
Expand Down Expand Up @@ -281,7 +281,7 @@ protected function loadEntity($entity, $version) {
foreach ([$camelName, 'Generic'] as $name) {
$action_dir = implode(DIRECTORY_SEPARATOR,
[$include_dir, 'api', "v${version}", $name]);
if (!is_dir($action_dir)) {
if (!\CRM_Utils_File::isDir($action_dir)) {
continue;
}

Expand Down