Issues (69)

Security Analysis    not enabled

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

  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.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  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.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  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.
  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.
  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.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
  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.
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  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.
  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.
  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.
  Header Injection
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/Service/MiscService.php (1 issue)

1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2017, Maxence Lange (<[email protected]>)
6
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
namespace OCA\CMSPico\Service;
25
26
use OCA\CMSPico\AppInfo\Application;
27
use OCA\CMSPico\Exceptions\ComposerException;
28
use OCA\CMSPico\Exceptions\FilesystemNotWritableException;
29
use OCA\CMSPico\Files\FileInterface;
30
use OCP\Files\AlreadyExistsException;
31
use OCP\Files\GenericFileException;
32
use OCP\Files\InvalidPathException;
33
use OCP\Files\NotPermittedException;
34
use OCP\IL10N;
35
use OCP\Security\ISecureRandom;
36
37
class MiscService
38
{
39
	/** @var IL10N */
40
	private $l10n;
41
42
	/** @var ConfigService */
43
	private $configService;
44
45
	/** @var FileService */
46
	private $fileService;
47
48
	/** @var string[] */
49
	private $textFileMagic;
50
51
	/** @var string[] */
52
	private $binaryFileMagic;
53
54
	/**
55
	 * MiscService constructor.
56
	 *
57
	 * @param IL10N         $l10n
58
	 * @param ConfigService $configService
59
	 * @param FileService   $fileService
60
	 */
61
	public function __construct(IL10N $l10n, ConfigService $configService, FileService $fileService)
62
	{
63
		$this->l10n = $l10n;
64
		$this->configService = $configService;
65
		$this->fileService = $fileService;
66
67
		$this->textFileMagic = [
68
			hex2bin('EFBBBF'),
69
			hex2bin('0000FEFF'),
70
			hex2bin('FFFE0000'),
71
			hex2bin('FEFF'),
72
			hex2bin('FFFE')
73
		];
74
75
		$this->binaryFileMagic = [
76
			'%PDF',
77
			hex2bin('89') . 'PNG'
78
		];
79
	}
80
81
	/**
82
	 * @param string $path
83
	 *
84
	 * @return string
85
	 * @throws InvalidPathException
86
	 */
87 29
	public function normalizePath(string $path): string
88
	{
89 29
		$path = str_replace('\\', '/', $path);
90 29
		$pathParts = explode('/', $path);
91
92 29
		$resultParts = [];
93 29
		foreach ($pathParts as $pathPart) {
94 29
			if (($pathPart === '') || ($pathPart === '.')) {
95 29
				continue;
96 29
			} elseif ($pathPart === '..') {
97 3
				if (empty($resultParts)) {
98
					throw new InvalidPathException();
99
				}
100
101 3
				array_pop($resultParts);
102 3
				continue;
103
			}
104
105 29
			$resultParts[] = $pathPart;
106
		}
107
108 29
		return implode('/', $resultParts);
109
	}
110
111
	/**
112
	 * @param string      $path
113
	 * @param string|null $basePath
114
	 *
115
	 * @return string
116
	 * @throws InvalidPathException
117
	 */
118 5
	public function getRelativePath(string $path, string $basePath = null): string
119
	{
120 5
		if (!$basePath) {
121
			$basePath = \OC::$SERVERROOT;
122
		}
123
124 5
		$basePath = $this->normalizePath($basePath);
125 5
		$basePathLength = strlen($basePath);
126
127 5
		$path = $this->normalizePath($path);
128
129 5
		if ($path === $basePath) {
130
			return '';
131 5
		} elseif (substr_compare($path, $basePath . '/', 0, $basePathLength + 1) === 0) {
132 5
			return substr($path, $basePathLength + 1);
133
		} else {
134 5
			throw new InvalidPathException();
135
		}
136
	}
137
138
	/**
139
	 * @param string      $path
140
	 * @param string|null $fileExtension
141
	 *
142
	 * @return false|string
143
	 * @throws InvalidPathException
144
	 */
145 5
	public function dropFileExtension(string $path, string $fileExtension = null): string
146
	{
147 5
		$fileName = basename($path);
148 5
		$fileExtensionPos = strrpos($fileName, '.');
149
150 5
		if ($fileExtension === null) {
151
			return $fileExtensionPos ? substr($path, 0, $fileExtensionPos) : $path;
152
		}
153
154 5
		if (($fileExtensionPos === false) || (substr_compare($fileName, $fileExtension, $fileExtensionPos) !== 0)) {
155
			throw new InvalidPathException();
156
		}
157
158 5
		return substr($path, 0, strlen($path) - strlen($fileExtension));
159
	}
160
161
	/**
162
	 * @param FileInterface $file
163
	 *
164
	 * @return bool
165
	 * @throws NotPermittedException
166
	 * @throws GenericFileException
167
	 */
168 3
	public function isBinaryFile(FileInterface $file): bool
169
	{
170
		try {
171 3
			$buffer = file_get_contents($file->getLocalPath(), false, null, 0, 1024);
172
		} catch (\Exception $e) {
173
			$buffer = false;
174
		}
175
176 3
		if ($buffer === false) {
177
			$buffer = substr($file->getContent(), 0, 1024);
178
		}
179
180 3
		if ($buffer === '') {
181
			return false;
182
		}
183
184 3
		foreach ($this->textFileMagic as $textFileMagic) {
185 3
			if (substr_compare($buffer, $textFileMagic, 0, strlen($textFileMagic)) === 0) {
186
				return false;
187
			}
188
		}
189
190 3
		foreach ($this->binaryFileMagic as $binaryFileMagic) {
191 3
			if (substr_compare($buffer, $binaryFileMagic, 0, strlen($binaryFileMagic)) === 0) {
192 3
				return true;
193
			}
194
		}
195
196 3
		return (strpos($buffer, "\0") !== false);
197
	}
198
199
	/**
200
	 * @param int    $length
201
	 * @param string $prefix
202
	 * @param string $suffix
203
	 *
204
	 * @return string
205
	 */
206
	public function getRandom(int $length = 10, string $prefix = '', string $suffix = ''): string
207
	{
208
		$randomChars = ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS;
209
		$random = \OC::$server->getSecureRandom()->generate($length, $randomChars);
0 ignored issues
show
Deprecated Code introduced by
The function OC\Server::getSecureRandom() has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

209
		$random = /** @scrutinizer ignore-deprecated */ \OC::$server->getSecureRandom()->generate($length, $randomChars);

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

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

Loading history...
210
		return ($prefix ? $prefix . '.' : '') . $random . ($suffix ? '.' . $suffix : '');
211
	}
212
213
	/**
214
	 * @throws ComposerException
215
	 */
216
	public function checkComposer(): void
217
	{
218
		$appPath = Application::getAppPath();
219
		if (!is_file($appPath . '/vendor/autoload.php')) {
220
			try {
221
				$relativeAppPath = $this->getRelativePath($appPath) . '/';
222
			} catch (InvalidPathException $e) {
223
				$relativeAppPath = 'apps/' . Application::APP_NAME . '/';
224
			}
225
226
			throw new ComposerException($this->l10n->t(
227
				'Failed to enable Pico CMS for Nextcloud: Couldn\'t find "%s". Make sure to install the app\'s '
228
						. 'dependencies by executing `composer install` in the app\'s install directory below "%s". '
229
						. 'Then try again enabling Pico CMS for Nextcloud.',
230
				[ $relativeAppPath . 'vendor/autoload.php', $relativeAppPath ]
231
			));
232
		}
233
	}
234
235
	/**
236
	 * @throws FilesystemNotWritableException
237
	 */
238
	public function checkPublicFolder(): void
239
	{
240
		$publicFolder = $this->fileService->getPublicFolder();
241
242
		try {
243
			try {
244
				$publicThemesFolder = $publicFolder->newFolder(PicoService::DIR_THEMES);
245
			} catch (AlreadyExistsException $e) {
246
				$publicThemesFolder = $publicFolder->getFolder(PicoService::DIR_THEMES);
247
			}
248
249
			$publicThemesTestFileName = $this->getRandom(10, 'tmp', Application::APP_NAME . '-test');
250
			$publicThemesTestFile = $publicThemesFolder->newFile($publicThemesTestFileName);
251
			$publicThemesTestFile->delete();
252
253
			try {
254
				$publicPluginsFolder = $publicFolder->newFolder(PicoService::DIR_PLUGINS);
255
			} catch (AlreadyExistsException $e) {
256
				$publicPluginsFolder = $publicFolder->getFolder(PicoService::DIR_PLUGINS);
257
			}
258
259
			$publicPluginsTestFileName = $this->getRandom(10, 'tmp', Application::APP_NAME . '-test');
260
			$publicPluginsTestFile = $publicPluginsFolder->newFile($publicPluginsTestFileName);
261
			$publicPluginsTestFile->delete();
262
		} catch (NotPermittedException $e) {
263
			try {
264
				$appDataPublicPath = Application::getAppPath() . '/appdata_public';
265
				$appDataPublicPath = $this->getRelativePath($appDataPublicPath) . '/';
266
			} catch (InvalidPathException $e) {
267
				$appDataPublicPath = 'apps/' . Application::APP_NAME . '/appdata_public/';
268
			}
269
270
			try {
271
				$dataPath = $this->configService->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data');
272
				$dataPath = $this->getRelativePath($dataPath) . '/';
273
			} catch (InvalidPathException $e) {
274
				$dataPath = 'data/';
275
			}
276
277
			throw new FilesystemNotWritableException($this->l10n->t(
278
				'Failed to enable Pico CMS for Nextcloud: The web server has no permission to create files and '
279
						. 'folders below "%s". Make sure to give the web server write access to this directory by '
280
						. 'changing its permissions and ownership to the same as of your "%s" directory. Then try '
281
						. 'again enabling Pico CMS for Nextcloud.',
282
				[ $appDataPublicPath, $dataPath ]
283
			));
284
		}
285
	}
286
}
287