Issues (194)

Security Analysis    no vulnerabilities found

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/Repository/AbstractEndpointRepository.php (7 issues)

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 Ps2alerts\Api\Repository;
4
5
use Aura\Sql\Profiler;
6
use Aura\SqlQuery\AbstractQuery;
7
use Aura\SqlQuery\QueryFactory;
8
use Ps2alerts\Api\Contract\ConfigAwareInterface;
9
use Ps2alerts\Api\Contract\ConfigAwareTrait;
10
use Ps2alerts\Api\Contract\DatabaseAwareInterface;
11
use Ps2alerts\Api\Contract\DatabaseAwareTrait;
12
use Ps2alerts\Api\Contract\RedisAwareInterface;
13
use Ps2alerts\Api\Contract\RedisAwareTrait;
14
use Ps2alerts\Api\Contract\UuidAwareInterface;
15
use Ps2alerts\Api\Contract\UuidAwareTrait;
16
17
abstract class AbstractEndpointRepository implements
18
    ConfigAwareInterface,
19
    DatabaseAwareInterface,
20
    RedisAwareInterface,
21
    UuidAwareInterface
22
{
23
    use ConfigAwareTrait;
24
    use DatabaseAwareTrait;
25
    use RedisAwareTrait;
26
    use UuidAwareTrait;
27
28
    /**
29
     * Determines the table that the DB is interfacing with
30
     *
31
     * @return string
32
     */
33
    abstract public function getTable();
34
35
    /**
36
     * Determines the primary key of the table
37
     *
38
     * @return string
39
     */
40
    abstract public function getPrimaryKey();
41
42
    /**
43
     * Determines the Result key of the table
44
     *
45
     * @return string
46
     */
47
    abstract public function getResultKey();
48
49
    /**
50
     * Allows the ability to overload and swap the DB driver if required
51
     *
52
     * @return \Aura\Sql\ExtendedPdo
53
     */
54
    protected function getDbDriver()
55
    {
56
        return $this->getDatabaseDriver();
57
    }
58
59
    /**
60
     * Allows the ability to overload and swap the DB driver if required
61
     *
62
     * @return \Aura\Sql\ExtendedPdo
63
     */
64
    protected function getDbArchiveDriver()
65
    {
66
        return $this->getDatabaseArchiveDriver();
67
    }
68
69
    /**
70
     * Builds a new query factory ready for use with the QueryObjects
71
     *
72
     * @param string $type    Type of query to use, such as Select, Update etc.
73
     * @param bool   $newOnly Whether we're getting a new query or using a raw one
74
     *
75
     * @return \Aura\SqlQuery\QueryInterface
76
     */
77
    public function newQuery($type = 'single', $newOnly = false)
78
    {
79
        $factory = new QueryFactory('mysql');
80
81
        if ($type === 'single') {
82
            $query = $factory->newSelect();
83
84
            if (!$newOnly) {
85
                $query->from($this->getTable());
86
            }
87
        } elseif ($type === 'update') {
88
            $query = $factory->newUpdate();
89
        } elseif ($type === 'delete') {
90
            $query = $factory->newDelete();
91
        }
92
93
        return $query;
0 ignored issues
show
The variable $query does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
94
    }
95
96
    /**
97
     * Executes the statement to the DB and returns the results
98
     *
99
     * @param  \Aura\SqlQuery\AbstractQuery $query   Query object to execute
100
     * @param  boolean                      $single  Return a sinlge record or not
101
     * @param  boolean                      $object  Return an object or not
102
     * @param  boolean                      $archive Flag to force to use the archive database
103
     *
104
     * @return array
105
     */
106
    public function fireStatementAndReturn(
107
        $query,
108
        $single  = false,
109
        $object  = false,
110
        $archive = false
111
    ) {
112
        $pdo = $this->getDbDriver();
113
        if ($archive === true) {
114
            // If we've been bounced back to search the archive, load that driver
115
            $pdo = $this->getDbArchiveDriver();
116
        }
117
118
        if ($this->getConfigItem('db_query_debug') === true) {
119
            $pdo->setProfiler(new Profiler);
120
            $pdo->getProfiler()->setActive(true);
121
            var_dump($pdo);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($pdo); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
122
            var_dump($query->getStatement());
123
            var_dump($query->getBindValues());
124
        }
125
126
        if ($single === false) {
127 View Code Duplication
            if ($object === false) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
                $return = $pdo->fetchAll($query->getStatement(), $query->getBindValues());
129
            } else {
130
                $return = $pdo->fetchObjects($query->getStatement(), $query->getBindValues());
131
            }
132 View Code Duplication
        } else {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
            if ($object === false) {
134
                $return = $pdo->fetchOne($query->getStatement(), $query->getBindValues());
135
            } else {
136
                $return = $pdo->fetchObject($query->getStatement(), $query->getBindValues());
137
            }
138
        }
139
140
        if ($this->getConfigItem('db_query_debug') === true) {
141
            var_dump($pdo->getProfiler()->getProfiles());
142
        }
143
144
        // If no data, recall this function with archive flag
145
        if (empty($return) && $archive === false) {
146
            return $this->fireStatementAndReturn(
147
                $query,
148
                $single,
149
                $object,
150
                true
151
            );
152
        }
153
154
        return $return;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $return; (array|false|object) is incompatible with the return type documented by Ps2alerts\Api\Repository...:fireStatementAndReturn of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
155
    }
