Skip to content

Commit 18ebe15

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: Update NEWS Fix #81211: Symlinks are followed when creating PHAR archive
2 parents bdf53cc + 33e4174 commit 18ebe15

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

ext/phar/phar_object.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,7 @@ static int phar_build(zend_object_iterator *iter, void *puser) /* {{{ */
13851385
zend_class_entry *ce = p_obj->c;
13861386
phar_archive_object *phar_obj = p_obj->p;
13871387
php_stream_statbuf ssb;
1388+
char ch;
13881389

13891390
value = iter->funcs->get_current_data(iter);
13901391

@@ -1505,7 +1506,7 @@ static int phar_build(zend_object_iterator *iter, void *puser) /* {{{ */
15051506
base = temp;
15061507
base_len = strlen(base);
15071508

1508-
if (strstr(fname, base)) {
1509+
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))) {
15091510
str_key_len = fname_len - base_len;
15101511

15111512
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"

0 commit comments

Comments
 (0)