Issues (78)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

lib/AvirWrapper.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Copyright (c) 2014 Victor Dubiniuk <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
namespace OCA\Files_Antivirus;
10
11
use OC\Files\Storage\Wrapper\Wrapper;
12
use OCA\Files_Antivirus\Activity\Provider;
13
use OCA\Files_Antivirus\AppInfo\Application;
14
use OCA\Files_Antivirus\Event\ScanStateEvent;
15
use OCA\Files_Antivirus\Scanner\ScannerFactory;
16
use OCP\Activity\IManager as ActivityManager;
17
use OCP\App;
18
use OCP\Files\InvalidContentException;
19
use OCP\IL10N;
20
use OCP\ILogger;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
use OCA\Files_Trashbin\Trash\ITrashManager;
23
24
class AvirWrapper extends Wrapper {
25
	
26
	/**
27
	 * Modes that are used for writing
28
	 * @var array
29
	 */
30
	private $writingModes = ['r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+'];
31
	
32
	/** @var ScannerFactory */
33
	protected $scannerFactory;
34
	
35
	/** @var IL10N */
36
	protected $l10n;
37
	
38
	/** @var ILogger */
39
	protected $logger;
40
41
	/** @var ActivityManager */
42
	protected $activityManager;
43
44
	/** @var bool */
45
	protected $isHomeStorage;
46
47
	/** @var bool */
48
	private $shouldScan = true;
49
50
	/**
51
	 * @param array $parameters
52
	 */
53 2
	public function __construct($parameters) {
54 2
		parent::__construct($parameters);
55 2
		$this->scannerFactory = $parameters['scannerFactory'];
56 2
		$this->l10n = $parameters['l10n'];
57 2
		$this->logger = $parameters['logger'];
58 2
		$this->activityManager = $parameters['activityManager'];
59 2
		$this->isHomeStorage = $parameters['isHomeStorage'];
60
61
		/** @var EventDispatcherInterface $eventDispatcher */
62 2
		$eventDispatcher = $parameters['eventDispatcher'];
63
		$eventDispatcher->addListener(ScanStateEvent::class, function (ScanStateEvent $event) {
64
			$this->shouldScan = $event->getState();
65 2
		});
66 2
	}
67
	
68
	/**
69
	 * Asynchronously scan data that are written to the file
70
	 * @param string $path
71
	 * @param string $mode
72
	 * @return resource | bool
73
	 */
74 1
	public function fopen($path, $mode) {
75 1
		$stream = $this->storage->fopen($path, $mode);
76
77
		/*
78
		 * Only check when
79
		 *  - it is a resource
80
		 *  - it is a writing mode
81
		 *  - if it is a homestorage it starts with files/
82
		 *  - if it is not a homestorage we always wrap (external storages)
83
		 */
84 1
		if ($this->shouldWrap($path) && is_resource($stream) && $this->isWritingMode($mode)) {
85 1
			$stream = $this->wrapSteam($path, $stream);
86
		}
87 1
		return $stream;
88
	}
89
90
	public function writeStream(string $path, $stream, int $size = null): int {
91
		if ($this->shouldWrap($path)) {
92
			$stream = $this->wrapSteam($path, $stream);
93
		}
94
		return parent::writeStream($path, $stream, $size);
95
	}
96
97 1
	private function shouldWrap(string $path): bool {
98 1
		return $this->shouldScan
99 1
			&& (!$this->isHomeStorage
100 1
				|| (strpos($path, 'files/') === 0
101 1
					|| strpos($path, '/files/') == 0)
102
			);
103
	}
104
105 1
	private function wrapSteam(string $path, $stream) {
106
		try {
107 1
			$scanner = $this->scannerFactory->getScanner();
108
			$scanner->initScanner();
109
			return CallbackReadDataWrapper::wrap(
110
				$stream,
111
				function ($count, $data) use ($scanner) {
112
					$scanner->onAsyncData($data);
113
				},
114
				function ($data) use ($scanner) {
115
					$scanner->onAsyncData($data);
116
				},
117
				function () use ($scanner, $path) {
118
					$status = $scanner->completeAsyncScan();
119
					if ((int)$status->getNumericStatus() === Status::SCANRESULT_INFECTED) {
120
						//prevent from going to trashbin
121
						if (App::isEnabled('files_trashbin')) {
0 ignored issues
show
Deprecated Code introduced by
The method OCP\App::isEnabled() has been deprecated with message: 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
122
							/** @var ITrashManager $trashManager */
123
							$trashManager = \OC::$server->query(ITrashManager::class);
124
							$trashManager->pauseTrash();
125
						}
126
127
						$owner = $this->getOwner($path);
128
						$this->unlink($path);
129
130
						if (App::isEnabled('files_trashbin')) {
0 ignored issues
show
Deprecated Code introduced by
The method OCP\App::isEnabled() has been deprecated with message: 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
131
							/** @var ITrashManager $trashManager */
132
							$trashManager = \OC::$server->query(ITrashManager::class);
133
							$trashManager->resumeTrash();
134
						}
135
							
136
						$this->logger->warning(
0 ignored issues
show
Deprecated Code introduced by
The method OCP\ILogger::warning() has been deprecated with message: 20.0.0 use \Psr\Log\LoggerInterface::warning

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
137
							'Infected file deleted. ' . $status->getDetails()
138
							. ' Account: ' . $owner . ' Path: ' . $path,
139
							['app' => 'files_antivirus']
140
						);
141
142
						$activity = $this->activityManager->generateEvent();
143
						$activity->setApp(Application::APP_NAME)
144
							->setSubject(Provider::SUBJECT_VIRUS_DETECTED_UPLOAD, [$status->getDetails()])
145
							->setMessage(Provider::MESSAGE_FILE_DELETED)
146
							->setObject('', 0, $path)
147
							->setAffectedUser($owner)
148
							->setType(Provider::TYPE_VIRUS_DETECTED);
149
						$this->activityManager->publish($activity);
150
151
						$this->logger->error('Infected file deleted. ' . $status->getDetails() .
0 ignored issues
show
Deprecated Code introduced by
The method OCP\ILogger::error() has been deprecated with message: 20.0.0 use \Psr\Log\LoggerInterface::error

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
152
							' File: ' . $path . ' Account: ' . $owner, ['app' => 'files_antivirus']);
153
154
						throw new InvalidContentException(
155
							$this->l10n->t(
156
								'Virus %s is detected in the file. Upload cannot be completed.',
157
								$status->getDetails()
158
							)
159
						);
160
					}
161
				}
162
			);
163 1
		} catch (\Exception $e) {
164 1
			$this->logger->logException($e);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\ILogger::logException() has been deprecated with message: 20.0.0 use the `exception` entry in the context of any method in \Psr\Log\LoggerInterface

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
$e is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165
		}
166 1
		return $stream;
167
	}
168
	
169
	/**
170
	 * Checks whether passed mode is suitable for writing
171
	 * @param string $mode
172
	 * @return bool
173
	 */
174 1
	private function isWritingMode($mode) {
175
		// Strip unessential binary/text flags
176 1
		$cleanMode = str_replace(
177 1
			['t', 'b'],
178 1
			['', ''],
179
			$mode
180
		);
181 1
		return in_array($cleanMode, $this->writingModes);
182
	}
183
}
184