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/ScopeService.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
 * ScopeService.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\ScopeModelInterface;
20
use sweelix\oauth2\server\interfaces\ScopeServiceInterface;
21
use yii\db\Exception as DatabaseException;
22
use Yii;
23
use yii\db\Query;
24
25
/**
26
 * This is the scope service for mySql
27
 *  database structure
28
 *    * oauth2:scopes:<sid> : hash (Scope)
29
 *    * oauth2:scopes:keys : set scopeIds
30
 *    * oauth2:scopes:defaultkeys : set default scopeIds
31
 *
32
 * @author Philippe Gaultier <[email protected]>
33
 * @copyright 2010-2017 Philippe Gaultier
34
 * @license http://www.sweelix.net/license license
35
 * @version 1.2.0
36
 * @link http://www.sweelix.net
37
 * @package sweelix\oauth2\server\services\mySql
38
 * @since 1.0.0
39
 */
40
class ScopeService extends BaseService implements ScopeServiceInterface
41
{
42
    /**
43
     * @var string sql scope client table
44
     */
45
    public $scopeClientTable = null;
46
47
    /**
48
     * @var string sql scopes table
49
     */
50
    public $scopesTable = null;
51
52
    /**
53
     * Save Scope
54
     * @param ScopeModelInterface $scope
55
     * @param null|array $attributes attributes to save
56
     * @return bool
57
     * @throws DatabaseException
58
     * @throws DuplicateIndexException
59
     * @throws DuplicateKeyException
60
     * @since 1.0.0
61
     */
62
    protected function insert(ScopeModelInterface $scope, $attributes)
63
    {
64
        $result = false;
65
        if (!$scope->beforeSave(true)) {
66
            return $result;
67
        }
68
        $entity = (new Query())
69
            ->select('*')
70
            ->from($this->scopesTable)
71
            ->where('id = :id', [':id' => $scope->id])
0 ignored issues
show
Accessing id on the interface sweelix\oauth2\server\in...ces\ScopeModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
72
            ->one($this->db);
73
        if ($entity !== false) {
74
            throw new DuplicateKeyException('Duplicate key "' . $scope->id . '"');
0 ignored issues
show
Accessing id on the interface sweelix\oauth2\server\in...ces\ScopeModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
75
        }
76
        $values = $scope->getDirtyAttributes($attributes);
0 ignored issues
show
It seems like $attributes defined by parameter $attributes on line 62 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...
77
        $scopeParameters = [];
78
        $this->setAttributesDefinitions($scope->attributesDefinition());
79
        foreach ($values as $key => $value) {
80
            if ($value !== null) {
81
                $scopeParameters[$key] = $this->convertToDatabase($key, $value);
82
            }
83
        }
84
        $scopeParameters['dateCreated'] = date('Y-m-d H:i:s');
85
        $scopeParameters['dateUpdated'] = date('Y-m-d H:i:s');
86
        try {
87
            $this->db->createCommand()
88
                ->insert($this->scopesTable, $scopeParameters)
89
                ->execute();
90
        } catch (DatabaseException $e) {
91
            // @codeCoverageIgnoreStart
92
            // we have a MYSQL exception, we should not discard
93
            Yii::debug('Error while inserting entity', __METHOD__);
94
            throw $e;
95
            // @codeCoverageIgnoreEnd
96
        }
97
        $changedAttributes = array_fill_keys(array_keys($values), null);
98
        $scope->setOldAttributes($values);
99
        $scope->afterSave(true, $changedAttributes);
100
        $result = true;
101
        return $result;
102
    }
103
104
    /**
105
     * Update ScopeModelInterface
106
     * @param ScopeModelInterface $scope
107
     * @param null|array $attributes attributes to save
108
     * @return bool
109
     * @throws DatabaseException
110
     * @throws DuplicateIndexException
111
     * @throws DuplicateKeyException
112
     */
113
    protected function update(ScopeModelInterface $scope, $attributes)
114
    {
115
        if (!$scope->beforeSave(false)) {
116
            return false;
117
        }
118
119
        $values = $scope->getDirtyAttributes($attributes);
0 ignored issues
show
It seems like $attributes defined by parameter $attributes on line 113 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...
120
        $modelKey = $scope->key();
121
        if (isset($values[$modelKey]) === true) {
122
            $entity = (new Query())
123
                ->select('*')
124
                ->from($this->scopesTable)
125
                ->where('id = :id', [':id' => $values[$modelKey]])
126
                ->one($this->db);
127
            if ($entity !== false) {
128
                throw new DuplicateKeyException('Duplicate key "' . $values[$modelKey] . '"');
129
            }
130
        }
131
        $scopeKey = isset($values[$modelKey]) ? $values[$modelKey] : $scope->getKey();
132
133
        $scopeParameters = [];
134
        $this->setAttributesDefinitions($scope->attributesDefinition());
135
        foreach ($values as $key => $value) {
136
            $scopeParameters[$key] = ($value !== null) ? $this->convertToDatabase($key, $value) : null;
137
        }
138
        $scopeParameters['dateUpdated'] = date('Y-m-d H:i:s');
139
        try {
140
            if (array_key_exists($modelKey, $values) === true) {
141
                $oldScopeKey = $scope->getOldKey();
142
                $this->db->createCommand()
143
                    ->update($this->scopesTable, $scopeParameters, 'id = :id', [':id' => $oldScopeKey])
144
                    ->execute();
145
            } else {
146
                $this->db->createCommand()
147
                    ->update($this->scopesTable, $scopeParameters, 'id = :id', [':id' => $scopeKey])
148
                    ->execute();
149
            }
150
        } catch (DatabaseException $e) {
151
            // @codeCoverageIgnoreStart
152
            // we have a MYSQL exception, we should not discard
153
            Yii::debug('Error while updating entity', __METHOD__);
154
            throw $e;
155
            // @codeCoverageIgnoreEnd
156
        }
157
158
        $changedAttributes = [];
159
        foreach ($values as $name => $value) {
160
            $oldAttributes = $scope->getOldAttributes();
161
            $changedAttributes[$name] = isset($oldAttributes[$name]) ? $oldAttributes[$name] : null;
162
            $scope->setOldAttribute($name, $value);
163
        }
164
        $scope->afterSave(false, $changedAttributes);
165
        return true;
166
    }
167
168
    /**
169
     * @inheritdoc
170
     */
171
    public function save(ScopeModelInterface $scope, $attributes)
172
    {
173
        if ($scope->getIsNewRecord()) {
174
            $result = $this->insert($scope, $attributes);
175
        } else {
176
            $result = $this->update($scope, $attributes);
177
        }
178
        return $result;
179
    }
180
181
    /**
182
     * @inheritdoc
183
     */
184
    public function findOne($key)
185
    {
186
        $record = null;
187
        $scopeData = (new Query())
188
            ->select('*')
189
            ->from($this->scopesTable)
190
            ->where('id = :id', [':id' => $key])
191
            ->one($this->db);
192
        if ($scopeData !== false) {
193
            $record = Yii::createObject('sweelix\oauth2\server\interfaces\ScopeModelInterface');
194
            /** @var ScopeModelInterface $record */
195
            $properties = $record->attributesDefinition();
196
            $this->setAttributesDefinitions($properties);
197
            $attributes = [];
198
            foreach ($scopeData as $key => $value) {
199
                if (isset($properties[$key]) === true) {
200
                    $scopeData[$key] = $this->convertToModel($key, $value);
201
                    $record->setAttribute($key, $scopeData[$key]);
202
                    $attributes[$key] = $scopeData[$key];
203
                    // @codeCoverageIgnoreStart
204
                } elseif ($record->canSetProperty($key)) {
205
                    // TODO: find a way to test attribute population
206
                    $record->{$key} = $value;
207
                }
208
                // @codeCoverageIgnoreEnd
209
            }
210
            if (empty($attributes) === false) {
211
                $record->setOldAttributes($attributes);
212
            }
213
            $record->afterFind();
214
        }
215
        return $record;
216
    }
217
218
    /**
219
     * @inheritdoc
220
     */
221
    public function delete(ScopeModelInterface $scope)
222
    {
223
        $result = false;
224
        if ($scope->beforeDelete()) {
225
            //TODO: check results to return correct information
226
            $this->db->createCommand()
227
                ->delete($this->scopesTable, 'id = :id', [':id' => $scope->getKey()])
228
                ->execute();
229
            $scope->setIsNewRecord(true);
230
            $scope->afterDelete();
231
            $result = true;
232
        }
233
        return $result;
234
    }
235
236
    /**
237
     * @inheritdoc
238
     */
239
    public function findAvailableScopeIds()
240
    {
241
        $tmpScopes = (new Query())
242
            ->select('id')
243
            ->from($this->scopesTable)
244
            ->all($this->db);
245
        $scopes = [];
246
        foreach ($tmpScopes as $scope) {
247
            $scopes[] = $scope['id'];
248
        }
249
        return $scopes;
250
    }
251
252
    /**
253
     * @inheritdoc
254
     */
255
    public function findDefaultScopeIds($clientId = null)
256
    {
257
        //TODO: add default scopes for clients
258
        $defaultScopeIds = [];
259
        if ($clientId !== null) {
260
            $tmpScopeIds = (new Query())
261
                ->select('scopeId as ID')
262
                ->from($this->scopeClientTable)
263
                ->where('clientId = :clientId', [':clientId' => $clientId])
264
                ->all($this->db);
265
        } else {
266
            $tmpScopeIds = (new Query())
267
                ->select('id as ID')
268
                ->from($this->scopesTable)
269
                ->where('isDefault = :isDefault', [':isDefault' => true])
270
                ->all($this->db);
271
        }
272
        foreach ($tmpScopeIds as $tmpScopeId) {
273
            $defaultScopeIds[] = $tmpScopeId['ID'];
274
        }
275
        return $defaultScopeIds;
276
    }
277
}
278