Issues (52)

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.

src/Utils/FilesystemHelper.php (5 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
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace Owncloud\Updater\Utils;
23
24
/**
25
 * Class FilesystemHelper
26
 *
27
 * @package Owncloud\Updater\Utils
28
 */
29
class FilesystemHelper {
30
31
	/**
32
	 * Wrapper for filemtime function
33
	 * @param string $path
34
	 * @return integer
35
	 */
36
	public function filemtime($path){
37
		return filemtime($path);
38
	}
39
40
	/**
41
	 * Wrapper for scandir function. 
42
	 * Filters current and parent directories
43
	 * @param string $path
44
	 * @return array
45
	 */
46
	public function scandirFiltered($path){
47
		$content = $this->scandir($path);
48
		if (is_array($content)){
49
			return array_diff($content, ['.', '..']);
50
		}
51
		return [];
52
	}
53
54
	/**
55
	 * Wrapper for scandir function
56
	 * @param string $path
57
	 * @return array
58
	 */
59
	public function scandir($path){
60
		return scandir($path);
61
	}
62
63
	/**
64
	 * Wrapper for file_exists function
65
	 * @param string $path
66
	 * @return bool
67
	 */
68
	public function fileExists($path){
69
		return file_exists($path);
70
	}
71
72
	/**
73
	 * Wrapper for is_writable function
74
	 * @param string $path
75
	 * @return bool
76
	 */
77
	public function isWritable($path){
78
		return is_writable($path);
79
	}
80
81
	/**
82
	 * Wrapper for is_dir function
83
	 * @param string $path
84
	 * @return bool
85
	 */
86
	public function isDir($path){
87
		return is_dir($path);
88
	}
89
90
	/**
91
	 * Wrapper for md5_file function
92
	 * @param string $path
93
	 * @return string
94
	 */
95
	public function md5File($path){
96
		return md5_file($path);
97
	}
98
99
	/**
100
	 * Wrapper for mkdir
101
	 * @param string $path
102
	 * @param bool $isRecursive
103
	 * @throws \Exception on error
104
	 */
105
	public function mkdir($path, $isRecursive = false){
106
		if (!mkdir($path, 0755, $isRecursive)){
107
			throw new \Exception("Unable to create $path");
108
		}
109
	}
110
111
	/**
112
	 * Copy recursive
113
	 * @param string $src  - source path
114
	 * @param string $dest - destination path
115
	 * @throws \Exception on error
116
	 */
117
	public function copyr($src, $dest, $stopOnError = true){
118
		if (is_dir($src)){
119
			if (!is_dir($dest)){
120
				try{
121
					$this->mkdir($dest);
122
				} catch (\Exception $e){
123
					if ($stopOnError){
124
						throw $e;
125
					}
126
				}
127
			}
128
			$files = scandir($src);
129
			foreach ($files as $file){
130
				if (!in_array($file, [".", ".."])){
131
					$this->copyr("$src/$file", "$dest/$file", $stopOnError);
132
				}
133
			}
134
		} elseif (file_exists($src)){
135
			if (!copy($src, $dest) && $stopOnError){
136
				throw new \Exception("Unable to copy $src to $dest");
137
			}
138
		}
139
	}
140
141
	/**
142
	 * Moves file/directory
143
	 * @param string $src  - source path
144
	 * @param string $dest - destination path
145
	 * @throws \Exception on error
146
	 */
147
	public function move($src, $dest){
148
		if (!rename($src, $dest)){
149
			throw new \Exception("Unable to move $src to $dest");
150
		}
151
	}
152
153
	/**
154
	 * Check permissions recursive
155
	 * @param string $src  - path to check
156
	 * @param Collection $collection - object to store incorrect permissions
157
	 */
158
	public function checkr($src, $collection){
159
		if (!file_exists($src)){
160
			return;
161
		}
162
		if (!is_writable($src)){
163
			$collection->addNotWritable($src);
164
		}
165
		if (!is_readable($src)){
166
			$collection->addNotReadable($src);
167
		}
168
		if (is_dir($src)){
169
			$files = scandir($src);
170
			foreach ($files as $file){
171
				if (!in_array($file, [".", ".."])){
172
					$this->checkr("$src/$file", $collection);
173
				}
174
			}
175
		}
176
	}
177
178
	/**
179
	 * @param string $path
180
	 */
181
	public function removeIfExists($path) {
182
		if (!file_exists($path)) {
183
			return;
184
		}
185
186
		if (is_dir($path)) {
187
			$this->rmdirr($path);
188
		} else {
189
			@unlink($path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
190
		}
191
	}
192
193
	/**
194
	 * @param $dir
195
	 * @return bool
196
	 */
197
	public function rmdirr($dir) {
198
		if(is_dir($dir)) {
199
			$files = scandir($dir);
200
			foreach($files as $file) {
201
				if ($file != "." && $file != "..") {
202
					$this->rmdirr("$dir/$file");
203
				}
204
			}
205
			@rmdir($dir);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
206
		}elseif(file_exists($dir)) {
207
			@unlink($dir);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
208
		}
209
		if(file_exists($dir)) {
210
			return false;
211
		}else{
212
			return true;
213
		}
214
	}
215
216
	/**
217
	 *
218
	 * @param string $old
219
	 * @param string $new
220
	 * @param string $temp
221
	 * @param string $dirName
222
	 */
223
	public function tripleMove($old, $new, $temp, $dirName){
224 View Code Duplication
		if ($this->fileExists($old . '/' . $dirName)){
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...
225
			$this->copyr($old . '/' . $dirName, $temp . '/' . $dirName, false);
226
			$this->rmdirr($old . '/' . $dirName);
227
		}
228 View Code Duplication
		if ($this->fileExists($new . '/' . $dirName)){
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...
229
			$this->copyr($new . '/' . $dirName, $old . '/' . $dirName, false);
230
			$this->rmdirr($new . '/' . $dirName);
231
		}
232
	}
233
234
}
235