1212 */
1313class Checksum_Plugin_Command extends Checksum_Base_Command {
1414
15+ /**
16+ * Maximum file size (in bytes) to read when detecting plugin version.
17+ * Set to 1MB to prevent memory exhaustion attacks.
18+ */
19+ const MAX_FILE_SIZE_FOR_VERSION_DETECTION = 1048576 ;
20+
1521 /**
1622 * Cached plugin data for all installed plugins.
1723 *
@@ -255,10 +261,10 @@ private function detect_version_from_directory( $plugin_dir ) {
255261 // Look for version in readme.txt first, as it's commonly accurate
256262 $ readme_file = $ plugin_path . '/readme.txt ' ;
257263 if ( file_exists ( $ readme_file ) && is_readable ( $ readme_file ) ) {
258- // Check file size to prevent memory exhaustion (limit to 1MB)
264+ // Check file size to prevent memory exhaustion
259265 $ file_size = filesize ( $ readme_file );
260- if ( false !== $ file_size && $ file_size < 1048576 ) {
261- $ readme_content = file_get_contents ( $ readme_file );
266+ if ( false !== $ file_size && $ file_size < self :: MAX_FILE_SIZE_FOR_VERSION_DETECTION ) {
267+ $ readme_content = @ file_get_contents ( $ readme_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
262268 if ( false !== $ readme_content && preg_match ( '/^Stable tag:\s*(.+)$/mi ' , $ readme_content , $ matches ) ) {
263269 $ version = trim ( $ matches [1 ] );
264270 if ( 'trunk ' !== strtolower ( $ version ) ) {
@@ -270,17 +276,20 @@ private function detect_version_from_directory( $plugin_dir ) {
270276
271277 // If not found in readme, try scanning PHP files for Version header
272278 $ files = glob ( $ plugin_path . '/*.php ' );
273- if ( is_array ( $ files ) ) {
279+ if ( is_array ( $ files ) && ! empty ( $ files ) ) {
274280 foreach ( $ files as $ file ) {
275- // Check file size to prevent memory exhaustion (limit to 1MB)
281+ // Check file size to prevent memory exhaustion
276282 $ file_size = filesize ( $ file );
277- if ( false !== $ file_size && $ file_size < 1048576 && is_readable ( $ file ) ) {
278- $ file_content = file_get_contents ( $ file );
283+ if ( false !== $ file_size && $ file_size < self :: MAX_FILE_SIZE_FOR_VERSION_DETECTION && is_readable ( $ file ) ) {
284+ $ file_content = @ file_get_contents ( $ file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
279285 if ( false !== $ file_content && preg_match ( '/^\s*\*\s*Version:\s*(.+)$/mi ' , $ file_content , $ matches ) ) {
280286 return trim ( $ matches [1 ] );
281287 }
282288 }
283289 }
290+ } elseif ( false === $ files ) {
291+ // glob() failed - likely a permission issue, but we can continue
292+ // Version will just not be detected from PHP files
284293 }
285294
286295 return false ;
0 commit comments