Issues (12)

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.

src/Plugin/PluginManager.php (1 issue)

Labels
Severity

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
namespace Nopolabs\Yabot\Plugin;
4
5
6
use Exception;
7
use Nopolabs\Yabot\Helpers\LogTrait;
8
use Nopolabs\Yabot\Message\Message;
9
use Nopolabs\Yabot\Yabot;
10
use Psr\Log\LoggerInterface;
11
use Slack\User;
12
use Throwable;
13
14
class PluginManager
15
{
16
    use LogTrait;
17
18
    const NO_PREFIX = '<none>';
19
    const AUTHED_USER_PREFIX = '<authed_user>';
20
    const DEFAULT_PRIORITY = 100;
21
22
    /** @var User */
23
    protected $authedUser;
24
25
    /** @var array */
26
    protected $pluginMap;
27
28
    /** @var array */
29
    protected $priorityMap;
30
31
    public function __construct(LoggerInterface $logger)
32
    {
33
        $this->setLog($logger);
34
35
        $this->priorityMap = [];
36
    }
37
38
    public function loadPlugin($pluginId, PluginInterface $plugin)
39
    {
40
        if (isset($this->pluginMap[$pluginId])) {
41
            $this->warning("$pluginId already loaded, ignoring duplicate.");
42
            return;
43
        }
44
45
        $this->addPlugin($pluginId, $plugin);
46
47
        $this->info('loaded', ['pluginId' => $pluginId, 'prefix' => $plugin->getPrefix()]);
48
    }
49
50
    public function getHelp() : array
51
    {
52
        $help = [];
53
        /** @var PluginInterface $plugin */
54
        foreach ($this->getPluginMap() as $pluginId => $plugin) {
55
            $prefix = $this->helpPrefix($plugin->getPrefix());
56
            $help[] = $pluginId;
57
            $lines = explode("\n", trim($plugin->help()));
58
            foreach ($lines as $line) {
59
                $help[] = '    '.str_replace('<prefix> ', $prefix, $line);
60
            }
61
        }
62
63
        return $help;
64
    }
65
66
    public function getStatuses() : array
67
    {
68
        $count = count($this->getPluginMap());
69
70
        $statuses = [];
71
        $statuses[] = "There are $count plugins loaded.";
72
        foreach ($this->getPluginMap() as $pluginId => $plugin) {
73
            /** @var PluginInterface $plugin */
74
            $statuses[] = "$pluginId ".$plugin->status();
75
        }
76
77
        return $statuses;
78
    }
79
80
    public function setAuthedUser(User $authedUser)
81
    {
82
        $this->authedUser = $authedUser;
83
84
        $this->updatePrefixes($authedUser->getUsername());
85
    }
86
87
    public function matchesPrefix($prefix, $text) : array
88
    {
89
        if ($prefix === self::NO_PREFIX) {
90
            return [$text, $text];
91
        }
92
93
        preg_match("/^$prefix\\s+(.*)/", $text, $matches);
94
95
        return $matches;
96
    }
97
98
    public function dispatchMessage(Message $message)
99
    {
100
        $text = $message->getFormattedText();
101
102
        $this->info('dispatchMessage: ', [
103
            'formattedText' => $text,
104
            'user' => $message->getUsername(),
105
            'channel' => $message->getChannel()->getName(),
0 ignored issues
show
Consider using $message->getChannel()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
106
            'isBot' => $message->isBot(),
107
            'isSelf' => $message->isSelf(),
108
        ]);
109
110
        foreach ($this->getPriorityMap() as $priority => $prefixMap) {
111
            $this->debug("Dispatching priority $priority");
112
113
            $this->dispatchToPrefixes($prefixMap, $message, $text);
114
115
            if ($message->isHandled()) {
116
                return;
117
            }
118
        }
119
    }
120
121
    protected function dispatchToPrefixes(array $prefixMap, Message $message, string $text)
122
    {
123
        foreach ($prefixMap as $prefix => $plugins) {
124
            if ($matches = $this->matchesPrefix($prefix, $text)) {
125
                $this->debug("Dispatching prefix '$prefix'");
126
127
                $message->setPluginText(ltrim($matches[1]));
128
129
                $this->dispatchToPlugins($plugins, $message);
130
131
                if ($message->isHandled()) {
132
                    return;
133
                }
134
            }
135
        }
136
    }
137
138
    protected function dispatchToPlugins(array $plugins, Message $message)
139
    {
140
        foreach ($plugins as $pluginId => $plugin) {
141
            /** @var PluginInterface $plugin */
142
            try {
143
                $plugin->handle($message);
144
145
            } catch (Throwable $throwable) {
146
                $errmsg = "Unhandled Exception in $pluginId\n"
147
                    .$throwable->getMessage()."\n"
148
                    .$throwable->getTraceAsString()."\n"
149
                    ."Payload data: ".json_encode($message->getData());
150
                $this->warning($errmsg);
151
            }
152
153
            if ($message->isHandled()) {
154
                return;
155
            }
156
        }
157
    }
158
159
    protected function addPlugin($pluginId, PluginInterface $plugin)
160
    {
161
        $this->pluginMap[$pluginId] = $plugin;
162
163
        $priority = $plugin->getPriority();
164
        if (!isset($this->priorityMap[$priority])) {
165
            $this->priorityMap[$priority] = [];
166
        }
167
168
        $prefix = $plugin->getPrefix();
169
        if (!isset($this->priorityMap[$priority][$prefix])) {
170
            $this->priorityMap[$priority][$prefix] = [];
171
        }
172
173
        $this->priorityMap[$priority][$prefix][$pluginId] = $plugin;
174
175
        krsort($this->priorityMap);
176
    }
177
178
    protected function getPluginMap() : array
179
    {
180
        return $this->pluginMap;
181
    }
182
183
    protected function getPriorityMap() : array
184
    {
185
        return $this->priorityMap;
186
    }
187
188
    protected function setPriorityMap(array $priorityMap)
189
    {
190
        $this->priorityMap = $priorityMap;
191
    }
192
193
    protected function getAuthedUser()
194
    {
195
        return $this->authedUser;
196
    }
197
198
    protected function updatePrefixes($authedUsername)
199
    {
200
        $updatedPriorityMap = [];
201
        foreach ($this->getPriorityMap() as $priority => $prefixMap) {
202
            $updatedPrefixMap = [];
203
            foreach ($prefixMap as $prefix => $plugins) {
204
                if ($prefix === self::AUTHED_USER_PREFIX) {
205
                    $prefix = '@'.$authedUsername;
206
                }
207
                $updatedPrefixMap[$prefix] = $plugins;
208
            }
209
            $updatedPriorityMap[$priority] = $updatedPrefixMap;
210
        }
211
        $this->priorityMap = $updatedPriorityMap;
212
    }
213
214
    protected function helpPrefix($prefix)
215
    {
216
        if ($prefix === self::NO_PREFIX) {
217
            return '';
218
        }
219
220
        if ($prefix === self::AUTHED_USER_PREFIX) {
221
            return '@'.$this->getAuthedUser()->getUsername().' ';
222
        }
223
224
        return "$prefix ";
225
    }
226
}