@@ -312,11 +312,12 @@ public function add($p_filelist)
312312 /**
313313 * @param string $p_path
314314 * @param bool $p_preserve
315+ * @param bool $p_symlinks
315316 * @return bool
316317 */
317- public function extract ($ p_path = '' , $ p_preserve = false )
318+ public function extract ($ p_path = '' , $ p_preserve = false , $ p_symlinks = true )
318319 {
319- return $ this ->extractModify ($ p_path , '' , $ p_preserve );
320+ return $ this ->extractModify ($ p_path , '' , $ p_preserve, $ p_symlinks );
320321 }
321322
322323 /**
@@ -557,11 +558,12 @@ public function addString($p_filename, $p_string, $p_datetime = false, $p_params
557558 * removed if present at the beginning of
558559 * the file/dir path.
559560 * @param boolean $p_preserve Preserve user/group ownership of files
561+ * @param boolean $p_symlinks Allow symlinks.
560562 *
561563 * @return boolean true on success, false on error.
562564 * @see extractList()
563565 */
564- public function extractModify ($ p_path , $ p_remove_path , $ p_preserve = false )
566+ public function extractModify ($ p_path , $ p_remove_path , $ p_preserve = false , $ p_symlinks = true )
565567 {
566568 $ v_result = true ;
567569 $ v_list_detail = array ();
@@ -573,7 +575,8 @@ public function extractModify($p_path, $p_remove_path, $p_preserve = false)
573575 "complete " ,
574576 0 ,
575577 $ p_remove_path ,
576- $ p_preserve
578+ $ p_preserve ,
579+ $ p_symlinks
577580 );
578581 $ this ->_close ();
579582 }
@@ -617,11 +620,12 @@ public function extractInString($p_filename)
617620 * removed if present at the beginning of
618621 * the file/dir path.
619622 * @param boolean $p_preserve Preserve user/group ownership of files
623+ * @param boolean $p_symlinks Allow symlinks.
620624 *
621625 * @return true on success, false on error.
622626 * @see extractModify()
623627 */
624- public function extractList ($ p_filelist , $ p_path = '' , $ p_remove_path = '' , $ p_preserve = false )
628+ public function extractList ($ p_filelist , $ p_path = '' , $ p_remove_path = '' , $ p_preserve = false , $ p_symlinks = true )
625629 {
626630 $ v_result = true ;
627631 $ v_list_detail = array ();
@@ -642,7 +646,8 @@ public function extractList($p_filelist, $p_path = '', $p_remove_path = '', $p_p
642646 "partial " ,
643647 $ v_list ,
644648 $ p_remove_path ,
645- $ p_preserve
649+ $ p_preserve ,
650+ $ p_symlinks
646651 );
647652 $ this ->_close ();
648653 }
@@ -726,7 +731,7 @@ public function setIgnoreRegexp($regexp)
726731 */
727732 public function setIgnoreList ($ list )
728733 {
729- $ regexp = str_replace (array ('# ' , '. ' , '^ ' , '$ ' ), array ('\# ' , '\. ' , '\^ ' , '\$ ' ), $ list );
734+ $ list = str_replace (array ('# ' , '. ' , '^ ' , '$ ' ), array ('\# ' , '\. ' , '\^ ' , '\$ ' ), $ list );
730735 $ regexp = '#/ ' . join ('$|/ ' , $ list ) . '# ' ;
731736 $ this ->setIgnoreRegexp ($ regexp );
732737 }
@@ -1268,7 +1273,7 @@ public function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $v_
12681273 while (($ v_buffer = fread ($ v_file , $ this ->buffer_length )) != '' ) {
12691274 $ buffer_length = strlen ("$ v_buffer " );
12701275 if ($ buffer_length != $ this ->buffer_length ) {
1271- $ pack_size = ((int )($ buffer_length / 512 ) + 1 ) * 512 ;
1276+ $ pack_size = ((int )($ buffer_length / 512 ) + ( $ buffer_length % 512 !== 0 ? 1 : 0 ) ) * 512 ;
12721277 $ pack_format = sprintf ('a%d ' , $ pack_size );
12731278 } else {
12741279 $ pack_format = sprintf ('a%d ' , $ this ->buffer_length );
@@ -1510,8 +1515,13 @@ public function _writeHeaderBlock(
15101515 $ userinfo = posix_getpwuid ($ p_uid );
15111516 $ groupinfo = posix_getgrgid ($ p_gid );
15121517
1513- $ v_uname = $ userinfo ['name ' ];
1514- $ v_gname = $ groupinfo ['name ' ];
1518+ if ($ userinfo === false || $ groupinfo === false ) {
1519+ $ v_uname = '' ;
1520+ $ v_gname = '' ;
1521+ } else {
1522+ $ v_uname = $ userinfo ['name ' ];
1523+ $ v_gname = $ groupinfo ['name ' ];
1524+ }
15151525 } else {
15161526 $ v_uname = '' ;
15171527 $ v_gname = '' ;
@@ -1720,7 +1730,7 @@ public function _readHeader($v_binary_data, &$v_header)
17201730
17211731 // ----- Extract the properties
17221732 $ v_header ['filename ' ] = rtrim ($ v_data ['filename ' ], "\0" );
1723- if ($ this ->_maliciousFilename ($ v_header ['filename ' ])) {
1733+ if ($ this ->_isMaliciousFilename ($ v_header ['filename ' ])) {
17241734 $ this ->_error (
17251735 'Malicious .tar detected, file " ' . $ v_header ['filename ' ] .
17261736 '" will not install in desired directory tree '
@@ -1790,9 +1800,9 @@ private function _tarRecToSize($tar_size)
17901800 *
17911801 * @return bool
17921802 */
1793- private function _maliciousFilename ($ file )
1803+ private function _isMaliciousFilename ($ file )
17941804 {
1795- if (strpos ($ file , 'phar :// ' ) === 0 ) {
1805+ if (strpos ($ file , ':// ' ) !== false ) {
17961806 return true ;
17971807 }
17981808 if (strpos ($ file , '../ ' ) !== false || strpos ($ file , '.. \\' ) !== false ) {
@@ -1828,7 +1838,7 @@ public function _readLongHeader(&$v_header)
18281838
18291839 $ v_filename = rtrim (substr ($ v_filename , 0 , $ v_filesize ), "\0" );
18301840 $ v_header ['filename ' ] = $ v_filename ;
1831- if ($ this ->_maliciousFilename ($ v_filename )) {
1841+ if ($ this ->_isMaliciousFilename ($ v_filename )) {
18321842 $ this ->_error (
18331843 'Malicious .tar detected, file " ' . $ v_filename .
18341844 '" will not install in desired directory tree '
@@ -1917,6 +1927,7 @@ private function _extractInString($p_filename)
19171927 * @param string $p_file_list
19181928 * @param string $p_remove_path
19191929 * @param bool $p_preserve
1930+ * @param bool $p_symlinks
19201931 * @return bool
19211932 */
19221933 public function _extractList (
@@ -1925,7 +1936,8 @@ public function _extractList(
19251936 $ p_mode ,
19261937 $ p_file_list ,
19271938 $ p_remove_path ,
1928- $ p_preserve = false
1939+ $ p_preserve = false ,
1940+ $ p_symlinks = true
19291941 )
19301942 {
19311943 $ v_result = true ;
@@ -2108,6 +2120,13 @@ public function _extractList(
21082120 }
21092121 }
21102122 } elseif ($ v_header ['typeflag ' ] == "2 " ) {
2123+ if (!$ p_symlinks ) {
2124+ $ this ->_warning ('Symbolic links are not allowed. '
2125+ . 'Unable to extract { '
2126+ . $ v_header ['filename ' ] . '} '
2127+ );
2128+ return false ;
2129+ }
21112130 if (@file_exists ($ v_header ['filename ' ])) {
21122131 @unlink ($ v_header ['filename ' ]);
21132132 }
0 commit comments