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/JtiService.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
 * JtiService.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\JtiModelInterface;
20
use sweelix\oauth2\server\interfaces\JtiServiceInterface;
21
use yii\db\Exception as DatabaseException;
22
use Yii;
23
use yii\db\Query;
24
25
/**
26
 * This is the jti service for mySql
27
 *  database structure
28
 *    * oauth2:jti:<jid> : hash (Jti)
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 JtiService extends BaseService implements JtiServiceInterface
39
{
40
    /**
41
     * @var string sql jtis table
42
     */
43
    public $jtisTable = null;
44
45
    /**
46
     * Save Jti
47
     * @param JtiModelInterface $jti
48
     * @param null|array $attributes attributes to save
49
     * @return bool
50
     * @throws DatabaseException
51
     * @throws DuplicateIndexException
52
     * @throws DuplicateKeyException
53
     * @since 1.0.0
54
     */
55
    protected function insert(JtiModelInterface $jti, $attributes)
56
    {
57
        $result = false;
58
        if (!$jti->beforeSave(true)) {
59
            return $result;
60
        }
61
        $jtiKey = $jti->getKey();
62
        $entity = (new Query())
63
            ->select('*')
64
            ->from($this->jtisTable)
65
            ->where('id = :id', [':id' => $jtiKey])
66
            ->one($this->db);
67
        if ($entity !== false) {
68
            throw new DuplicateKeyException('Duplicate key "' . $jtiKey . '"');
69
        }
70
        $values = $jti->getDirtyAttributes($attributes);
0 ignored issues
show
It seems like $attributes defined by parameter $attributes on line 55 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...
71
        $jtisParameters = [];
72
        $this->setAttributesDefinitions($jti->attributesDefinition());
73
        foreach ($values as $key => $value) {
74
            if ($value !== null) {
75
                $jtisParameters[$key] = $this->convertToDatabase($key, $value);
76
            }
77
        }
78
        $jtisParameters['dateCreated'] = date('Y-m-d H:i:s');
79
        $jtisParameters['dateUpdated'] = date('Y-m-d H:i:s');
80
        try {
81
            $this->db->createCommand()
82
                ->insert($this->jtisTable, $jtisParameters)
83
                ->execute();
84
        } catch (DatabaseException $e) {
85
            // @codeCoverageIgnoreStart
86
            // we have a MYSQL exception, we should not discard
87
            Yii::debug('Error while inserting entity', __METHOD__);
88
            throw $e;
89
            // @codeCoverageIgnoreEnd
90
        }
91
        $changedAttributes = array_fill_keys(array_keys($values), null);
92
        $jti->setOldAttributes($values);
93
        $jti->afterSave(true, $changedAttributes);
94
        $result = true;
95
        return $result;
96
    }
97
98
    /**
99
     * Update Jti
100
     * @param JtiModelInterface $jti
101
     * @param null|array $attributes attributes to save
102
     * @return bool
103
     * @throws DatabaseException
104
     * @throws DuplicateIndexException
105
     * @throws DuplicateKeyException
106
     */
107
    protected function update(JtiModelInterface $jti, $attributes)
108
    {
109
        if (!$jti->beforeSave(false)) {
110
            return false;
111
        }
112
113
        $values = $jti->getDirtyAttributes($attributes);
0 ignored issues
show
It seems like $attributes defined by parameter $attributes on line 107 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...
114
        $modelKey = $jti->key();
115
        if (isset($values[$modelKey]) === true) {
116
            $entity = (new Query())
117
                ->select('*')
118
                ->from($this->jtisTable)
119
                ->where('id = :id', [':id' => $values[$modelKey]])
120
                ->one($this->db);
121
            if ($entity !== false) {
122
                throw new DuplicateKeyException('Duplicate key "' . $values[$modelKey] . '"');
123
            }
124
        }
125
        $jtiKey = isset($values[$modelKey]) ? $values[$modelKey] : $jti->getKey();
126
127
        $jtiParameters = [];
128
        $this->setAttributesDefinitions($jti->attributesDefinition());
129
        foreach ($values as $key => $value) {
130
            $jtiParameters[$key] = ($value !== null) ? $this->convertToDatabase($key, $value) : null;
131
        }
132
        $jtiParameters['dateUpdated'] = date('Y-m-d H:i:s');
133
        try {
134
            if (array_key_exists($modelKey, $values) === true) {
135
                $oldJtiKey = $jti->getOldKey();
136
                $this->db->createCommand()
137
                    ->update($this->jtisTable, $jtiParameters, 'id = :id', [':id' => $oldJtiKey])
138
                    ->execute();
139
            } else {
140
                $this->db->createCommand()
141
                    ->update($this->jtisTable, $jtiParameters, 'id = :id', [':id' => $jtiKey])
142
                    ->execute();
143
            }
144
        } catch (DatabaseException $e) {
145
            // @codeCoverageIgnoreStart
146
            // we have a MYSQL exception, we should not discard
147
            Yii::debug('Error while updating entity', __METHOD__);
148
            throw $e;
149
            // @codeCoverageIgnoreEnd
150
        }
151
152
        $changedAttributes = [];
153
        foreach ($values as $name => $value) {
154
            $oldAttributes = $jti->getOldAttributes();
155
            $changedAttributes[$name] = isset($oldAttributes[$name]) ? $oldAttributes[$name] : null;
156
            $jti->setOldAttribute($name, $value);
157
        }
158
        $jti->afterSave(false, $changedAttributes);
159
        return true;
160
    }
