Issues (37)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  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.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  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.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  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.
  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.
  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.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
  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.
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  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.
  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.
  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.
  Header Injection
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/Config/ConfigManager.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace GeminiLabs\Pollux\Config;
4
5
use GeminiLabs\Pollux\Application;
6
use GeminiLabs\Pollux\Config\Config;
7
use GeminiLabs\Pollux\MetaBox\SiteMetaManager;
8
use Symfony\Component\Yaml\Exception\DumpException;
9
use Symfony\Component\Yaml\Exception\ParseException;
10
use Symfony\Component\Yaml\Yaml;
11
12
/**
13
 * @property int $updated
14
 */
15
class ConfigManager extends SiteMetaManager
16
{
17
	const RAW_STRINGS = [
18
		// '__', '_n', '_x', 'esc_attr__', 'esc_html__', 'sprintf',
19
	];
20
21
	public $compiled;
22
23
	public $parseError = false;
24
25
	/**
26
	 * @var Application
27
	 */
28
	protected $app;
29
30
	public function __construct( Application $app )
31
	{
32
		$this->app = $app;
33
		$this->options = $this->buildConfig();
34
		$this->compiled = $this->compile();
35
	}
36
37
	/**
38
	 * @return array
39
	 */
40
	public function buildConfig()
41
	{
42
		$yamlFile = $this->getYamlFile();
43
		$yaml = $this->normalizeYamlValues( $this->normalize(
44
			$this->parseYaml( file_get_contents( $yamlFile ), $yamlFile )
45
		));
46
		if( !$yaml['disable_config'] ) {
47
			$config = array_filter( (array) get_option( Config::id(), [] ));
48
		}
49
		return empty( $config )
50
			? $this->setTimestamp( $yaml, filemtime( $yamlFile ))
51
			: $this->normalizeYamlValues( $this->normalize( $config ));
52
	}
53
54
	/**
55
	 * @param bool $force
56
	 * @return object
57
	 */
58
	public function compile( $force = false )
59
	{
60
		$configFile = $this->getCompileDestination();
61
		if( $force || $this->shouldCompile( $configFile )) {
62
			$config = $this->normalizeArray( $this->options );
0 ignored issues
show
It seems like $this->options can also be of type boolean; however, parameter $array of GeminiLabs\Pollux\Config...nager::normalizeArray() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
			$config = $this->normalizeArray( /** @scrutinizer ignore-type */ $this->options );
Loading history...
63
			if( $this->parseError ) {
64
				return (object) $config;
65
			}
66
			file_put_contents( $configFile, sprintf( '<?php // DO NOT MODIFY THIS FILE DIRECTLY!%sreturn (object) %s;',
67
				PHP_EOL,
68
				$this->parseRawStrings( var_export( $this->setTimestamp( $config ), true ))
69
			));
70
		}
71
		return include $configFile;
72
	}
73
74
	/**
75
	 * @return string
76
	 */
77
	public function convertArrayToYaml( array $array )
78
	{
79
		return !empty( $array )
80
			? trim( $this->parseRawStrings( $this->dumpYaml( $array )))
81
			: '';
82
	}
83
84
	/**
85
	 * @return string
86
	 */
87
	public function getCompileDestination( $filename = 'pollux-config.php' )
88
	{
89
		$filename = apply_filters( 'pollux/config/dist/file', $filename );
90
		$storagePath = apply_filters( 'pollux/config/dist/location', WP_CONTENT_DIR );
91
		wp_mkdir_p( $storagePath );
92
		return sprintf( '%s%s', trailingslashit( $storagePath ), $filename );
93
	}
94
95
	/**
96
	 * @return string
97
	 */
98
	public function getYamlFile()
99
	{
100
		if( $file = $this->app->getFile( 'pollux.yml' )) {
101
			return $file;
102
		}
103
		return $this->app->path( 'defaults.yml' );
104
	}
105
106
	/**
107
	 * @return array
108
	 */
109
	public function normalizeArray( array $array )
110
	{
111
		array_walk( $array, function( &$value, $key ) {
112
			if( !is_numeric( $value ) && is_string( $value )) {
113
				$value = $this->parseYaml( $value, $key );
114
				if( $this->parseError == $key ) {
115
					$value = [];
116
				}
117
			}
118
		});
119
		return $array;
120
	}
121
122
	/**
123
	 * @return array
124
	 */
125
	public function normalizeYamlValues( array $array )
126
	{
127
		return array_map( function( $value ) {
128
			return is_array( $value )
129
				? $this->convertArrayToYaml( $value )
130
				: $value;
131
		}, $array );
132
	}
133
134
	/**
135
	 * @return array
136
	 */
137
	public function setTimestamp( array $config, $timestamp = null )
138
	{
139
		$timestamp || $timestamp = time();
140
		$config['updated'] = $timestamp;
141
		return $config;
142
	}
143
144
	/**
145
	 * @return string|null
146
	 */
147
	protected function dumpYaml( array $array )
148
	{
149
		try {
150
			return Yaml::dump( $array, 13, 2 );
151
		}
152
		catch( DumpException $e ) {
153
			$this->app->make( 'Notice' )->addError( $e->getMessage() );
154
		}
155
	}
156
157
	/**
158
	 * @return array
159
	 */
160
	protected function normalize( array $config )
161
	{
162
		return wp_parse_args(
163
			$config,
164
			$this->parseYaml(
165
				file_get_contents( $this->app->path( 'defaults.yml' )),
166
				$this->app->path( 'defaults.yml' )
167
			)
168
		);
169
	}
170
171
	/**
172
	 * @param string $configString
173
	 * @return string
174
	 * @todo only allow raw strings when we can parse them properly without using eval()
175
	 */
176
	protected function parseRawStrings( $configString )
177
	{
178
		$strings = apply_filters( 'pollux/config/raw_strings', static::RAW_STRINGS );
179
		if( empty( $strings )) {
180
			return $configString;
181
		}
182
		$pattern = '/(\')((' . implode( '|', $strings ) . ')\(?.+\))(\')/';
183
		return stripslashes(
184
			preg_replace_callback( $pattern, function( $matches ) {
185
				return str_replace( "''", "'", $matches[2] );
186
			}, $configString )
187
		);
188
	}
189
190
	/**
191
	 * @link http://api.symfony.com/3.2/Symfony/Component/Yaml/Exception/ParseException.html
192
	 * @return array
193
	 */
194
	protected function parseYaml( $value, $file = null )
195
	{
196
		try {
197
			return (array) Yaml::parse( $value );
198
		}
199
		catch( ParseException $e ) {
200
			$this->parseError = $file;
201
			if( $file ) {
202
				$file = sprintf( '<code>%s</code>', $file );
203
			}
204
			$this->app->make( 'Notice' )->addError([
205
				sprintf( '<strong>Pollux Error:</strong> Unable to parse config at line %s (near "%s").',
206
					$e->getParsedLine(),
207
					$e->getSnippet()
208
				),
209
				$file
210
			]);
211
			return $value;
212
		}
213
	}
214
215
	/**
216
	 * @param string $configFile
217
	 * @return bool
218
	 */
219
	protected function shouldCompile( $configFile )
220
	{
221
		if( !file_exists( $configFile )) {
222
			return true;
223
		}
224
		$config = include $configFile;
225
		if( $this->updated >= $config->updated ) {
226
			return true;
227
		}
228
		return filemtime( $this->getYamlFile() ) >= $config->updated;
229
	}
230
}
231