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

AvirWrapper.php « lib - github.com/nextcloud/files_antivirus.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b11b2728d77116dd36864e3e4b2853d4b7760b6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
 * Copyright (c) 2014 Victor Dubiniuk <victor.dubiniuk@gmail.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace OCA\Files_Antivirus;

use OC\Files\Storage\Wrapper\Wrapper;
use OCA\Files_Antivirus\Activity\Provider;
use OCA\Files_Antivirus\AppInfo\Application;
use OCA\Files_Antivirus\Event\ScanStateEvent;
use OCA\Files_Antivirus\Scanner\ScannerFactory;
use OCP\Activity\IManager as ActivityManager;
use OCP\App;
use OCP\Files\InvalidContentException;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use OCA\Files_Trashbin\Trash\ITrashManager;

class AvirWrapper extends Wrapper {

	/**
	 * Modes that are used for writing
	 * @var array
	 */
	private $writingModes = ['r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+'];

	/** @var ScannerFactory */
	protected $scannerFactory;

	/** @var IL10N */
	protected $l10n;
	
	/** @var LoggerInterface */
	protected $logger;

	/** @var ActivityManager */
	protected $activityManager;

	/** @var bool */
	protected $isHomeStorage;

	/** @var bool */
	private $shouldScan = true;

	/**
	 * @param array $parameters
	 */
	public function __construct($parameters) {
		parent::__construct($parameters);
		$this->scannerFactory = $parameters['scannerFactory'];
		$this->l10n = $parameters['l10n'];
		$this->logger = $parameters['logger'];
		$this->activityManager = $parameters['activityManager'];
		$this->isHomeStorage = $parameters['isHomeStorage'];

		/** @var EventDispatcherInterface $eventDispatcher */
		$eventDispatcher = $parameters['eventDispatcher'];
		$eventDispatcher->addListener(ScanStateEvent::class, function (ScanStateEvent $event) {
			$this->shouldScan = $event->getState();
		});
	}

	/**
	 * Asynchronously scan data that are written to the file
	 * @param string $path
	 * @param string $mode
	 * @return resource | false
	 */
	public function fopen($path, $mode) {
		$stream = $this->storage->fopen($path, $mode);

		/*
		 * Only check when
		 *  - it is a resource
		 *  - it is a writing mode
		 *  - if it is a homestorage it starts with files/
		 *  - if it is not a homestorage we always wrap (external storages)
		 */
		if ($this->shouldWrap($path) && is_resource($stream) && $this->isWritingMode($mode)) {
			$stream = $this->wrapSteam($path, $stream);
		}
		return $stream;
	}

	public function writeStream(string $path, $stream, int $size = null): int {
		if ($this->shouldWrap($path)) {
			$stream = $this->wrapSteam($path, $stream);
		}
		return parent::writeStream($path, $stream, $size);
	}

	private function shouldWrap(string $path): bool {
		return $this->shouldScan
			&& (!$this->isHomeStorage
				|| (strpos($path, 'files/') === 0
					|| strpos($path, '/files/') === 0)
			);
	}

	private function wrapSteam(string $path, $stream) {
		try {
			$scanner = $this->scannerFactory->getScanner();
			$scanner->initScanner();
			return CallbackReadDataWrapper::wrap(
				$stream,
				function ($count, $data) use ($scanner) {
					$scanner->onAsyncData($data);
				},
				function ($data) use ($scanner) {
					$scanner->onAsyncData($data);
				},
				function () use ($scanner, $path) {
					$status = $scanner->completeAsyncScan();
					if ((int)$status->getNumericStatus() === Status::SCANRESULT_INFECTED) {
						//prevent from going to trashbin
						if (App::isEnabled('files_trashbin')) {
							/** @var ITrashManager $trashManager */
							$trashManager = \OC::$server->query(ITrashManager::class);
							$trashManager->pauseTrash();
						}

						$owner = $this->getOwner($path);
						$this->unlink($path);

						if (App::isEnabled('files_trashbin')) {
							/** @var ITrashManager $trashManager */
							$trashManager = \OC::$server->query(ITrashManager::class);
							$trashManager->resumeTrash();
						}

						$this->logger->warning(
							'Infected file deleted. ' . $status->getDetails()
							. ' Account: ' . $owner . ' Path: ' . $path,
							['app' => 'files_antivirus']
						);

						$activity = $this->activityManager->generateEvent();
						$activity->setApp(Application::APP_NAME)
							->setSubject(Provider::SUBJECT_VIRUS_DETECTED_UPLOAD, [$status->getDetails()])
							->setMessage(Provider::MESSAGE_FILE_DELETED)
							->setObject('', 0, $path)
							->setAffectedUser($owner)
							->setType(Provider::TYPE_VIRUS_DETECTED);
						$this->activityManager->publish($activity);

						$this->logger->error('Infected file deleted. ' . $status->getDetails() .
							' File: ' . $path . ' Account: ' . $owner, ['app' => 'files_antivirus']);

						throw new InvalidContentException(
							$this->l10n->t(
								'Virus %s is detected in the file. Upload cannot be completed.',
								$status->getDetails()
							)
						);
					}
				}
			);
		} catch (\Exception $e) {
			$this->logger->error($e->getMessage(), ['exception' => $e]);
		}
		return $stream;
	}

	/**
	 * Checks whether passed mode is suitable for writing
	 * @param string $mode
	 * @return bool
	 */
	private function isWritingMode($mode) {
		// Strip unessential binary/text flags
		$cleanMode = str_replace(
			['t', 'b'],
			['', ''],
			$mode
		);
		return in_array($cleanMode, $this->writingModes);
	}
}