161
162
    /**
163
     * @inheritdoc
164
     */
165
    public function save(JtiModelInterface $jti, $attributes)
166
    {
167
        if ($jti->getIsNewRecord()) {
168
            $result = $this->insert($jti, $attributes);
169
        } else {
170
            $result = $this->update($jti, $attributes);
171
        }
172
        return $result;
173
    }
174
175
    /**
176
     * @inheritdoc
177
     */
178
    public function findOne($key)
179
    {
180
        $record = null;
181
        $jtiData = (new Query())
182
            ->select('*')
183
            ->from($this->jtisTable)
184
            ->where('id = :id', [':id' => $key])
185
            ->one($this->db);
186
        if ($jtiData !== false) {
187
            $record = Yii::createObject('sweelix\oauth2\server\interfaces\JtiModelInterface');
188
            /** @var JtiModelInterface $record */
189
            $properties = $record->attributesDefinition();
190
            $this->setAttributesDefinitions($properties);
191
            $attributes = [];
192
            foreach ($jtiData as $key => $value) {
193
                if (isset($properties[$key]) === true) {
194
                    $jtiData[$key] = $this->convertToModel($key, $value);
195
                    $record->setAttribute($key, $jtiData[$key]);
196
                    $attributes[$key] = $jtiData[$key];
197
                    // @codeCoverageIgnoreStart
198
                } elseif ($record->canSetProperty($key)) {
199
                    // TODO: find a way to test attribute population
200
                    $record->{$key} = $value;
201
                }
202
                // @codeCoverageIgnoreEnd
203
            }
204
            if (empty($attributes) === false) {
205
                $record->setOldAttributes($attributes);
206
            }
207
            $record->afterFind();
208
        }
209
        return $record;
210
    }
211
212
    /**
213
     * @inheritdoc
214
     */
215
    public function delete(JtiModelInterface $jti)
216
    {
217
        $result = false;
218
        if ($jti->beforeDelete()) {
219
            //TODO: check results to return correct information
220
            $this->db->createCommand()
221
                ->delete($this->jtisTable, 'id = :id', [':id' => $jti->getKey()])
222
                ->execute();
223
            $jti->setIsNewRecord(true);
224
            $jti->afterDelete();
225
            $result = true;
226
        }
227
        return $result;
228
    }
229
230
    /**
231
     * @inheritdoc
232
     */
233
    public function findAllBySubject($subject)
234
    {
235
        $jtisList = (new Query())
236
            ->select('*')
237
            ->from($this->jtisTable)
238
            ->where('subject = :subject', [':subject' => $subject])
239
            ->all($this->db);
240
        $jtis = [];
241
        foreach ($jtisList as $jti) {
242
            $result = $this->findOne($jti['id']);
243
            if ($result instanceof JtiModelInterface) {
244
                $jtis[] = $result;
245
            }
246
        }
247
        return $jtis;
248
    }
249
250
    /**
251
     * @inheritdoc
252
     */
253
    public function deleteAllBySubject($subject)
254
    {
255
        $jtis = $this->findAllBySubject($subject);
256
        foreach ($jtis as $jti) {
257
            $this->delete($jti);
258
        }
259
        return true;
260
    }
261
262
    /**
263
     * @inheritdoc
264
     */
265
    public function findAllByClientId($clientId)
266
    {
267
        $jtisList = (new Query())
268
            ->select('*')
269
            ->from($this->jtisTable)
270
            ->where('clientId = :clientId', [':clientId' => $clientId])
271
            ->all($this->db);
272
        $jtis = [];
273
        foreach ($jtisList as $jti) {
274
            $result = $this->findOne($jti['id']);
275
            if ($result instanceof JtiModelInterface) {
276
                $jtis[] = $result;
277
            }
278
        }
279
        return $jtis;
280
    }
281
282
    /**
283
     * @inheritdoc
284
     */
285
    public function deleteAllByClientId($clientId)
286
    {
287
        $jtis = $this->findAllByClientId($clientId);
288
        foreach ($jtis as $jti) {
289
            $this->delete($jti);
290
        }
291
        return true;
292
    }
293
294
    /**
295
     * @inheritdoc
296
     */
297
    public function deleteAllExpired()
298
    {
299
        $jtis = (new Query())->select('*')
300
            ->from($this->jtisTable)
301
            ->where('expires < :date', [':date' => date('Y-m-d H:i:s')])
302
            ->all($this->db);
303
        foreach ($jtis as $jtiQuery) {
304
            $jti = $this->findOne($jtiQuery['id']);
305
            if ($jti instanceof JtiModelInterface) {
306
                $this->delete($jti);
307
            }
308
        }
309
        return true;
310
    }
311
312
    /**
313
     * @inheritdoc
314
     */
315
    public function findAll()
316
    {
317
        $jtisList = (new Query())
318
            ->select('*')
319
            ->from($this->jtisTable)
320
            ->all($this->db);
321
        $jtis = [];
322
        foreach ($jtisList as $jti) {
323
            $result = $this->findOne($jti['id']);
324
            if ($result instanceof JtiModelInterface) {
325
                $jtis[] = $result;
326
            }
327
        }
328
        return $jtis;
329
    }
330
}