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/Item.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) 2015 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 OCA\Files_Antivirus\Activity\Provider;
12
use OCA\Files_Antivirus\AppInfo\Application;
13
use OCA\Files_Antivirus\Db\ItemMapper;
14
use OCP\Activity\IManager as ActivityManager;
15
use OCP\App;
16
use OCP\AppFramework\Db\DoesNotExistException;
17
use OCP\Files\File;
18
use OCP\Files\IRootFolder;
19
use OCA\Files_Trashbin\Trash\ITrashManager;
20
use OCP\ILogger;
21
22
class Item {
23
	/**
24
	 * file handle, user to read from the file
25
	 * @var resource
26
	 */
27
	protected $fileHandle;
28
29
	/** @var AppConfig */
30
	private $config;
31
32
	/** @var ActivityManager */
33
	private $activityManager;
34
35
	/** @var ItemMapper */
36
	private $itemMapper;
37
38
	/** @var ILogger */
39
	private $logger;
40
41
	/** @var IRootFolder */
42
	private $rootFolder;
43
44
	/** @var File */
45
	private $file;
46
	private $isCron;
47
48
	/**
49
	 * Item constructor.
50
	 *
51
	 * @param AppConfig $appConfig
52
	 * @param ActivityManager $activityManager
53
	 * @param ItemMapper $itemMapper
54
	 * @param ILogger $logger
55
	 * @param IRootFolder $rootFolder
56
	 * @param File $file
57
	 * @param bool $isCron
58
	 */
59
	public function __construct(AppConfig $appConfig,
60
								ActivityManager $activityManager,
61
								ItemMapper $itemMapper,
62
								ILogger $logger,
63
								IRootFolder $rootFolder,
64
								File $file,
65
								$isCron) {
66
		$this->config = $appConfig;
67
		$this->activityManager = $activityManager;
68
		$this->itemMapper = $itemMapper;
69
		$this->logger = $logger;
70
		$this->rootFolder = $rootFolder;
71
		$this->file = $file;
72
		$this->isCron = $isCron;
73
	}
74
75
	/**
76
	 * Reads a file portion by portion until the very end
77
	 * @return string|boolean
78
	 */
79
	public function fread() {
80
		if (!($this->file->getSize() > 0)) {
81
			return false;
82
		}
83
84
		if (is_null($this->fileHandle)) {
85
			$this->getFileHandle();
86
		}
87
88
		if (!is_null($this->fileHandle) && !$this->feof()) {
89
			return fread($this->fileHandle, $this->config->getAvChunkSize());
90
		}
91
		return false;
92
	}
93
94
	/**
95
	 * Action to take if this item is infected
96
	 */
97
	public function processInfected(Status $status) {
98
		$infectedAction = $this->config->getAvInfectedAction();
99
100
		$shouldDelete = $infectedAction === 'delete';
101
102
		$message = $shouldDelete ? Provider::MESSAGE_FILE_DELETED : '';
103
104
		$userFolder = $this->rootFolder->getUserFolder($this->file->getOwner()->getUID());
105
		$path = $userFolder->getRelativePath($this->file->getPath());
106
107
		$activity = $this->activityManager->generateEvent();
108
		$activity->setApp(Application::APP_NAME)
109
			->setSubject(Provider::SUBJECT_VIRUS_DETECTED_SCAN, [$status->getDetails()])
110
			->setMessage($message)
111
			->setObject('file', $this->file->getId(), $path)
112
			->setAffectedUser($this->file->getOwner()->getUID())
113
			->setType(Provider::TYPE_VIRUS_DETECTED);
114
		$this->activityManager->publish($activity);
115
116
		if ($shouldDelete) {
117
			if ($this->isCron) {
118
				$msg = 'Infected file deleted (during background scan)';
119
			} else {
120
				$msg = 'Infected file deleted.';
121
			}
122
			$this->logError($msg . ' ' . $status->getDetails());
123
			$this->deleteFile();
124
		} else {
125
			if ($this->isCron) {
126
				$msg = 'Infected file found (during background scan)';
127
			} else {
128
				$msg = 'Infected file found.';
129
			}
130
			$this->logError($msg . ' ' . $status->getDetails());
131
			$this->updateCheckTime();
132
		}
133
	}
134
135
	/**
136
	 * Action to take if this item status is unclear
137
	 * @param Status $status
138
	 */
139
	public function processUnchecked(Status $status) {
140
		//TODO: Show warning to the user: The file can not be checked
141
		$this->logError('Not Checked. ' . $status->getDetails());
142
	}
143
144
	/**
145
	 * Action to take if this item status is not infected
146
	 */
147
	public function processClean() {
148
		$this->updateCheckTime();
149
	}
150
151
	/**
152
	 * Update the check-time of this item to current time
153
	 */
154
	private function updateCheckTime() {
155
		try {
156
			try {
157
				$item = $this->itemMapper->findByFileId($this->file->getId());
158
				$this->itemMapper->delete($item);
159
			} catch (DoesNotExistException $e) {
160
				//Just ignore
161
			}
162
163
			$item = new \OCA\Files_Antivirus\Db\Item();
164
			$item->setFileid($this->file->getId());
165
			$item->setCheckTime(time());
166
			$this->itemMapper->insert($item);
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Files_Antivirus\Db\ItemMapper::insert() has been deprecated with message: 14.0.0 Move over to QBMapper

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...
167
		} catch (\Exception $e) {
168
			$this->logger->error(__METHOD__.', exception: '.$e->getMessage(), ['app' => 'files_antivirus']);
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...
169
		}
170
	}
171
172
	/**
173
	 * Check if the end of file is reached
174
	 * @return boolean
175
	 */
176
	private function feof() {
177
		$isDone = feof($this->fileHandle);
178
		if ($isDone) {
179
			$this->logDebug('Scan is done');
180
			fclose($this->fileHandle);
181
			$this->fileHandle = null;
182
		}
183
		return $isDone;
184
	}
185
186
	/**
187
	 * Opens a file for reading
188
	 * @throws \RuntimeException
189
	 */
190
	private function getFileHandle() {
191
		$fileHandle = $this->file->fopen('r');
192
		if ($fileHandle === false) {
193
			$this->logError('Can not open for reading.');
194
			throw new \RuntimeException();
195
		}
196
197
		$this->logDebug('Scan started');
198
		$this->fileHandle = $fileHandle;
199
	}
200
201
	/**
202
	 * Delete infected file
203
	 */
204
	private function deleteFile() {
205
		//prevent from going to trashbin
206
		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...
207
			/** @var ITrashManager $trashManager */
208
			$trashManager = \OC::$server->query(ITrashManager::class);
209
			$trashManager->pauseTrash();
210
		}
211
		$this->file->delete();
212
		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...
213
			/** @var ITrashManager $trashManager */
214
			$trashManager = \OC::$server->query(ITrashManager::class);
215
			$trashManager->resumeTrash();
216
		}
217
	}
218
219
	private function generateExtraInfo() {
220
		$owner = $this->file->getOwner();
221
222
		if ($owner === null) {
223
			$ownerInfo = ' Account: NO OWNER FOUND';
224
		} else {
225
			$ownerInfo = ' Account: ' . $owner->getUID();
226
		}
227
228
		$extra = ' File: ' . $this->file->getId()
229
			. $ownerInfo
230
			. ' Path: ' . $this->file->getPath();
231
232
		return $extra;
233
	}
234
235
	/**
236
	 * @param string $message
237
	 */
238
	public function logDebug($message) {
239
		$this->logger->debug($message . $this->generateExtraInfo(), ['app' => 'files_antivirus']);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\ILogger::debug() has been deprecated with message: 20.0.0 use \Psr\Log\LoggerInterface::debug

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...
240
	}
241
242
	/**
243
	 * @param string $message
244
	 */
245
	public function logError($message) {
246
		$this->logger->error($message . $this->generateExtraInfo(), ['app' => 'files_antivirus']);
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...
247
	}
248
}
249