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/Pico.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
declare(strict_types=1);
25
26
namespace OCA\CMSPico;
27
28
use HTMLPurifier;
29
use HTMLPurifier_Config;
30
use HTMLPurifier_HTML5Config;
31
use OCA\CMSPico\Exceptions\WebsiteInvalidFilesystemException;
32
use OCA\CMSPico\Files\FileInterface;
33
use OCA\CMSPico\Files\FolderInterface;
34
use OCA\CMSPico\Files\Glob\GlobIterator;
35
use OCA\CMSPico\Files\NodeInterface;
36
use OCA\CMSPico\Model\Website;
37
use OCA\CMSPico\Model\WebsiteRequest;
38
use OCA\CMSPico\Service\PicoService;
39
use OCP\Files\InvalidPathException;
40
use OCP\Files\NotFoundException;
41
use OCP\Files\NotPermittedException;
42
use Symfony\Component\Yaml\Exception\ParseException;
43
44
class Pico extends \Pico
45
{
46
	/**
47
	 * API version 0, used by Pico 0.9 and earlier
48
	 *
49
	 * @var int
50
	 */
51
	public const API_VERSION_0 = 0;
52
53
	/**
54
	 * API version 1, used by Pico 1.0
55
	 *
56
	 * @var int
57
	 */
58
	public const API_VERSION_1 = 1;
59
60
	/**
61
	 * API version 2, used by Pico 2.0
62
	 *
63
	 * @var int
64
	 */
65
	public const API_VERSION_2 = 2;
66
67
	/**
68
	 * API version 3, used by Pico 2.1
69
	 *
70
	 * @var int
71
	 */
72
	public const API_VERSION_3 = 3;
73
74
	/** @var PicoService */
75
	private $picoService;
76
77
	/** @var HTMLPurifier */
78
	private $htmlPurifier;
79
80
	/** @var WebsiteRequest */
81
	private $websiteRequest;
82
83
	/** @var Website */
84
	private $website;
85
86
	/**
87
	 * Pico constructor.
88
	 *
89
	 * {@inheritDoc}
90
	 */
91 5
	public function __construct($rootDir, $configDir, $pluginsDir, $themesDir, $enableLocalPlugins = true)
92
	{
93 5
		$this->picoService = \OC::$server->query(PicoService::class);
0 ignored issues
show
Deprecated Code introduced by
The function OC\ServerContainer::query() has been deprecated: 20.0.0 use \Psr\Container\ContainerInterface::get ( Ignorable by Annotation )

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

93
		$this->picoService = /** @scrutinizer ignore-deprecated */ \OC::$server->query(PicoService::class);

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...
94
95 5
		parent::__construct($rootDir, $configDir, $pluginsDir, $themesDir, $enableLocalPlugins);
96 5
	}
97
98
	/**
99
	 * {@inheritDoc}
100
	 *
101
	 * @return string
102
	 * @throws WebsiteInvalidFilesystemException
103
	 * @throws InvalidPathException
104
	 * @throws NotFoundException
105
	 * @throws NotPermittedException
106
	 */
107 5
	public function run()
108
	{
109 5
		return parent::run();
110
	}
111
112
	/**
113
	 * Set's Nextcloud's website and website request instances.
114
	 *
115
	 * @param WebsiteRequest $websiteRequest Nextcloud's website request instance
116
	 *
117
	 * @return void
118
	 */
119 5
	public function setNextcloudWebsite(WebsiteRequest $websiteRequest)
120
	{
121 5
		$this->websiteRequest = $websiteRequest;
122 5
		$this->website = $websiteRequest->getWebsite();
123 5
	}
124
125
	/**
126
	 * Set's Pico's request URL.
127
	 *
128
	 * @param string $requestUrl request URL
129
	 *
130
	 * @return void
131
	 */
132 5
	public function setRequestUrl($requestUrl)
133
	{
134 5
		$this->requestUrl = $requestUrl;
135 5
	}
136
137
	/**
138
	 * Don't let Pico evaluate the request URL.
139
	 *
140
	 * @see Pico::setRequestUrl()
141
	 *
142
	 * @return void
143
	 */
144 5
	protected function evaluateRequestUrl()
145
	{
146
		// do nothing
147 5
	}
148
149
	/**
150
	 * Checks whether a file is readable in Nextcloud and returns the raw contents of this file
151
	 *
152
	 * @param string $absolutePath file path
153
	 *
154
	 * @return string raw contents of the file
155
	 * @throws WebsiteInvalidFilesystemException
156
	 * @throws InvalidPathException
157
	 * @throws NotFoundException
158
	 * @throws NotPermittedException
159
	 */
160 5
	public function loadFileContent($absolutePath)
161
	{
162
		/** @var FolderInterface $folder */
163
		/** @var string $basePath */
164
		/** @var string $relativePath */
165 5
		[ $folder, $basePath, $relativePath ] = $this->picoService->getRelativePath($this->website, $absolutePath);
166
167 5
		$file = $folder->getFile($relativePath);
168 5
		return $file->getContent();
169
	}
170
171
	/**
172
	 * Returns the parsed and purified file meta from raw file contents.
173
	 *
174
	 * @param string $rawContent
175
	 * @param string[] $headers
176
	 *
177
	 * @return array
178
	 * @throws ParseException
179
	 */
180 5
	public function parseFileMeta($rawContent, array $headers)
181
	{
182 5
		$meta = parent::parseFileMeta($rawContent, $headers);
183 5
		return $this->purifyFileMeta($meta);
184
	}
185
186
	/**
187
	 * Purifies file meta.
188
	 *
189
	 * @param array $meta
190
	 *
191
	 * @return array
192
	 */
193 5
	protected function purifyFileMeta(array $meta): array
194
	{
195 5
		$newMeta = [];
196 5
		foreach ($meta as $key => $value) {
197 5
			if (is_array($value)) {
198 3
				$newMeta[$key] = $this->purifyFileMeta($value);
199
			} else {
200 5
				$newMeta[$key] = $this->getHtmlPurifier()->purify($value);
201
			}
202
		}
203
204 5
		return $newMeta;
205
	}
206
207
	/**
208
	 * Returns the parsed and purified contents of a page.
209
	 *
210
	 * @param string $markdown
211
	 * @param bool   $singleLine
212
	 *
213
	 * @return string
214
	 */
215 5
	public function parseFileContent($markdown, $singleLine = false)
216
	{
217 5
		$content = parent::parseFileContent($markdown, $singleLine);
218 5
		return $this->purifyFileContent($content);
219
	}
220
221
	/**
222
	 * Purifies file content.
223
	 *
224
	 * @param string $content
225
	 *
226
	 * @return string
227
	 */
228 5
	protected function purifyFileContent(string $content): string
229
	{
230 5
		return $this->getHtmlPurifier()->purify($content);
231
	}
232
233
	/**
234
	 * Returns the HTMLPurifier instance.
235
	 *
236
	 * @return HTMLPurifier
237
	 */
238 5
	public function getHtmlPurifier(): HTMLPurifier
239
	{
240 5
		if ($this->htmlPurifier === null) {
241 5
			$this->htmlPurifier = new HTMLPurifier($this->getHtmlPurifierConfig());
242
243 5
			$this->triggerEvent('onHtmlPurifier', [ &$this->htmlPurifier ]);
244
		}
245
246 5
		return $this->htmlPurifier;
247
	}
248
249
	/**
250
	 * Returns the HTMLPurifier_Config instance.
251
	 *
252
	 * @return HTMLPurifier_Config
253
	 */
254 5
	private function getHtmlPurifierConfig(): HTMLPurifier_Config
255
	{
256 5
		$config = HTMLPurifier_HTML5Config::createDefault();
257 5
		$config->autoFinalize = false;
258
259 5
		$config->set('Attr.EnableID', true);
260
261 5
		$allowedSchemes = array_merge($config->get('URI.AllowedSchemes'), [ 'data' => true ]);
262 5
		$config->set('URI.AllowedSchemes', $allowedSchemes);
263
264 5
		$config->set('HTML.Allowed', 'a[href|target]');
265 5
		$config->set('Attr.AllowedFrameTargets', [ '_blank' ]);
266
267 5
		$config->finalize();
268
269 5
		return $config;
270
	}
271
272
	/**
273
	 * @param string $absolutePath
274
	 * @param string $fileExtension
275
	 * @param int    $order
276
	 *
277
	 * @return string[]
278
	 * @throws WebsiteInvalidFilesystemException
279
	 * @throws InvalidPathException
280
	 */
281 5
	public function getFiles($absolutePath, $fileExtension = '', $order = \Pico::SORT_ASC)
282
	{
283
		/** @var FolderInterface $folder */
284
		/** @var string $basePath */
285
		/** @var string $relativePath */
286 5
		[ $folder, $basePath, $relativePath ] = $this->picoService->getRelativePath($this->website, $absolutePath);
287
288 5
		if ($folder->isLocal()) {
289 5
			return parent::getFiles($absolutePath, $fileExtension, $order);
290
		}
291
292
		$folderFilter = function (NodeInterface $node, int $key, FolderInterface $folder) use ($fileExtension) {
293
			$fileName = $node->getName();
294
295
			// exclude hidden files/dirs starting with a .
296
			// exclude files ending with a ~ (vim/nano backup) or # (emacs backup)
297
			if (($fileName[0] === '.') || in_array($fileName[-1], [ '~', '#' ], true)) {
298
				return false;
299
			}
300
301
			if ($node->isFile()) {
302
				/** @var FileInterface $node */
303
				if ($fileExtension && ($fileExtension !== '.' . $node->getExtension())) {
304
					return false;
305
				}
306
			}
307
308
			return true;
309
		};
310
311
		try {
312
			$folderIterator = new \RecursiveCallbackFilterIterator($folder->fakeRoot(), $folderFilter);
313
314
			$result = [];
315
			foreach (new \RecursiveIteratorIterator($folderIterator) as $file) {
316
				$result[] = $basePath . '/' . $relativePath . $file->getPath();
317
			}
318
319
			return ($order === \Pico::SORT_DESC) ? array_reverse($result) : $result;
320
		} catch (\Exception $e) {
321
			return [];
322
		}
323
	}
324
325
	/**
326
	 * @param string $absolutePathPattern
327
	 * @param int    $order
328
	 *
329
	 * @return string[]
330
	 * @throws WebsiteInvalidFilesystemException
331
	 * @throws InvalidPathException
332
	 */
333 5
	public function getFilesGlob($absolutePathPattern, $order = \Pico::SORT_ASC)
334
	{
335
		/** @var FolderInterface $folder */
336
		/** @var string $basePath */
337
		/** @var string $pattern */
338 5
		[ $folder, $basePath, $pattern ] = $this->picoService->getRelativePath($this->website, $absolutePathPattern);
339
340 5
		if ($folder->isLocal()) {
341 5
			return parent::getFilesGlob($absolutePathPattern, $order);
342
		}
343
344
		try {
345
			$result = [];
346
			foreach (new GlobIterator($folder, $pattern) as $file) {
347
				$fileName = $file->getName();
348
349
				// exclude files ending with a ~ (vim/nano backup) or # (emacs backup)
350
				if (in_array($fileName[-1], [ '~', '#' ], true)) {
351
					continue;
352
				}
353
354
				$result[] = $basePath . $file->getPath();
355
			}
356
357
			return ($order === \Pico::SORT_DESC) ? array_reverse($result) : $result;
358
		} catch (\Exception $e) {
359
			return [];
360
		}
361
	}
362
}
363