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/services/mySql/AuthCodeService.php (2 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
 * AuthCodeService.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author Philippe Gaultier <[email protected]>
8
 * @copyright 2010-2017 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version 1.2.0
11
 * @link http://www.sweelix.net
12
 * @package sweelix\oauth2\server\services\mySql
13
 */
14
15
namespace sweelix\oauth2\server\services\mySql;
16
17
use sweelix\oauth2\server\exceptions\DuplicateIndexException;
18
use sweelix\oauth2\server\exceptions\DuplicateKeyException;
19
use sweelix\oauth2\server\interfaces\AuthCodeModelInterface;
20
use sweelix\oauth2\server\interfaces\AuthCodeServiceInterface;
21
use yii\db\Exception as DatabaseException;
22
use Yii;
23
use yii\db\Query;
24
25
/**
26
 * This is the auth code service for mySql
27
 *  database structure
28
 *    * oauth2:authCodes:<aid> : hash (AuthCode)
29
 *
30
 * @author Philippe Gaultier <[email protected]>
31
 * @copyright 2010-2017 Philippe Gaultier
32
 * @license http://www.sweelix.net/license license
33
 * @version 1.2.0
34
 * @link http://www.sweelix.net
35
 * @package sweelix\oauth2\server\services\mySql
36
 * @since 1.0.0
37
 */
38
class AuthCodeService extends BaseService implements AuthCodeServiceInterface
39
{
40
    /**
41
     * @var string sql authorizationCodes table
42
     */
43
    public $authorizationCodesTable = null;
44
45
    /**
46
     * @var string sql scope authorizationCode table
47
     */
48
    public $scopeAuthorizationCodeTable = null;
49
50
    /**
51
     * Save Auth Code
52
     * @param AuthCodeModelInterface $authCode
53
     * @param null|array $attributes attributes to save
54
     * @return bool
55
     * @throws DatabaseException
56
     * @throws DuplicateIndexException
57
     * @throws DuplicateKeyException
58
     * @since 1.0.0
59
     */
60
    protected function insert(AuthCodeModelInterface $authCode, $attributes)
61
    {
62
        $result = false;
63
        if (!$authCode->beforeSave(true)) {
64
            return $result;
65
        }
66
        $authCodeKey = $authCode->getKey();
67
        $entity = (new Query())
68
            ->select('*')
69
            ->from($this->authorizationCodesTable)
70
            ->where('id = :id', [':id' => $authCodeKey])
71
            ->one($this->db);
72
        if ($entity !== false) {
73
            throw new DuplicateKeyException('Duplicate key "' . $authCodeKey . '"');
74
        }
75
        $values = $authCode->getDirtyAttributes($attributes);
0 ignored issues
show
It seems like $attributes defined by parameter $attributes on line 60 can also be of type array; however, sweelix\oauth2\server\in...e::getDirtyAttributes() does only seem to accept array<integer,string>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
76
        $authCodeParameters = [];
77
        $this->setAttributesDefinitions($authCode->attributesDefinition());
78
        foreach ($values as $key => $value) {
79
            if (($value !== null) && ($key !== 'scopes')) {
80
                $authCodeParameters[$key] = $this->convertToDatabase($key, $value);
81
            }
82
        }
83
        $authCodeParameters['dateCreated'] = date('Y-m-d H:i:s');
84
        $authCodeParameters['dateUpdated'] = date('Y-m-d H:i:s');
85
        try {
86
            $this->db->createCommand()
87
                ->insert($this->authorizationCodesTable, $authCodeParameters)
88
                ->execute();
89
            if (!empty($values['scopes'])) {
90
                $values['scopes'] = array_unique($values['scopes']);
91
                foreach ($values['scopes'] as $scope) {
92
                    $scopeAuthCodeParams = [
93
                        'scopeId' => $scope,
94
                        'authorizationCodeId' => $authCodeKey
95
                    ];
96
                    $this->db->createCommand()
97
                        ->insert($this->scopeAuthorizationCodeTable, $scopeAuthCodeParams)
98
                        ->execute();
99
                }
100
            }
101
        } catch (DatabaseException $e) {
102
            // @codeCoverageIgnoreStart
103
            // we have a MYSQL exception, we should not discard
104
            Yii::debug('Error while inserting entity', __METHOD__);
105
            throw $e;
106
            // @codeCoverageIgnoreEnd
107
        }
108
        $changedAttributes = array_fill_keys(array_keys($values), null);
109
        $authCode->setOldAttributes($values);
110
        $authCode->afterSave(true, $changedAttributes);
111
        $result = true;
112
        return $result;
113
    }
114
115
    /**
116
     * Update Auth Code
117
     * @param AuthCodeModelInterface $authCode
118
     * @param null|array $attributes attributes to save
119
     * @return bool
120
     * @throws DatabaseException
121
     * @throws DuplicateIndexException
122
     * @throws DuplicateKeyException
123
     */
124
    protected function update(AuthCodeModelInterface $authCode, $attributes)
125
    {
126
        if (!$authCode->beforeSave(false)) {
127
            return false;
128
        }
129
130
        $values = $authCode->getDirtyAttributes($attributes);
0 ignored issues
show
It seems like $attributes defined by parameter $attributes on line 124 can also be of type array; however, sweelix\oauth2\server\in...e::getDirtyAttributes() does only seem to accept array<integer,string>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
131
        $modelKey = $authCode->key();
132
        if (isset($values[$modelKey]) === true) {
133
            $entity = (new Query())
134
                ->select('*')
135
                ->from($this->authorizationCodesTable)
136
                ->where('id = :id', [':id' => $values[$modelKey]])
137
                ->one($this->db);
138
            if ($entity !== false) {
139
                throw new DuplicateKeyException('Duplicate key "' . $values[$modelKey] . '"');
140
            }
141
        }
142
        $authCodeKey = isset($values[$modelKey]) ? $values[$modelKey] : $authCode->getKey();
143
144
        $authCodeParameters = [];
145
        $this->setAttributesDefinitions($authCode->attributesDefinition());
146
        foreach ($values as $key => $value) {
147
            if ($key !== 'scopes') {
148
                $authCodeParameters[$key] = ($value !== null) ? $this->convertToDatabase($key, $value) : null;
149
            }
150
        }
151
        $authCodeParameters['dateUpdated'] = date('Y-m-d H:i:s');
152
        try {
153
            if (array_key_exists($modelKey, $values) === true) {
154
                $oldAuthCodeKey = $authCode->getOldKey();
155
                $this->db->createCommand()
156
                    ->update($this->authorizationCodesTable, $authCodeParameters, 'id = :id', [':id' => $oldAuthCodeKey])
157
                    ->execute();
158
            } else {
159
                $this->db->createCommand()
160
                    ->update($this->authorizationCodesTable, $authCodeParameters, 'id = :id', [':id' => $authCodeKey])
161
                    ->execute();
162
            }
163
            if (isset($values['scopes'])) {
164
                $values['scopes'] = array_unique($values['scopes']);
165
                $scopeAuthCodes = (new Query())
166
                    ->select('*')
167
                    ->from($this->scopeAuthorizationCodeTable)
168
                    ->where('authorizationCodeId = :authorizationCodeId', [':authorizationCodeId' => $authCodeKey])
169
                    ->all($this->db);
170
                foreach ($scopeAuthCodes as $scopeAuthCode) {
171
                    if (($index = array_search($scopeAuthCode['scopeId'], $values['scopes'])) === false) {
172
                        $this->db->createCommand()
173
                            ->delete($this->scopeAuthorizationCodeTable,
174
                                'authorizationCodeId = :authorizationCodeId AND scopeId = :scopeId',
175
                                [':authorizationCodeId' => $authCodeKey, ':scopeId' => $scopeAuthCode['scopeId']])
176
                            ->execute();
177
                    } else {
178
                        unset($values['scopes'][$index]);
179
                    }
180
                }
181
                foreach ($values['scopes'] as $scope) {
182
                    $scopeAuthCodeParams = [
183
                        'scopeId' => $scope,
184
                        'authorizationCodeId' => $authCodeKey
185
                    ];
186
                    $this->db->createCommand()
187
                        ->insert($this->scopeAuthorizationCodeTable, $scopeAuthCodeParams)
188
                        ->execute();
189
                }
190
            }
191
        } catch (DatabaseException $e) {
192
            // @codeCoverageIgnoreStart
193
            // we have a MYSQL exception, we should not discard
194
            Yii::debug('Error while updating entity', __METHOD__);
195
            throw $e;
196
            // @codeCoverageIgnoreEnd
197
        }
198
199
        $changedAttributes = [];
200
        foreach ($values as $name => $value) {
201
            $oldAttributes = $authCode->getOldAttributes();
202
            $changedAttributes[$name] = isset($oldAttributes[$name]) ? $oldAttributes[$name] : null;
203
            $authCode->setOldAttribute($name, $value);
204
        }
205
        $authCode->afterSave(false, $changedAttributes);
206
        return true;
207
    }
208
209
    /**
210
     * @inheritdoc
211
     */
212
    public function save(AuthCodeModelInterface $authCode, $attributes)
213
    {
214
        if ($authCode->getIsNewRecord()) {
215
            $result = $this->insert($authCode, $attributes);
216
        } else {
217
            $result = $this->update($authCode, $attributes);
218
        }
219
        return $result;
220
    }
221
222
    /**
223
     * @inheritdoc
224
     */
225
    public function findOne($key)
226
    {
227
        $record = null;
228
        $authCodeData = (new Query())
229
            ->select('*')
230
            ->from($this->authorizationCodesTable)
231
            ->where('id = :id', [':id' => $key])
232
            ->one($this->db);
233
234
        if ($authCodeData !== false) {
235
            $authCodeData['scopes'] = [];
236
            $tmpScopes = (new Query())
237
                ->select('scopeId')
238
                ->from($this->scopeAuthorizationCodeTable)
239
                ->all($this->db);
240
            foreach ($tmpScopes as $scope) {
241
                $authCodeData['scopes'][] = $scope['scopeId'];
242
            }
243
244
            $record = Yii::createObject('sweelix\oauth2\server\interfaces\AuthCodeModelInterface');
245
            /** @var AuthCodeModelInterface $record */
246
            $properties = $record->attributesDefinition();
247
            $this->setAttributesDefinitions($properties);
248
            $attributes = [];
249
            foreach ($authCodeData as $key => $value) {
250
                if (isset($properties[$key]) === true) {
251
                    $authCodeData[$key] = $this->convertToModel($key, $value);
252
                    $record->setAttribute($key, $authCodeData[$key]);
253
                    $attributes[$key] = $authCodeData[$key];
254
                    // @codeCoverageIgnoreStart
255
                } elseif ($record->canSetProperty($key)) {
256
                    // TODO: find a way to test attribute population
257
                    $record->{$key} = $value;
258
                }
259
                // @codeCoverageIgnoreEnd
260
            }
261
            if (empty($attributes) === false) {
262
                $record->setOldAttributes($attributes);
263
            }
264
            $record->afterFind();
265
        }
266
        return $record;
267
    }
268
269
    /**
270
     * @inheritdoc
271
     */
272
    public function delete(AuthCodeModelInterface $authCode)
273
    {
274
        $result = false;
275
        if ($authCode->beforeDelete()) {
276
            //TODO: check results to return correct information
277
            $this->db->createCommand()
278
                ->delete($this->authorizationCodesTable, 'id = :id', [':id' => $authCode->getKey()])
279
                ->execute();
280
            $authCode->setIsNewRecord(true);
281
            $authCode->afterDelete();
282
            $result = true;
283
        }
284
        return $result;
285
    }
286
}