This commit is contained in:
Javier Casares 2026-08-23 07:08:42 +00:00
commit e6cba7ef6a

View file

@ -377,10 +377,59 @@ function wpvulnerability_debug_get_component_status() {
return $status;
}
/**
* Returns the slug of the first installed plugin or theme.
*
* Mirrors the slug derivation used by the vulnerability data loaders: the
* folder name, falling back to the text domain for single-file plugins.
*
* @since 5.1.6
*
* @param string $type Either 'plugin' or 'theme'.
*
* @return string Slug, or an empty string when nothing of that type is installed.
*/
function wpvulnerability_debug_first_installed_slug( $type ) {
if ( 'theme' === $type ) {
foreach ( wp_get_themes() as $theme ) {
return $theme->get_stylesheet();
}
return '';
}
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( ! function_exists( 'get_plugins' ) ) {
return '';
}
foreach ( get_plugins() as $plugin_file => $plugin_data ) {
$folder = explode( '/', $plugin_file );
$slug = trim( (string) $folder[0] );
if ( '' !== $slug && false === strpos( $plugin_file, '/' ) ) {
// Single-file plugin at the plugins root: use the file name.
$slug = basename( $slug, '.php' );
}
if ( '' === $slug && isset( $plugin_data['TextDomain'] ) ) {
$td_raw = $plugin_data['TextDomain'];
$slug = trim( is_scalar( $td_raw ) ? (string) $td_raw : '' );
}
if ( '' !== $slug ) {
return $slug;
}
}
return '';
}
/**
* Tests API connectivity for a specific component.
*
* @since 4.3.0
* @since 5.1.6 Core collapses pre-release versions to their stable milestone,
* and plugins/themes use the real slug-based API routes.
*
* @param string $component The component to test (e.g., 'core', 'plugins', 'php').
*
@ -403,33 +452,45 @@ function wpvulnerability_debug_test_api_component( $component ) {
return $result;
}
// Get version for the component.
// Build the API URL for the component. Core and software routes take a
// version; the plugin/theme routes take a slug only.
$url = '';
$version = null;
switch ( $component ) {
case 'core':
global $wp_version;
$version = $wp_version;
// The API only serves stable milestones: collapse pre-release
// suffixes (e.g. 7.1-alpha-62421) so the check keeps working.
$version = preg_replace( '/-(?:alpha|beta|rc).*$/i', '', trim( (string) $wp_version ) );
if ( ! is_string( $version ) || '' === $version ) {
$result['message'] = __( 'WordPress version not detected for this component.', 'wpvulnerability' );
return $result;
}
$url = WPVULNERABILITY_API_HOST . 'core/' . wpvulnerability_sanitize_version( $version ) . '/';
break;
case 'plugins':
case 'themes':
// Use a generic version for testing.
$version = '1.0.0';
$slug = wpvulnerability_debug_first_installed_slug( 'plugins' === $component ? 'plugin' : 'theme' );
if ( '' === $slug ) {
$result['message'] = 'plugins' === $component
? __( 'No plugins detected to test the API endpoint.', 'wpvulnerability' )
: __( 'No themes detected to test the API endpoint.', 'wpvulnerability' );
return $result;
}
$url = WPVULNERABILITY_API_HOST . ( 'plugins' === $component ? 'plugin/' : 'theme/' ) . sanitize_title( $slug ) . '/';
break;
default:
if ( function_exists( 'wpvulnerability_get_software_version' ) ) {
$version = wpvulnerability_get_software_version( $component );
}
if ( ! $version ) {
$result['message'] = __( 'No local version detected; the API endpoint was not called.', 'wpvulnerability' );
return $result;
}
$url = WPVULNERABILITY_API_HOST . $component . '/' . wpvulnerability_sanitize_version( $version ) . '/';
break;
}
if ( ! $version ) {
$result['message'] = __( 'Version not detected for this component.', 'wpvulnerability' );
return $result;
}
// Build API URL.
$url = WPVULNERABILITY_API_HOST . $component . '/' . $version . '/';
// Execute request with timing.
$start_time = microtime( true );
$response = wp_remote_get(
@ -537,6 +598,47 @@ function wpvulnerability_debug_get_cron_status() {
return $cron_status;
}
/**
* Masks sensitive configuration values (webhook URLs, tokens, emails).
*
* Used whenever configuration data is displayed or exported for debugging,
* so shared debug files never contain usable secrets or recipient addresses.
*
* @since 5.1.3
*
* @param mixed $config Plugin configuration.
* @return array<mixed, mixed> Configuration with sensitive values masked.
*/
function wpvulnerability_debug_mask_config( $config ) {
if ( ! is_array( $config ) ) {
return array();
}
$secret_keys = array( 'slack_webhook', 'teams_webhook', 'discord_webhook', 'telegram_bot_token', 'telegram_chat_id' );
foreach ( $secret_keys as $key ) {
if ( isset( $config[ $key ] ) && is_scalar( $config[ $key ] ) && '' !== (string) $config[ $key ] ) {
$value = (string) $config[ $key ];
$config[ $key ] = strlen( $value ) > 8 ? substr( $value, 0, 4 ) . '...' . substr( $value, -4 ) : '...';
}
}
if ( isset( $config['emails'] ) && is_scalar( $config['emails'] ) && '' !== (string) $config['emails'] ) {
$masked = array();
foreach ( explode( ',', (string) $config['emails'] ) as $email ) {
$parts = explode( '@', trim( $email ) );
if ( count( $parts ) < 2 || '' === $parts[1] ) {
$masked[] = '...';
continue;
}
$masked[] = ( '' !== $parts[0] ? substr( $parts[0], 0, 1 ) . '***' : '***' ) . '@' . $parts[1];
}
$config['emails'] = implode( ',', $masked );
}
return $config;
}
/**
* Exports comprehensive debug information as JSON.
*
@ -553,7 +655,7 @@ function wpvulnerability_debug_export_info() {
'timestamp' => current_time( 'mysql' ),
'system_info' => wpvulnerability_debug_get_system_info(),
'components' => wpvulnerability_debug_get_component_status(),
'configuration' => $config,
'configuration' => wpvulnerability_debug_mask_config( $config ),
'cron_status' => wpvulnerability_debug_get_cron_status(),
'vulnerability_counts' => array(),
);
@ -594,6 +696,13 @@ function wpvulnerability_debug_clear_all_caches() {
}
}
// Plugin data timestamp option, written by wpvulnerability_plugin_get_data().
if ( is_multisite() ) {
delete_site_option( 'wpvulnerability-plugins-cache-data' );
} else {
delete_option( 'wpvulnerability-plugins-cache-data' );
}
return true;
}
@ -657,8 +766,318 @@ function wpvulnerability_debug_get_option_names() {
*/
function wpvulnerability_debug_get_option_value( $option_name ) {
if ( is_multisite() ) {
return get_site_option( $option_name, null );
$value = get_site_option( $option_name, null );
} else {
return get_option( $option_name, null );
$value = get_option( $option_name, null );
}
// Mask secrets when displaying the configuration in the options viewer.
if ( 'wpvulnerability-config' === $option_name && is_array( $value ) ) {
$value = wpvulnerability_debug_mask_config( $value );
}
return $value;
}
/**
* Returns the PHP extensions WordPress makes use of, grouped by relevance.
*
* Purely informational: presence or absence of an extension is neither good
* nor bad. Grouping and notes follow the WordPress core recommendations.
*
* @since 5.1.6
*
* @return array<array{label: string, extensions: array<string, string>}> Each group has a
* label and a map of extension name to description.
*/
function wpvulnerability_debug_get_php_extensions() {
return array(
array(
'label' => __( 'Built-in (no hosting action required)', 'wpvulnerability' ),
'extensions' => array(
'pcre' => __( 'Regular expression engine used throughout PHP and WordPress.', 'wpvulnerability' ),
),
),
array(
'label' => __( 'Required', 'wpvulnerability' ),
'extensions' => array(
'hash' => __( 'Hashing, including passwords and update packages.', 'wpvulnerability' ),
'json' => __( 'Communications with other servers and JSON data processing.', 'wpvulnerability' ),
'mysqli' => __( 'Connects to MySQL/MariaDB for database interactions.', 'wpvulnerability' ),
),
),
array(
'label' => __( 'Highly recommended', 'wpvulnerability' ),
'extensions' => array(
'curl' => __( 'Performs remote request operations.', 'wpvulnerability' ),
'dom' => __( 'Validates Text Widget content and configures IIS7+ automatically.', 'wpvulnerability' ),
'exif' => __( 'Works with metadata stored in images.', 'wpvulnerability' ),
'fileinfo' => __( 'Detects MIME types of file uploads.', 'wpvulnerability' ),
'igbinary' => __( 'Drop-in replacement for the standard PHP serializer; improves performance.', 'wpvulnerability' ),
'imagick' => __( 'Better image quality for media uploads and PDF thumbnail support.', 'wpvulnerability' ),
'intl' => __( 'Locale-aware operations: formatting, transliteration, collation, timezones.', 'wpvulnerability' ),
'mbstring' => __( 'Properly handles UTF-8 text.', 'wpvulnerability' ),
'openssl' => __( 'SSL-based connections to other hosts.', 'wpvulnerability' ),
'xml' => __( 'XML parsing, such as from a third-party site.', 'wpvulnerability' ),
'zip' => __( 'Decompresses Plugin, Theme, and WordPress update packages.', 'wpvulnerability' ),
),
),
array(
'label' => __( 'Object cache (only one is needed)', 'wpvulnerability' ),
'extensions' => array(
'apcu' => __( 'In-memory key-value store for PHP.', 'wpvulnerability' ),
'memcached' => __( 'Distributed memory object caching system.', 'wpvulnerability' ),
'opcache' => __( 'Stores precompiled PHP bytecode to reduce repeated parsing and compilation.', 'wpvulnerability' ),
'redis' => __( 'PHP extension for interfacing with Redis.', 'wpvulnerability' ),
),
),
array(
'label' => __( 'Optional improvements', 'wpvulnerability' ),
'extensions' => array(
'timezonedb' => __( 'Timezone database used by the PHP date and time functions.', 'wpvulnerability' ),
),
),
array(
'label' => __( 'Fallbacks / situational use', 'wpvulnerability' ),
'extensions' => array(
'bcmath' => __( 'Arbitrary precision mathematics.', 'wpvulnerability' ),
'filter' => __( 'Securely filters user input.', 'wpvulnerability' ),
'gd' => __( 'Functionally limited fallback for image manipulation when Imagick is unavailable.', 'wpvulnerability' ),
'iconv' => __( 'Converts between character sets.', 'wpvulnerability' ),
'shmop' => __( 'Reads, writes, creates and deletes Unix shared memory segments.', 'wpvulnerability' ),
'simplexml' => __( 'XML parsing.', 'wpvulnerability' ),
'sodium' => __( 'Signature validation and securely random bytes.', 'wpvulnerability' ),
'xmlreader' => __( 'XML parsing.', 'wpvulnerability' ),
'zlib' => __( 'Gzip compression and decompression.', 'wpvulnerability' ),
),
),
array(
'label' => __( 'File changes when files are not writeable (transports)', 'wpvulnerability' ),
'extensions' => array(
'ssh2' => __( 'Shell, remote execution, tunneling and file transfer over SSH.', 'wpvulnerability' ),
'ftp' => __( 'FTP client access for updates and installations.', 'wpvulnerability' ),
'sockets' => __( 'Low-level socket communication functions.', 'wpvulnerability' ),
),
),
);
}
/**
* Detects whether a PHP extension is loaded.
*
* Handles special cases where the internal extension name differs from the
* common name or where availability depends on functions instead.
*
* @since 5.1.6
*
* @param string $extension Extension name as listed in the recommendations.
*
* @return bool True when the extension is loaded.
*/
function wpvulnerability_debug_extension_loaded( $extension ) {
if ( 'opcache' === $extension ) {
return extension_loaded( 'opcache' )
|| extension_loaded( 'Zend OPcache' )
|| function_exists( 'opcache_get_status' );
}
return extension_loaded( $extension );
}
/**
* Gathers system package information relevant to WordPress.
*
* Informational only. Shell probes honour the plugin shell-exec policy
* (security mode, disable constant) through the safe wrapper; when probing
* is not possible the version is reported as unknown.
*
* @since 5.1.6
*
* @return array<array{name: string, available: bool, version: string|null}> Package rows.
*/
function wpvulnerability_debug_get_system_packages() {
$packages = array();
// cURL: reuse the shared detector (PHP extension first, then CLI probe).
$curl_version = function_exists( 'wpvulnerability_detect_curl' ) ? wpvulnerability_detect_curl() : null;
$packages[] = array(
'name' => 'curl',
'available' => null !== $curl_version,
'version' => $curl_version,
);
// ImageMagick: reuse the shared detector.
$imagemagick = function_exists( 'wpvulnerability_detect_imagemagick' ) ? wpvulnerability_detect_imagemagick() : array();
$im_raw = isset( $imagemagick['version'] ) ? $imagemagick['version'] : null;
$im_version = is_string( $im_raw ) ? $im_raw : null;
$packages[] = array(
'name' => 'ImageMagick',
'available' => null !== $im_version,
'version' => $im_version,
);
// Ghost Script: enables Imagick/ImageMagick PDF thumbnail generation.
$gs_version = null;
if ( wpvulnerability_can_shell_exec() ) {
$gs_output = wpvulnerability_safe_shell_exec( 'gs', 'gs --version' );
$gs_matches = array();
if ( is_string( $gs_output ) && preg_match( '/(\d+\.\d+(?:\.\d+)?)/', trim( $gs_output ), $gs_matches ) ) {
$gs_version = $gs_matches[1];
}
}
$packages[] = array(
'name' => 'Ghost Script',
'available' => null !== $gs_version,
'version' => $gs_version,
);
// OpenSSL: report the OpenSSL linked into the PHP build.
$openssl_version = null;
if ( defined( 'OPENSSL_VERSION_TEXT' ) && preg_match( '/(?:OpenSSL|LibreSSL|BoringSSL)\s+(\d[\w.]*)/', (string) OPENSSL_VERSION_TEXT, $o_matches ) ) {
$openssl_version = $o_matches[1];
}
$packages[] = array(
'name' => 'OpenSSL',
'available' => defined( 'OPENSSL_VERSION_TEXT' ),
'version' => $openssl_version,
);
// WebP and AVIF support: check Imagick formats, GD, then CLI tools.
foreach (
array(
'WebP' => array(
'format' => 'WEBP',
'gd_key' => 'WebP Support',
'tool' => 'cwebp',
'command' => 'cwebp -version',
),
'AVIF' => array(
'format' => 'AVIF',
'gd_key' => 'AVIF Support',
'tool' => 'avifenc',
'command' => 'avifenc --version',
),
)
as $format_name => $format_check
) {
$supported = false;
$version = null;
if ( extension_loaded( 'imagick' ) && class_exists( 'Imagick' ) ) {
try {
$imagick = new Imagick();
$supported = in_array( strtoupper( $format_check['format'] ), $imagick->queryFormats( $format_check['format'] ), true );
} catch ( Exception $e ) {
$supported = false;
}
}
if ( ! $supported && extension_loaded( 'gd' ) && function_exists( 'gd_info' ) ) {
$gd_info = gd_info();
$supported = ! empty( $gd_info[ $format_check['gd_key'] ] );
}
if ( ! $supported && wpvulnerability_can_shell_exec() ) {
$tool_output = wpvulnerability_safe_shell_exec( $format_check['tool'], $format_check['command'] );
if ( is_string( $tool_output ) && '' !== trim( $tool_output ) ) {
$supported = true;
$v_matches = array();
if ( preg_match( '/(\d+\.\d+(?:\.\d+)?)/', $tool_output, $v_matches ) ) {
$version = $v_matches[1];
}
}
}
$packages[] = array(
'name' => $format_name,
'available' => $supported,
'version' => $version,
);
}
return $packages;
}
/**
* Renders the Debug tab section: PHP extensions and system packages.
*
* Informational listing only: nothing here is good or bad.
*
* @since 5.1.6
*
* @return void
*/
function wpvulnerability_render_debug_section_php_extensions() {
$groups = wpvulnerability_debug_get_php_extensions();
$packages = wpvulnerability_debug_get_system_packages();
?>
<div class="wpvulnerability-debug-section">
<h3><?php esc_html_e( 'PHP Extensions', 'wpvulnerability' ); ?></h3>
<p>
<?php esc_html_e( 'WordPress core makes use of various PHP extensions when they are available. This list is informational only: a missing extension is neither good nor bad.', 'wpvulnerability' ); ?>
</p>
<?php
foreach ( $groups as $group ) :
?>
<h4><?php echo esc_html( $group['label'] ); ?></h4>
<table class="widefat striped" style="max-width: 720px;">
<tbody>
<?php
foreach ( $group['extensions'] as $extension => $description ) :
$loaded = wpvulnerability_debug_extension_loaded( $extension );
?>
<tr>
<td style="width: 90px;">
<span style="color: <?php echo $loaded ? '#00a32a;' : '#8c8f94;'; ?>"><?php echo $loaded ? esc_html__( 'Loaded', 'wpvulnerability' ) : esc_html__( 'Not loaded', 'wpvulnerability' ); ?></span>
</td>
<td style="width: 110px;"><code><?php echo esc_html( $extension ); ?></code></td>
<td><?php echo esc_html( $description ); ?></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
<?php
endforeach;
?>
<h3><?php esc_html_e( 'System Packages', 'wpvulnerability' ); ?></h3>
<p>
<?php esc_html_e( 'System software WordPress can leverage. Informational only.', 'wpvulnerability' ); ?>
</p>
<table class="widefat striped" style="max-width: 720px;">
<tbody>
<?php
foreach ( $packages as $package ) :
?>
<tr>
<td style="width: 90px;">
<span style="color: <?php echo $package['available'] ? '#00a32a;' : '#8c8f94;'; ?>">
<?php echo $package['available'] ? esc_html__( 'Available', 'wpvulnerability' ) : esc_html__( 'Not detected', 'wpvulnerability' ); ?>
</span>
</td>
<td style="width: 110px;"><code><?php echo esc_html( (string) $package['name'] ); ?></code></td>
<td>
<?php
if ( null !== $package['version'] && '' !== (string) $package['version'] ) {
echo esc_html(
sprintf(
/* translators: %s: package version number */
__( 'Version %s', 'wpvulnerability' ),
(string) $package['version']
)
);
} elseif ( ! $package['available'] ) {
esc_html_e( 'No local version detected.', 'wpvulnerability' );
}
?>
</td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
</div>
<?php
}