Skip to content

Commit 2ff853a

Browse files
cmb69smalyshev
authored andcommitted
Fix #81211: Symlinks are followed when creating PHAR archive
It is insufficient to check whether the `base` is contained in `fname`; we also need to ensure that `fname` is properly separated. And of course, `fname` has to start with `base`.
1 parent 53ea910 commit 2ff853a

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

ext/phar/phar_object.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,7 @@ static int phar_build(zend_object_iterator *iter, void *puser) /* {{{ */
14051405
zend_class_entry *ce = p_obj->c;
14061406
phar_archive_object *phar_obj = p_obj->p;
14071407
php_stream_statbuf ssb;
1408+
char ch;
14081409

14091410
value = iter->funcs->get_current_data(iter);
14101411

@@ -1528,7 +1529,7 @@ static int phar_build(zend_object_iterator *iter, void *puser) /* {{{ */
15281529
base = temp;
15291530
base_len = strlen(base);
15301531

1531-
if (strstr(fname, base)) {
1532+
if (fname_len >= base_len && strncmp(fname, base, base_len) == 0 && ((ch = fname[base_len - IS_SLASH(base[base_len - 1])]) == '\0' || IS_SLASH(ch))) {
15321533
str_key_len = fname_len - base_len;
15331534

15341535
if (str_key_len <= 0) {

ext/phar/tests/bug81211.phpt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
Bug #81211 (Symlinks are followed when creating PHAR archive)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('phar')) die('skip phar extension is not available');
6+
if (PHP_OS_FAMILY === 'Windows') {
7+
if (false === include __DIR__ . '/../../standard/tests/file/windows_links/common.inc') {
8+
die('skip windows_links/common.inc is not available');
9+
}
10+
skipIfSeCreateSymbolicLinkPrivilegeIsDisabled(__FILE__);
11+
}
12+
?>
13+
--FILE--
14+
<?php
15+
mkdir(__DIR__ . '/bug81211');
16+
mkdir(__DIR__ . '/bug81211/foobar');
17+
mkdir(__DIR__ . '/bug81211/foo');
18+
19+
file_put_contents(__DIR__ . '/bug81211/foobar/file', 'this file should NOT be included in the archive!');
20+
symlink(__DIR__ . '/bug81211/foobar/file', __DIR__ . '/bug81211/foo/symlink');
21+
22+
$archive = new PharData(__DIR__ . '/bug81211/archive.tar');
23+
try {
24+
$archive->buildFromDirectory(__DIR__ . '/bug81211/foo');
25+
} catch (UnexpectedValueException $ex) {
26+
echo $ex->getMessage(), PHP_EOL;
27+
}
28+
try {
29+
$archive->buildFromIterator(new RecursiveDirectoryIterator(__DIR__ . '/bug81211/foo', FilesystemIterator::SKIP_DOTS), __DIR__ . '/bug81211/foo');
30+
} catch (UnexpectedValueException $ex) {
31+
echo $ex->getMessage(), PHP_EOL;
32+
}
33+
?>
34+
--CLEAN--
35+
<?php
36+
@unlink(__DIR__ . '/bug81211/archive.tar');
37+
@unlink(__DIR__ . '/bug81211/foo/symlink');
38+
@unlink(__DIR__ . '/bug81211/foobar/file');
39+
@rmdir(__DIR__ . '/bug81211/foo');
40+
@rmdir(__DIR__ . '/bug81211/foobar');
41+
@rmdir(__DIR__ . '/bug81211');
42+
?>
43+
--EXPECTF--
44+
Iterator RecursiveIteratorIterator returned a path "%s%ebug81211\foobar\file" that is not in the base directory "%s%ebug81211\foo"
45+
Iterator RecursiveDirectoryIterator returned a path "%s%ebug81211\foobar\file" that is not in the base directory "%s%ebug81211\foo"

ext/standard/tests/file/windows_links/common.inc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,11 @@ function get_mountvol() {
2020
return "$sysroot\\System32\\mountvol.exe";
2121
}
2222

23-
?>
23+
function skipIfSeCreateSymbolicLinkPrivilegeIsDisabled(string $filename) {
24+
$ln = "$filename.lnk";
25+
$ret = exec("mklink $ln " . __FILE__ .' 2>&1', $out);
26+
@unlink($ln);
27+
if (strpos($ret, 'privilege') !== false) {
28+
die('skip SeCreateSymbolicLinkPrivilege not enabled');
29+
}
30+
}

0 commit comments

Comments
 (0)