Merge pull request #21027 from owncloud/fix-undefined-behaviour

Fix PHPDoc + Add handling for error cases
This commit is contained in:
Thomas Müller 2016-01-08 12:35:26 +01:00
commit 074a7fd475
3 changed files with 69 additions and 13 deletions

View File

@ -213,7 +213,7 @@ class KeyManager {
} }
/** /**
* @param $password * @param string $password
* @return bool * @return bool
*/ */
public function checkRecoveryPassword($password) { public function checkRecoveryPassword($password) {

View File

@ -68,10 +68,6 @@ class Recovery {
* @var IFile * @var IFile
*/ */
private $file; private $file;
/**
* @var string
*/
private $recoveryKeyId;
/** /**
* @param IUserSession $user * @param IUserSession $user
@ -102,7 +98,6 @@ class Recovery {
} }
/** /**
* @param $recoveryKeyId
* @param string $password * @param string $password
* @return bool * @return bool
*/ */
@ -112,6 +107,9 @@ class Recovery {
if (!$keyManager->recoveryKeyExists()) { if (!$keyManager->recoveryKeyExists()) {
$keyPair = $this->crypt->createKeyPair(); $keyPair = $this->crypt->createKeyPair();
if(!is_array($keyPair)) {
return false;
}
$this->keyManager->setRecoveryKey($password, $keyPair); $this->keyManager->setRecoveryKey($password, $keyPair);
} }
@ -134,6 +132,9 @@ class Recovery {
public function changeRecoveryKeyPassword($newPassword, $oldPassword) { public function changeRecoveryKeyPassword($newPassword, $oldPassword) {
$recoveryKey = $this->keyManager->getSystemPrivateKey($this->keyManager->getRecoveryKeyId()); $recoveryKey = $this->keyManager->getSystemPrivateKey($this->keyManager->getRecoveryKeyId());
$decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $oldPassword); $decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $oldPassword);
if($decryptedRecoveryKey === false) {
return false;
}
$encryptedRecoveryKey = $this->crypt->encryptPrivateKey($decryptedRecoveryKey, $newPassword); $encryptedRecoveryKey = $this->crypt->encryptPrivateKey($decryptedRecoveryKey, $newPassword);
$header = $this->crypt->generateHeader(); $header = $this->crypt->generateHeader();
if ($encryptedRecoveryKey) { if ($encryptedRecoveryKey) {
@ -264,9 +265,10 @@ class Recovery {
$encryptedKey = $this->keyManager->getSystemPrivateKey($this->keyManager->getRecoveryKeyId()); $encryptedKey = $this->keyManager->getSystemPrivateKey($this->keyManager->getRecoveryKeyId());
$privateKey = $this->crypt->decryptPrivateKey($encryptedKey, $recoveryPassword); $privateKey = $this->crypt->decryptPrivateKey($encryptedKey, $recoveryPassword);
if($privateKey !== false) {
$this->recoverAllFiles('/' . $user . '/files/', $privateKey, $user); $this->recoverAllFiles('/' . $user . '/files/', $privateKey, $user);
} }
}
/** /**
* recover users files * recover users files

View File

@ -59,14 +59,44 @@ class RecoveryTest extends TestCase {
*/ */
private $instance; private $instance;
public function testEnableAdminRecovery() { public function testEnableAdminRecoverySuccessful() {
$this->keyManagerMock->expects($this->exactly(2)) $this->keyManagerMock->expects($this->exactly(2))
->method('recoveryKeyExists') ->method('recoveryKeyExists')
->willReturnOnConsecutiveCalls(false, true); ->willReturnOnConsecutiveCalls(false, true);
$this->cryptMock->expects($this->once()) $this->cryptMock->expects($this->once())
->method('createKeyPair') ->method('createKeyPair')
->willReturn(true); ->willReturn([
'publicKey' => 'privateKey',
'privateKey' => 'publicKey',
]);
$this->keyManagerMock->expects($this->once())
->method('setRecoveryKey')
->willReturn(false);
$this->keyManagerMock->expects($this->exactly(2))
->method('checkRecoveryPassword')
->willReturnOnConsecutiveCalls(true, true);
$this->assertTrue($this->instance->enableAdminRecovery('password'));
$this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage);
$this->assertEquals(1, self::$tempStorage['recoveryAdminEnabled']);
$this->assertTrue($this->instance->enableAdminRecovery('password'));
}
public function testEnableAdminRecoveryCouldNotCheckPassword() {
$this->keyManagerMock->expects($this->exactly(2))
->method('recoveryKeyExists')
->willReturnOnConsecutiveCalls(false, true);
$this->cryptMock->expects($this->once())
->method('createKeyPair')
->willReturn([
'publicKey' => 'privateKey',
'privateKey' => 'publicKey',
]);
$this->keyManagerMock->expects($this->once()) $this->keyManagerMock->expects($this->once())
->method('setRecoveryKey') ->method('setRecoveryKey')
@ -83,7 +113,19 @@ class RecoveryTest extends TestCase {
$this->assertFalse($this->instance->enableAdminRecovery('password')); $this->assertFalse($this->instance->enableAdminRecovery('password'));
} }
public function testChangeRecoveryKeyPassword() { public function testEnableAdminRecoveryCouldNotCreateKey() {
$this->keyManagerMock->expects($this->once())
->method('recoveryKeyExists')
->willReturn(false);
$this->cryptMock->expects($this->once())
->method('createKeyPair')
->willReturn(false);
$this->assertFalse($this->instance->enableAdminRecovery('password'));
}
public function testChangeRecoveryKeyPasswordSuccessful() {
$this->assertFalse($this->instance->changeRecoveryKeyPassword('password', $this->assertFalse($this->instance->changeRecoveryKeyPassword('password',
'passwordOld')); 'passwordOld'));
@ -101,6 +143,19 @@ class RecoveryTest extends TestCase {
'passwordOld')); 'passwordOld'));
} }
public function testChangeRecoveryKeyPasswordCouldNotDecryptPrivateRecoveryKey() {
$this->assertFalse($this->instance->changeRecoveryKeyPassword('password', 'passwordOld'));
$this->keyManagerMock->expects($this->once())
->method('getSystemPrivateKey');
$this->cryptMock->expects($this->once())
->method('decryptPrivateKey')
->will($this->returnValue(false));
$this->assertFalse($this->instance->changeRecoveryKeyPassword('password', 'passwordOld'));
}
public function testDisableAdminRecovery() { public function testDisableAdminRecovery() {
$this->keyManagerMock->expects($this->exactly(2)) $this->keyManagerMock->expects($this->exactly(2))
@ -145,8 +200,7 @@ class RecoveryTest extends TestCase {
$this->cryptMock->expects($this->once()) $this->cryptMock->expects($this->once())
->method('decryptPrivateKey'); ->method('decryptPrivateKey');
$this->assertNull($this->instance->recoverUsersFiles('password', $this->assertNull($this->instance->recoverUsersFiles('password', 'admin'));
'admin'));
} }
public function testRecoverFile() { public function testRecoverFile() {