Skip to content

Commit 32ba58d

Browse files
committed
Handle Failed to open stream: Permission denied in FileCache::import()
1 parent 6f0b0e9 commit 32ba58d

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

php/WP_CLI/FileCache.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ public function read( $key, $ttl = null ) {
162162
public function import( $key, $source ) {
163163
$filename = $this->prepare_write( $key );
164164

165+
if ( ! is_readable( $source ) ) {
166+
return false;
167+
}
168+
165169
if ( $filename ) {
166170
return copy( $source, $filename ) && touch( $filename );
167171
}

tests/test-file-cache.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,47 @@ public function test_import() {
128128
$cache->clear();
129129
unlink( $fixture_filepath );
130130
}
131+
132+
/**
133+
* @see https://github.com/wp-cli/wp-cli/pull/5947
134+
*/
135+
public function test_import_do_not_use_cache_file_cannot_be_read() {
136+
$max_size = 32;
137+
$ttl = 60;
138+
$cache_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-file-cache', true );
139+
$cache = new FileCache( $cache_dir, $ttl, $max_size );
140+
141+
$tmp_dir = Utils\get_temp_dir() . uniqid( 'wp-cli-test-file-cache-import', true );
142+
mkdir( $tmp_dir );
143+
144+
$key = 'plugin/my-fixture-plugin-1.0.0.zip';
145+
$fixture_filepath = $tmp_dir . '/my-bad-permissions-fixture-plugin-1.0.0.zip';
146+
147+
$zip = new ZipArchive();
148+
$zip->open( $fixture_filepath, ZIPARCHIVE::CREATE );
149+
$zip->addFile( __FILE__ );
150+
$zip->close();
151+
152+
chmod( $fixture_filepath, 0000 );
153+
154+
// "Warning: copy(-.): Failed to open stream: Permission denied".
155+
$error = null;
156+
set_error_handler(
157+
function ( $errno, $errstr ) use ( &$error ) {
158+
$error = $errstr;
159+
}
160+
);
161+
162+
$result = $cache->import( $key, $fixture_filepath );
163+
164+
restore_error_handler();
165+
166+
self::assertNull( $error );
167+
self::assertFalse( $result );
168+
169+
// Clean up.
170+
$cache->clear();
171+
unlink( $fixture_filepath );
172+
rmdir( $tmp_dir );
173+
}
131174
}

0 commit comments

Comments
 (0)