Skip to content

Commit ca88b1d

Browse files
icewind1991backportbot[bot]
authored andcommitted
Set umask before operations that create local files
this solves issues where "other php stuff" is messing with the umask Signed-off-by: Robin Appelman <[email protected]>
1 parent 6a29b60 commit ca88b1d

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

lib/private/Files/Storage/Local.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ public function getId() {
8787

8888
public function mkdir($path) {
8989
$sourcePath = $this->getSourcePath($path);
90+
$oldMask = umask(022);
9091
$result = @mkdir($sourcePath, 0777, true);
91-
chmod($sourcePath, 0755);
92+
umask($oldMask);
9293
return $result;
9394
}
9495

@@ -258,11 +259,13 @@ public function touch($path, $mtime = null) {
258259
if ($this->file_exists($path) and !$this->isUpdatable($path)) {
259260
return false;
260261
}
262+
$oldMask = umask(022);
261263
if (!is_null($mtime)) {
262264
$result = @touch($this->getSourcePath($path), $mtime);
263265
} else {
264266
$result = @touch($this->getSourcePath($path));
265267
}
268+
umask($oldMask);
266269
if ($result) {
267270
clearstatcache(true, $this->getSourcePath($path));
268271
}
@@ -275,7 +278,10 @@ public function file_get_contents($path) {
275278
}
276279

277280
public function file_put_contents($path, $data) {
278-
return file_put_contents($this->getSourcePath($path), $data);
281+
$oldMask = umask(022);
282+
$result = file_put_contents($this->getSourcePath($path), $data);
283+
umask($oldMask);
284+
return $result;
279285
}
280286

281287
public function unlink($path) {
@@ -345,12 +351,18 @@ public function copy($path1, $path2) {
345351
if ($this->is_dir($path1)) {
346352
return parent::copy($path1, $path2);
347353
} else {
348-
return copy($this->getSourcePath($path1), $this->getSourcePath($path2));
354+
$oldMask = umask(022);
355+
$result = copy($this->getSourcePath($path1), $this->getSourcePath($path2));
356+
umask($oldMask);
357+
return $result;
349358
}
350359
}
351360

352361
public function fopen($path, $mode) {
353-
return fopen($this->getSourcePath($path), $mode);
362+
$oldMask = umask(022);
363+
$result = fopen($this->getSourcePath($path), $mode);
364+
umask($oldMask);
365+
return $result;
354366
}
355367

356368
public function hash($type, $path, $raw = false) {

tests/lib/Files/Storage/LocalTest.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,21 @@ public function testEtagChange() {
6363
$this->assertNotEquals($etag1, $etag2);
6464
}
6565

66-
66+
6767
public function testInvalidArgumentsEmptyArray() {
6868
$this->expectException(\InvalidArgumentException::class);
6969

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

73-
73+
7474
public function testInvalidArgumentsNoArray() {
7575
$this->expectException(\InvalidArgumentException::class);
7676

7777
new \OC\Files\Storage\Local(null);
7878
}
7979

80-
80+
8181
public function testDisallowSymlinksOutsideDatadir() {
8282
$this->expectException(\OCP\Files\ForbiddenException::class);
8383

@@ -108,4 +108,35 @@ public function testDisallowSymlinksInsideDatadir() {
108108
$storage->file_put_contents('sym/foo', 'bar');
109109
$this->addToAssertionCount(1);
110110
}
111+
112+
public function testWriteUmaskFilePutContents() {
113+
$oldMask = umask(0333);
114+
$this->instance->file_put_contents('test.txt', 'sad');
115+
umask($oldMask);
116+
$this->assertTrue($this->instance->isUpdatable('test.txt'));
117+
}
118+
119+
public function testWriteUmaskMkdir() {
120+
$oldMask = umask(0333);
121+
$this->instance->mkdir('test.txt');
122+
umask($oldMask);
123+
$this->assertTrue($this->instance->isUpdatable('test.txt'));
124+
}
125+
126+
public function testWriteUmaskFopen() {
127+
$oldMask = umask(0333);
128+
$handle = $this->instance->fopen('test.txt', 'w');
129+
fwrite($handle, 'foo');
130+
fclose($handle);
131+
umask($oldMask);
132+
$this->assertTrue($this->instance->isUpdatable('test.txt'));
133+
}
134+
135+
public function testWriteUmaskCopy() {
136+
$this->instance->file_put_contents('source.txt', 'sad');
137+
$oldMask = umask(0333);
138+
$this->instance->copy('source.txt', 'test.txt');
139+
umask($oldMask);
140+
$this->assertTrue($this->instance->isUpdatable('test.txt'));
141+
}
111142
}

0 commit comments

Comments
 (0)