Skip to content

Commit 4b86c43

Browse files
committed
check for filename blacklist in OC_Filesystem::isValidPath
1 parent 6540c0f commit 4b86c43

File tree

2 files changed

+104
-21
lines changed

2 files changed

+104
-21
lines changed

lib/filesystem.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ static public function isValidPath($path){
368368
if(strstr($path,'/../') || strrchr($path, '/') === '/..' ){
369369
return false;
370370
}
371+
if(self::isFileBlacklisted($path)){
372+
return false;
373+
}
371374
return true;
372375
}
373376

@@ -376,21 +379,23 @@ static public function isValidPath($path){
376379
* Listens to write and rename hooks
377380
* @param array $data from hook
378381
*/
379-
static public function isBlacklisted($data){
380-
$blacklist = array('.htaccess');
382+
static public function isBlacklisted($data) {
381383
if (isset($data['path'])) {
382384
$path = $data['path'];
383385
} else if (isset($data['newpath'])) {
384386
$path = $data['newpath'];
385387
}
386388
if (isset($path)) {
387-
$filename = strtolower(basename($path));
388-
if (in_array($filename, $blacklist)) {
389-
$data['run'] = false;
390-
}
389+
$data['run'] = !self::isFileBlacklisted($path);
391390
}
392391
}
393-
392+
393+
static public function isFileBlacklisted($path){
394+
$blacklist = array('.htaccess');
395+
$filename = strtolower(basename($path));
396+
return in_array($filename, $blacklist);
397+
}
398+
394399
/**
395400
* following functions are equivalent to their php builtin equivalents for arguments/return values.
396401
*/

tests/lib/filesystem.php

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,98 @@ public function setUp(){
4545
OC_Filesystem::clearMounts();
4646
}
4747

48-
public function testMount(){
49-
OC_Filesystem::mount('OC_Filestorage_Local',self::getStorageData(),'/');
50-
$this->assertEqual('/',OC_Filesystem::getMountPoint('/'));
51-
$this->assertEqual('/',OC_Filesystem::getMountPoint('/some/folder'));
52-
$this->assertEqual('',OC_Filesystem::getInternalPath('/'));
53-
$this->assertEqual('some/folder',OC_Filesystem::getInternalPath('/some/folder'));
54-
55-
OC_Filesystem::mount('OC_Filestorage_Local',self::getStorageData(),'/some');
56-
$this->assertEqual('/',OC_Filesystem::getMountPoint('/'));
57-
$this->assertEqual('/some/',OC_Filesystem::getMountPoint('/some/folder'));
58-
$this->assertEqual('/some/',OC_Filesystem::getMountPoint('/some/'));
59-
$this->assertEqual('/',OC_Filesystem::getMountPoint('/some'));
60-
$this->assertEqual('folder',OC_Filesystem::getInternalPath('/some/folder'));
48+
public function testMount() {
49+
OC_Filesystem::mount('OC_Filestorage_Local', self::getStorageData(), '/');
50+
$this->assertEqual('/', OC_Filesystem::getMountPoint('/'));
51+
$this->assertEqual('/', OC_Filesystem::getMountPoint('/some/folder'));
52+
$this->assertEqual('', OC_Filesystem::getInternalPath('/'));
53+
$this->assertEqual('some/folder', OC_Filesystem::getInternalPath('/some/folder'));
54+
55+
OC_Filesystem::mount('OC_Filestorage_Local', self::getStorageData(), '/some');
56+
$this->assertEqual('/', OC_Filesystem::getMountPoint('/'));
57+
$this->assertEqual('/some/', OC_Filesystem::getMountPoint('/some/folder'));
58+
$this->assertEqual('/some/', OC_Filesystem::getMountPoint('/some/'));
59+
$this->assertEqual('/', OC_Filesystem::getMountPoint('/some'));
60+
$this->assertEqual('folder', OC_Filesystem::getInternalPath('/some/folder'));
61+
}
62+
63+
public function testNormalize() {
64+
$this->assertEqual('/path', OC_Filesystem::normalizePath('/path/'));
65+
$this->assertEqual('/path/', OC_Filesystem::normalizePath('/path/', false));
66+
$this->assertEqual('/path', OC_Filesystem::normalizePath('path'));
67+
$this->assertEqual('/path', OC_Filesystem::normalizePath('\path'));
68+
$this->assertEqual('/foo/bar', OC_Filesystem::normalizePath('/foo//bar/'));
69+
$this->assertEqual('/foo/bar', OC_Filesystem::normalizePath('/foo////bar'));
70+
if (class_exists('Normalizer')) {
71+
$this->assertEqual("/foo/bar\xC3\xBC", OC_Filesystem::normalizePath("/foo/baru\xCC\x88"));
72+
}
73+
}
74+
75+
public function testBlacklist() {
76+
OC_Hook::clear('OC_Filesystem');
77+
OC_Hook::connect('OC_Filesystem', 'write', 'OC_Filesystem', 'isBlacklisted');
78+
OC_Hook::connect('OC_Filesystem', 'rename', 'OC_Filesystem', 'isBlacklisted');
79+
80+
$run = true;
81+
OC_Hook::emit(
82+
OC_Filesystem::CLASSNAME,
83+
OC_Filesystem::signal_write,
84+
array(
85+
OC_Filesystem::signal_param_path => '/test/.htaccess',
86+
OC_Filesystem::signal_param_run => &$run
87+
)
88+
);
89+
$this->assertFalse($run);
90+
91+
if (OC_Filesystem::getView()) {
92+
$user = OC_User::getUser();
93+
} else {
94+
$user = uniqid();
95+
OC_Filesystem::init('/' . $user . '/files');
96+
}
97+
98+
OC_Filesystem::mount('OC_Filestorage_Temporary', array(), '/');
99+
100+
$rootView = new OC_FilesystemView('');
101+
$rootView->mkdir('/' . $user);
102+
$rootView->mkdir('/' . $user . '/files');
103+
104+
$this->assertFalse($rootView->file_put_contents('/.htaccess', 'foo'));
105+
$this->assertFalse(OC_Filesystem::file_put_contents('/.htaccess', 'foo'));
106+
$fh = fopen(__FILE__, 'r');
107+
$this->assertFalse(OC_Filesystem::file_put_contents('/.htaccess', $fh));
108+
}
109+
110+
public function testHooks() {
111+
if (OC_Filesystem::getView()) {
112+
$user = OC_User::getUser();
113+
} else {
114+
$user = uniqid();
115+
OC_Filesystem::init('/' . $user . '/files');
116+
}
117+
OC_Hook::clear('OC_Filesystem');
118+
OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook');
119+
120+
OC_Filesystem::mount('OC_Filestorage_Temporary', array(), '/');
121+
122+
$rootView = new OC_FilesystemView('');
123+
$rootView->mkdir('/' . $user);
124+
$rootView->mkdir('/' . $user . '/files');
125+
126+
OC_Filesystem::file_put_contents('/foo', 'foo');
127+
OC_Filesystem::mkdir('/bar');
128+
OC_Filesystem::file_put_contents('/bar//foo', 'foo');
129+
130+
$tmpFile = OC_Helper::tmpFile();
131+
file_put_contents($tmpFile, 'foo');
132+
$fh = fopen($tmpFile, 'r');
133+
OC_Filesystem::file_put_contents('/bar//foo', $fh);
134+
}
135+
136+
public function dummyHook($arguments) {
137+
$path = $arguments['path'];
138+
$this->assertEqual($path, OC_Filesystem::normalizePath($path)); //the path passed to the hook should already be normalized
61139
}
62140
}
63141

64-
?>
142+
?>

0 commit comments

Comments
 (0)