This repository was archived by the owner on May 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathSafeFile.php
More file actions
executable file
·223 lines (200 loc) · 7.23 KB
/
SafeFile.php
File metadata and controls
executable file
·223 lines (200 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* OWASP Enterprise Security API (ESAPI)
*
* This file is part of the Open Web Application Security Project (OWASP)
* Enterprise Security API (ESAPI) project.
*
* PHP version 5.2
*
* LICENSE: This source file is subject to the New BSD license. You should read
* and accept the LICENSE before you use, modify, and/or redistribute this
* software.
*
* @category OWASP
* @package ESAPI
* @author Andrew van der Stock <vanderaj@owasp.org>
* @author Martin Reiche <martin.reiche.ka@googlemail.com>
* @author Arnaud Labenne <arnaud.labenne@dotsafe.fr>
* @author Mike Boberski <boberski_michael@bah.com>
* @copyright 2009-2010 The OWASP Foundation
* @license http://www.opensource.org/licenses/bsd-license.php New BSD license
* @version SVN: $Id$
* @link http://www.owasp.org/index.php/ESAPI
*/
/**
* SafeFile requires ValidationException and EnterpriseSecurityException.
*/
require_once dirname(__FILE__).'/errors/ValidationException.php';
/**
* Use this ESAPI security control to read files from the operating
* system.
*
* The idea behind this interface is to extend the PHP SplFileObject
* to prevent against null byte injections and other unforeseen problems
* resulting from unprintable characters causing problems in path
* lookups. This does NOT prevent against directory traversal attacks.
*
* @category OWASP
* @package ESAPI
* @author Andrew van der Stock <vanderaj@owasp.org>
* @author Martin Reiche <martin.reiche.ka@googlemail.com>
* @author Arnaud Labenne <arnaud.labenne@dotsafe.fr>
* @author Mike Boberski <boberski_michael@bah.com>
* @copyright 2009-2010 The OWASP Foundation
* @license http://www.opensource.org/licenses/bsd-license.php New BSD license
* @version Release: @package_version@
* @link http://www.owasp.org/index.php/ESAPI
*/
class SafeFile extends SplFileObject
{
private $_PERCENTS_PAT = "/(%)([0-9a-f])([0-9a-f])/i";
private $_FILE_BLACKLIST_PAT = "/([\\/:*?<>|])/";
private $_DIR_BLACKLIST_PAT = "/([*?<>|])/";
/**
* Creates an extended SplFileObject from the given filename, which
* prevents against null byte injections and unprintable characters.
*
* @param string $path the path to the file (path && file name)
*
* @return does not return a value.
*/
function __construct($path)
{
try {
@parent::__construct($path);
} catch (Exception $e) {
throw new EnterpriseSecurityException(
'Failed to open stream',
'Failed to open stream ' . $e->getMessage()
);
}
$this->_doDirCheck($path);
$this->_doFileCheck($path);
$this->_doExtraCheck($path);
}
/**
* Checks the directory against null bytes and unprintable characters.
*
* @param string $path the path to the file (path && file name)
*
* @return does not return a value
* @exception ValidationException thrown if check fails
*/
private function _doDirCheck($path)
{
$dir = $this->getPath();
if ( preg_match($this->_DIR_BLACKLIST_PAT, $dir) ) {
throw new ValidationException(
'Invalid directory',
"Directory path ({$dir}) contains illegal character. "
);
}
if ( preg_match($this->_PERCENTS_PAT, $dir) ) {
throw new ValidationException(
'Invalid directory',
"Directory path ({$dir}) contains encoded characters. "
);
}
$ch = $this->_containsUnprintableCharacters($path);
if ($ch != -1) {
throw new ValidationException(
'Invalid directory',
"Directory path ({$dir}) contains unprintable character. "
);
}
}
/**
* Checks the file name against null bytes and unprintable characters.
*
* @param string $path the file name
*
* @return does not return a value
* @exception ValidationException thrown if check fails
*/
private function _doFileCheck($path)
{
$filename = $this->getFilename();
// Workaround for PHP == 5.2.0 getFilename returns entire path.
if (preg_match('/[\/\\\]/', $filename)) {
// this _might_ be a filename with a slash in it or it might be
// a full path.
$charEncP = mb_detect_encoding($path);
$pathLen = mb_strlen($path, $charEncP);
$charEncF = mb_detect_encoding($filename);
$fileLen = mb_strlen($filename, $charEncF);
if ($pathLen === $fileLen) {
// filename is the entire path!
$dir = $this->getPath();
$charEncD = mb_detect_encoding($dir);
$dirLen = mb_strlen($dir, $charEncD);
// sanity check that the entire path returned by getFilename is
// longer than the path returned by getPath
if ($fileLen <= $dirLen) {
throw new ValidationException(
'Invalid file',
'The path returned from SplFileObject::getFilename should have been shorter than the path returned by SplFileObject::getPath.'
);
}
// Assume file name is $filename with $dir+slash knocked off it.
$dirLen += 1;
$filename
= mb_substr($filename, $dirLen, $fileLen-$dirLen, $charEncF);
}
}
if ( preg_match($this->_FILE_BLACKLIST_PAT, $filename) ) {
throw new ValidationException(
'Invalid file',
"File path ({$filename}) contains illegal character.");
}
if ( preg_match($this->_PERCENTS_PAT, $filename) ) {
throw new ValidationException(
'Invalid file',
"File path ({$filename}) contains encoded characters."
);
}
$ch = $this->_containsUnprintableCharacters($filename);
if ($ch != -1) {
throw new ValidationException(
'Invalid file',
"File path ({$filename}) contains unprintable character."
);
}
}
/**
* Checks the specified string for unprintable characters (ASCII range
* from 0 to 31 and from 127 to 255).
*
* @param string $s the string to check for unprintable characters
*
* @return int the value of the first unprintable character found, or -1
*/
private function _containsUnprintableCharacters($s)
{
for ($i = 0; $i < strlen($s); $i++) {
$ch = $s[$i];
if (ord($ch) < 32 || ord($ch) > 126) {
return $ch;
}
}
return -1;
}
/**
* Checks if the last character is a slash
*
* @param string $path the string to check
*
* @return does not return a value
* @exception ValidationException thrown if check fails
*/
private function _doExtraCheck($path)
{
$last = substr($path, -1);
if ($last === '/') {
throw new ValidationException(
'Invalid file',
"File path ({$path}) contains an extra slash."
);
}
}
}