Issues (6)

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

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
namespace Weew\Config;
4
5
use Weew\Config\Exceptions\InvalidConfigValueException;
6
use Weew\Config\Exceptions\MissingConfigException;
7
8
class Config implements IConfig {
9
    /**
10
     * @var array
11
     */
12
    protected $config;
13
14
    /**
15
     * @var IConfigParser
16
     */
17
    protected $parser;
18
19
    /**
20
     * @param array $config
21
     * @param IConfigParser $parser
22
     */
23
    public function __construct(array $config = [], IConfigParser $parser = null) {
24
        if ( ! $parser instanceof IConfigParser) {
25
            $parser = $this->createConfigParser();
26
        }
27
28
        $this->setConfigParser($parser);
29
        $this->setConfig($config);
30
    }
31
32
    /**
33
     * @return array
34
     */
35
    public function getConfig() {
36
        return $this->config;
37
    }
38
39
    /**
40
     * @param array $config
41
     */
42
    public function setConfig(array $config) {
43
        $this->config = $config;
44
    }
45
46
    /**
47
     * @param $key
48
     * @param null $default
49
     *
50
     * @return mixed
51
     */
52
    public function get($key, $default = null) {
53
        $key = $this->getAbsoluteConfigKey($key);
54
55
        return $this->getConfigParser()
56
            ->parse($this, array_get($this->config, $key, $default));
57
    }
58
59
    /**
60
     * @param $key
61
     * @param null $default
62
     *
63
     * @return mixed
64
     */
65
    public function getRaw($key, $default = null) {
66
        return array_get($this->config, $key, $default);
67
    }
68
69
    /**
70
     * @param $key
71
     * @param $value
72
     *
73
     * @return IConfig
74
     */
75
    public function set($key, $value) {
76
        $key = $this->getAbsoluteConfigKey($key);
77
78
        array_set($this->config, $key, $value);
79
80
        return $this;
81
    }
82
83
    /**
84
     * @param $key
85
     *
86
     * @return bool
87
     */
88
    public function has($key) {
89
        $key = $this->getAbsoluteConfigKey($key);
90
91
        return array_has($this->config, $key);
92
    }
93
94
    /**
95
     * @param $key
96
     *
97
     * @return IConfig
98
     */
99
    public function remove($key) {
100
        $key = $this->getAbsoluteConfigKey($key);
101
102
        array_remove($this->config, $key);
103
104
        return $this;
105
    }
106
107
    /**
108
     * @param array $config
109
     *
110
     * @return IConfig
111
     */
112
    public function merge(array $config) {
113
        $this->setConfig(
114
            array_extend_distinct($this->getConfig(), $config)
115
        );
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param IConfig $config
122
     *
123
     * @return IConfig
124
     */
125
    public function extend(IConfig $config) {
126
        $this->merge($config->getConfig());
127
128
        return $this;
129
    }
130
131
    /**
132
     * @param $key
133
     * @param null $errorMessage
134
     * @param null $scalarType
135
     *
136
     * @return IConfig
137
     * @throws InvalidConfigValueException
138
     * @throws MissingConfigException
139
     */
140
    public function ensure($key, $errorMessage = null, $scalarType = null) {
141
        if ( ! $this->has($key)) {
142
            if ($errorMessage === null) {
143
                $errorMessage = sprintf('Missing config at key "%s".', $key);
144
            } else {
145
                $errorMessage = s('%s: %s', $key, $errorMessage);
146
            }
147
148
            throw new MissingConfigException($errorMessage);
149
        }
150
151
        if ($scalarType !== null) {
152
            if ( ! str_starts_with(gettype($this->get($key)), $scalarType)) {
153
                $errorMessage = sprintf('%s: Config value at key "%s" should be of type "%s".', $key, $key, $scalarType);
154
155
                throw new InvalidConfigValueException($errorMessage);
156
            }
157
        }
158
159
        return $this;
160
    }
161
162
    /**
163
     * @return array
164
     */
165
    public function toArray() {
166
        return $this->getConfigParser()
167
            ->parse($this, $this->getConfig());
168
    }
169
170
    /**
171
     * @return ConfigParser
172
     */
173
    protected function createConfigParser() {
174
        return new ConfigParser();
175
    }
176
177
    /**
178
     * @return IConfigParser
179
     */
180
    public function getConfigParser() {
181
        return $this->parser;
182
    }
183
184
    /**
185
     * @param IConfigParser $parser
186
     */
187
    public function setConfigParser(IConfigParser $parser) {
188
        $this->parser = $parser;
189
    }
190
191
    /**
192
     * @param $key
193
     *
194
     * @return string
195
     */
196
    public function getAbsoluteConfigKey($key) {
197
        $prefix = null;
0 ignored issues
show
$prefix is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
198
        $parser = $this->getConfigParser();
199
        $steps = explode('.', $key);
200
        $config = $this->config;
201
202
        foreach ($steps as $step) {
203
            if ( ! is_array($config)) {
204
                break;
205
            }
206
207
            array_shift($steps);
208
            $config = array_get($config, $step);
209
210
            if ($parser->isReference($config)) {
211
                array_unshift($steps, $parser->parseReferencePath($config));
212
                $config = implode('.', $steps);
213
214
                return $this->getAbsoluteConfigKey($config);
215
            }
216
        }
217
218
        return $key;
219
    }
220
}
221