Skip to content

Commit

Permalink
Merge pull request #27349 from nextcloud/backport/25280/stable20
Browse files Browse the repository at this point in the history
[stable20] Set umask before operations that create local files
  • Loading branch information
icewind1991 authored Jun 7, 2021
2 parents ac5d4a8 + b486855 commit 9f62c80
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/private/Avatar/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ protected function generateAvatarFromSvg(int $size) {
$avatar->readImageBlob($svg);
$avatar->setImageFormat('png');
$image = new OC_Image();
$image->loadFromData($avatar);
return $image->data();
$image->loadFromData((string)$avatar);
$data = $image->data();
return $data === null ? false : $data;
} catch (\Exception $e) {
return false;
}
Expand Down
23 changes: 19 additions & 4 deletions lib/private/Files/Storage/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ public function getId() {
}

public function mkdir($path) {
return @mkdir($this->getSourcePath($path), 0777, true);
$sourcePath = $this->getSourcePath($path);
$oldMask = umask(022);
$result = @mkdir($sourcePath, 0777, true);
umask($oldMask);
return $result;
}

public function rmdir($path) {
Expand Down Expand Up @@ -256,11 +260,13 @@ public function touch($path, $mtime = null) {
if ($this->file_exists($path) and !$this->isUpdatable($path)) {
return false;
}
$oldMask = umask(022);
if (!is_null($mtime)) {
$result = @touch($this->getSourcePath($path), $mtime);
} else {
$result = @touch($this->getSourcePath($path));
}
umask($oldMask);
if ($result) {
clearstatcache(true, $this->getSourcePath($path));
}
Expand All @@ -273,7 +279,10 @@ public function file_get_contents($path) {
}

public function file_put_contents($path, $data) {
return file_put_contents($this->getSourcePath($path), $data);
$oldMask = umask(022);
$result = file_put_contents($this->getSourcePath($path), $data);
umask($oldMask);
return $result;
}

public function unlink($path) {
Expand Down Expand Up @@ -343,12 +352,18 @@ public function copy($path1, $path2) {
if ($this->is_dir($path1)) {
return parent::copy($path1, $path2);
} else {
return copy($this->getSourcePath($path1), $this->getSourcePath($path2));
$oldMask = umask(022);
$result = copy($this->getSourcePath($path1), $this->getSourcePath($path2));
umask($oldMask);
return $result;
}
}

public function fopen($path, $mode) {
return fopen($this->getSourcePath($path), $mode);
$oldMask = umask(022);
$result = fopen($this->getSourcePath($path), $mode);
umask($oldMask);
return $result;
}

public function hash($type, $path, $raw = false) {
Expand Down
37 changes: 34 additions & 3 deletions tests/lib/Files/Storage/LocalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,21 @@ public function testEtagChange() {
$this->assertNotEquals($etag1, $etag2);
}


public function testInvalidArgumentsEmptyArray() {
$this->expectException(\InvalidArgumentException::class);

new \OC\Files\Storage\Local([]);
}


public function testInvalidArgumentsNoArray() {
$this->expectException(\InvalidArgumentException::class);

new \OC\Files\Storage\Local(null);
}


public function testDisallowSymlinksOutsideDatadir() {
$this->expectException(\OCP\Files\ForbiddenException::class);

Expand Down Expand Up @@ -108,4 +108,35 @@ public function testDisallowSymlinksInsideDatadir() {
$storage->file_put_contents('sym/foo', 'bar');
$this->addToAssertionCount(1);
}

public function testWriteUmaskFilePutContents() {
$oldMask = umask(0333);
$this->instance->file_put_contents('test.txt', 'sad');
umask($oldMask);
$this->assertTrue($this->instance->isUpdatable('test.txt'));
}

public function testWriteUmaskMkdir() {
$oldMask = umask(0333);
$this->instance->mkdir('test.txt');
umask($oldMask);
$this->assertTrue($this->instance->isUpdatable('test.txt'));
}

public function testWriteUmaskFopen() {
$oldMask = umask(0333);
$handle = $this->instance->fopen('test.txt', 'w');
fwrite($handle, 'foo');
fclose($handle);
umask($oldMask);
$this->assertTrue($this->instance->isUpdatable('test.txt'));
}

public function testWriteUmaskCopy() {
$this->instance->file_put_contents('source.txt', 'sad');
$oldMask = umask(0333);
$this->instance->copy('source.txt', 'test.txt');
umask($oldMask);
$this->assertTrue($this->instance->isUpdatable('test.txt'));
}
}
4 changes: 4 additions & 0 deletions tests/lib/Preview/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ private function getPreview($provider) {
$file = new File(\OC::$server->getRootFolder(), $this->rootView, $this->imgPath);
$preview = $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp);

if (get_class($this) === BitmapTest::class && $preview === null) {
$this->markTestSkipped('An error occured while operating with Imagick.');
}

$this->assertNotEquals(false, $preview);
$this->assertEquals(true, $preview->valid());

Expand Down

0 comments on commit 9f62c80

Please sign in to comment.