Skip to content

Commit bcbc053

Browse files
committed
Fix bug #79082 - Files added to tar with Phar::buildFromIterator have all-access permissions
1 parent 90ae181 commit bcbc053

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

ext/phar/phar_object.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,7 @@ static int phar_build(zend_object_iterator *iter, void *puser) /* {{{ */
14241424
char *str_key;
14251425
zend_class_entry *ce = p_obj->c;
14261426
phar_archive_object *phar_obj = p_obj->p;
1427+
php_stream_statbuf ssb;
14271428

14281429
value = iter->funcs->get_current_data(iter);
14291430

@@ -1691,6 +1692,16 @@ static int phar_build(zend_object_iterator *iter, void *puser) /* {{{ */
16911692
php_stream_copy_to_stream_ex(fp, p_obj->fp, PHP_STREAM_COPY_ALL, &contents_len);
16921693
data->internal_file->uncompressed_filesize = data->internal_file->compressed_filesize =
16931694
php_stream_tell(p_obj->fp) - data->internal_file->offset;
1695+
if (php_stream_stat(fp, &ssb) != -1) {
1696+
data->internal_file->flags = ssb.sb.st_mode & PHAR_ENT_PERM_MASK ;
1697+
} else {
1698+
#ifndef _WIN32
1699+
mode_t mask;
1700+
mask = umask(0);
1701+
umask(mask);
1702+
data->internal_file->flags &= ~mask;
1703+
#endif
1704+
}
16941705
}
16951706

16961707
if (close_fp) {

ext/phar/tests/bug79082.phpt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--TEST--
2+
Phar: Bug #79082: Files added to tar with Phar::buildFromIterator have all-access permissions
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("phar")) die("skip");
6+
if (defined("PHP_WINDOWS_VERSION_MAJOR")) die("skip not for Windows")
7+
?>
8+
--FILE--
9+
<?php
10+
umask(022);
11+
var_dump(decoct(umask()));
12+
chmod(__DIR__ . '/test79082/test79082-testfile', 0644);
13+
chmod(__DIR__ . '/test79082/test79082-testfile2', 0400);
14+
15+
foreach([Phar::TAR => 'tar', Phar::ZIP => 'zip'] as $mode => $ext) {
16+
clearstatcache();
17+
$phar = new PharData(__DIR__ . '/test79082.' . $ext, null, null, $mode);
18+
$phar->buildFromIterator(new \RecursiveDirectoryIterator(__DIR__ . '/test79082', \FilesystemIterator::SKIP_DOTS), __DIR__ . '/test79082');
19+
$phar->extractTo(__DIR__);
20+
var_dump(decoct(stat(__DIR__ . '/test79082-testfile')['mode']));
21+
var_dump(decoct(stat(__DIR__ . '/test79082-testfile2')['mode']));
22+
unlink(__DIR__ . '/test79082-testfile');
23+
unlink(__DIR__ . '/test79082-testfile2');
24+
}
25+
foreach([Phar::TAR => 'tar', Phar::ZIP => 'zip'] as $mode => $ext) {
26+
clearstatcache();
27+
$phar = new PharData(__DIR__ . '/test79082-d.' . $ext, null, null, $mode);
28+
$phar->buildFromDirectory(__DIR__ . '/test79082');
29+
$phar->extractTo(__DIR__);
30+
var_dump(decoct(stat(__DIR__ . '/test79082-testfile')['mode']));
31+
var_dump(decoct(stat(__DIR__ . '/test79082-testfile2')['mode']));
32+
unlink(__DIR__ . '/test79082-testfile');
33+
unlink(__DIR__ . '/test79082-testfile2');
34+
}
35+
?>
36+
--CLEAN--
37+
<?
38+
unlink(__DIR__ . '/test79082.tar');
39+
unlink(__DIR__ . '/test79082.zip');
40+
unlink(__DIR__ . '/test79082-d.tar');
41+
unlink(__DIR__ . '/test79082-d.zip');
42+
?>
43+
--EXPECT--
44+
string(2) "22"
45+
string(6) "100644"
46+
string(6) "100400"
47+
string(6) "100644"
48+
string(6) "100400"
49+
string(6) "100644"
50+
string(6) "100400"
51+
string(6) "100644"
52+
string(6) "100400"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

0 commit comments

Comments
 (0)