Skip to content

Commit c771034

Browse files
committed
Improved and hardened file handling in apache plugin.
1 parent e1a747a commit c771034

File tree

2 files changed

+249
-166
lines changed

2 files changed

+249
-166
lines changed

server/lib/classes/system.inc.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -611,23 +611,30 @@ function get_user_attributes($user){
611611
*
612612
*/
613613
function chown($file, $owner, $allow_symlink = false){
614+
global $app;
614615
if($allow_symlink == false && $this->checkpath($file) == false) {
615616
$app->log("Action aborted, file is a symlink: $file",LOGLEVEL_WARN);
616617
return false;
617618
}
618-
return chown($file, $owner);
619+
if(file_exists($file)) {
620+
return chown($file, $owner);
621+
}
619622
}
620623

621624
function chgrp($file, $group = '', $allow_symlink = false){
625+
global $app;
622626
if($allow_symlink == false && $this->checkpath($file) == false) {
623627
$app->log("Action aborted, file is a symlink: $file",LOGLEVEL_WARN);
624628
return false;
625629
}
626-
return chgrp($file, $group);
630+
if(file_exists($file)) {
631+
return chgrp($file, $group);
632+
}
627633
}
628634

629635
//* Change the mode of a file
630636
function chmod($file, $mode, $allow_symlink = false) {
637+
global $app;
631638
if($allow_symlink == false && $this->checkpath($file) == false) {
632639
$app->log("Action aborted, file is a symlink: $file",LOGLEVEL_WARN);
633640
return false;
@@ -636,15 +643,17 @@ function chmod($file, $mode, $allow_symlink = false) {
636643
}
637644

638645
function file_put_contents($filename, $data, $allow_symlink = false) {
646+
global $app;
639647
if($allow_symlink == false && $this->checkpath($filename) == false) {
640648
$app->log("Action aborted, file is a symlink: $filename",LOGLEVEL_WARN);
641649
return false;
642650
}
643-
unlink($filename);
651+
if(file_exists($filename)) unlink($filename);
644652
return file_put_contents($filename, $data);
645653
}
646654

647655
function file_get_contents($filename, $allow_symlink = false) {
656+
global $app;
648657
if($allow_symlink == false && $this->checkpath($filename) == false) {
649658
$app->log("Action aborted, file is a symlink: $filename",LOGLEVEL_WARN);
650659
return false;
@@ -653,6 +662,7 @@ function file_get_contents($filename, $allow_symlink = false) {
653662
}
654663

655664
function rename($filename, $new_filename, $allow_symlink = false) {
665+
global $app;
656666
if($allow_symlink == false && $this->checkpath($filename) == false) {
657667
$app->log("Action aborted, file is a symlink: $filename",LOGLEVEL_WARN);
658668
return false;
@@ -661,6 +671,7 @@ function rename($filename, $new_filename, $allow_symlink = false) {
661671
}
662672

663673
function mkdir($dirname, $allow_symlink = false) {
674+
global $app;
664675
if($allow_symlink == false && $this->checkpath($dirname) == false) {
665676
$app->log("Action aborted, file is a symlink: $dirname",LOGLEVEL_WARN);
666677
return false;
@@ -669,7 +680,9 @@ function mkdir($dirname, $allow_symlink = false) {
669680
}
670681

671682
function unlink($file) {
672-
return unlink($file);
683+
if(file_exists($filename)) {
684+
return unlink($filename);
685+
}
673686
}
674687

675688
function copy($file1,$file2) {
@@ -685,7 +698,7 @@ function checkpath($path) {
685698
if(!preg_match('/[a-zA-Z0-9_\.\-]{1,}/',$path)) return false;
686699

687700
//* Check path for symlinks
688-
$path_parts = explode($path);
701+
$path_parts = explode('/',$path);
689702
$testpath = '';
690703
foreach($path_parts as $p) {
691704
$testpath .= '/'.$p;
@@ -1203,6 +1216,7 @@ function get_time(){
12031216
}
12041217

12051218
function replaceLine($filename,$search_pattern,$new_line,$strict = 0,$append = 1) {
1219+
global $app;
12061220
if($this->checkpath($filename) == false) {
12071221
$app->log("Action aborted, file is a symlink: $filename",LOGLEVEL_WARN);
12081222
return false;
@@ -1242,6 +1256,7 @@ function replaceLine($filename,$search_pattern,$new_line,$strict = 0,$append = 1
12421256
}
12431257

12441258
function removeLine($filename,$search_pattern,$strict = 0) {
1259+
global $app;
12451260
if($this->checkpath($filename) == false) {
12461261
$app->log("Action aborted, file is a symlink: $filename",LOGLEVEL_WARN);
12471262
return false;

0 commit comments

Comments
 (0)