Skip to content

Commit e5c9523

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

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
@@ -1439,6 +1439,7 @@ static int phar_build(zend_object_iterator *iter, void *puser) /* {{{ */
14391439
char *str_key;
14401440
zend_class_entry *ce = p_obj->c;
14411441
phar_archive_object *phar_obj = p_obj->p;
1442+
php_stream_statbuf ssb;
14421443

14431444
value = iter->funcs->get_current_data(iter);
14441445

@@ -1718,6 +1719,16 @@ static int phar_build(zend_object_iterator *iter, void *puser) /* {{{ */
17181719
php_stream_copy_to_stream_ex(fp, p_obj->fp, PHP_STREAM_COPY_ALL, &contents_len);
17191720
data->internal_file->uncompressed_filesize = data->internal_file->compressed_filesize =
17201721
php_stream_tell(p_obj->fp) - data->internal_file->offset;
1722+
if (php_stream_stat(fp, &ssb) != -1) {
1723+
data->internal_file->flags = ssb.sb.st_mode & PHAR_ENT_PERM_MASK ;
1724+
} else {
1725+
#ifndef _WIN32
1726+
mode_t mask;
1727+
mask = umask(0);
1728+
umask(mask);
1729+
data->internal_file->flags &= ~mask;
1730+
#endif
1731+
}
17211732
}
17221733

17231734
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)