Skip to content

Commit 7e49e8e

Browse files
committed
Fix open_basedir check for glob:// opendir wrapper
php_check_open_basedir() expects a local filesystem path, but we're handing it a `glob://...` URI instead. Move the check to after the path trim so that we're checking a meaningful pathspec.
1 parent db89095 commit 7e49e8e

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Glob wrapper bypasses open_basedir
3+
--INI--
4+
open_basedir=/does_not_exist
5+
--SKIPIF--
6+
<?php
7+
if (!in_array("glob", stream_get_wrappers())) echo "skip";
8+
--FILE--
9+
<?php
10+
11+
foreach ( [ __DIR__, "glob://".__DIR__ ] as $spec) {
12+
echo "** Opening $spec\n";
13+
$dir = opendir($spec);
14+
if (!$dir) {
15+
echo "Failed to open $spec\n";
16+
continue;
17+
}
18+
if (false === readdir($dir)) {
19+
echo "No files in $spec\n";
20+
continue;
21+
}
22+
}
23+
--EXPECTF--
24+
** Opening %s
25+
26+
Warning: opendir(): open_basedir restriction in effect. File(/%s) is not within the allowed path(s): (/does_not_exist) in %s/glob-wrapper.php on line 5
27+
28+
Warning: opendir(/%s): failed to open dir: Operation not permitted in %s/glob-wrapper.php on line 5
29+
Failed to open /%s
30+
** Opening glob://%s
31+
32+
Warning: opendir(): open_basedir restriction in effect. File(/%s) is not within the allowed path(s): (/does_not_exist) in %s/glob-wrapper.php on line 5
33+
34+
Warning: opendir(glob://%s): failed to open dir: operation failed in %s/glob-wrapper.php on line 5
35+
Failed to open glob://%s

main/streams/glob_wrapper.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,17 @@ static php_stream *php_glob_stream_opener(php_stream_wrapper *wrapper, const cha
213213
int ret;
214214
const char *tmp, *pos;
215215

216-
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path TSRMLS_CC)) {
217-
return NULL;
218-
}
219-
220216
if (!strncmp(path, "glob://", sizeof("glob://")-1)) {
221217
path += sizeof("glob://")-1;
222218
if (opened_path) {
223219
*opened_path = estrdup(path);
224220
}
225221
}
226222

223+
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path TSRMLS_CC)) {
224+
return NULL;
225+
}
226+
227227
pglob = ecalloc(sizeof(*pglob), 1);
228228

229229
if (0 != (ret = glob(path, pglob->flags & GLOB_FLAGMASK, NULL, &pglob->glob))) {

0 commit comments

Comments
 (0)