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

Add flag to disallow symlinks. #27

Merged
merged 2 commits into from
Dec 3, 2019
Merged
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
28 changes: 21 additions & 7 deletions Archive/Tar.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,12 @@ public function add($p_filelist)
/**
* @param string $p_path
* @param bool $p_preserve
* @param bool $p_symlinks
* @return bool
*/
public function extract($p_path = '', $p_preserve = false)
public function extract($p_path = '', $p_preserve = false, $p_symlinks = true)
{
return $this->extractModify($p_path, '', $p_preserve);
return $this->extractModify($p_path, '', $p_preserve, $p_symlinks);
}

/**
Expand Down Expand Up @@ -557,11 +558,12 @@ public function addString($p_filename, $p_string, $p_datetime = false, $p_params
* removed if present at the beginning of
* the file/dir path.
* @param boolean $p_preserve Preserve user/group ownership of files
* @param boolean $p_symlinks Allow symlinks.
*
* @return boolean true on success, false on error.
* @see extractList()
*/
public function extractModify($p_path, $p_remove_path, $p_preserve = false)
public function extractModify($p_path, $p_remove_path, $p_preserve = false, $p_symlinks = true)
{
$v_result = true;
$v_list_detail = array();
Expand All @@ -573,7 +575,8 @@ public function extractModify($p_path, $p_remove_path, $p_preserve = false)
"complete",
0,
$p_remove_path,
$p_preserve
$p_preserve,
$p_symlinks
);
$this->_close();
}
Expand Down Expand Up @@ -617,11 +620,12 @@ public function extractInString($p_filename)
* removed if present at the beginning of
* the file/dir path.
* @param boolean $p_preserve Preserve user/group ownership of files
* @param boolean $p_symlinks Allow symlinks.
*
* @return true on success, false on error.
* @see extractModify()
*/
public function extractList($p_filelist, $p_path = '', $p_remove_path = '', $p_preserve = false)
public function extractList($p_filelist, $p_path = '', $p_remove_path = '', $p_preserve = false, $p_symlinks = true)
{
$v_result = true;
$v_list_detail = array();
Expand All @@ -642,7 +646,8 @@ public function extractList($p_filelist, $p_path = '', $p_remove_path = '', $p_p
"partial",
$v_list,
$p_remove_path,
$p_preserve
$p_preserve,
$p_symlinks
);
$this->_close();
}
Expand Down Expand Up @@ -1917,6 +1922,7 @@ private function _extractInString($p_filename)
* @param string $p_file_list
* @param string $p_remove_path
* @param bool $p_preserve
* @param bool $p_symlinks
* @return bool
*/
public function _extractList(
Expand All @@ -1925,7 +1931,8 @@ public function _extractList(
$p_mode,
$p_file_list,
$p_remove_path,
$p_preserve = false
$p_preserve = false,
$p_symlinks = true
)
{
$v_result = true;
Expand Down Expand Up @@ -2108,6 +2115,13 @@ public function _extractList(
}
}
} elseif ($v_header['typeflag'] == "2") {
if (!$p_symlinks) {
$this->_warning('Symbolic links are not allowed. '
. 'Unable to extract {'
. $v_header['filename'] . '}'
);
return false;
}
if (@file_exists($v_header['filename'])) {
@unlink($v_header['filename']);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/symlink_disallow.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
test symbolic links
--SKIPIF--
--FILE--
<?php
require_once dirname(__FILE__) . '/setup.php.inc';
$me = dirname(__FILE__) . '/testit';
$tar = new Archive_Tar(dirname(__FILE__) . '/testsymlink.tar');
$tar->extract('', false, false);
$phpunit->assertErrors(array(
array(
'package' => 'PEAR_Error',
'message' => 'Symbolic links are not allowed. Unable to extract {testme/symlink.txt}'
),
), 'Warning thrown');
$phpunit->assertFileExists('testme', 'dir');
$phpunit->assertFileNotExists('testme/file1.txt', 'file1.txt');
$phpunit->assertFileNotExists('testme/symlink.txt', 'symlink.txt');
echo 'tests done';
?>
--CLEAN--
<?php
@unlink('testme/file1.txt');
@unlink('testme/symlink.txt');
@rmdir('testme');
?>
--EXPECT--
tests done