156
157
    /**
158
     * Allows for Raw SQL firing without the query builder
159
     *
160
     * @param  string  $sql
161
     * @param  boolean $single
162
     *
163
     * @return array
164
     */
165
    public function readRaw($sql, $single = false)
166
    {
167
        $pdo = $this->getDbDriver();
168
169
        if ($single === false) {
170
            return $pdo->fetchAll($sql);
171
        }
172
173
        return $pdo->fetchOne($sql);
174
    }
175
176
    /**
177
     * Reads a single record from the database
178
     *
179
     * @param  string $id
180
     * @param  string $keyType
181
     * @param  boolean $object
182
     * @param  boolean $archive
183
     *
184
     * @return array
185
     */
186 View Code Duplication
    public function readSinglebyId($id, $keyType = 'primary', $object = false, $archive = false)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188
        $query = $this->newQuery();
189
        $key = $this->returnKeyType($keyType);
190
191
        $query->cols(['*'])
192
              ->where("{$key} = ?", $id);
193
194
        return $this->fireStatementAndReturn($query, true, $object, $archive);
195
    }
196
197
    /**
198
     * Allows reading of elements by ID including those from the archive
199
     *
200
     * @param  string $id
201
     * @param  string $keyType
202
     * @param  boolean $object
203
     *
204
     * @return array
205
     */
206
    public function readSingleByIdWithArchive($id, $keyType = 'primary', $object = false)
207
    {
208
        return array_merge(
209
            $this->readSingleById($id, $keyType, $object),
210
            $this->readSingleById($id, $keyType, $object, true)
211
        );
212
    }
213
214
    /**
215
     * Reads all related records from the database
216
     *
217
     * @param  string  $id
218
     * @param  string  $keyType Field to search on
219
     * @param  boolean $archive
220
     *
221
     * @return array
222
     */
223 View Code Duplication
    public function readAllById($id, $keyType = 'primary', $archive = false)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
    {
225
        $query = $this->newQuery();
226
        $key = $this->returnKeyType($keyType);
227
228
        $query->cols(['*'])
229
              ->where("{$key} = ?", $id);
230
231
        return $this->fireStatementAndReturn($query, false, false, $archive);
232
    }
233
234
    /**
235
     * Grab all data from the database including from the archive
236
     *
237
     * @param  string $id
238
     * @param  string $keyType
239
     *
240
     * @return array
241
     */
242
    public function readAllByIdWithArchive($id, $keyType = 'primary')
243
    {
244
        return array_merge(
245
            $this->readAllById($id, $keyType),
246
            $this->readAllById($id, $keyType, true)
247
        );
248
    }
249
250
    /**
251
     * Reads all records based off a simple where statement
252
     *
253
     * @param  array $fields
254
     *
255
     * @return array
256
     */
257
    public function readAllByFields($fields)
258
    {
259
        $query = $this->newQuery();
260
        $query->cols(['*']);
261
262
        foreach ($fields as $field => $value) {
263
            $query->where("{$field} = ?", $value);
264
        }
265
266
        return $this->fireStatementAndReturn($query);
267
    }
268
269
    /**
270
     * Returns all records with no filtering
271
     *
272
     * @return array
273
     */
274
    public function readAll()
275
    {
276
        $query = $this->newQuery();
277
        $query->cols(['*']);
278
279
        return $this->fireStatementAndReturn($query);
280
    }
281
282
    /**
283
     * Reads the count of records based off a where statement
284
     *
285
     * @param  array $fields
286
     *
287
     * @return array
288
     */
289
    public function readCountByFields($fields)
290
    {
291
        $query = $this->newQuery();
292
        $key   = $this->returnKeyType('primary');
293
294
        $query->cols(["COUNT({$key}) as COUNT"]);
295
296
        foreach ($fields as $field => $value) {
297
            $query->where("{$field} = ?", $value);
298
        }
299
300
        $result = $this->fireStatementAndReturn($query);
301
302
        // Done this to prevent the need for clients to also do this. Returns a single number this way.
303
        return $result[0]["COUNT"];
304
    }
305
306
    /**
307
     * Sets the proper key to search on based off a string
308
     *
309
     * @param  string $key
310
     *
311
     * @return string
312
     */
313
    public function returnKeyType($key)
314
    {
315
        switch ($key) {
316
            case 'result':
317
                return $this->getResultKey();
318
            case 'primary':
319
                return $this->getPrimaryKey();
320
            default:
321
                return $key;
322
        }
323
    }
324
325
    /**
326
     * Generates a quoted string which is appropiate for WHERE IN statements
327
     *
328
     * @param  array  $array
329
     *
330
     * @return string
331
     */
332
    public function generateWhereInString(array $array)
333
    {
334
        $pdo = $this->getDbDriver();
335
        $string = "({$pdo->quote($array)})";
336
        return $string;
337
    }
338
}
339