Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/undo-ransomware/ransomware_detection.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Held <ilovemilk@wusa.io>2020-12-17 20:26:13 +0300
committerMatthias Held <ilovemilk@wusa.io>2020-12-17 20:26:13 +0300
commitac8eea004dde1c960b5d500873f7f90bfd40f66c (patch)
tree57ae1a21a98d94a278dde5100276905044e2119e
parentafaef1d0c06709f8b7c74a744b384b3c11ece2c1 (diff)
add sanity checks
-rw-r--r--lib/Controller/FileOperationController.php31
-rw-r--r--tests/Unit/Controller/FileOperationControllerTest.php38
-rw-r--r--tests/Unit/Scanner/StorageStructureTest.php72
3 files changed, 48 insertions, 93 deletions
diff --git a/lib/Controller/FileOperationController.php b/lib/Controller/FileOperationController.php
index d4627c9..675c19f 100644
--- a/lib/Controller/FileOperationController.php
+++ b/lib/Controller/FileOperationController.php
@@ -60,8 +60,10 @@ class FileOperationController extends Controller
/** @var Classifier */
protected $classifier;
+ /** @var ITrashManager */
protected $trashManager;
+ /** @var IUserManager */
protected $userManager;
/** @var string */
@@ -76,6 +78,8 @@ class FileOperationController extends Controller
* @param Folder $userFolder
* @param FileOperationService $service
* @param Classifier $classifier
+ * @param ITrashManager $trashManager
+ * @param IUserManager $userManager
* @param string $userId
*/
public function __construct(
@@ -161,8 +165,8 @@ class FileOperationController extends Controller
foreach ($ids as $id) {
try {
$file = $this->service->find($id);
- if (is_null($file->getPath()) || is_null($file->getOriginalName())) {
- $this->logger->warning('recover: File path or name is null.', array('app' => Application::APP_ID));
+ if (is_null($file->getPath()) || $file->getPath() === '/' || is_null($file->getOriginalName())) {
+ $this->logger->warning('recover: File path or name is null or user root folder.', array('app' => Application::APP_ID));
return;
}
switch ($file->getCommand()) {
@@ -182,16 +186,21 @@ class FileOperationController extends Controller
// It's not necessary to use the real path
$trashItem = $this->trashManager->getTrashNodeById($this->userManager->get($this->userId), $file->getFileId());
$name = substr($trashItem->getName(), 0, strrpos($trashItem->getName(), "."));
- $path = str_replace("files_trashbin/files/", "", $trashItem->getInternalPath());
- $time = str_replace($name.".d", "", $path);
- if (Trashbin::restore($path, $name, $time) !== false) {
- $this->service->deleteById($id, true);
-
- $recovered++;
- array_push($filesRecovered, $id);
+ if (strpos($trashItem->getInternalPath(), "files_trashbin/files/") !== false) {
+ $path = str_replace("files_trashbin/files/", "", $trashItem->getInternalPath());
+ $time = str_replace($name.".d", "", $path);
+ if (Trashbin::restore($path, $name, $time) !== false) {
+ $this->service->deleteById($id, true);
+
+ $recovered++;
+ array_push($filesRecovered, $id);
+ }
+ // File does not exist
+ $badRequest = false;
+ } else {
+ $this->logger->warning('recover: File or folder is not located in the trashbin.', array('app' => Application::APP_ID));
+ return;
}
- // File does not exist
- $badRequest = false;
break;
case Monitor::RENAME:
$this->service->deleteById($id, true);
diff --git a/tests/Unit/Controller/FileOperationControllerTest.php b/tests/Unit/Controller/FileOperationControllerTest.php
index 06a29e7..ce8184a 100644
--- a/tests/Unit/Controller/FileOperationControllerTest.php
+++ b/tests/Unit/Controller/FileOperationControllerTest.php
@@ -28,8 +28,10 @@ use OCA\RansomwareDetection\Db\FileOperation;
use OCA\RansomwareDetection\Controller\FileOperationController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
+use OCA\Files_Trashbin\Trash\ITrashManager;
use OCP\Files\File;
use OCP\Files\Folder;
+use OCP\IUserManager;
use Test\TestCase;
class FileOperationControllerTest extends TestCase
@@ -37,6 +39,12 @@ class FileOperationControllerTest extends TestCase
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
protected $request;
+ /** @var ITrashManager|\PHPUnit_Framework_MockObject_MockObject */
+ protected $trashManager;
+
+ /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
+ protected $userManager;
+
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
protected $userSession;
@@ -75,6 +83,10 @@ class FileOperationControllerTest extends TestCase
->getMock();
$this->folder = $this->getMockBuilder('OCP\Files\Folder')
->getMock();
+ $this->trashManager = $this->getMockBuilder('OCA\Files_Trashbin\Trash\ITrashManager')
+ ->getMock();
+ $this->userManager = $this->getMockBuilder('OCP\IUserManager')
+ ->getMock();
$connection = $this->getMockBuilder('OCP\IDBConnection')
->getMock();
$mapper = $this->getMockBuilder('OCA\RansomwareDetection\Db\FileOperationMapper')
@@ -103,6 +115,8 @@ class FileOperationControllerTest extends TestCase
$this->folder,
$this->service,
$this->classifier,
+ $this->trashManager,
+ $this->userManager,
'john'
);
$file = $this->getMockBuilder(FileOperation::class)
@@ -175,15 +189,13 @@ class FileOperationControllerTest extends TestCase
$this->folder,
$this->service,
$this->classifier,
+ $this->trashManager,
+ $this->userManager,
'john'
])
- ->setMethods(['deleteFromStorage', 'getTrashFiles'])
+ ->setMethods(['deleteFromStorage'])
->getMock();
- $controller->expects($this->any())
- ->method('getTrashFiles')
- ->willReturn([]);
-
$this->service->method('find')
->willReturn($fileOperation);
@@ -210,9 +222,11 @@ class FileOperationControllerTest extends TestCase
$this->folder,
$this->service,
$this->classifier,
+ $this->trashManager,
+ $this->userManager,
'john'
])
- ->setMethods(['getTrashFiles'])
+ ->setMethods([])
->getMock();
$fileOperationWrite = new FileOperation();
@@ -220,10 +234,6 @@ class FileOperationControllerTest extends TestCase
$fileOperationWrite->setPath('/admin/files');
$fileOperationWrite->setOriginalName('test.jpg');
- $controller->expects($this->any())
- ->method('getTrashFiles')
- ->willReturn([]);
-
$this->service->method('find')
->will($this->throwException(new \OCP\AppFramework\Db\MultipleObjectsReturnedException('test')));
@@ -243,6 +253,8 @@ class FileOperationControllerTest extends TestCase
$this->folder,
$this->service,
$this->classifier,
+ $this->trashManager,
+ $this->userManager,
'john'
);
@@ -270,6 +282,8 @@ class FileOperationControllerTest extends TestCase
$this->folder,
$this->service,
$this->classifier,
+ $this->trashManager,
+ $this->userManager,
'john'
);
$file = $this->createMock(File::class);
@@ -295,6 +309,8 @@ class FileOperationControllerTest extends TestCase
$this->folder,
$this->service,
$this->classifier,
+ $this->trashManager,
+ $this->userManager,
'john'
);
$file = $this->createMock(File::class);
@@ -321,6 +337,8 @@ class FileOperationControllerTest extends TestCase
$this->folder,
$this->service,
$this->classifier,
+ $this->trashManager,
+ $this->userManager,
'john'
);
$folder = $this->createMock(Folder::class);
diff --git a/tests/Unit/Scanner/StorageStructureTest.php b/tests/Unit/Scanner/StorageStructureTest.php
deleted file mode 100644
index 8139647..0000000
--- a/tests/Unit/Scanner/StorageStructureTest.php
+++ /dev/null
@@ -1,72 +0,0 @@
-<?php
-
-/**
- * @copyright Copyright (c) 2018 Matthias Held <matthias.held@uni-konstanz.de>
- * @author Matthias Held <matthias.held@uni-konstanz.de>
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-
-namespace OCA\RansomwareDetection\tests\Unit\Scanner;
-
-use OCA\RansomwareDetection\Scanner\StorageStructure;
-use Test\TestCase;
-
-class StorageStructureTest extends TestCase
-{
- /** @var StorageStructure */
- protected $storageStructure;
-
- public function setUp(): void
- {
- parent::setUp();
-
- $this->storageStructure = new StorageStructure();
- }
-
- public function testDefaultParameters() {
- $this->assertEquals($this->storageStructure->getNumberOfFiles(), 0);
- $this->assertEquals($this->storageStructure->getFiles(), []);
- }
-
- public function testGetNumberOfFiles() {
- $this->assertEquals($this->storageStructure->getNumberOfFiles(), 0);
- }
-
- public function testSetNumberOfFiles() {
- $this->storageStructure->setNumberOfFiles(10);
- $this->assertEquals($this->storageStructure->getNumberOfFiles(), 10);
- }
-
- public function testIncreaseNumberOfFiles() {
- $this->storageStructure->increaseNumberOfFiles();
- $this->assertEquals($this->storageStructure->getNumberOfFiles(), 1);
- }
-
- public function testGetFiles() {
- $this->assertEquals($this->storageStructure->getFiles(), []);
- }
-
- public function testSetFiles() {
- $this->storageStructure->setFiles([10]);
- $this->assertEquals($this->storageStructure->getFiles(), [10]);
- }
-
- public function testAddFiles() {
- $this->storageStructure->addFile(11);
- $this->storageStructure->addFile(10);
- $this->assertEquals($this->storageStructure->getFiles(), [11, 10]);
- }
-}