Issues (183)

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/Db.php (1 issue)

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
namespace FMUP;
3
4
use FMUP\Db\Factory;
5
use FMUP\Logger;
6
7
/**
8
 * Class Db
9
 * @package FMUP
10
 */
11
class Db implements Logger\LoggerInterface
12
{
13
    use Logger\LoggerTrait;
14
15
    protected $driver = Factory::DRIVER_PDO;
16
    protected $params = array();
17
    private $driverInstance = null;
18
    /**
19
     * @var Factory
20
     */
21
    private $factory;
22
23
    /**
24
     * @param array $params
25
     */
26 18
    public function __construct(array $params = array())
27
    {
28 18
        $this->driver = isset($params['db_driver']) ? $params['db_driver'] : Factory::DRIVER_PDO;
29 18
        $this->params = $params;
30 18
    }
31
32
    /**
33
     * @return Db\DbInterface|null
34
     * @throws Db\Exception
35
     */
36 1
    public function getDriver()
37
    {
38 1
        if (!is_null($this->driverInstance)) {
39 1
            return $this->driverInstance;
40
        }
41
42 1
        $driverInstance = $this->getFactory()->create($this->driver, $this->params);
43 1
        if ($driverInstance instanceof Logger\LoggerInterface && true === $this->hasLogger()) {
44 1
            $driverInstance->setLogger($this->getLogger());
45
        }
46
47 1
        $this->driverInstance = $driverInstance;
48 1
        return $this->driverInstance;
49
    }
50
51
    /**
52
     * @return Factory
53
     */
54 2
    public function getFactory()
55
    {
56 2
        if (!$this->factory) {
57 2
            $this->factory = Factory::getInstance();
58
        }
59 2
        return $this->factory;
60
    }
61
62
    /**
63
     * @param Factory $factory
64
     * @return $this
65
     */
66 1
    public function setFactory(Factory $factory)
67
    {
68 1
        $this->factory = $factory;
69 1
        return $this;
70
    }
71
72
    /**
73
     * @param string $sql
74
     * @param array $params
75
     * @return bool
76
     */
77 1
    public function query($sql, array $params = array())
78
    {
79 1
        $statement = $this->getDriver()->prepare($sql);
80
81 1
        return $this->getDriver()->execute($statement, $params);
82
    }
83
84
    /**
85
     * @param string $sql
86
     * @param array $params
87
     * @return mixed
88
     * @throws Db\Exception
89
     * @deprecated use self::getIterator() instead
90
     */
91 1
    public function fetchAll($sql, array $params = array())
92
    {
93 1
        $statement = $this->getDriver()->prepare($sql);
94 1
        $this->getDriver()->execute($statement, $params);
95 1
        $arrayResult = $this->getDriver()->fetchAll($statement);
96 1
        return empty($arrayResult) ? array() : new \ArrayIterator($arrayResult);
97
    }
98
99
    /**
100
     * @param string $sql
101
     * @param array $params
102
     * @return array
103
     */
104 1
    public function fetchRow($sql, array $params = array())
105
    {
106 1
        $statement = $this->getDriver()->prepare($sql);
107 1
        $this->getDriver()->execute($statement, $params);
108
109 1
        return $this->getDriver()->fetchRow($statement);
110
    }
111
112
    /**
113
     * @return bool
114
     */
115 1
    public function beginTransaction()
116
    {
117 1
        return $this->getDriver()->beginTransaction();
118
    }
119
120
    /**
121
     * @return bool
122
     */
123 1
    public function commit()
124
    {
125 1
        return $this->getDriver()->commit();
126
    }
127
128
    /**
129
     * @return bool
130
     */
131 1
    public function rollback()
132
    {
133 1
        return $this->getDriver()->rollback();
134
    }
135
136
    /**
137
     * @param string $name
138
     * @return string
139
     */
140 1
    public function lastInsertId($name = null)
141
    {
142 1
        return $this->getDriver()->lastInsertId($name);
143
    }
144
145
    /**
146
     * Force reconnection
147
     * @return Db\DbInterface
148
     */
149 1
    public function forceReconnect()
150
    {
151 1
        return $this->getDriver()->forceReconnect();
152
    }
153
154
    /**
155
     * Retrieve an iterator instead of array for data rows
156
     * @param string $sql
157
     * @param array $params
158
     * @return Db\FetchIterator
159
     */
160 2
    public function getIterator($sql, array $params = array())
161
    {
162 2
        return new Db\FetchIterator($this->getDriver()->prepare($sql), $this->getDriver(), $params);
0 ignored issues
show
It seems like $this->getDriver() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
163
    }
164
}
165