Issues (17)

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.

lib/Config/BuilderFactory.php (3 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 Magium\Configuration\Config;
4
5
use Magium\Configuration\Config\Storage\Mongo;
6
use Magium\Configuration\Config\Storage\RelationalDatabase;
7
use Magium\Configuration\File\Configuration\ConfigurationFileRepository;
8
use Magium\Configuration\File\Context\AbstractContextConfigurationFile;
9
use Magium\Configuration\InvalidConfigurationException;
10
use Magium\Configuration\Manager\CacheFactory;
11
use MongoDB\Client;
12
use Zend\Db\Adapter\Adapter;
13
14
class BuilderFactory implements BuilderFactoryInterface
15
{
16
    protected $configuration;
17
    protected $adapter;
18
    protected $contextFile;
19
    protected $baseDirectory;
20
21 16
    public function __construct(
22
        \SplFileInfo $baseDirectory,
23
        \SimpleXMLElement $configuration,
24
        AbstractContextConfigurationFile $contextConfigurationFile
25
    )
26
    {
27 16
        $this->configuration = $configuration;
28 16
        $this->contextFile = $contextConfigurationFile;
29 16
        if (!$baseDirectory->isDir()) {
30 1
            throw new InvalidConfigurationException('Base directory must be a directory');
31
        }
32 15
        $this->baseDirectory = $baseDirectory;
33 15
    }
34
35 3
    protected function getCache(\SimpleXMLElement $element)
36
    {
37 3
        $cacheFactory = new CacheFactory();
38 3
        return $cacheFactory->getCache($element);
39
    }
40
41 7
    public function getDatabaseConfiguration()
42
    {
43 7
        $config = json_encode($this->configuration->persistenceConfiguration);
44 7
        $config = json_decode($config, true);
45 7
        return $config;
46
    }
47
48 3
    public function getRelationalAdapter()
49
    {
50 3
        if (!class_exists(Adapter::class)) {
51
            throw new \Exception('Please make sure you have zendframework/zend-db installed for data storage');
52
        }
53 3
        if (!$this->adapter instanceof Adapter) {
54 3
            $config = $this->getDatabaseConfiguration();
55 3
            $this->adapter = new Adapter($config);
56
        }
57 3
        return $this->adapter;
58
    }
59
60 4
    public function getMongoDsnString()
61
    {
62 4
        $config = $this->getDatabaseConfiguration();
63 4
        $dsn = 'mongodb://';  //[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]';
64 4
        $at = false;
65 4
        if (!empty($config['username'])) {
66 1
            $dsn .= $config['username'];
67 1
            $at = true;
68
        }
69 4
        if (!empty($config['password'])) {
70 1
            $dsn .= ':'.$config['password'];
71 1
            $at = true;
72
        }
73 4
        if ($at) {
74 1
            $dsn .= '@';
75
        }
76 4
        $dsn .= $config['hostname'];
77 4
        if (!empty($config['port'])) {
78 1
            $dsn .= ':' . $config['port'];
79
        }
80 4
        return $dsn;
81
    }
82
83 2
    public function getMongoAdapter()
84
    {
85 2
        if (!class_exists(Client::class)) {
86
            throw new \Exception('Please make sure you have mongodb/mongodb installed for data storage');
87
        }
88 2
        $config = $this->getDatabaseConfiguration();
89 2
        $dsn = $this->getMongoDsnString();
90 2
        $client = new Client($dsn);
91 2
        $collection = empty($config['table'])?Mongo::TABLE:$config['table'];
92 2
        return new Mongo($client->selectCollection($config['database'], $collection));
93
    }
94
95 5
    public function getPersistence()
96
    {
97 5
        if (empty($this->configuration->persistenceConfiguration->driver)) {
98 1
            throw new \Exception('Please set your driver type either corresponding to its Zend DB adapter '
99
                . 'name or the specific document database, such as mongo.  Ensure that you have either installed '
100 1
                . 'zendframework/zend-db or mongodb/mongodb depending on where you want to store your configuration.');
101
        }
102 4
        if (empty($this->configuration->persistenceConfiguration->database)) {
103 1
            throw new \Exception('You must specify a database in your persistenceConfiguration');
104
        }
105 3
        if (stripos($this->configuration->persistenceConfiguration->driver, 'mongo') === 0) {
106
            return $this->getMongoAdapter();
107
        }
108 3
        $persistence = new RelationalDatabase($this->getRelationalAdapter(), $this->contextFile);
109 3
        return $persistence;
110
    }
111
112 6
    public function getSecureBaseDirectories()
113
    {
114 6
        $cwd = getcwd();
115 6
        $path = $this->baseDirectory->getRealPath();
116 6
        chdir($path);
117 6
        $config = $this->configuration->configurationDirectories;
0 ignored issues
show
The property configurationDirectories does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
118 6
        $config = json_encode($config);
119 6
        $config = json_decode($config, true);
120 6
        $baseDirs = [];
121 6
        if (is_array($config) && isset($config['directory'])) {
122 4 View Code Duplication
            if (!is_array($config['directory'])) {
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...
123 3
                $config['directory'] = [$config['directory']];
124
            }
125 4
            foreach ($config['directory'] as $dir) {
126 4
                $path = realpath($dir);
127 4
                if (!is_dir($path)) {
128 1
                    throw new InvalidConfigurationLocationException('A secure configuration path cannot be determined for the directory: ' . $dir);
129
                }
130 3
                $baseDirs[] = $path;
131
            }
132
        }
133 5
        chdir($cwd);
134 5
        return $baseDirs;
135
    }
136
137 5
    public function getConfigurationFiles(array $secureBaseDirectories = [])
138
    {
139 5
        $config = json_encode($this->configuration->configurationFiles);
140 5
        $config = json_decode($config, true);
141 5
        $files = [];
142 5
        if (!empty($config['file'])) {
143 3 View Code Duplication
            if (!is_array($config['file'])) {
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...
144 1
                $config['file'] = [$config['file']];
145
            }
146 3
            foreach ($config['file'] as $file) {
147 3
                $found = false;
148 3
                foreach ($secureBaseDirectories as $base) {
149 3
                    chdir($base);
150 3
                    $path = realpath($file);
151 3
                    if ($path) {
152 3
                        $found = true;
153 3
                        $files[] = $path;
154
                    }
155
                }
156 3
                if (!$found) {
157
                    throw new InvalidConfigurationLocationException('Could not find file: ' . $file);
158
                }
159
            }
160
        }
161 5
        return $files;
162
    }
163
164 3
    public function getBuilder()
165
    {
166
        // This method expects that chdir() has been called on the same level as the magium-configuration.xml file
167 3
        $cache = $this->getCache($this->configuration->cache);
168 3
        $persistence = $this->getPersistence();
169 3
        $secureBases = $this->getSecureBaseDirectories();
170 2
        $configurationFiles = $this->getConfigurationFiles($secureBases);
171 2
        $repository = ConfigurationFileRepository::getInstance($secureBases, $configurationFiles);
172
173
        /*
174
         * We only populate up to the secureBases because adding a DIC or service manager by configuration starts
175
         * making the configuration-based approach pay off less.  If you need a DIC or service manager for your
176
         * configuration builder (which you will if you use object/method callbacks for value filters) then you need
177
         * to wire the Builder object with your own code.
178
         */
179
180 2
        $builder = new Builder(
181 2
            $cache,
182 2
            $persistence,
183 2
            $repository
184
        );
185
186 2
        return $builder;
187
    }
188
189
}
190