Issues (18)

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/PhpFileCache.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
namespace Mouf\Utils\Cache;
3
use Mouf\Utils\Log\LogInterface;
4
use Psr\Log\LoggerInterface;
5
6
/**
7
 * This package contains a cache mechanism that relies on executable PHP files.
8
 * For a file cache, it is quite fast because it relies on the Opcache to cache data.
9
 *
10
 * It features best performances than the regular FileCache. However, be aware that it generates PHP files
11
 * that are directly "included" in your application, so no third party should have access to the repository where
12
 * you store the cache.
13
 *
14
 */
15
class PhpFileCache extends FileCache {
16
17
	public function __construct() {
18
		// Let's check if the parameters are ok.
19
		// We should not allow opcache file revalidation should be set to 0
20
		if (ini_get('opcache.revalidate_freq') != 0) {
21
			throw new \Exception("In order to use PhpFileCache, you must set the parameter 'opcache.revalidate_freq' to 0 in your php.ini file. Current value: '".ini_get('opcache.revalidate_freq')."'");
22
		}
23
	}
24
25
	/**
26
	 * Returns the cached value for the key passed in parameter.
27
	 *
28
	 * @param string $key
29
	 * @return mixed
30
	 */
31
	public function get($key) {
32
		$filename = $this->getFileName($key);
33
34
		if ( ! is_file($filename)) {
35
			if ($this->log) {
36
				if ($this->log instanceof LoggerInterface) {
37
					$this->log->info("Retrieving key '{key}' from file cache: cache miss.", array('key'=>$key));
38
				} else {
39
					$this->log->trace("Retrieving key '$key' from file cache: cache miss.");
40
				}
41
			}
42
			return null;
43
		}
44
		$value = include $filename;
45
46
		if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
47
			if ($this->log) {
48
				if ($this->log instanceof LoggerInterface) {
49
					$this->log->info("Retrieving key '{key}' from file cache: key outdated, cache miss.", array('key'=>$key));
50
				} else {
51
					$this->log->trace("Retrieving key '$key' from file cache: key outdated, cache miss.");
52
				}
53
			}
54
			return null;
55
		}
56
57
		if ($this->log) {
58
			if ($this->log instanceof LoggerInterface) {
59
				$this->log->info("Retrieving key '{key}' from file cache.", array('key'=>$key));
60
			} else {
61
				$this->log->trace("Retrieving key '$key' from file cache.");
62
			}
63
		}
64
65
		return $value['data'];
66
	}
67
	
68
	/**
69
	 * Sets the value in the cache.
70
	 *
71
	 * @param string $key The key of the value to store
72
	 * @param mixed $value The value to store
73
	 * @param float $timeToLive The time to live of the cache, in seconds.
74
	 */
75
	public function set($key, $value, $timeToLive = null) {
76
		$filename = $this->getFileName($key);
77
		//$this->log->trace("Storing value in cache: key '$key', value '".var_export($value, true)."'");
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
		if ($this->log) {
79
			if ($this->log instanceof LoggerInterface) {
80
				$this->log->info("Storing value in cache: key '{key}'", array('key'=>$key));
81
			} else {
82
				$this->log->trace("Storing value in cache: key '$key'");
83
			}
84
		}
85
86
        $oldUmask = umask(0);
87
88 View Code Duplication
		if (!is_writable($filename)) {
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...
89
			if (!file_exists($this->getDirectory())) {
90
				mkdir($this->getDirectory(), 0777, true);
91
			}
92
		}
93
		
94 View Code Duplication
		if ($timeToLive == null) {
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...
95
			if (empty($this->defaultTimeToLive)) {
96
				$timeOut = 0;
97
			} else {
98
				$timeOut = time() + $this->defaultTimeToLive;
99
			}
100
		} else {
101
			$timeOut = time() + $timeToLive;
102
		}
103
104
		$data = array(
105
			'lifetime'  => $timeOut,
106
			'data'      => $value
107
		);
108
109
        $serializeData = serialize($data);
110
        if(strpos($serializeData, 'r:') !== false || (is_object($value) && !method_exists($value, '__set_state'))){
111
            $code   = sprintf('<?php return unserialize(%s);', var_export($serializeData, true));
112
            file_put_contents($filename, $code);
113
        }else{
114
            $data  = var_export($data, true);
115
            $code   = sprintf('<?php return %s;', $data);
116
            file_put_contents($filename, $code);
117
        }
118
119
        // Cache is shared with group, not with the rest of the world.
120
        chmod($filename, 0660);
121
122
        umask($oldUmask);
123
	}
124
125
}
126