Browse Source

Prevent deleting storage root

Storage mount points are not deletable, so make sure that the unlink
operation and its hooks aren't run in such cases.

Note that some storages might recursively delete their contents when
calling unlink on their root. This fix prevents that as well.
tags/v7.0.0alpha2
Vincent Petry 10 years ago
parent
commit
f642ad3961
2 changed files with 55 additions and 0 deletions
  1. 13
    0
      lib/private/files/view.php
  2. 42
    0
      tests/lib/files/view.php

+ 13
- 0
lib/private/files/view.php View File

@@ -336,6 +336,19 @@ class View {
}

public function unlink($path) {
if ($path === '' || $path === '/') {
// do not allow deleting the root
return false;
}
$postFix = (substr($path, -1, 1) === '/') ? '/' : '';
$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
if (!$internalPath || $internalPath === '' || $internalPath === '/') {
// do not allow deleting the storage's root / the mount point
// because for some storages it might delete the whole contents
// but isn't supposed to work that way
return false;
}
return $this->basicOperation('unlink', $path, array('delete'));
}


+ 42
- 0
tests/lib/files/view.php View File

@@ -306,6 +306,48 @@ class View extends \PHPUnit_Framework_TestCase {
$this->assertTrue($rootView->file_exists('anotherfolder/bar.txt'));
}

/**
* @medium
*/
function testUnlink() {
$storage1 = $this->getTestStorage();
$storage2 = $this->getTestStorage();
\OC\Files\Filesystem::mount($storage1, array(), '/');
\OC\Files\Filesystem::mount($storage2, array(), '/substorage');

$rootView = new \OC\Files\View('');
$rootView->file_put_contents('/foo.txt', 'asd');
$rootView->file_put_contents('/substorage/bar.txt', 'asd');

$this->assertTrue($rootView->file_exists('foo.txt'));
$this->assertTrue($rootView->file_exists('substorage/bar.txt'));

$this->assertTrue($rootView->unlink('foo.txt'));
$this->assertTrue($rootView->unlink('substorage/bar.txt'));

$this->assertFalse($rootView->file_exists('foo.txt'));
$this->assertFalse($rootView->file_exists('substorage/bar.txt'));
}

/**
* @medium
*/
function testUnlinkRootMustFail() {
$storage1 = $this->getTestStorage();
$storage2 = $this->getTestStorage();
\OC\Files\Filesystem::mount($storage1, array(), '/');
\OC\Files\Filesystem::mount($storage2, array(), '/substorage');

$rootView = new \OC\Files\View('');
$rootView->file_put_contents('/foo.txt', 'asd');
$rootView->file_put_contents('/substorage/bar.txt', 'asd');

$this->assertFalse($rootView->unlink(''));
$this->assertFalse($rootView->unlink('/'));
$this->assertFalse($rootView->unlink('substorage'));
$this->assertFalse($rootView->unlink('/substorage'));
}

/**
* @medium
*/

Loading…
Cancel
Save