2014-08-27 14:28:51 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 07:02:58 +00:00
|
|
|
namespace Test\Security;
|
|
|
|
|
2017-01-09 13:26:31 +00:00
|
|
|
use OC\Files\Storage\Temporary;
|
2017-10-26 11:46:16 +00:00
|
|
|
use OC\Files\View;
|
2014-08-27 14:28:51 +00:00
|
|
|
use \OC\Security\CertificateManager;
|
2016-09-12 19:38:07 +00:00
|
|
|
use OCP\IConfig;
|
2017-01-09 13:26:31 +00:00
|
|
|
use OCP\ILogger;
|
2017-05-19 20:51:26 +00:00
|
|
|
use OCP\Security\ISecureRandom;
|
2014-08-27 14:28:51 +00:00
|
|
|
|
2015-11-03 00:52:41 +00:00
|
|
|
/**
|
|
|
|
* Class CertificateManagerTest
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*/
|
2014-11-07 08:46:37 +00:00
|
|
|
class CertificateManagerTest extends \Test\TestCase {
|
2015-12-22 16:42:28 +00:00
|
|
|
use \Test\Traits\UserTrait;
|
|
|
|
use \Test\Traits\MountProviderTrait;
|
2014-08-27 14:28:51 +00:00
|
|
|
|
|
|
|
/** @var CertificateManager */
|
|
|
|
private $certificateManager;
|
|
|
|
/** @var String */
|
|
|
|
private $username;
|
2017-05-19 20:51:26 +00:00
|
|
|
/** @var ISecureRandom */
|
|
|
|
private $random;
|
2014-08-27 14:28:51 +00:00
|
|
|
|
2014-11-10 22:30:38 +00:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2014-11-07 08:46:37 +00:00
|
|
|
$this->username = $this->getUniqueID('', 20);
|
2015-12-22 16:42:28 +00:00
|
|
|
$this->createUser($this->username, '');
|
|
|
|
|
|
|
|
$storage = new \OC\Files\Storage\Temporary();
|
|
|
|
$this->registerMount($this->username, $storage, '/' . $this->username . '/');
|
2014-08-27 14:28:51 +00:00
|
|
|
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_User::setUserId('');
|
|
|
|
\OC\Files\Filesystem::tearDown();
|
|
|
|
\OC_Util::setupFS($this->username);
|
|
|
|
|
2016-09-12 19:38:07 +00:00
|
|
|
$config = $this->createMock(IConfig::class);
|
2015-08-06 14:51:31 +00:00
|
|
|
$config->expects($this->any())->method('getSystemValue')
|
|
|
|
->with('installed', false)->willReturn(true);
|
|
|
|
|
2017-05-19 20:51:26 +00:00
|
|
|
$this->random = $this->createMock(ISecureRandom::class);
|
|
|
|
$this->random->method('generate')
|
|
|
|
->willReturn('random');
|
|
|
|
|
|
|
|
$this->certificateManager = new CertificateManager(
|
|
|
|
$this->username,
|
|
|
|
new \OC\Files\View(),
|
|
|
|
$config,
|
|
|
|
$this->createMock(ILogger::class),
|
|
|
|
$this->random
|
|
|
|
);
|
2014-08-27 14:28:51 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 22:30:38 +00:00
|
|
|
protected function tearDown() {
|
2015-12-17 14:59:23 +00:00
|
|
|
$user = \OC::$server->getUserManager()->get($this->username);
|
2015-12-22 16:42:28 +00:00
|
|
|
if ($user !== null) {
|
|
|
|
$user->delete();
|
|
|
|
}
|
2014-11-10 22:30:38 +00:00
|
|
|
parent::tearDown();
|
2014-08-27 14:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function assertEqualsArrays($expected, $actual) {
|
|
|
|
sort($expected);
|
|
|
|
sort($actual);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testListCertificates() {
|
|
|
|
// Test empty certificate bundle
|
|
|
|
$this->assertSame(array(), $this->certificateManager->listCertificates());
|
|
|
|
|
|
|
|
// Add some certificates
|
2015-12-22 16:42:28 +00:00
|
|
|
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
|
2014-08-27 14:28:51 +00:00
|
|
|
$certificateStore = array();
|
2015-12-22 16:42:28 +00:00
|
|
|
$certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
|
2014-08-27 14:28:51 +00:00
|
|
|
$this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
|
|
|
|
|
|
|
|
// Add another certificates
|
2015-12-22 16:42:28 +00:00
|
|
|
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
|
|
|
|
$certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
|
2014-08-27 14:28:51 +00:00
|
|
|
$this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage Certificate could not get parsed.
|
|
|
|
*/
|
|
|
|
function testAddInvalidCertificate() {
|
|
|
|
$this->certificateManager->addCertificate('InvalidCertificate', 'invalidCertificate');
|
|
|
|
}
|
|
|
|
|
2015-04-16 08:48:25 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dangerousFileProvider() {
|
|
|
|
return [
|
|
|
|
['.htaccess'],
|
|
|
|
['../../foo.txt'],
|
|
|
|
['..\..\foo.txt'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:20:05 +00:00
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage Filename is not valid
|
2015-04-16 08:48:25 +00:00
|
|
|
* @dataProvider dangerousFileProvider
|
|
|
|
* @param string $filename
|
2015-04-15 14:20:05 +00:00
|
|
|
*/
|
2015-04-16 08:48:25 +00:00
|
|
|
function testAddDangerousFile($filename) {
|
2015-12-22 16:42:28 +00:00
|
|
|
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt'), $filename);
|
2014-08-27 14:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testRemoveDangerousFile() {
|
|
|
|
$this->assertFalse($this->certificateManager->removeCertificate('../../foo.txt'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function testRemoveExistingFile() {
|
2015-12-22 16:42:28 +00:00
|
|
|
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
|
2014-08-27 14:28:51 +00:00
|
|
|
$this->assertTrue($this->certificateManager->removeCertificate('GoodCertificate'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function testGetCertificateBundle() {
|
2015-01-20 16:00:29 +00:00
|
|
|
$this->assertSame('/' . $this->username . '/files_external/rootcerts.crt', $this->certificateManager->getCertificateBundle());
|
2014-08-27 14:28:51 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 13:51:48 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestNeedRebundling
|
|
|
|
*
|
|
|
|
* @param string $uid
|
|
|
|
* @param int $CaBundleMtime
|
|
|
|
* @param int $systemWideMtime
|
|
|
|
* @param int $targetBundleMtime
|
|
|
|
* @param int $targetBundleExists
|
|
|
|
* @param bool $expected
|
|
|
|
*/
|
|
|
|
function testNeedRebundling($uid,
|
|
|
|
$CaBundleMtime,
|
|
|
|
$systemWideMtime,
|
|
|
|
$targetBundleMtime,
|
|
|
|
$targetBundleExists,
|
|
|
|
$expected
|
|
|
|
) {
|
|
|
|
|
2017-10-26 11:46:16 +00:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-11 13:51:48 +00:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-09-12 19:38:07 +00:00
|
|
|
$config = $this->createMock(IConfig::class);
|
2016-07-11 13:51:48 +00:00
|
|
|
|
|
|
|
/** @var CertificateManager | \PHPUnit_Framework_MockObject_MockObject $certificateManager */
|
|
|
|
$certificateManager = $this->getMockBuilder('OC\Security\CertificateManager')
|
2017-05-19 20:51:26 +00:00
|
|
|
->setConstructorArgs([$uid, $view, $config, $this->createMock(ILogger::class), $this->random])
|
2016-07-11 13:51:48 +00:00
|
|
|
->setMethods(['getFilemtimeOfCaBundle', 'getCertificateBundle'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$certificateManager->expects($this->any())->method('getFilemtimeOfCaBundle')
|
|
|
|
->willReturn($CaBundleMtime);
|
|
|
|
|
|
|
|
$certificateManager->expects($this->at(1))->method('getCertificateBundle')
|
|
|
|
->with($uid)->willReturn('targetBundlePath');
|
|
|
|
|
|
|
|
$view->expects($this->any())->method('file_exists')
|
|
|
|
->with('targetBundlePath')
|
|
|
|
->willReturn($targetBundleExists);
|
|
|
|
|
|
|
|
|
|
|
|
if ($uid !== null && $targetBundleExists) {
|
|
|
|
$certificateManager->expects($this->at(2))->method('getCertificateBundle')
|
|
|
|
->with(null)->willReturn('SystemBundlePath');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$view->expects($this->any())->method('filemtime')
|
|
|
|
->willReturnCallback(function($path) use ($systemWideMtime, $targetBundleMtime) {
|
|
|
|
if ($path === 'SystemBundlePath') {
|
|
|
|
return $systemWideMtime;
|
|
|
|
} elseif ($path === 'targetBundlePath') {
|
|
|
|
return $targetBundleMtime;
|
|
|
|
}
|
|
|
|
throw new \Exception('unexpected path');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertSame($expected,
|
|
|
|
$this->invokePrivate($certificateManager, 'needsRebundling', [$uid])
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataTestNeedRebundling() {
|
|
|
|
return [
|
|
|
|
//values: uid, CaBundleMtime, systemWideMtime, targetBundleMtime, targetBundleExists, expected
|
|
|
|
|
|
|
|
// compare minimum of CaBundleMtime and systemWideMtime with targetBundleMtime
|
|
|
|
['user1', 10, 20, 30, true, false],
|
|
|
|
['user1', 10, 20, 15, true, true],
|
|
|
|
['user1', 10, 5, 30, true, false],
|
|
|
|
['user1', 10, 5, 8, true, true],
|
|
|
|
|
|
|
|
// if no user exists we ignore 'systemWideMtime' because this is the bundle we re-build
|
|
|
|
[null, 10, 20, 30, true, false],
|
|
|
|
[null, 10, 20, 15, true, false],
|
|
|
|
[null, 10, 20, 8, true, true],
|
|
|
|
[null, 10, 5, 30, true, false],
|
|
|
|
[null, 10, 5, 8, true, true],
|
|
|
|
|
|
|
|
// if no target bundle exists we always build a new one
|
|
|
|
['user1', 10, 20, 30, false, true],
|
|
|
|
['user1', 10, 20, 15, false, true],
|
|
|
|
['user1', 10, 5, 30, false, true],
|
|
|
|
['user1', 10, 5, 8, false, true],
|
|
|
|
[null, 10, 20, 30, false, true],
|
|
|
|
[null, 10, 20, 15, false, true],
|
|
|
|
[null, 10, 20, 8, false, true],
|
|
|
|
[null, 10, 5, 30, false, true],
|
|
|
|
[null, 10, 5, 8, false, true],
|
|
|
|
];
|
|
|
|
}
|
2015-01-20 19:34:34 +00:00
|
|
|
}
|