Skip to content

Commit

Permalink
exclude directories from beeing processed
Browse files Browse the repository at this point in the history
extended case: search at any place of the path given

adding case insensitiveness

added suggestions and improvements

removed unnecessary function parameter
  • Loading branch information
mmattel committed May 26, 2015
1 parent 156881e commit 07f3234
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 1 deletion.
9 changes: 9 additions & 0 deletions apps/files/ajax/newfile.php
Expand Up @@ -62,6 +62,15 @@
exit();
}

if (\OC\Files\Filesystem::isExcludedDir($fileName)) {
$result['data'] = array('message' => $l10n->t(
'The name %s has been excluded by the admin for files and directories. Please choose a different name.',
$fileName)
);
OCP\JSON::error($result);
exit();
}

$target = $dir.'/'.$fileName;

if (\OC\Files\Filesystem::file_exists($target)) {
Expand Down
9 changes: 9 additions & 0 deletions apps/files/ajax/newfolder.php
Expand Up @@ -54,6 +54,15 @@
return;
}

if (\OC\Files\Filesystem::isExcludedDir($folderName)) {
$result['data'] = array('message' => $l10n->t(
'The name %s has been excluded by the admin for files and directories. Please choose a different name.',
$folderName)
);
OCP\JSON::error($result);
exit();
}

if (!\OC\Files\Filesystem::file_exists($dir . '/')) {
$result['data'] = array('message' => (string)$l10n->t(
'The target folder has been moved or deleted.'),
Expand Down
13 changes: 13 additions & 0 deletions config/config.sample.php
Expand Up @@ -803,6 +803,19 @@
*/
'blacklisted_files' => array('.htaccess'),

/**
* Exclude specific directory names and disallow
* scanning, creating and renaming using these names. Case insensitive.
* Excluded directories will not show up.
* Use when the storage backend supports eg snapshot directories to be excluded.
* WARNING: USE THIS ONLY IF YOU KNOW WHAT YOU ARE DOING.
*/
'excluded_directories' =>
array (
'.snapshot',
'~snapshot',
),

/**
* Define a default folder for shared files and folders other than root.
*/
Expand Down
3 changes: 3 additions & 0 deletions lib/private/connector/sabre/custompropertiesbackend.php
Expand Up @@ -103,6 +103,9 @@ public function propFind($path, PropFind $propFind) {
} catch (ServiceUnavailable $e) {
// might happen for unavailable mount points, skip
return;
} catch (Forbidden $e) {
// might happen for excluded mount points, skip
return;
} catch (NotFound $e) {
// in some rare (buggy) cases the node might not be found,
// we catch the exception to prevent breaking the whole list with a 404
Expand Down
6 changes: 6 additions & 0 deletions lib/private/connector/sabre/directory.php
Expand Up @@ -71,6 +71,12 @@ class Directory extends \OC\Connector\Sabre\Node
*/
public function createFile($name, $data = null) {

# the check here is necessary, because createFile uses put covered in sabre/file.php
# and not touch covered in files/view.php
if (\OC\Files\Filesystem::isExcludedDir($name)) {
throw new \Sabre\DAV\Exception\Forbidden();
}

try {
// for chunked upload also updating a existing file is a "createFile"
// because we create all the chunks before re-assemble them to the existing file.
Expand Down
4 changes: 4 additions & 0 deletions lib/private/connector/sabre/objecttree.php
Expand Up @@ -101,6 +101,10 @@ public function getNodeForPath($path) {
if (!$this->fileView) {
throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
}
// if the path has been entered manually and eg not via a file explorer
if (\OC\Files\Filesystem::isExcludedDir($path)) {
throw new \Sabre\DAV\Exception\Forbidden();
}

$path = trim($path, '/');
if (isset($this->cache[$path])) {
Expand Down
4 changes: 3 additions & 1 deletion lib/private/files/cache/scanner.php
Expand Up @@ -277,7 +277,9 @@ protected function getNewChildren($folder) {
if (is_resource($dh)) {
while (($file = readdir($dh)) !== false) {
if (!Filesystem::isIgnoredDir($file)) {
$children[] = $file;
if (!Filesystem::isExcludedDir($file)) {
$children[] = $file;
}
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions lib/private/files/filesystem.php
Expand Up @@ -587,6 +587,26 @@ static public function isIgnoredDir($dir) {
return false;
}

/**
* check if the directory should be excluded
* A config.php parameter array can contain additional directory names to be excluded
* @param String $dir
* @return boolean
*/
static public function isExcludedDir($dir) {
$excluded = array();
$path_parts = array();
$excluded = \OC::$server->getSystemConfig()->getValue('excluded_directories', array());
$path_parts = explode('/', $dir);
$excluded = array_map('strtolower', $excluded);
$path_parts = array_map('strtolower', $path_parts);
$match = array_intersect($path_parts, $excluded);
if ($match) {
return true;
}
return false;
}

/**
* following functions are equivalent to their php builtin equivalents for arguments/return values.
*/
Expand Down
3 changes: 3 additions & 0 deletions lib/private/files/view.php
Expand Up @@ -596,6 +596,7 @@ public function rename($path1, $path2) {
Filesystem::isValidPath($path2)
and Filesystem::isValidPath($path1)
and !Filesystem::isFileBlacklisted($path2)
and !Filesystem::isExcludedDir($path2)
) {
$path1 = $this->getRelativePath($absolutePath1);
$path2 = $this->getRelativePath($absolutePath2);
Expand Down Expand Up @@ -699,6 +700,7 @@ public function copy($path1, $path2, $preserveMtime = false) {
Filesystem::isValidPath($path2)
and Filesystem::isValidPath($path1)
and !Filesystem::isFileBlacklisted($path2)
and !Filesystem::isExcludedDir($path2)
) {
$path1 = $this->getRelativePath($absolutePath1);
$path2 = $this->getRelativePath($absolutePath2);
Expand Down Expand Up @@ -923,6 +925,7 @@ private function basicOperation($operation, $path, $hooks = array(), $extraParam
$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
if (Filesystem::isValidPath($path)
and !Filesystem::isFileBlacklisted($path)
and !Filesystem::isExcludedDir($path)
) {
$path = $this->getRelativePath($absolutePath);
if ($path == null) {
Expand Down

0 comments on commit 07f3234

Please sign in to comment.