0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-05-21 05:30:55 +00:00
nextcloud_server/tests/lib/TempManagerTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

195 lines
5.7 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test;
use bantu\IniGetWrapper\IniGetWrapper;
2016-09-07 20:25:35 +02:00
use OCP\IConfig;
use Psr\Log\LoggerInterface;
class TempManagerTest extends \Test\TestCase {
protected $baseDir = null;
protected function setUp(): void {
parent::setUp();
$this->baseDir = $this->getManager()->getTempBaseDir() . $this->getUniqueID('/oc_tmp_test');
if (!is_dir($this->baseDir)) {
mkdir($this->baseDir);
}
}
protected function tearDown(): void {
if ($this->baseDir !== null) {
\OC_Helper::rmdirr($this->baseDir);
}
$this->baseDir = null;
parent::tearDown();
}
/**
* @param ?LoggerInterface $logger
* @param ?IConfig $config
* @return \OC\TempManager
*/
protected function getManager($logger = null, $config = null) {
if (!$logger) {
$logger = $this->createMock(LoggerInterface::class);
}
if (!$config) {
2016-09-07 20:25:35 +02:00
$config = $this->createMock(IConfig::class);
2015-08-29 17:42:20 +01:00
$config->method('getSystemValue')
->with('tempdirectory', null)
->willReturn('/tmp');
}
$iniGetWrapper = $this->createMock(IniGetWrapper::class);
$manager = new \OC\TempManager($logger, $config, $iniGetWrapper);
if ($this->baseDir) {
$manager->overrideTempBaseDir($this->baseDir);
}
return $manager;
}
public function testGetFile(): void {
$manager = $this->getManager();
$file = $manager->getTemporaryFile('txt');
$this->assertStringEndsWith('.txt', $file);
$this->assertTrue(is_file($file));
$this->assertTrue(is_writable($file));
file_put_contents($file, 'bar');
$this->assertEquals('bar', file_get_contents($file));
}
public function testGetFolder(): void {
$manager = $this->getManager();
$folder = $manager->getTemporaryFolder();
$this->assertStringEndsWith('/', $folder);
$this->assertTrue(is_dir($folder));
$this->assertTrue(is_writable($folder));
file_put_contents($folder . 'foo.txt', 'bar');
$this->assertEquals('bar', file_get_contents($folder . 'foo.txt'));
}
public function testCleanFiles(): void {
$manager = $this->getManager();
$file1 = $manager->getTemporaryFile('txt');
$file2 = $manager->getTemporaryFile('txt');
$this->assertTrue(file_exists($file1));
$this->assertTrue(file_exists($file2));
$manager->clean();
$this->assertFalse(file_exists($file1));
$this->assertFalse(file_exists($file2));
}
public function testCleanFolder(): void {
$manager = $this->getManager();
$folder1 = $manager->getTemporaryFolder();
$folder2 = $manager->getTemporaryFolder();
touch($folder1 . 'foo.txt');
touch($folder1 . 'bar.txt');
$this->assertTrue(file_exists($folder1));
$this->assertTrue(file_exists($folder2));
$this->assertTrue(file_exists($folder1 . 'foo.txt'));
$this->assertTrue(file_exists($folder1 . 'bar.txt'));
$manager->clean();
$this->assertFalse(file_exists($folder1));
$this->assertFalse(file_exists($folder2));
$this->assertFalse(file_exists($folder1 . 'foo.txt'));
$this->assertFalse(file_exists($folder1 . 'bar.txt'));
}
public function testCleanOld(): void {
$manager = $this->getManager();
$oldFile = $manager->getTemporaryFile('txt');
$newFile = $manager->getTemporaryFile('txt');
$folder = $manager->getTemporaryFolder();
$nonOcFile = $this->baseDir . '/foo.txt';
file_put_contents($nonOcFile, 'bar');
$past = time() - 2 * 3600;
touch($oldFile, $past);
touch($folder, $past);
touch($nonOcFile, $past);
$manager2 = $this->getManager();
$manager2->cleanOld();
$this->assertFalse(file_exists($oldFile));
$this->assertFalse(file_exists($folder));
$this->assertTrue(file_exists($nonOcFile));
$this->assertTrue(file_exists($newFile));
}
public function testLogCantCreateFile(): void {
2016-06-07 16:20:07 +02:00
$this->markTestSkipped('TODO: Disable because fails on drone');
$logger = $this->createMock(LoggerInterface::class);
$manager = $this->getManager($logger);
chmod($this->baseDir, 0500);
$logger->expects($this->once())
->method('warning')
->with($this->stringContains('Can not create a temporary file in directory'));
$this->assertFalse($manager->getTemporaryFile('txt'));
}
public function testLogCantCreateFolder(): void {
2016-06-07 16:20:07 +02:00
$this->markTestSkipped('TODO: Disable because fails on drone');
$logger = $this->createMock(LoggerInterface::class);
$manager = $this->getManager($logger);
chmod($this->baseDir, 0500);
$logger->expects($this->once())
->method('warning')
->with($this->stringContains('Can not create a temporary folder in directory'));
$this->assertFalse($manager->getTemporaryFolder());
}
public function testGenerateTemporaryPathWithPostfix(): void {
$logger = $this->createMock(LoggerInterface::class);
$tmpManager = self::invokePrivate(
$this->getManager($logger),
'generateTemporaryPath',
['postfix']
Fix collision on temporary files + adjust permissions This changeset hardens the temporary file and directory creation to address multiple problems that may lead to exposure of files to other users, data loss or other unexpected behaviour that is impossible to debug. **[CWE-668: Exposure of Resource to Wrong Sphere](https://cwe.mitre.org/data/definitions/668.html)** The temporary file and folder handling as implemented in ownCloud is performed using a MD5 hash over `time()` concatenated with `rand()`. This is insufficiently and leads to the following security problems: The generated filename could already be used by another user. It is not verified whether the file is already used and thus temporary files might be used for another user as well resulting in all possible stuff such as "user has file of other user". Effectively this leaves us with: 1. A timestamp based on seconds (no entropy at all) 2. `rand()` which returns usually a number between 0 and 2,147,483,647 Considering the birthday paradox and that we use this method quite often (especially when handling external storage) this is quite error prone and needs to get addressed. This behaviour has been fixed by using `tempnam` instead for single temporary files. For creating temporary directories an additional postfix will be appended, the solution is for directories still not absolutely bulletproof but the best I can think about at the moment. Improvement suggestions are welcome. **[CWE-378: Creation of Temporary File With Insecure Permissions](https://cwe.mitre.org/data/definitions/378.html)** Files were created using `touch()` which defaults to a permission of 0644. Thus other users on the machine may read potentially sensitive information as `/tmp/` is world-readable. However, ownCloud always encourages users to use a dedicated machine to run the ownCloud instance and thus this is no a high severe issue. Permissions have been adjusted to 0600. **[CWE-379: Creation of Temporary File in Directory with Incorrect Permissions](https://cwe.mitre.org/data/definitions/379.html)** Files were created using `mkdir()` which defaults to a permission of 0777. Thus other users on the machine may read potentially sensitive information as `/tmp/` is world-readable. However, ownCloud always encourages users to use a dedicated machine to run the ownCloud instance and thus this is no a high severe issue. Permissions have been adjusted to 0700.Please enter the commit message for your changes.
2015-04-23 14:29:15 +02:00
);
$this->assertStringEndsWith('.postfix', $tmpManager);
Fix collision on temporary files + adjust permissions This changeset hardens the temporary file and directory creation to address multiple problems that may lead to exposure of files to other users, data loss or other unexpected behaviour that is impossible to debug. **[CWE-668: Exposure of Resource to Wrong Sphere](https://cwe.mitre.org/data/definitions/668.html)** The temporary file and folder handling as implemented in ownCloud is performed using a MD5 hash over `time()` concatenated with `rand()`. This is insufficiently and leads to the following security problems: The generated filename could already be used by another user. It is not verified whether the file is already used and thus temporary files might be used for another user as well resulting in all possible stuff such as "user has file of other user". Effectively this leaves us with: 1. A timestamp based on seconds (no entropy at all) 2. `rand()` which returns usually a number between 0 and 2,147,483,647 Considering the birthday paradox and that we use this method quite often (especially when handling external storage) this is quite error prone and needs to get addressed. This behaviour has been fixed by using `tempnam` instead for single temporary files. For creating temporary directories an additional postfix will be appended, the solution is for directories still not absolutely bulletproof but the best I can think about at the moment. Improvement suggestions are welcome. **[CWE-378: Creation of Temporary File With Insecure Permissions](https://cwe.mitre.org/data/definitions/378.html)** Files were created using `touch()` which defaults to a permission of 0644. Thus other users on the machine may read potentially sensitive information as `/tmp/` is world-readable. However, ownCloud always encourages users to use a dedicated machine to run the ownCloud instance and thus this is no a high severe issue. Permissions have been adjusted to 0600. **[CWE-379: Creation of Temporary File in Directory with Incorrect Permissions](https://cwe.mitre.org/data/definitions/379.html)** Files were created using `mkdir()` which defaults to a permission of 0777. Thus other users on the machine may read potentially sensitive information as `/tmp/` is world-readable. However, ownCloud always encourages users to use a dedicated machine to run the ownCloud instance and thus this is no a high severe issue. Permissions have been adjusted to 0700.Please enter the commit message for your changes.
2015-04-23 14:29:15 +02:00
}
public function testGenerateTemporaryPathTraversal(): void {
$logger = $this->createMock(LoggerInterface::class);
$tmpManager = self::invokePrivate(
Fix collision on temporary files + adjust permissions This changeset hardens the temporary file and directory creation to address multiple problems that may lead to exposure of files to other users, data loss or other unexpected behaviour that is impossible to debug. **[CWE-668: Exposure of Resource to Wrong Sphere](https://cwe.mitre.org/data/definitions/668.html)** The temporary file and folder handling as implemented in ownCloud is performed using a MD5 hash over `time()` concatenated with `rand()`. This is insufficiently and leads to the following security problems: The generated filename could already be used by another user. It is not verified whether the file is already used and thus temporary files might be used for another user as well resulting in all possible stuff such as "user has file of other user". Effectively this leaves us with: 1. A timestamp based on seconds (no entropy at all) 2. `rand()` which returns usually a number between 0 and 2,147,483,647 Considering the birthday paradox and that we use this method quite often (especially when handling external storage) this is quite error prone and needs to get addressed. This behaviour has been fixed by using `tempnam` instead for single temporary files. For creating temporary directories an additional postfix will be appended, the solution is for directories still not absolutely bulletproof but the best I can think about at the moment. Improvement suggestions are welcome. **[CWE-378: Creation of Temporary File With Insecure Permissions](https://cwe.mitre.org/data/definitions/378.html)** Files were created using `touch()` which defaults to a permission of 0644. Thus other users on the machine may read potentially sensitive information as `/tmp/` is world-readable. However, ownCloud always encourages users to use a dedicated machine to run the ownCloud instance and thus this is no a high severe issue. Permissions have been adjusted to 0600. **[CWE-379: Creation of Temporary File in Directory with Incorrect Permissions](https://cwe.mitre.org/data/definitions/379.html)** Files were created using `mkdir()` which defaults to a permission of 0777. Thus other users on the machine may read potentially sensitive information as `/tmp/` is world-readable. However, ownCloud always encourages users to use a dedicated machine to run the ownCloud instance and thus this is no a high severe issue. Permissions have been adjusted to 0700.Please enter the commit message for your changes.
2015-04-23 14:29:15 +02:00
$this->getManager($logger),
'generateTemporaryPath',
['../Traversal\\../FileName']
);
$this->assertStringEndsNotWith('./Traversal\\../FileName', $tmpManager);
$this->assertStringEndsWith('.Traversal..FileName', $tmpManager);
}
2015-08-29 17:42:20 +01:00
public function testGetTempBaseDirFromConfig(): void {
$dir = $this->getManager()->getTemporaryFolder();
2016-09-07 20:25:35 +02:00
$config = $this->createMock(IConfig::class);
2015-08-29 17:42:20 +01:00
$config->expects($this->once())
->method('getSystemValue')
->with('tempdirectory', null)
->willReturn($dir);
$this->baseDir = null; // prevent override
$tmpManager = $this->getManager(null, $config);
$this->assertEquals($dir, $tmpManager->getTempBaseDir());
}
}