diff --git a/_test/tests/inc/tar.test.php b/_test/tests/inc/tar.test.php index 9801ca1e0c4f97165fea50f821b34661d1460df9..417f1a85384d3b93a745bd49d621c7ff8821a8c3 100644 --- a/_test/tests/inc/tar.test.php +++ b/_test/tests/inc/tar.test.php @@ -58,6 +58,8 @@ class Tar_TestCase extends DokuWikiTest { $tar->addData('another/testdata3.txt', 'testcontent3'); $tar->close(); +copy ($tmp, '/tmp/test.tar'); + $this->assertTrue(filesize($tmp) > 30); //arbitrary non-zero number $data = file_get_contents($tmp); @@ -66,7 +68,7 @@ class Tar_TestCase extends DokuWikiTest { $this->assertTrue(strpos($data, 'testcontent3') !== false, 'Content in TAR'); // fullpath might be too long to be stored as full path FS#2802 - $this->assertTrue(strpos($data, "$tdir") !== false, 'Path in TAR'); + $this->assertTrue(strpos($data, "$tdir") !== false, "Path in TAR '$tdir'"); $this->assertTrue(strpos($data, "testdata1.txt") !== false, 'File in TAR'); $this->assertTrue(strpos($data, 'noway/testdata2.txt') !== false, 'Path in TAR'); @@ -396,4 +398,23 @@ class Tar_TestCase extends DokuWikiTest { $this->assertEquals(512*4, strlen($file)); // 1 header block + data block + 2 footer blocks } + + + public function test_cleanPath(){ + $tar = new Tar(); + $tests = array ( + '/foo/bar' => 'foo/bar', + '/foo/bar/' => 'foo/bar', + 'foo//bar' => 'foo/bar', + 'foo/0/bar' => 'foo/0/bar', + 'foo/../bar' => 'bar', + 'foo/bang/bang/../../bar' => 'foo/bar', + 'foo/../../bar' => 'bar', + 'foo/.././../bar' => 'bar', + ); + + foreach($tests as $in => $out){ + $this->assertEquals($out, $tar->cleanPath($in), "Input: $in"); + } + } } diff --git a/inc/Tar.class.php b/inc/Tar.class.php index d1a38ea0e0a9d2b4ad8f395afffca434ca9bf79d..bc87d7d29f6ece3f6e9f6cecfbfb3399b360cd71 100644 --- a/inc/Tar.class.php +++ b/inc/Tar.class.php @@ -568,29 +568,23 @@ class Tar { } /** - * Cleans up a path and removes relative parts + * Cleans up a path and removes relative parts, also strips leading slashes * * @param string $p_dir * @return string */ - protected function cleanPath($p_dir) { - $r = ''; - if($p_dir) { - $subf = explode("/", $p_dir); - - for($i = count($subf) - 1; $i >= 0; $i--) { - if($subf[$i] == ".") { - # do nothing - } elseif($subf[$i] == "..") { - $i--; - } elseif(!$subf[$i] && $i != count($subf) - 1 && $i) { - # do nothing - } else { - $r = $subf[$i].($i != (count($subf) - 1) ? "/".$r : ""); - } + public function cleanPath($path) { + $path=explode('/', $path); + $newpath=array(); + foreach($path as $p) { + if ($p === '' || $p === '.') continue; + if ($p==='..') { + array_pop($newpath); + continue; } + array_push($newpath, $p); } - return $r; + return trim(implode('/', $newpath), '/'); } /**