Skip to content

Commit f65d065

Browse files
committed
Security fixes
- Disable unused upload form: /main/upload/upload.document.php. - Update .htaccess to disable php execution inside web/ (before it was only web/css). - Add phar extension in the php2phps() function - Document upload, check the destination path to be inside the course with Security::check_abs_path - Add api_protect_course_script() - Add course/user validations
1 parent d41ce4e commit f65d065

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

main/inc/lib/fileUpload.lib.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
function php2phps($file_name)
2626
{
27-
return preg_replace('/\.(php.?|phtml.?)(\.){0,1}.*$/i', '.phps', $file_name);
27+
return preg_replace('/\.(phar.?|php.?|phtml.?)(\.){0,1}.*$/i', '.phps', $file_name);
2828
}
2929

3030
/**
@@ -238,7 +238,7 @@ function handle_uploaded_document(
238238
$sessionId = null,
239239
$treat_spaces_as_hyphens = true
240240
) {
241-
if (!$userId) {
241+
if (empty($uploadedFile) || empty($userId) || empty($courseInfo) || empty($documentDir) || empty($uploadPath)) {
242242
return false;
243243
}
244244

@@ -258,7 +258,6 @@ function handle_uploaded_document(
258258

259259
// Just in case process_uploaded_file is not called
260260
$maxSpace = DocumentManager::get_course_quota();
261-
262261
// Check if there is enough space to save the file
263262
if (!DocumentManager::enough_space($uploadedFile['size'], $maxSpace)) {
264263
if ($output) {
@@ -268,6 +267,21 @@ function handle_uploaded_document(
268267
return false;
269268
}
270269

270+
if ($uploadPath !== '/') {
271+
$uploadPath = $uploadPath.'/';
272+
}
273+
274+
if (!Security::check_abs_path($documentDir.$uploadPath, $documentDir.'/')) {
275+
Display::addFlash(
276+
Display::return_message(
277+
get_lang('Forbidden'),
278+
'error'
279+
)
280+
);
281+
return false;
282+
}
283+
284+
271285
// If the want to unzip, check if the file has a .zip (or ZIP,Zip,ZiP,...) extension
272286
if ($unzip == 1 && preg_match('/.zip$/', strtolower($uploadedFile['name']))) {
273287
return unzip_uploaded_document(
@@ -310,7 +324,7 @@ function handle_uploaded_document(
310324
return false;
311325
} else {
312326
// If the upload path differs from / (= root) it will need a slash at the end
313-
if ($uploadPath != '/') {
327+
if ($uploadPath !== '/') {
314328
$uploadPath = $uploadPath.'/';
315329
}
316330

@@ -1137,6 +1151,10 @@ function unzip_uploaded_document(
11371151
$onlyUploadFile = false,
11381152
$whatIfFileExists = 'overwrite'
11391153
) {
1154+
if (empty($courseInfo) || empty($userInfo) || empty($uploaded_file) || empty($uploadPath)) {
1155+
return false;
1156+
}
1157+
11401158
$zip = new PclZip($uploaded_file['tmp_name']);
11411159

11421160
// Check the zip content (real size and file extension)

main/upload/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
$_course = api_get_course_info();
2222

23+
api_protect_course_script(true);
24+
2325
$htmlHeadXtra[] = "<script>
2426
function check_unzip() {
2527
if (document.upload.unzip.checked) {

main/upload/upload.document.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
/* For licensing terms, see /license.txt */
33

4+
exit;
5+
46
/**
57
* Process part of the document sub-process for upload. This script MUST BE included by upload/index.php
68
* as it prepares most of the variables needed here.

main/upload/upload.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
require_once __DIR__.'/../inc/global.inc.php';
1515

16+
api_protect_course_script(true);
17+
1618
$toolFromSession = Session::read('my_tool');
1719

1820
// return to index if no tool is set
@@ -40,6 +42,6 @@
4042
case TOOL_STUDENTPUBLICATION:
4143
case TOOL_DOCUMENT:
4244
default:
43-
require 'upload.document.php';
45+
//require 'upload.document.php';
4446
break;
4547
}

main/upload/upload_ppt.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
require_once __DIR__.'/../inc/global.inc.php';
1515

16+
api_protect_course_script(true);
17+
1618
if (isset($_POST['convert'])) {
1719
$cwdir = getcwd();
1820
if (isset($_FILES['user_file'])) {

main/upload/upload_word.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
require_once __DIR__.'/../inc/global.inc.php';
1414

15+
api_protect_course_script(true);
16+
1517
$form_style = '<style>
1618
.row {
1719
width: 200px;

0 commit comments

Comments
 (0)