From 595d7add2716c65afdb49a7567b73a6972fec9a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 7 Nov 2022 17:25:42 +0100 Subject: [PATCH 1/4] Check quota on file copy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/dav/lib/Connector/Sabre/QuotaPlugin.php | 23 +++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/apps/dav/lib/Connector/Sabre/QuotaPlugin.php b/apps/dav/lib/Connector/Sabre/QuotaPlugin.php index f2b652e33201a..0e5ffe517283e 100644 --- a/apps/dav/lib/Connector/Sabre/QuotaPlugin.php +++ b/apps/dav/lib/Connector/Sabre/QuotaPlugin.php @@ -44,7 +44,6 @@ * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License */ class QuotaPlugin extends \Sabre\DAV\ServerPlugin { - /** @var \OC\Files\View */ private $view; @@ -79,6 +78,7 @@ public function initialize(\Sabre\DAV\Server $server) { $server->on('beforeWriteContent', [$this, 'beforeWriteContent'], 10); $server->on('beforeCreateFile', [$this, 'beforeCreateFile'], 10); $server->on('beforeMove', [$this, 'beforeMove'], 10); + $server->on('beforeCopy', [$this, 'beforeCopy'], 10); } /** @@ -138,6 +138,27 @@ public function beforeMove($source, $destination) { return $this->checkQuota($path, $sourceNode->getSize()); } + /** + * Check quota on the target destination before a copy. + */ + public function beforeCopy(string $sourcePath, string $destinationPath): bool { + $sourceNode = $this->server->tree->getNodeForPath($sourcePath); + if (!$sourceNode instanceof Node) { + return false; + } + + // get target node for proper path conversion + if ($this->server->tree->nodeExists($destinationPath)) { + $destinationNode = $this->server->tree->getNodeForPath($destinationPath); + $path = $destinationNode->getPath(); + } else { + $parentNode = $this->server->tree->getNodeForPath(dirname($destinationPath)); + $path = $parentNode->getPath(); + } + + return $this->checkQuota($path, $sourceNode->getSize()); + } + /** * This method is called before any HTTP method and validates there is enough free space to store the file From 5aab36a3f6553d8ead2be2d82578e4f09ae7c8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 8 Nov 2022 17:13:45 +0100 Subject: [PATCH 2/4] Fix psalm issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/dav/lib/Connector/Sabre/QuotaPlugin.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/apps/dav/lib/Connector/Sabre/QuotaPlugin.php b/apps/dav/lib/Connector/Sabre/QuotaPlugin.php index 0e5ffe517283e..dccd10d6021db 100644 --- a/apps/dav/lib/Connector/Sabre/QuotaPlugin.php +++ b/apps/dav/lib/Connector/Sabre/QuotaPlugin.php @@ -150,9 +150,15 @@ public function beforeCopy(string $sourcePath, string $destinationPath): bool { // get target node for proper path conversion if ($this->server->tree->nodeExists($destinationPath)) { $destinationNode = $this->server->tree->getNodeForPath($destinationPath); + if (!$destinationNode instanceof Node) { + return false; + } $path = $destinationNode->getPath(); } else { $parentNode = $this->server->tree->getNodeForPath(dirname($destinationPath)); + if (!$parentNode instanceof Node) { + return false; + } $path = $parentNode->getPath(); } From 8a14131a84302d26c76ccda971adac1b8cef02a5 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 14 Nov 2022 14:19:47 +0100 Subject: [PATCH 3/4] fix dirname usage Signed-off-by: Robin Appelman --- apps/dav/lib/Connector/Sabre/QuotaPlugin.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/dav/lib/Connector/Sabre/QuotaPlugin.php b/apps/dav/lib/Connector/Sabre/QuotaPlugin.php index dccd10d6021db..70a83ccaa6810 100644 --- a/apps/dav/lib/Connector/Sabre/QuotaPlugin.php +++ b/apps/dav/lib/Connector/Sabre/QuotaPlugin.php @@ -155,7 +155,11 @@ public function beforeCopy(string $sourcePath, string $destinationPath): bool { } $path = $destinationNode->getPath(); } else { - $parentNode = $this->server->tree->getNodeForPath(dirname($destinationPath)); + $parent = dirname($destinationPath); + if ($parent === '.') { + $parent = ''; + } + $parentNode = $this->server->tree->getNodeForPath($parent); if (!$parentNode instanceof Node) { return false; } From d07c5c6d2dc782aef603b09a333112752bb03dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 15 Nov 2022 18:11:15 +0100 Subject: [PATCH 4/4] Apply the same fix as on beforeCopy on beforeMove MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let’s be safe Signed-off-by: Côme Chilliet --- apps/dav/lib/Connector/Sabre/QuotaPlugin.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/dav/lib/Connector/Sabre/QuotaPlugin.php b/apps/dav/lib/Connector/Sabre/QuotaPlugin.php index 70a83ccaa6810..7c6bf64602ee5 100644 --- a/apps/dav/lib/Connector/Sabre/QuotaPlugin.php +++ b/apps/dav/lib/Connector/Sabre/QuotaPlugin.php @@ -131,7 +131,11 @@ public function beforeMove($source, $destination) { $destinationNode = $this->server->tree->getNodeForPath($destination); $path = $destinationNode->getPath(); } else { - $parentNode = $this->server->tree->getNodeForPath(dirname($destination)); + $parent = dirname($destination); + if ($parent === '.') { + $parent = ''; + } + $parentNode = $this->server->tree->getNodeForPath($parent); $path = $parentNode->getPath(); }