Issues (1751)

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.

lib/modules/mod_cacherss.php (12 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
define("DIRMODE", 0770);
4
5
class cacherss {
6
	protected $httphelper;
7
	protected $timeout;
8
	protected $xmldata;
9
10
	public function __construct( $httphelper, $timeout = 1800 ) {
11
		$this->httphelper = $httphelper;
12
		$this->timeout = $timeout;
13
	}
14
15
	public function load( $url ) {
16
		$data = $this->httphelper->load( $url, "", time() - $this->timeout );
17
		$this->xmldata = $data["data"];
18
19
		$rssfeed = new cacherssfeed(); // load from string
20
		$result = $rssfeed->parseString( $this->xmldata );
21
22
		return $result;
23
	}
24
25
	public function titlelink($url) {
26
		$data = $this->httphelper->load( $url, "", time() - $this->timeout );
27
		$this->xmldata = $data["data"];
28
29
		$rssfeed = new cacherssfeed(); // load from string
30
31
		$rssfeed->parseString( $this->xmldata);
32
		$rss_channel = $rssfeed->info();
33
		return array("title" => $rss_channel['title'], "link" => $rss_channel['link']);
34
	}
35
}
36
37
38
class pinp_cacherss {
39 View Code Duplication
	public function _load( $url, $timeout = 1800 ) {
40
		global $AR;
41
		$cachelocation = $AR->dir->install . "/files/cache/rss/";
42
		$cache = new cacherss_cache( $cachelocation );
43
		$httphelper = new httphelper( $cache );
44
		$rss = new cacherss( $httphelper, $timeout );
45
		return $rss->load( $url );
46
	}
47
48 View Code Duplication
	public function _titlelink( $url, $timeout = 1800 ) {
49
		global $AR;
50
		$cachelocation = $AR->dir->install . "/files/cache/rss/";
51
		$cache = new cacherss_cache( $cachelocation );
52
		$httphelper = new httphelper( $cache );
53
		$rss = new cacherss( $httphelper, $timeout );
54
		return $rss->titlelink( $url );
55
	}
56
}
57
58
59
class cacherssfeed {
60
	protected $xmldata;
61
	protected $ns;
62
	protected $elements;
63
	protected $rss_items;
64
	protected $rss_channel;
65
	protected $encoding;
66
	protected $parser;
67
68
	public function parseString( $xmldata ) {
69
		// reset namestack
70
		$this->xmldata = $xmldata;
71
		$this->ns = array();
72
		$this->elements = array();
73
		$this->rss_items = array();
74
		$this->rss_channel = array();
75
76
		// Finding the RSS feed source encoding - thanks to the pointers on
77
		// http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
78
		//
79
		// Read the first part of the RSS feed to find the encoding.
80
81
82
		// Prepare a regexp to find the source encoding, and match it. If we find it, use that - otherwise assume UTF-8 as the default XML encoding.
83
		$encoding_regexp = '/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m';
84
85
		// $encoding_regexp = '/.*encoding=[\'"]/m';
86
87 View Code Duplication
		if (preg_match($encoding_regexp, $this->xmldata, $matches)) {
88
			$this->encoding = strtoupper($matches[1]);
89
		} else {
90
			$this->encoding = "UTF-8";
91
		}
92
93
		// The RSS library only understands UTF-8, US-ASCII and ISO-8859-1, so only give it this. For other encodings we'll default to UTF-8.
94
95 View Code Duplication
		if($this->encoding == "UTF-8" || $this->encoding == "US-ASCII" || $this->encoding == "ISO-8859-1") {
96
			$this->parser = xml_parser_create($this->encoding);
97
		} else {
98
			$this->parser = xml_parser_create("UTF-8");
99
			$this->encoding = "UTF-8";
100
		}
101
102
		// Check if we have valid xml
103
		$parsetest = xml_parse(xml_parser_create($this->encoding), $xmldata);
104
		if (!$parsetest) {
105
			//echo "XML doesn't parse\n";
106
			return false;
107
		}
108
109
		//$this->parser = xml_parser_create();
110
		xml_set_object($this->parser, $this);
111
		xml_set_element_handler($this->parser, "startElement", "endElement");
112
		xml_set_character_data_handler($this->parser, "characterData");
113
		xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
114
		xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
115
116
		// return the array
117
		return $this->getArray();
118
119
	}
120
121
	protected function startElement($parser, $name, $attribs) {
0 ignored issues
show
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $attribs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
		$newElement = array();
0 ignored issues
show
$newElement 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...
123
		$element = &$this->elements;
124 View Code Duplication
		foreach ($this->ns as $n) {
125
			$parentElement = $element;
0 ignored issues
show
$parentElement 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...
126
			if (is_array($element) && isset($element[$n])) {
127
				$element = &$element[$n];
128
			}
129
		}
130
		$this->ns[] = $name;
131 View Code Duplication
		switch ($name) {
132
			default:
0 ignored issues
show
default: if (is_arra...t[$name] = $newElement; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
133
				if (is_array($attribs)) {
134
					foreach ($attribs as $key => $value) {
135
						$element[$name . ":" . $key] = $value;
136
					}
137
				}
138
				$element[$name] = $newElement;
139
		}
140
	}
141
142
	protected function endElement($parser, $name) {
0 ignored issues
show
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
		$element = &$this->elements;
144 View Code Duplication
		foreach ($this->ns as $n) {
145
			$parentElement = $element;
146
			if (is_array($element) && isset($element[$n])) {
147
				$element = &$element[$n];
148
			}
149
		}
150
		switch ($name) {
151
			case 'item':
152
				$this->rss_items[] = $element;
153
				unset($parentElement[$name]);
154
			break;
155
			case 'channel':
156
				$this->rss_channel = $element;
157
			break;
158
		}
159
		array_pop($this->ns);
160
	}
161
162
	protected function characterData($parser, $data) {
0 ignored issues
show
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
163
		$element = &$this->elements;
164 View Code Duplication
		foreach ($this->ns as $n) {
165
			if (is_array($element) && isset($element[$n])) {
166
				$element = &$element[$n];
167
			}
168
		}
169 View Code Duplication
		switch ($n) {
0 ignored issues
show
The variable $n seems to be defined by a foreach iteration on line 164. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
170
			case 'textinput':
171
			case 'item':
172
			case 'rss':
173
			case 'channel':
174
			case 'image':
175
			case 'rdf:RDF':
176
			case 'rdf:Seq':
177
			case 'rdf:li':
178
			case 'items':
179
				break;
180
			default:
181
				if (!$element) {
182
					$element = "";
183
				}
184
				$element .= $data;
185
			break;
186
		}
187
	}
188
189
	public function current() {
190
		return $this->rss_items[0];
191
	}
192
193
	public function info() {
194
		return $this->rss_channel;
195
	}
196
197
	public function next() {
198
		// this is needed
199
		if (!$this->parser) {
200
			return false;
201
		}
202
203
		xml_set_object($this->parser, $this);
204
205
		/* remove the last item from the queue */
206
		if (count($this->rss_items)) {
207
			array_shift($this->rss_items);
208
		}
209
		if (!count($this->rss_items) && $this->xmldata) {
210
			$rss_data = $this->xmldata;
211
			// $this->xmldata = false;
212
213 View Code Duplication
			if (!xml_parse($this->parser, $rss_data, $eof)) {
0 ignored issues
show
The variable $eof does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
214
				$this->error = sprintf("XML error: %s at line %d",
0 ignored issues
show
The property error does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
215
					xml_error_string(xml_get_error_code($this->parser)),
216
					xml_get_current_line_number($this->parser));
217
			}
218
		}
219
		return $this->rss_items[0];
220
	}
221
222
	public function getArray() {
223
		$result=array();
224
		do {
225
			$result[]=$this->current();
226
		} while ($this->next());
227
		return $result;
228
	}
229
}
230
231
class httphelper {
232
	protected $cache;
233
	public $error;
234
235
	public function __construct( $cache ) {
236
		$this->cache = $cache;
237
	}
238
239
	public function load( $url, $meta="", $maxage=0, $user="" ) {
240
		$result = $this->cache->load($url, $maxage, $user);
241
		if( !$result && $maxage >= 0 ) {
242
			$client  = ar('http')->client();
243
			$data = $client->get($url);
244
			if( $data != "" ) {
245
				$result = $this->cache->save($url, $data, $meta, $user);
246
			}
247
		}
248
		return $result;
249
	}
250
}
251
252
class cacherss_cache {
253
	protected $path;
254
255
	public function __construct( $path ) {
256
		// FIXME: forceer $path eindigen op een / en security.
257
258
		if (!is_dir($path) && !file_exists($path)) {
259
			mkdir($path, DIRMODE);
260
		}
261
262
		$this->path = $path;
263
	}
264
265
266
	public function load( $tag, $maxage = 0, $user = "" ) {
267
268
		if( !$tag ) {
269
			return false;
270
		}
271
272
		$tag = $this->escape( $tag );
273
274
		$result = false;
275
276
277
		if( $user != "" ) {
278
			$user = $user."/";
279
		}
280
281
		$path = $this->path. $user. $tag;
282
		if (file_exists($path) && filectime($path) >= $maxage) {
283
			$fp = fopen( $path, "rb" );
284
			$data=fread($fp,filesize($path) );
285
			$timestamp = filectime($path);
286
			fclose($fp);
287
			$metapath = $path.".meta";
288
			$meta = "";
289
			if( file_exists($metapath)) {
290
				$fp = fopen( $metapath, "rb" );
291
				$meta = fread($fp, filesize($metapath) );
292
				fclose($fp);
293
			}
294
			$result = array( "data" => unserialize($data), "meta" => unserialize($meta), "timestamp" => $timestamp );
295
		}
296
297
		return $result;
298
	}
299
300
	public function save( $tag, $data, $meta = "", $user = "" ) {
301
302
		if( !$tag ) {
303
			return false;
304
		}
305
306
		$result = false;
0 ignored issues
show
$result 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...
307
308
		$user = $this->escape( $user );
309
310
		if( $user != "" ) {
311
			$user = $user."/";
312
		}
313
314
		$tag = $this->escape( $tag );
315
		$data = serialize( $data );
316
		$meta = serialize( $meta );
317
318
		$path = $this->path.$user;
319
		if( $path != $this->path ) {
320
			// user folder maken
321
			if( !file_exists($path) ) {
322
				if( !@mkdir($path, 0770) ) {
323
					// abort abort abort!
324
					return false;
325
				}
326
			}
327
		}
328
		$path = $this->path.$user.$tag;
329
330
		$fp = fopen( $path, "wb");
331
		$result=fwrite($fp, $data);
332
		fclose($fp);
333
		if( $result ) {
334
			$metapath = $path.".meta";
335
			$fp = fopen( $metapath, "wb" );
336
			$result = fwrite($fp, $meta);
337
			fclose($fp);
338
		}
339
		if( $result ) {
340
			$result = array( "data" => unserialize($data), "meta" => unserialize($meta), "timestamp" => filectime($path) );
341
		}
342
		return $result;
343
	}
344
345
	public function clear( $tag, $user = "" ) {
346
347
		if( !$tag ) {
348
			return false;
349
		}
350
		$tag = $this->escape( $tag );
351
352
		$user = $this->escape( $user );
353
354
		if( $user != "" ) {
355
			$user = $user."/";
356
		}
357
358
		$path = $this->path.$user.$tag;
359
		if( file_exists($path) ) {
360
			$result = unlink($path);
361
			$metapath = $path.".meta";
362
			if( file_exists($metapath) ) {
363
				unlink($metapath);
364
			}
365
		}
366
367
		return $result;
0 ignored issues
show
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
368
	}
369
370 View Code Duplication
	protected function escape($path) {
371
		// This function will return an escaped path. All the characters not supported by Ariadne will be encoded.
372
		// See also path_escape_callback
373
374
		// Returns an empty string if no path, or an empty path was given.
375
		$result = "";
376
		if ($path) {
377
			$result = preg_replace_callback(
378
				'/[^A-Za-z0-9-]/', function ($char) {
379
					// Replaces characters in the path with their number.
380
					// Quite similar to " " -> "%20" for HTML escape, but we use _ instead of %
381
					// This function is to be used as a callback for preg_replace_callback
382
					if ($char[0]) {
383
						if ($char[0]=="_") {
384
							return "__";
385
						} else {
386
							return "_".dechex(ord($char[0]));
387
						}
388
					}
389
				},
390
				$path
391
			);
392
		}
393
		return $result;
394
	}
395
396 View Code Duplication
	function unescape($path) {
397
		$result = "";
398
		if ($path) {
399
			$result = preg_replace_callback(
400
				'/(_[0-9a-fA-F][0-9a-fA-F]|__)/',
401
				function ( $matches ) {
402
					// Two types of escaped characters can be here, the
403
					// underscore or other characters. Check for the
404
					// underscore first.
405
406
					$char = $matches[0];
407
					if ($char[1] == "_") {
408
					// It is the underscore, return it as a character.
409
						return "_";
410
					}
411
412
					// Assume it is an escaped character here. Find the
413
					// numbers in hex, turn them back to decimal, get
414
					// the corresponding character and return it.
415
416
					return chr(hexdec(substr($char, 1, 2)));
417
				},
418
				$path
419
			);
420
		}
421
		return $result;
422
	}
423
}
424