From b3b080e1c04274fb880ce79c8ee8a8e5d0702989 Mon Sep 17 00:00:00 2001
From: Vincent Petry <pvince81@owncloud.com>
Date: Mon, 13 Jul 2015 14:20:35 +0200
Subject: [PATCH] Strip public webdav info about sharing and mount points

Remove information about whether a file is an incoming share or mount
point when requested over public webdav.
---
 apps/files_sharing/publicwebdav.php         |  2 +-
 lib/private/connector/sabre/filesplugin.php | 18 ++++++++++--
 tests/lib/connector/sabre/filesplugin.php   | 31 +++++++++++++++++++--
 3 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/apps/files_sharing/publicwebdav.php b/apps/files_sharing/publicwebdav.php
index c0a9dc328d1..5bde908109d 100644
--- a/apps/files_sharing/publicwebdav.php
+++ b/apps/files_sharing/publicwebdav.php
@@ -45,7 +45,7 @@ $defaults = new OC_Defaults();
 $server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend, $defaults->getName()));
 // FIXME: The following line is a workaround for legacy components relying on being able to send a GET to /
 $server->addPlugin(new \OC\Connector\Sabre\DummyGetResponsePlugin());
-$server->addPlugin(new \OC\Connector\Sabre\FilesPlugin($objectTree));
+$server->addPlugin(new \OC\Connector\Sabre\FilesPlugin($objectTree, true));
 $server->addPlugin(new \OC\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig()));
 $server->addPlugin(new \OC\Connector\Sabre\ExceptionLoggerPlugin('webdav', \OC::$server->getLogger()));
 
diff --git a/lib/private/connector/sabre/filesplugin.php b/lib/private/connector/sabre/filesplugin.php
index 608e8cd9017..84620f454aa 100644
--- a/lib/private/connector/sabre/filesplugin.php
+++ b/lib/private/connector/sabre/filesplugin.php
@@ -55,11 +55,20 @@ class FilesPlugin extends \Sabre\DAV\ServerPlugin {
 	 */
 	private $tree;
 
+	/**
+	 * Whether this is public webdav.
+	 * If true, some returned information will be stripped off.
+	 *
+	 * @var bool
+	 */
+	private $isPublic;
+
 	/**
 	 * @param \Sabre\DAV\Tree $tree
 	 */
-	public function __construct(\Sabre\DAV\Tree $tree) {
+	public function __construct(\Sabre\DAV\Tree $tree, $isPublic = false) {
 		$this->tree = $tree;
+		$this->isPublic = $isPublic;
 	}
 
 	/**
@@ -129,7 +138,12 @@ class FilesPlugin extends \Sabre\DAV\ServerPlugin {
 			});
 
 			$propFind->handle(self::PERMISSIONS_PROPERTYNAME, function() use ($node) {
-				return $node->getDavPermissions();
+				$perms = $node->getDavPermissions();
+				if ($this->isPublic) {
+					// remove mount information
+					$perms = str_replace(['S', 'M'], '', $perms);
+				}
+				return $perms;
 			});
 
 			$propFind->handle(self::GETETAG_PROPERTYNAME, function() use ($node) {
diff --git a/tests/lib/connector/sabre/filesplugin.php b/tests/lib/connector/sabre/filesplugin.php
index 54d43d66dda..a4cf9f7bfb9 100644
--- a/tests/lib/connector/sabre/filesplugin.php
+++ b/tests/lib/connector/sabre/filesplugin.php
@@ -62,7 +62,7 @@ class FilesPlugin extends \Test\TestCase {
 			->will($this->returnValue('"abc"'));
 		$node->expects($this->any())
 			->method('getDavPermissions')
-			->will($this->returnValue('R'));
+			->will($this->returnValue('DWCKMSR'));
 
 		return $node;
 	}
@@ -98,11 +98,36 @@ class FilesPlugin extends \Test\TestCase {
 		$this->assertEquals('"abc"', $propFind->get(self::GETETAG_PROPERTYNAME));
 		$this->assertEquals(123, $propFind->get(self::FILEID_PROPERTYNAME));
 		$this->assertEquals(null, $propFind->get(self::SIZE_PROPERTYNAME));
-		$this->assertEquals('R', $propFind->get(self::PERMISSIONS_PROPERTYNAME));
+		$this->assertEquals('DWCKMSR', $propFind->get(self::PERMISSIONS_PROPERTYNAME));
 		$this->assertEquals('http://example.com/', $propFind->get(self::DOWNLOADURL_PROPERTYNAME));
 		$this->assertEquals(array(self::SIZE_PROPERTYNAME), $propFind->get404Properties());
 	}
 
+	public function testGetPublicPermissions() {
+		$this->plugin = new \OC\Connector\Sabre\FilesPlugin($this->tree, true);
+		$this->plugin->initialize($this->server);
+
+		$propFind = new \Sabre\DAV\PropFind(
+			'/dummyPath',
+			[
+				self::PERMISSIONS_PROPERTYNAME,
+			],
+			0
+		);
+
+		$node = $this->createTestNode('\OC\Connector\Sabre\File');
+		$node->expects($this->any())
+			->method('getDavPermissions')
+			->will($this->returnValue('DWCKMSR'));
+
+		$this->plugin->handleGetProperties(
+			$propFind,
+			$node
+		);
+
+		$this->assertEquals('DWCKR', $propFind->get(self::PERMISSIONS_PROPERTYNAME));
+	}
+
 	public function testGetPropertiesForDirectory() {
 		$node = $this->createTestNode('\OC\Connector\Sabre\Directory');
 
@@ -132,7 +157,7 @@ class FilesPlugin extends \Test\TestCase {
 		$this->assertEquals('"abc"', $propFind->get(self::GETETAG_PROPERTYNAME));
 		$this->assertEquals(123, $propFind->get(self::FILEID_PROPERTYNAME));
 		$this->assertEquals(1025, $propFind->get(self::SIZE_PROPERTYNAME));
-		$this->assertEquals('R', $propFind->get(self::PERMISSIONS_PROPERTYNAME));
+		$this->assertEquals('DWCKMSR', $propFind->get(self::PERMISSIONS_PROPERTYNAME));
 		$this->assertEquals(null, $propFind->get(self::DOWNLOADURL_PROPERTYNAME));
 		$this->assertEquals(array(self::DOWNLOADURL_PROPERTYNAME), $propFind->get404Properties());
 	}
-- 
GitLab