Issues (2)

Security Analysis    no request data  

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/Factory.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
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/cache/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/cache
7
 */
8
9
namespace Flipbox\Cache;
10
11
use Flipbox\Cache\Exceptions\InvalidDriverException;
12
use Flipbox\Skeleton\Helpers\ArrayHelper;
13
use Flipbox\Skeleton\Helpers\ObjectHelper;
14
use Flipbox\Skeleton\Logger\AutoLoggerTrait;
15
use Psr\Log\LoggerInterface;
16
use Stash\Driver\BlackHole as DummyDriver;
17
use Stash\Interfaces\DriverInterface;
18
use Stash\Interfaces\PoolInterface;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 2.0.0
23
 */
24
class Factory
25
{
26
27
    use AutoLoggerTrait;
28
29
    /**
30
     * This is the default time, in seconds, that objects are cached for.
31
     *
32
     * @var int
33
     */
34
    public $defaultDuration = 432000;
35
36
    /**
37
     * The default driver
38
     *
39
     * @var string
40
     */
41
    public $defaultDriver = 'file';
42
43
    /**
44
     * Registered cache drivers
45
     *
46
     * @var DriverInterface[]
47
     */
48
    protected $registeredDrivers = [];
49
50
    /**
51
     * Constructor
52
     *
53
     * @param LoggerInterface $logger
54
     */
55
    public function __construct(LoggerInterface $logger)
56
    {
57
        $this->setLogger($logger);
58
59
        // Manually register the default driver
60
        $this->registeredDrivers['dummy'] = new DummyDriver();
61
    }
62
63
    /*******************************************
64
     * CREATE
65
     *******************************************/
66
67
    /**
68
     * Create a new cache pool
69
     *
70
     * @param array $config
71
     * @return PoolInterface
72
     * @throws InvalidDriverException
73
     */
74
    public function create($config = []): PoolInterface
75
    {
76
        // Array
77
        $config = ArrayHelper::toArray($config);
78
79
        // Get driver from config
80
        if (!$driverType = ObjectHelper::findClassFromConfig($config)) {
81
            $driverType = $this->defaultDriver;
82
83
            $this->warning(
84
                "Cache pool configuration did not indicate a driver type...using default",
85
                [
86
                    'config' => $config,
87
                    'default' => $driverType
88
                ]
89
            );
90
        }
91
92
        /** @var Pool $pool */
93
        $pool = new Pool(
94
            $this->autoGetDriver($driverType)
95
        );
96
97
        // Set logger
98
        $pool->setLogger($this->getLogger());
0 ignored issues
show
It seems like $this->getLogger() can be null; however, setLogger() 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...
99
100
        // Set duration
101
        $pool->setItemDuration(ArrayHelper::getValue(
102
            $config,
103
            'duration',
104
            $this->defaultDuration
105
        ));
106
107
        return $pool;
108
    }
109
110
    /**
111
     * Get a cache driver.  If the driver is not found, return the default.
112
     *
113
     * @param $identifier
114
     * @return DriverInterface
115
     * @throws InvalidDriverException
116
     */
117
    public function autoGetDriver($identifier): DriverInterface
118
    {
119
        if (!$driver = ArrayHelper::getValue($this->registeredDrivers, $identifier)) {
120
            $this->critical(
121
                "Cache driver not available...switching to default",
122
                [
123
                    'driver' => $identifier,
124
                    'default' => $this->defaultDriver,
125
                    'registered' => array_keys(
126
                        $this->registeredDrivers
127
                    )
128
                ]
129
            );
130
131
            // Get default driver
132
            $driver = $this->getDriver($this->defaultDriver);
133
        }
134
        return $driver;
135
    }
136
137
    /**
138
     * Get a cache driver
139
     *
140
     * @param $identifier
141
     * @return DriverInterface
142
     * @throws InvalidDriverException
143
     */
144
    public function getDriver($identifier): DriverInterface
145
    {
146
        if (!$driver = ArrayHelper::getValue($this->registeredDrivers, $identifier)) {
147
            throw new InvalidDriverException(sprintf(
148
                "Driver type '%s' is not registered.",
149
                $identifier
150
            ));
151
        }
152
        return $driver;
153
    }
154
155
    /**
156
     * Register a cache driver for use
157
     *
158
     * @param $identifier
159
     * @param DriverInterface $driver
160
     * @throws InvalidDriverException
161
     */
162
    public function registerDriver($identifier, DriverInterface $driver)
163
    {
164
        // Handle already taken
165
        if (ArrayHelper::keyExists($identifier, $this->registeredDrivers)) {
166
            throw new InvalidDriverException(sprintf(
167
                "Driver type '%s' is already registered.",
168
                $identifier
169
            ));
170
        }
171
172
        $this->info(
173
            "Registered cache driver",
174
            [
175
                'driver' => $identifier,
176
                'class' => get_class($driver)
177
            ]
178
        );
179
180
        $this->registeredDrivers[$identifier] = $driver;
181
    }
182
183
    /**
184
     * Register an array of drivers
185
     *
186
     * @param DriverInterface[] $drivers
187
     * @throws InvalidDriverException
188
     */
189
    public function registerDrivers(array $drivers)
190
    {
191
        foreach ($drivers as $handle => $driver) {
192
            $this->registerDriver($handle, $driver);
193
        }
194
    }
195
196
    /**
197
     * Get an array of registered cache drivers
198
     *
199
     * @return DriverInterface[]
200
     */
201
    public function getDrivers(): array
202
    {
203
        return $this->registeredDrivers;
204
    }
205
}
206