Issues (21)

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/Activity/Provider.php (4 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) 2016 Joas Schilling <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\AnnouncementCenter\Activity;
24
25
use OCA\AnnouncementCenter\Manager;
26
use OCA\AnnouncementCenter\Model\Announcement;
27
use OCA\AnnouncementCenter\Model\AnnouncementDoesNotExistException;
28
use OCP\Activity\IEvent;
29
use OCP\Activity\IManager as IActivityManager;
30
use OCP\Activity\IProvider;
31
use OCP\IURLGenerator;
32
use OCP\IUser;
33
use OCP\IUserManager;
34
use OCP\L10N\IFactory;
35
36
class Provider implements IProvider {
37
38
	/** @var IFactory */
39
	protected $languageFactory;
40
41
	/** @var IURLGenerator */
42
	protected $url;
43
44
	/** @var IActivityManager */
45
	protected $activityManager;
46
47
	/** @var IUserManager */
48
	protected $userManager;
49
50
	/** @var Manager */
51
	protected $manager;
52
53
	/** @var string[] */
54
	protected $displayNames = [];
55
56 1 View Code Duplication
	public function __construct(
0 ignored issues
show
This method seems to be duplicated in 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...
57
		IFactory $languageFactory,
58
		IURLGenerator $url,
59
		IActivityManager $activityManager,
60
		IUserManager $userManager,
61
		Manager $manager
62
	) {
63 1
		$this->languageFactory = $languageFactory;
64 1
		$this->url = $url;
65 1
		$this->activityManager = $activityManager;
66 1
		$this->userManager = $userManager;
67 1
		$this->manager = $manager;
68 1
	}
69
70
	/**
71
	 * @param string $language
72
	 * @param IEvent $event
73
	 * @param IEvent|null $previousEvent
74
	 * @return IEvent
75
	 * @throws \InvalidArgumentException
76
	 * @since 11.0.0
77
	 */
78
	public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent {
79
		if ($event->getApp() !== 'announcementcenter' || (
80
			$event->getSubject() !== 'announcementsubject' && // 3.1 and later
81
			strpos($event->getSubject(), 'announcementsubject#') !== 0) // 3.0 and before
82
		) {
83
			throw new \InvalidArgumentException('Unknown subject');
84
		}
85
86
		$l = $this->languageFactory->get('announcementcenter', $language);
87
88
		if (method_exists($this->activityManager, 'getRequirePNG') && $this->activityManager->getRequirePNG()) {
89
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('announcementcenter', 'announcementcenter-dark.png')));
90
		} else {
91
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('announcementcenter', 'announcementcenter-dark.svg')));
92
		}
93
94
		$parameters = $this->getParameters($event);
95
96
		try {
97
			$announcement = $this->manager->getAnnouncement($parameters['announcement']);
98
99
			$parsedParameters = $this->getParsedParameters($parameters, $announcement);
100 View Code Duplication
			if ($parsedParameters['actor']['id'] === $this->activityManager->getCurrentUserId()) {
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...
101
				$subject = $l->t('You announced “{announcement}”');
102
				unset($parsedParameters['actor']);
103
			} else {
104
				$subject = $l->t('{actor} announced “{announcement}”');
105
			}
106
			$event->setParsedMessage($announcement->getMessage());
107
		} catch (AnnouncementDoesNotExistException $e) {
108
			$parsedParameters = $this->getParsedParameters($parameters);
109 View Code Duplication
			if ($parsedParameters['actor']['id'] === $this->activityManager->getCurrentUserId()) {
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...
110
				$subject = $l->t('You posted an announcement');
111
				unset($parsedParameters['actor']);
112
			} else {
113
				$subject = $l->t('{actor} posted an announcement');
114
			}
115
116
			$event->setParsedMessage($l->t('The announcement does not exist anymore'));
117
		}
118
119
120
		$this->setSubjects($event, $subject, $parsedParameters);
121
122
		return $event;
123
	}
124
125
	protected function getParameters(IEvent $event): array {
126
		$parameters = $event->getSubjectParameters();
127
		if (isset($parameters['announcement'])) {
128
			return $parameters;
129
		}
130
131
		// Legacy fallback from before 3.4.0
132
		return [
133
			'author' => $parameters[0] ?? '',
134
			'announcement' => $event->getObjectId(),
135
		];
136
	}
137
138
	protected function setSubjects(IEvent $event, string $subject, array $parameters) {
139
		$placeholders = $replacements = [];
140
		foreach ($parameters as $placeholder => $parameter) {
141
			$placeholders[] = '{' . $placeholder . '}';
142
			$replacements[] = $parameter['name'];
143
		}
144
145
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
146
			->setRichSubject($subject, $parameters);
147
	}
148
149
	protected function getParsedParameters(array $parameters, Announcement $announcement = null): array {
150
		if ($announcement !== null) {
151
			return [
152
				'actor' => $this->generateUserParameter($parameters['author']),
153
				'announcement' => $this->generateAnnouncementParameter($announcement),
154
			];
155
		}
156
157
		return [
158
			'actor' => $this->generateUserParameter($parameters['author']),
159
		];
160
	}
161
162
	protected function generateAnnouncementParameter(Announcement $announcement): array {
163
		return [
164
			'type' => 'announcement',
165
			'id' => $announcement->getId(),
166
			'name' => $announcement->getSubject(),
167
			'link' => $this->url->linkToRouteAbsolute('announcementcenter.page.index', [
168
				'announcement' => $announcement->getId(),
169
			]),
170
		];
171
	}
172
173
	protected function generateUserParameter(string $uid): array {
174
		if (!isset($this->displayNames[$uid])) {
175
			$this->displayNames[$uid] = $this->getDisplayName($uid);
176
		}
177
178
		return [
179
			'type' => 'user',
180
			'id' => $uid,
181
			'name' => $this->displayNames[$uid],
182
		];
183
	}
184
185
	protected function getDisplayName(string $uid): string {
186
		$user = $this->userManager->get($uid);
187
		if ($user instanceof IUser) {
0 ignored issues
show
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
188
			return $user->getDisplayName();
189
		}
190
		return $uid;
191
	}
192
}
193