Issues (213)

Security Analysis    not enabled

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/Controller/TemplatesController.php (3 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
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2018 John Molakvoæ <[email protected]>
5
 *
6
 * @author John Molakvoæ <[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
25
namespace OCA\Richdocuments\Controller;
26
27
use OCA\Richdocuments\TemplateManager;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Http;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\AppFramework\Http\FileDisplayResponse;
32
use OCP\AppFramework\Http\JSONResponse;
33
use OCP\AppFramework\Http\NotFoundResponse;
34
use OCP\Files\Node;
35
use OCP\Files\NotFoundException;
36
use OCP\Files\SimpleFS\ISimpleFile;
37
use OCP\IL10N;
38
use OCP\IPreview;
39
use OCP\IRequest;
40
use OC\Files\Filesystem;
41
42
class TemplatesController extends Controller {
43
	/** @var IL10N */
44
	private $l10n;
45
46
	/** @var TemplateManager */
47
	private $manager;
48
49
	/** @var IPreview */
50
	private $preview;
51
52
	/** @var int Max template size */
53
	private $maxSize = 20 * 1024 * 1024;
54
55
	/**
56
	 * Templates controller
57
	 *
58
	 * @param string $appName
59
	 * @param IRequest $request
60
	 * @param L10N $l10n
61
	 * @param TemplateManager $manager
62
	 * @param IPreview $preview
63
	 */
64
	public function __construct($appName,
65
								IRequest $request,
66
								IL10N $l10n,
67
								TemplateManager $manager,
68
								IPreview $preview) {
69
		parent::__construct($appName, $request);
70
71
		$this->appName = $appName;
72
		$this->request = $request;
73
		$this->l10n    = $l10n;
74
		$this->manager = $manager;
75
		$this->preview = $preview;
76
	}
77
78
	/**
79
	 * @NoAdminRequired
80
	 * @NoCSRFRequired
81
	 *
82
	 * Get preview for a specific template
83
	 *
84
	 * @param int $fileId The template id
85
	 * @param int $x
86
	 * @param int $y
87
	 * @param bool $a
88
	 * @param bool $forceIcon
89
	 * @param string $mode
90
	 * @return DataResponse
91
	 * @throws NotFoundResponse
92
	 */
93
	public function getPreview($fileId,
94
		$x = 150,
95
		$y = 150,
96
		$a = false,
97
		$forceIcon = true,
98
		$mode = 'fill') {
99
100
		if ($fileId === '' || $x === 0 || $y === 0) {
101
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
102
		}
103
104
		try {
105
			$template = $this->manager->get($fileId);
106
		} catch (NotFoundException $e) {
107
			return new DataResponse([], Http::STATUS_NOT_FOUND);
108
		}
109
110
		if ($template instanceof ISimpleFile) {
111
			return new DataResponse([], Http::STATUS_NOT_FOUND);
112
		}
113
114
		return $this->fetchPreview($template, $x, $y, $a, $forceIcon, $mode);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->fetchPreview($tem...$a, $forceIcon, $mode); of type OCP\AppFramework\Http\Da...ttp\FileDisplayResponse adds the type OCP\AppFramework\Http\FileDisplayResponse to the return on line 114 which is incompatible with the return type documented by OCA\Richdocuments\Contro...sController::getPreview of type OCP\AppFramework\Http\DataResponse.
Loading history...
115
	}
116
117
	/**
118
	 * Add a global template
119
	 *
120
	 * @return JSONResponse
121
	 */
122
	public function add() {
123
		$files = $this->request->getUploadedFile('files');
124
125
		if (!is_null($files)) {
126
			if ($files['error'][0] === 0
127
				&& is_uploaded_file($files['tmp_name'][0])
128
				&& !Filesystem::isFileBlacklisted($files['tmp_name'][0])) {
129
130
				// TODO: ensure the size limit is decent for preview
131 View Code Duplication
				if ($files['size'][0] > $this->maxSize) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
					return new JSONResponse(
133
						['data' => ['message' => $this->l10n->t('File is too big')]],
134
						Http::STATUS_BAD_REQUEST
135
					);
136
				}
137
138 View Code Duplication
				if (!$this->manager->isValidTemplateMime($files['type'][0])) {
139
					return new JSONResponse(
140
						['data' => ['message' => $this->l10n->t('Only template files can be uploaded')]],
141
						Http::STATUS_BAD_REQUEST
142
					);
143
				}
144
145
				$templateName = $files['name'][0];
146
				$templateFile = file_get_contents($files['tmp_name'][0]);
147
148
				unlink($files['tmp_name'][0]);
149
150
				$template = $this->manager->add($templateName, $templateFile);
151
152
				return new JSONResponse(
153
					['data' => $template],
154
					Http::STATUS_CREATED
155
				);
156
			}
157
		}
158
159
		return new JSONResponse(
160
			['data' => ['message' => $this->l10n->t('Invalid file provided')]],
161
			Http::STATUS_BAD_REQUEST
162
		);
163
	}
164
165
	/**
166
	 * Delete a global template
167
	 *
168
	 * @param int $fileId
169
	 * @return JSONResponse
170
	 */
171
	public function delete($fileId) {
172
		try {
173
			$this->manager->delete($fileId);
174
175
			return new JSONResponse(
176
				['data' => ['status' => 'success']],
177
				Http::STATUS_NO_CONTENT
178
			);
179
		} catch (NotFoundException $e) {
180
			return new JSONResponse(
181
				['data' => ['message' => $this->l10n->t('Template not found')]],
182
				Http::STATUS_NOT_FOUND
183
			);
184
		}
185
	}
186
187
	/**
188
	 * @param Node $node
189
	 * @param int $x
190
	 * @param int $y
191
	 * @param bool $a
192
	 * @param bool $forceIcon
193
	 * @param string $mode
194
	 * @return DataResponse|FileDisplayResponse
195
	 */
196
	private function fetchPreview(
197
		Node $node,
198
		$x,
199
		$y,
200
		$a = false,
201
		$forceIcon = true,
202
		string $mode): Http\Response {
203
204
		if (!($node instanceof Node) || (!$forceIcon && !$this->preview->isAvailable($node))) {
205
			return new DataResponse([], Http::STATUS_NOT_FOUND);
206
		}
207
		if (!$node->isReadable()) {
208
			return new DataResponse([], Http::STATUS_FORBIDDEN);
209
		}
210
211
		try {
212
			$f        = $this->preview->getPreview($node, $x, $y, !$a, $mode);
0 ignored issues
show
$node of type object<OCP\Files\Node> is not a sub-type of object<OCP\Files\File>. It seems like you assume a child interface of the interface OCP\Files\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
213
			$response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
214
			$response->cacheFor(3600 * 24);
215
216
			return $response;
217
		} catch (NotFoundException $e) {
218
			return new DataResponse([], Http::STATUS_NOT_FOUND);
219
		} catch (\InvalidArgumentException $e) {
220
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
221
		}
222
	}
223
}
224