[Pkg-owncloud-commits] [owncloud] 10/83: Fixed normalizePath() to strip out single dot dirs

David Prévot taffit at moszumanska.debian.org
Wed Dec 18 13:05:24 UTC 2013


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch 5.0
in repository owncloud.

commit e44831d10f47e409623f2213488cac72915ffb69
Author: Vincent Petry <pvince81 at owncloud.com>
Date:   Thu Nov 14 13:15:03 2013 +0100

    Fixed normalizePath() to strip out single dot dirs
    
    Now removing "/./" and trailing "/." from the paths when normalizing.
    
    Backport of c06d8bb0071839480f9e458e58630ca0c205b9cb
---
 lib/files/filesystem.php       | 20 ++++++++++++---
 tests/lib/files/filesystem.php | 55 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 71 insertions(+), 4 deletions(-)

diff --git a/lib/files/filesystem.php b/lib/files/filesystem.php
index 14ca882..1850d50 100644
--- a/lib/files/filesystem.php
+++ b/lib/files/filesystem.php
@@ -589,18 +589,32 @@ class Filesystem {
 		}
 		//no windows style slashes
 		$path = str_replace('\\', '/', $path);
+
 		//add leading slash
 		if ($path[0] !== '/') {
 			$path = '/' . $path;
 		}
-		//remove duplicate slashes
-		while (strpos($path, '//') !== false) {
-			$path = str_replace('//', '/', $path);
+
+		// remove '/./'
+		// ugly, but str_replace() can't replace them all in one go
+		// as the replacement itself is part of the search string
+		// which will only be found during the next iteration
+		while (strpos($path, '/./') !== false) {
+			$path = str_replace('/./', '/', $path);
 		}
+		// remove sequences of slashes
+		$path = preg_replace('#/{2,}#', '/', $path);
+
 		//remove trailing slash
 		if ($stripTrailingSlash and strlen($path) > 1 and substr($path, -1, 1) === '/') {
 			$path = substr($path, 0, -1);
 		}
+
+		// remove trailing '/.'
+		if (substr($path, -2) == '/.') {
+			$path = substr($path, 0, -2);
+		}
+
 		//normalize unicode if possible
 		$path = \OC_Util::normalizeUnicode($path);
 		return $path;
diff --git a/tests/lib/files/filesystem.php b/tests/lib/files/filesystem.php
index bef70cc..83e0d28 100644
--- a/tests/lib/files/filesystem.php
+++ b/tests/lib/files/filesystem.php
@@ -66,17 +66,70 @@ class Filesystem extends \PHPUnit_Framework_TestCase {
 	}
 
 	public function testNormalize() {
+		$this->assertEquals('/', \OC\Files\Filesystem::normalizePath(''));
+		$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('/'));
+		$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('/', false));
+		$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('//'));
+		$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('//', false));
 		$this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('/path/'));
 		$this->assertEquals('/path/', \OC\Files\Filesystem::normalizePath('/path/', false));
 		$this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('path'));
-		$this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('\path'));
 		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo//bar/'));
+		$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('/foo//bar/', false));
 		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo////bar'));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/////bar'));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/bar/.'));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/bar/./'));
+		$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('/foo/bar/./', false));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/bar/./.'));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/bar/././'));
+		$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('/foo/bar/././', false));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/./bar/'));
+		$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('/foo/./bar/', false));
+		$this->assertEquals('/foo/.bar', \OC\Files\Filesystem::normalizePath('/foo/.bar/'));
+		$this->assertEquals('/foo/.bar/', \OC\Files\Filesystem::normalizePath('/foo/.bar/', false));
+		$this->assertEquals('/foo/.bar/tee', \OC\Files\Filesystem::normalizePath('/foo/.bar/tee'));
+
+		// normalize does not resolve '..' (by design)
+		$this->assertEquals('/foo/..', \OC\Files\Filesystem::normalizePath('/foo/../'));
+
 		if (class_exists('Patchwork\PHP\Shim\Normalizer')) {
 			$this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("/foo/baru\xCC\x88"));
 		}
 	}
 
+	public function testNormalizeWindowsPaths() {
+		$this->assertEquals('/', \OC\Files\Filesystem::normalizePath(''));
+		$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('\\'));
+		$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('\\', false));
+		$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('\\\\'));
+		$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('\\\\', false));
+		$this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('\\path\\'));
+		$this->assertEquals('/path/', \OC\Files\Filesystem::normalizePath('\\path\\', false));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\\\bar\\'));
+		$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('\\foo\\\\bar\\', false));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\\\\\\\bar'));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\\\\\\\\\bar'));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.'));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\'));
+		$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\', false));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\.'));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\.\\'));
+		$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\.\\', false));
+		$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\.\\bar\\'));
+		$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('\\foo\\.\\bar\\', false));
+		$this->assertEquals('/foo/.bar', \OC\Files\Filesystem::normalizePath('\\foo\\.bar\\'));
+		$this->assertEquals('/foo/.bar/', \OC\Files\Filesystem::normalizePath('\\foo\\.bar\\', false));
+		$this->assertEquals('/foo/.bar/tee', \OC\Files\Filesystem::normalizePath('\\foo\\.bar\\tee'));
+
+		// normalize does not resolve '..' (by design)
+		$this->assertEquals('/foo/..', \OC\Files\Filesystem::normalizePath('\\foo\\..\\'));
+
+		if (class_exists('Patchwork\PHP\Shim\Normalizer')) {
+			$this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("\\foo\\baru\xCC\x88"));
+		}
+	}
+
 	public function testHooks() {
 		if(\OC\Files\Filesystem::getView()){
 			$user = \OC_User::getUser();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list