Skip to content

Commit 2bba3fa

Browse files
committed
Security: Plugin: VChamilo: Verify course container real root
Fix GHSA-c4fc-vjm9-9mvc
1 parent b939fc8 commit 2bba3fa

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

plugin/vchamilo/views/manage.testdatapath.php

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*/
1212

1313
// Loading configuration.
14+
use Symfony\Component\Filesystem\Filesystem;
15+
use Symfony\Component\Finder\Finder;
16+
1417
require_once __DIR__.'/../../../main/inc/global.inc.php';
1518

1619
api_protect_admin_script();
@@ -21,38 +24,50 @@
2124
$dataroot = $_REQUEST['dataroot'];
2225

2326
$absalternatecourse = Virtual::getConfig('vchamilo', 'course_real_root');
24-
if (!empty($absalternatecourse)) {
25-
// this is the relocated case
26-
$coursedir = str_replace('//', '/', $absalternatecourse.'/'.$dataroot);
27-
} else {
28-
// this is the standard local case
29-
$coursedir = api_get_path(SYS_PATH).$dataroot;
30-
}
3127

32-
if (is_dir($coursedir)) {
33-
$DIR = opendir($coursedir);
34-
$cpt = 0;
35-
$hasfiles = false;
36-
while (($file = readdir($DIR)) && !$hasfiles) {
37-
if (!preg_match("/^\\./", $file)) {
38-
$hasfiles = true;
28+
try {
29+
if (!empty($absalternatecourse)) {
30+
if (!is_dir($absalternatecourse) || !realpath($absalternatecourse)) {
31+
throw new Exception("$absalternatecourse is not a directory");
3932
}
40-
}
41-
closedir($DIR);
4233

43-
if ($hasfiles) {
44-
echo '<div class="error">'.$plugin->get_lang('datapathnotavailable').'</div>';
34+
$absalternatecourse = realpath($absalternatecourse);
35+
36+
// this is the relocated case
37+
$coursedir = str_replace('//', '/', $absalternatecourse.'/'.$dataroot);
4538
} else {
46-
echo '<div class="success">'.$plugin->get_lang('datapathavailable').'</div>';
39+
// this is the standard local case
40+
$coursedir = api_get_path(SYS_PATH).$dataroot;
4741
}
48-
echo stripslashes($coursedir);
49-
} else {
50-
if (@mkdir($coursedir, 02777, true)) {
51-
echo '<div class="success">'.$plugin->get_lang('datapathcreated').'</div>';
42+
43+
$fileSystem = new Filesystem();
44+
45+
$components = explode('://', $coursedir);
46+
$components = array_reverse($components);
47+
$coursedir = $components[0];
48+
49+
if (is_file($coursedir)) {
50+
throw new Exception("$coursedir is file");
51+
}
52+
53+
if ($fileSystem->exists($coursedir) && is_dir($coursedir)) {
54+
$finder = new Finder();
55+
$finder->files()->in($coursedir);
56+
57+
if ($finder->hasResults()) {
58+
echo '<div class="error">'.$plugin->get_lang('datapathnotavailable').'</div>';
59+
} else {
60+
echo '<div class="success">'.$plugin->get_lang('datapathavailable').'</div>';
61+
}
5262
} else {
53-
echo '<div class="error">'.$plugin->get_lang('couldnotcreatedataroot').'</div>';
63+
$fileSystem->mkdir($coursedir, 02777);
64+
65+
echo '<div class="success">'.$plugin->get_lang('datapathcreated').'</div>';
5466
}
67+
5568
echo stripslashes($coursedir);
69+
} catch (Exception $exception) {
70+
echo '<div class="error">'.$exception->getMessage().'</div>';
5671
}
5772

5873
echo "</p>";

0 commit comments

Comments
 (0)