Skip to content

Commit 37efbc4

Browse files
committed
Make sure we don't scan files that can not be accessed
Signed-off-by: Joas Schilling <[email protected]>
1 parent 72eb5d1 commit 37efbc4

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/private/files/cache/scanner.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ protected function getData($path) {
131131
*/
132132
public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) {
133133

134+
if (!\OC::$server->getDatabaseConnection()->supports4ByteText()) {
135+
// verify database - e.g. mysql only 3-byte chars
136+
if (preg_match('%(?:
137+
\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
138+
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
139+
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
140+
)%xs', $file)) {
141+
// 4-byte characters are not supported in file names
142+
return null;
143+
}
144+
}
145+
146+
try {
147+
$this->storage->verifyPath(dirname($file), basename($file));
148+
} catch (\Exception $e) {
149+
return null;
150+
}
151+
134152
// only proceed if $file is not a partial file nor a blacklisted file
135153
if (!self::isPartialFile($file) and !Filesystem::isFileBlacklisted($file)) {
136154

@@ -162,6 +180,9 @@ public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData =
162180
// scan the parent if it's not in the cache (id -1) and the current file is not the root folder
163181
if ($file and $parentId === -1) {
164182
$parentData = $this->scanFile($parent);
183+
if (!$parentData) {
184+
return null;
185+
}
165186
$parentId = $parentData['fileid'];
166187
}
167188
if ($parent) {

tests/lib/files/cache/scanner.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ function testFile() {
6969
$this->assertEquals($cachedData['mimetype'], 'image/png');
7070
}
7171

72+
function testFile4Byte() {
73+
$data = "dummy file data\n";
74+
$this->storage->file_put_contents('foo🙈.txt', $data);
75+
76+
if (\OC::$server->getDatabaseConnection()->supports4ByteText()) {
77+
$this->assertNotNull($this->scanner->scanFile('foo🙈.txt'));
78+
$this->assertTrue($this->cache->inCache('foo🙈.txt'), true);
79+
80+
$cachedData = $this->cache->get('foo🙈.txt');
81+
$this->assertEquals(strlen($data), $cachedData['size']);
82+
$this->assertEquals('text/plain', $cachedData['mimetype']);
83+
$this->assertNotEquals(-1, $cachedData['parent']); //parent folders should be scanned automatically
84+
} else {
85+
$this->assertNull($this->scanner->scanFile('foo🙈.txt'));
86+
$this->assertFalse($this->cache->inCache('foo🙈.txt'), true);
87+
}
88+
}
89+
90+
function testFileInvalidChars() {
91+
$data = "dummy file data\n";
92+
$this->storage->file_put_contents("foo\nbar.txt", $data);
93+
94+
$this->assertNull($this->scanner->scanFile("foo\nbar.txt"));
95+
$this->assertFalse($this->cache->inCache("foo\nbar.txt"), true);
96+
}
97+
7298
private function fillTestFolders() {
7399
$textData = "dummy file data\n";
74100
$imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');

0 commit comments

Comments
 (0)