mirror of
https://github.com/nextcloud/server.git
synced 2026-04-15 22:11:17 -04:00
Use database for keeping track of the version
This commit is contained in:
parent
3badf5caf5
commit
5ccb9dfa7e
6 changed files with 58 additions and 9 deletions
|
|
@ -55,6 +55,9 @@ class Encryption implements IEncryptionModule {
|
|||
/** @var string */
|
||||
private $path;
|
||||
|
||||
/** @var string */
|
||||
private $realPath;
|
||||
|
||||
/** @var string */
|
||||
private $user;
|
||||
|
||||
|
|
@ -167,6 +170,7 @@ class Encryption implements IEncryptionModule {
|
|||
*/
|
||||
public function begin($path, $user, $mode, array $header, array $accessList) {
|
||||
$this->path = $this->getPathToRealFile($path);
|
||||
$this->realPath = $this->path;
|
||||
$this->accessList = $accessList;
|
||||
$this->user = $user;
|
||||
$this->isWriteOperation = false;
|
||||
|
|
@ -182,7 +186,7 @@ class Encryption implements IEncryptionModule {
|
|||
$this->fileKey = $this->keyManager->getFileKey($this->path, $this->user);
|
||||
}
|
||||
|
||||
$this->version = (int)$this->keyManager->getVersion($this->path);
|
||||
$this->version = (int)$this->keyManager->getVersion($this->realPath);
|
||||
|
||||
if (
|
||||
$mode === 'w'
|
||||
|
|
@ -360,7 +364,10 @@ class Encryption implements IEncryptionModule {
|
|||
*/
|
||||
public function update($path, $uid, array $accessList) {
|
||||
$fileKey = $this->keyManager->getFileKey($path, $uid);
|
||||
$version = $this->keyManager->getVersion($path);
|
||||
if(empty($this->realPath)) {
|
||||
$this->realPath = $path;
|
||||
}
|
||||
$version = $this->keyManager->getVersion($this->realPath);
|
||||
|
||||
if (!empty($fileKey)) {
|
||||
|
||||
|
|
|
|||
|
|
@ -25,12 +25,14 @@
|
|||
namespace OCA\Encryption;
|
||||
|
||||
use OC\Encryption\Exceptions\DecryptionFailedException;
|
||||
use OC\Files\View;
|
||||
use OCA\Encryption\Crypto\Encryption;
|
||||
use OCA\Encryption\Exceptions\PrivateKeyMissingException;
|
||||
use OCA\Encryption\Exceptions\PublicKeyMissingException;
|
||||
use OCA\Encryption\Crypto\Crypt;
|
||||
use OCP\Encryption\Keys\IStorage;
|
||||
use OCP\IConfig;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\ILogger;
|
||||
use OCP\IUserSession;
|
||||
|
||||
|
|
@ -416,18 +418,35 @@ class KeyManager {
|
|||
* Get the current version of a file
|
||||
*
|
||||
* @param string $path
|
||||
* @return mixed
|
||||
* @return int
|
||||
*/
|
||||
public function getVersion($path) {
|
||||
return $this->keyStorage->getFileKey($path, 'version', Encryption::ID);
|
||||
$view = new \OC\Files\View();
|
||||
$fileInfo = $view->getFileInfo($path);
|
||||
if($fileInfo === false) {
|
||||
return 0;
|
||||
}
|
||||
return $fileInfo->getEncryptedVersion();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current version of a file
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $version
|
||||
*/
|
||||
public function setVersion($path, $version) {
|
||||
$this->keyStorage->setFileKey($path, 'version', $version, Encryption::ID);
|
||||
$view = new \OC\Files\View();
|
||||
$fileInfo= $view->getFileInfo($path);
|
||||
|
||||
if($fileInfo !== false) {
|
||||
$fileId = $fileInfo->getId();
|
||||
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
|
||||
$qb->update('filecache')
|
||||
->set('encrypted', $qb->createNamedParameter($version))
|
||||
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($fileId)))
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -165,7 +165,15 @@ class Storage {
|
|||
$mtime = $users_view->filemtime('files/' . $filename);
|
||||
$users_view->copy('files/' . $filename, 'files_versions/' . $filename . '.v' . $mtime);
|
||||
// call getFileInfo to enforce a file cache entry for the new version
|
||||
$users_view->getFileInfo('files_versions/' . $filename . '.v' . $mtime);
|
||||
$newFileInfo = $users_view->getFileInfo('files_versions/' . $filename . '.v' . $mtime);
|
||||
|
||||
// Keep the "encrypted" value of the original file
|
||||
$oldVersion = $files_view->getFileInfo($filename)->getEncryptedVersion();
|
||||
$qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
|
||||
$qb->update('filecache')
|
||||
->set('encrypted', $qb->createNamedParameter($oldVersion))
|
||||
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($newFileInfo->getId())))
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
9
lib/private/files/cache/cache.php
vendored
9
lib/private/files/cache/cache.php
vendored
|
|
@ -145,6 +145,7 @@ class Cache implements ICache {
|
|||
$data['size'] = 0 + $data['size'];
|
||||
$data['mtime'] = (int)$data['mtime'];
|
||||
$data['storage_mtime'] = (int)$data['storage_mtime'];
|
||||
$data['encryptedVersion'] = (int)$data['encrypted'];
|
||||
$data['encrypted'] = (bool)$data['encrypted'];
|
||||
$data['storage'] = $this->storageId;
|
||||
$data['mimetype'] = $this->mimetypeLoader->getMimetypeById($data['mimetype']);
|
||||
|
|
@ -345,8 +346,12 @@ class Cache implements ICache {
|
|||
$queryParts[] = '`mtime`';
|
||||
}
|
||||
} elseif ($name === 'encrypted') {
|
||||
// Boolean to integer conversion
|
||||
$value = $value ? 1 : 0;
|
||||
if(isset($data['encryptedVersion'])) {
|
||||
$value = $data['encryptedVersion'];
|
||||
} else {
|
||||
// Boolean to integer conversion
|
||||
$value = $value ? 1 : 0;
|
||||
}
|
||||
}
|
||||
$params[] = $value;
|
||||
$queryParts[] = '`' . $name . '`';
|
||||
|
|
|
|||
|
|
@ -193,6 +193,15 @@ class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
|
|||
return $this->data['encrypted'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the currently version used for the HMAC in the encryption app
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getEncryptedVersion() {
|
||||
return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -131,11 +131,12 @@ class Encryption extends Wrapper {
|
|||
// update file cache
|
||||
if ($info) {
|
||||
$info = $info->getData();
|
||||
$info['encrypted'] = $info['encryptedVersion'];
|
||||
} else {
|
||||
$info = [];
|
||||
$info['encrypted'] = true;
|
||||
}
|
||||
|
||||
$info['encrypted'] = true;
|
||||
$info['size'] = $size;
|
||||
$this->getCache()->put($path, $info);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue