Issues (199)

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.

src/commands/MigrateRedisController.php (4 issues)

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 sweelix\oauth2\server\commands;
4
5
use sweelix\oauth2\server\interfaces\AccessTokenModelInterface;
6
use sweelix\oauth2\server\interfaces\JtiModelInterface;
7
use sweelix\oauth2\server\interfaces\RefreshTokenModelInterface;
8
use sweelix\oauth2\server\services\redis\AccessTokenService;
9
use sweelix\oauth2\server\services\redis\ClientService;
10
use sweelix\oauth2\server\services\redis\JtiService;
11
use sweelix\oauth2\server\services\redis\RefreshTokenService;
12
use Yii;
13
use yii\console\Controller;
14
use yii\console\ExitCode;
15
use sweelix\oauth2\server\Module;
16
use yii\di\Instance;
17
use yii\redis\Connection;
18
19
class MigrateRedisController extends Controller
20
{
21
    public function actionMigrate()
22
    {
23
        try {
24
            $module = Module::getInstance();
25
            /** @var Connection $db */
26
            $db = Instance::ensure($module->db, Connection::class);
27
28
            ///////////////
29
            ///
30
            /// AccessToken
31
            ///
32
            ///////////////
33
            $this->stdout('Start migrating AccessTokens ...' . "\n");
34
            /** @var AccessTokenService $accessTokenService */
35
            $accessTokenService = Yii::createObject('sweelix\oauth2\server\interfaces\AccessTokenServiceInterface');
36
            $accessTokenList = $accessTokenService->getAccessTokenListKey();
37
            if ($db->executeCommand('EXISTS', [$accessTokenList]) == 1) {
38
                $prompt = $this->prompt('The key "'.$accessTokenList.'" already exists. Do you want to continue ? (yes, no)', [
39
                    'required' => true,
40
                ]);
41
                switch ($prompt) {
42
                    case 'no':
43
                    case 'n':
44
                        exit();
45
                }
46
            }
47
            $userListKey = $accessTokenService->getUserListKey();
48
            if ($db->executeCommand('EXISTS', [$userListKey]) == 1) {
49
                $prompt = $this->prompt('The key "'.$userListKey.'" already exists. Do you want to continue ? (yes, no)', [
50
                    'required' => true,
51
                ]);
52
                switch ($prompt) {
53
                    case 'no':
54
                    case 'n':
55
                        exit();
56
                }
57
            }
58
            $accessToken = Yii::createObject('sweelix\oauth2\server\interfaces\AccessTokenModelInterface');
59
            $accessTokenClass = get_class($accessToken);
60
            $accessTokens = $db->executeCommand('KEYS', [$accessTokenService->getUserAccessTokensKey('*')]);
61
            if (!empty($accessTokens)) {
62
                $db->executeCommand('DEL', $accessTokens);
63
            }
64
            $accessTokens = $db->executeCommand('KEYS', [$accessTokenService->getClientAccessTokensKey('*')]);
65
            if (!empty($accessTokens)) {
66
                $db->executeCommand('DEL', $accessTokens);
67
            }
68
            $accessTokens = $db->executeCommand('KEYS', [$accessTokenService->getAccessTokenKey('*')]);
69
            foreach ($accessTokens as $accessTokenHash) {
0 ignored issues
show
The expression $accessTokens of type boolean|string|null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
70
                if ($db->executeCommand('TYPE', [$accessTokenHash]) !== 'hash') {
71
                    continue;
72
                }
73
                $accessTokenData = $db->executeCommand('HGETALL', [$accessTokenHash]);
74
                if (!empty($accessTokenData)) {
75
                    /** @var AccessTokenModelInterface $accessToken */
76
                    $accessToken = $accessTokenClass::findOne($accessTokenData[1]); // id
77
                    if (!empty($accessToken)) {
78
                        $accessToken->delete();
79
                        $accessToken->save();
80
                    }
81
                }
82
            }
83
            $this->stdout('Done migrating AccessTokens !' . "\n");
84
85
            ///////////////
86
            ///
87
            /// Clients
88
            ///
89
            ///////////////
90
            $this->stdout('Start migrating Clients ...' . "\n");
91
            /** @var ClientService $clientService */
92
            $clientService = Yii::createObject('sweelix\oauth2\server\interfaces\ClientServiceInterface');
93
            $clientList = $clientService->getClientListKey();
94
            if ($db->executeCommand('EXISTS', [$clientList]) == 1) {
95
                $prompt = $this->prompt('The key "'.$clientList.'" already exists. Do you want to continue ? (yes, no)', [
96
                    'required' => true,
97
                ]);
98
                switch ($prompt) {
99
                    case 'no':
100
                    case 'n':
101
                        exit();
102
                }
103
            }
104
            $clients = $db->executeCommand('KEYS', [$clientService->getClientKey('*')]);
105
            foreach ($clients as $clientHash) {
0 ignored issues
show
The expression $clients of type boolean|string|null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
106
                if ($db->executeCommand('TYPE', [$clientHash]) !== 'hash') {
107
                    continue;
108
                }
109
                $clientData = $db->executeCommand('HGETALL', [$clientHash]);
110
                if (!empty($clientData)) {
111
                    $db->executeCommand('SADD', [$clientService->getClientListKey(), $clientData[1]]);
112
                }
113
            }
114
            $this->stdout('Done migrating Clients !' . "\n");
115
116
            ///////////////
117
            ///
118
            /// Jti
119
            ///
120
            ///////////////
121
            $this->stdout('Start migrating Jtis ...' . "\n");
122
            /** @var JtiService $jtiService */
123
            $jtiService = Yii::createObject('sweelix\oauth2\server\interfaces\JtiServiceInterface');
124
            $jtiList = $jtiService->getJtiListKey();
125
            if ($db->executeCommand('EXISTS', [$jtiList]) == 1) {
126
                $prompt = $this->prompt('The key "'.$jtiList.'" already exists. Do you want to continue ? (yes, no)', [
127
                    'required' => true,
128
                ]);
129
                switch ($prompt) {
130
                    case 'no':
131
                    case 'n':
132
                        exit();
133
                }
134
            }
135
            $jti = Yii::createObject('sweelix\oauth2\server\interfaces\JtiModelInterface');
136
            $jtiClass = get_class($jti);
137
            $jtis = $db->executeCommand('KEYS', [$jtiService->getSubjectJtisKey('*')]);
138
            if (!empty($jtis)) {
139
                $db->executeCommand('DEL', $jtis);
140
            }
141
            $jtis = $db->executeCommand('KEYS', [$jtiService->getSubjectJtisKey('*')]);
142
            if (!empty($jtis)) {
143
                $db->executeCommand('DEL', $jtis);
144
            }
145
            $jtis = $db->executeCommand('KEYS', [$jtiService->getJtiKey('*')]);
146
            foreach ($jtis as $jtiHash) {
0 ignored issues
show
The expression $jtis of type boolean|string|null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
147
                if ($db->executeCommand('TYPE', [$jtiHash]) !== 'hash') {
148
                    continue;
149
                }
150
                $jtiData = $db->executeCommand('HGETALL', [$jtiHash]);
151
                if (!empty($jtiData)) {
152
                    /** @var JtiModelInterface $jti */
153
                    $jti = $jtiClass::findOne($jtiData[1]); // id
154
                    if (!empty($jti)) {
155
                        $jti->delete();
156
                        $jti->save();
157
                    }
158
                }
159
            }
160
            $this->stdout('Done migrating Jtis !' . "\n");
161
162
            ///////////////
163
            ///
164
            /// RefreshToken
165
            ///
166
            ///////////////
167
            $this->stdout('Start migrating RefreshTokens ...' . "\n");
168
            /** @var RefreshTokenService $refreshTokenService */
169
            $refreshTokenService = Yii::createObject('sweelix\oauth2\server\interfaces\RefreshTokenServiceInterface');
170
            $refreshTokenList = $refreshTokenService->getRefreshTokenListKey();
171
            if ($db->executeCommand('EXISTS', [$refreshTokenList]) == 1) {
172
                $prompt = $this->prompt('The key "'.$refreshTokenList.'" already exists. Do you want to continue ? (yes, no)', [
173
                    'required' => true,
174
                ]);
175
                switch ($prompt) {
176
                    case 'no':
177
                    case 'n':
178
                        exit();
179
                }
180
            }
181
            $refreshToken = Yii::createObject('sweelix\oauth2\server\interfaces\RefreshTokenModelInterface');
182
            $refreshTokenClass = get_class($refreshToken);
183
            $refreshTokens = $db->executeCommand('KEYS', [$refreshTokenService->getUserRefreshTokensKey('*')]);
184
            if (!empty($refreshTokens)) {
185
                $db->executeCommand('DEL', $refreshTokens);
186
            }
187
            $refreshTokens = $db->executeCommand('KEYS', [$refreshTokenService->getClientRefreshTokensKey('*')]);
188
            if (!empty($refreshTokens)) {
189
                $db->executeCommand('DEL', $refreshTokens);
190
            }
191
            $refreshTokens = $db->executeCommand('KEYS', [$refreshTokenService->getRefreshTokenKey('*')]);
192
            foreach ($refreshTokens as $refreshTokenHash) {
0 ignored issues
show
The expression $refreshTokens of type boolean|string|null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
193
                if ($db->executeCommand('TYPE', [$refreshTokenHash]) !== 'hash') {
194
                    continue;
195
                }
196
                $refreshTokenData = $db->executeCommand('HGETALL', [$refreshTokenHash]);
197
                if (!empty($refreshTokenData)) {
198
                    /** @var RefreshTokenModelInterface $refreshToken */
199
                    $refreshToken = $refreshTokenClass::findOne($refreshTokenData[1]); // id
200
                    if (!empty($refreshToken)) {
201
                        $refreshToken->delete();
202
                        $refreshToken->save();
203
                    }
204
                }
205
            }
206
            $this->stdout('Done migrating RefreshTokens !' . "\n");
207
208
            $this->stdout('Migration done !' . "\n");
209
            $exitCode = ExitCode::OK;
210
        } catch (\Exception $e) {
211
            $this->stderr($e->getMessage() . "\n");
212
            $exitCode = ExitCode::getReason($e->getCode());
213
        }
214
        return $exitCode;
215
    }
216
}
217