20, 'redirection' => 0, 'sslverify' => true, 'stream' => true, 'filename' => $tmp, 'limit_response_size' => self::MAX_DOWNLOAD, 'user-agent' => 'Shieldlume Security/0.1' ) ); self::checkDirectory( $directory, $directoryIdentity ); clearstatcache( true, $tmp ); $after = @lstat( $tmp ); if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) || ! $after || $after['ino'] !== $before['ino'] || $after['dev'] !== $before['dev'] || 1 !== $after['nlink'] || 0100000 !== ( $after['mode'] & 0170000 ) || 0600 !== ( $after['mode'] & 0777 ) || $after['size'] >= self::MAX_DOWNLOAD ) { throw new \RuntimeException( 'Official repository package unavailable, unsafe or larger than the download budget.' ); } $zip = new \ZipArchive(); if ( true !== $zip->open( $tmp, \ZipArchive::RDONLY ) ) { throw new \RuntimeException( 'Invalid official repository package.' ); } try { $entries = self::entries( $zip, $slug ); $result = $inspect( $zip, $entries ); $result['source'] = array( 'provider' => 'WordPress.org', 'url' => $url, 'package_url' => $url, 'package_sha256' => hash_file( 'sha256', $tmp ), 'fetched_at' => gmdate( 'c' ), 'expires_at' => gmdate( 'c', time() + 86400 ), 'transport' => 'HTTPS certificate validation; redirects refused', 'reference_hash' => 'SHA-256 derived from the exact official package over authenticated HTTPS; no independent theme checksum service or safety guarantee.' ); return $result; } finally { $zip->close(); } } finally { if ( ! $cleanup() ) { throw new \RuntimeException( 'The temporary repository archive could not be safely removed. Ask your host to review the plugin repository-staging directory.' ); } } } public static function entries( \ZipArchive $zip, string $slug ): array { if ( $zip->numFiles > 10000 ) { throw new \RuntimeException( 'Repository archive entry budget exceeded.' ); } $total = 0; $names = array(); $entries = array(); $deadline = microtime( true ) + 5; for ( $i = 0; $i < $zip->numFiles; $i++ ) { $entry = $zip->statIndex( $i ); if ( ! is_array( $entry ) || microtime( true ) > $deadline ) { throw new \RuntimeException( 'Repository archive inspection budget exceeded.' ); } $zip->getExternalAttributesIndex( $i, $opsys, $attrs ); PathGuard::archiveEntry( $entry['name'], $entry['size'], $entry['comp_size'], ( $attrs >> 16 ) & 0xffff, 16777216 ); if ( ! str_starts_with( $entry['name'], $slug . '/' ) || isset( $names[ rtrim( $entry['name'], '/' ) ] ) || ! empty( $entry['encryption_method'] ) ) { throw new \RuntimeException( 'Repository archive contains unexpected, duplicate or encrypted entries.' ); } $names[ rtrim( $entry['name'], '/' ) ] = true; $total += $entry['size']; if ( $total > 134217728 ) { throw new \RuntimeException( 'Repository archive expansion budget exceeded.' ); } if ( str_ends_with( $entry['name'], '/' ) ) { continue; } $relative = substr( $entry['name'], strlen( $slug ) + 1 ); PathGuard::relative( $relative ); if ( strlen( $relative ) > 512 ) { throw new \RuntimeException( 'Repository archive path exceeds the reference budget.' ); } $entries[ $relative ] = $entry; } return $entries; } public static function content( \ZipArchive $zip, string $slug, string $path, array $entries, int $limit = 2097152 ): string { PathGuard::relative( $path ); $entry = $entries[ $path ] ?? null; if ( ! $entry || $entry['size'] > $limit ) { throw new \RuntimeException( 'Reference file unavailable or too large.' ); } $content = $zip->getFromName( $slug . '/' . $path, $limit + 1 ); if ( ! is_string( $content ) || strlen( $content ) !== $entry['size'] ) { throw new \RuntimeException( 'Reference package read failed.' ); } return $content; } public static function checkHeader( string $content, string $type, string $version, string $name ): void { $label = 'theme' === $type ? 'Theme Name' : 'Plugin Name'; $header = substr( str_replace( "\r", "\n", $content ), 0, 8192 ); foreach ( array( 'Version' => $version, $label => $name ) as $field => $expected ) { $value = preg_match( '/^[ \t\/*#@]*' . preg_quote( $field, '/' ) . ':\s*(.{0,500})$/mi', $header, $match ) ? substr( trim( preg_replace( '/\s*(?:\*\/|\?>).*$/', '', $match[1] ) ), 0, 200 ) : ''; if ( $expected !== $value ) { throw new \RuntimeException( 'Official package header does not match the owner-confirmed component name and version.' ); } } } }