cron|manual|cli. * @return array|WP_Error */ public static function run( $args = array() ) { if ( get_transient( self::LOCK ) ) { return new WP_Error( 'sunsetly_locked', __( 'A scan is already running.', 'sunsetly' ) ); } set_transient( self::LOCK, 1, 10 * MINUTE_IN_SECONDS ); try { $report = self::build( $args ); } finally { delete_transient( self::LOCK ); } $previous = self::report(); update_option( self::REPORT, $report, false ); self::push_history( $report ); $prev_ids = array(); if ( $previous && ! empty( $previous['findings'] ) ) { foreach ( $previous['findings'] as $f ) { $prev_ids[ $f['id'] ] = $f; } } $new = array(); $now_ids = array(); foreach ( $report['findings'] as $f ) { $now_ids[ $f['id'] ] = true; if ( null !== $previous && ! isset( $prev_ids[ $f['id'] ] ) ) { $new[] = $f; } } $resolved = array(); foreach ( $prev_ids as $id => $f ) { if ( ! isset( $now_ids[ $id ] ) ) { $resolved[] = $f; } } /** * Fires after each scan. Extensions can hook in here, e.g. for reports or logs. * * @param array $report Full report. * @param array $new Findings that did not exist in the previous scan. * @param array $resolved Findings that disappeared since the previous scan. */ do_action( 'sunsetly_scan_complete', $report, $new, $resolved ); return $report; } /** * Build the report. * * @param array $args Args. * @return array */ public static function build( $args = array() ) { global $wp_version; Sunsetly_HTTP::$errors = array(); if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if ( ! function_exists( 'get_core_updates' ) ) { require_once ABSPATH . 'wp-admin/includes/update.php'; } $findings = array(); $inventory = self::inventory(); $db = Sunsetly_Lifecycle::detect_db(); // ---- Lifecycle: PHP + database ------------------------------------------------- $php = Sunsetly_Lifecycle::status( 'PHP', 'php', PHP_VERSION ); $dbs = Sunsetly_Lifecycle::status( $db['label'], $db['product'], $db['version'] ); $php_cves = Sunsetly_Vulnerabilities::for_component( 'php', 'php', PHP_VERSION ); $findings = array_merge( $findings, self::lifecycle_findings( $php, $php_cves ) ); $findings = array_merge( $findings, self::lifecycle_findings( $dbs, array() ) ); // ---- WordPress core ------------------------------------------------------------ $core_state = Sunsetly_WPOrg::core_status( $wp_version ); $core_vulns = Sunsetly_Vulnerabilities::for_component( 'core', 'wordpress', $wp_version, $inventory ); $core_new = ''; $updates = function_exists( 'get_core_updates' ) ? get_core_updates() : array(); if ( is_array( $updates ) && isset( $updates[0]->response ) && 'upgrade' === $updates[0]->response ) { $core_new = (string) $updates[0]->current; } foreach ( $core_vulns as $v ) { $findings[] = self::vuln_finding( 'core', 'wordpress', 'WordPress', $wp_version, true, $v, $core_new ); } if ( 'insecure' === $core_state && empty( $core_vulns ) ) { $findings[] = array( 'id' => 'core-insecure-' . $wp_version, 'severity' => 'critical', 'category' => 'vulnerability', 'type' => 'core', 'slug' => 'wordpress', 'name' => 'WordPress', 'installed' => $wp_version, /* translators: %s: WordPress version number. */ 'title' => sprintf( __( 'WordPress %s is marked insecure by WordPress.org', 'sunsetly' ), $wp_version ), 'detail' => __( 'This version has publicly known security fixes you are missing.', 'sunsetly' ), /* translators: %s: WordPress version to update to. */ 'fix' => $core_new ? sprintf( __( 'Update WordPress to %s now.', 'sunsetly' ), $core_new ) : __( 'Update WordPress now.', 'sunsetly' ), 'link' => admin_url( 'update-core.php' ), 'security_update' => true, ); } elseif ( 'outdated' === $core_state && $core_new ) { $findings[] = self::update_finding( 'core', 'wordpress', 'WordPress', $wp_version, $core_new, admin_url( 'update-core.php' ) ); } // ---- Plugins + themes ---------------------------------------------------------- $plugin_updates = get_site_transient( 'update_plugins' ); $theme_updates = get_site_transient( 'update_themes' ); $months = (int) Sunsetly_Settings::get( 'abandoned_months' ); $wporg_budget = (int) apply_filters( 'sunsetly_wporg_lookups_per_scan', 80 ); $readiness = array( 'current' => $php['branch'], 'target' => ( $php['recommended'] && version_compare( $php['recommended'], $php['branch'], '>' ) ) ? $php['recommended'] : '', 'checked' => 0, 'ready' => 0, 'items' => array(), ); $target_released = ''; if ( $readiness['target'] ) { $tc = Sunsetly_Lifecycle::match_cycle( Sunsetly_Lifecycle::cycles( 'php' ), $readiness['target'] ); $target_released = $tc ? $tc['released'] : ''; } foreach ( $inventory as $item ) { $new_version = ''; if ( 'plugin' === $item['type'] && isset( $plugin_updates->response[ $item['file'] ]->new_version ) ) { $new_version = (string) $plugin_updates->response[ $item['file'] ]->new_version; } elseif ( 'theme' === $item['type'] && isset( $theme_updates->response[ $item['slug'] ]['new_version'] ) ) { $new_version = (string) $theme_updates->response[ $item['slug'] ]['new_version']; } $vulns = Sunsetly_Vulnerabilities::for_component( $item['type'], $item['slug'], $item['version'], $inventory ); $is_sec = false; foreach ( $vulns as $v ) { $f = self::vuln_finding( $item['type'], $item['slug'], $item['name'], $item['version'], $item['active'], $v, $new_version ); $is_sec = $is_sec || ! empty( $f['security_update'] ); $findings[] = $f; } if ( $new_version && ! $vulns && Sunsetly_Settings::get( 'check_routine_updates' ) ) { $url = 'plugin' === $item['type'] ? admin_url( 'plugins.php?plugin_status=upgrade' ) : admin_url( 'themes.php' ); $findings[] = self::update_finding( $item['type'], $item['slug'], $item['name'], $item['version'], $new_version, $url ); } // WordPress.org health: closed / abandoned / PHP readiness (plugins only). if ( 'plugin' !== $item['type'] || $wporg_budget <= 0 ) { continue; } --$wporg_budget; $info = Sunsetly_WPOrg::plugin_info( $item['slug'] ); if ( null === $info ) { if ( $readiness['target'] && $item['active'] ) { $readiness['items'][] = array( 'name' => $item['name'], 'state' => 'unknown', 'reason' => __( 'Premium or custom plugin — test on a staging copy before switching PHP.', 'sunsetly' ), ); } continue; } if ( ! empty( $info['closed'] ) ) { $findings[] = array( 'id' => 'closed-' . $item['slug'], 'severity' => 'high', 'category' => 'abandoned', 'type' => 'plugin', 'slug' => $item['slug'], 'name' => $item['name'], 'installed' => $item['version'], /* translators: %s: plugin name. */ 'title' => sprintf( __( '%s was closed on WordPress.org', 'sunsetly' ), $item['name'] ), 'detail' => trim( __( 'Plugins are often closed because of an unresolved security issue. It will receive no further updates.', 'sunsetly' ) . ' ' . $info['reason_text'], " \t\n\r\0\x0B" ), 'fix' => __( 'Replace it with a maintained alternative and delete it.', 'sunsetly' ), 'link' => 'https://wordpress.org/plugins/' . rawurlencode( $item['slug'] ) . '/', ); continue; } $last = $info['last_updated'] ? strtotime( preg_replace( '/\s*GMT$/', '', $info['last_updated'] ) ) : 0; if ( $last && ( time() - $last ) > $months * MONTH_IN_SECONDS ) { $age = (int) floor( ( time() - $last ) / MONTH_IN_SECONDS ); $findings[] = array( 'id' => 'abandoned-' . $item['slug'], 'severity' => 'medium', 'category' => 'abandoned', 'type' => 'plugin', 'slug' => $item['slug'], 'name' => $item['name'], 'installed' => $item['version'], /* translators: 1: plugin, 2: months */ 'title' => sprintf( __( '%1$s has not been updated in %2$d months', 'sunsetly' ), $item['name'], $age ), 'detail' => __( 'Abandoned plugins stop receiving security fixes and are a favourite target for attackers.', 'sunsetly' ), 'fix' => __( 'Plan a replacement with an actively maintained plugin.', 'sunsetly' ), 'link' => 'https://wordpress.org/plugins/' . rawurlencode( $item['slug'] ) . '/', ); } if ( $info['requires_php'] && version_compare( PHP_VERSION, $info['requires_php'], '<' ) ) { $findings[] = array( 'id' => 'needs-php-' . $item['slug'] . '-' . $info['requires_php'], 'severity' => 'medium', 'category' => 'readiness', 'type' => 'plugin', 'slug' => $item['slug'], 'name' => $item['name'], 'installed' => $item['version'], /* translators: 1: plugin, 2: php version */ 'title' => sprintf( __( 'Latest %1$s requires PHP %2$s — you are stuck on an old version', 'sunsetly' ), $item['name'], $info['requires_php'] ), 'detail' => __( 'You cannot receive future updates (including security fixes) for this plugin until PHP is upgraded.', 'sunsetly' ), 'fix' => __( 'Upgrade PHP with your host.', 'sunsetly' ), 'link' => '', ); } if ( $readiness['target'] && $item['active'] ) { ++$readiness['checked']; $state = 'ready'; $reason = __( 'Actively maintained since the target PHP version was released.', 'sunsetly' ); if ( $target_released && $last && $last < strtotime( $target_released ) ) { $state = 'risk'; /* translators: %s: php version */ $reason = sprintf( __( 'Not updated since PHP %s was released — compatibility unverified.', 'sunsetly' ), $readiness['target'] ); } if ( 'ready' === $state ) { ++$readiness['ready']; } $readiness['items'][] = array( 'name' => $item['name'], 'state' => $state, 'reason' => $reason, ); } } $readiness['percent'] = $readiness['checked'] ? (int) round( 100 * $readiness['ready'] / $readiness['checked'] ) : null; // ---- Assemble -------------------------------------------------------------------- $findings = Sunsetly_Util::sort_findings( apply_filters( 'sunsetly_findings', $findings, $inventory ) ); $score = Sunsetly_Util::score( $findings ); $counts = array_fill_keys( Sunsetly_Util::SEVERITIES, 0 ); foreach ( $findings as $f ) { if ( isset( $counts[ $f['severity'] ] ) ) { ++$counts[ $f['severity'] ]; } } $report = array( 'generated' => time(), 'trigger' => isset( $args['trigger'] ) ? $args['trigger'] : 'manual', 'score' => $score['score'], 'grade' => $score['grade'], 'counts' => $counts, 'env' => array( 'site' => home_url(), 'php' => PHP_VERSION, 'wp' => $wp_version, 'db' => $db['label'] . ' ' . $db['version'], 'server' => isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : '', 'plugins' => count( array_filter( $inventory, function ( $i ) { return 'plugin' === $i['type']; } ) ), 'themes' => count( array_filter( $inventory, function ( $i ) { return 'theme' === $i['type']; } ) ), ), 'lifecycle' => array( 'php' => $php, 'db' => $dbs, ), 'core_state' => $core_state, 'findings' => $findings, 'readiness' => $readiness, 'calendar' => self::calendar( $php, $dbs ), 'source' => Sunsetly_Vulnerabilities::source(), 'errors' => array_values( array_unique( Sunsetly_HTTP::$errors ) ), ); return apply_filters( 'sunsetly_report', $report ); } /** * Installed plugins + themes. * * @return array[] */ public static function inventory() { $items = array(); $active = (array) get_option( 'active_plugins', array() ); if ( is_multisite() ) { $active = array_merge( $active, array_keys( (array) get_site_option( 'active_sitewide_plugins', array() ) ) ); } foreach ( get_plugins() as $file => $data ) { $slug = false !== strpos( $file, '/' ) ? dirname( $file ) : basename( $file, '.php' ); $items[] = array( 'type' => 'plugin', 'file' => $file, 'slug' => $slug, 'name' => $data['Name'], 'version' => $data['Version'], 'active' => in_array( $file, $active, true ), ); } $current = get_stylesheet(); $parent = get_template(); foreach ( wp_get_themes() as $slug => $theme ) { $items[] = array( 'type' => 'theme', 'file' => $slug, 'slug' => $slug, 'name' => $theme->get( 'Name' ), 'version' => $theme->get( 'Version' ), 'active' => ( $slug === $current || $slug === $parent ), ); } return apply_filters( 'sunsetly_inventory', $items ); } /** * Findings for a lifecycle status block. * * @param array $s Status. * @param array $cves Known CVEs for the exact version. * @return array[] */ public static function lifecycle_findings( $s, $cves ) { $out = array(); $type = 'php' === $s['product'] ? 'php' : 'db'; $base = array( 'category' => 'lifecycle', 'type' => $type, 'slug' => $s['product'], 'name' => $s['label'], 'installed' => $s['installed'], 'link' => 'php' === $type ? 'https://www.php.net/supported-versions.php' : 'https://endoflife.date/' . $s['product'], ); /* translators: 1: product name (PHP, MySQL or MariaDB), 2: recommended version. */ $to = $s['recommended'] ? sprintf( __( 'Ask your host to move to %1$s %2$s.', 'sunsetly' ), $s['label'], $s['recommended'] ) : __( 'Ask your host to upgrade.', 'sunsetly' ); if ( 'eol' === $s['state'] ) { $out[] = $base + array( 'id' => 'eol-' . $s['product'] . '-' . $s['branch'], 'severity' => 'php' === $type ? 'critical' : 'high', /* translators: 1: product, 2: branch, 3: date */ 'title' => sprintf( __( '%1$s %2$s is end-of-life since %3$s', 'sunsetly' ), $s['label'], $s['branch'], $s['security_until'] ), /* translators: %s: product name (PHP, MySQL or MariaDB). */ 'detail' => sprintf( __( 'It no longer receives security fixes. Every new %s vulnerability discovered stays open on this site forever.', 'sunsetly' ), $s['label'] ), 'fix' => $to, ); } elseif ( 'security' === $s['state'] && null !== $s['days_security'] ) { $d = (int) $s['days_security']; $sev = $d <= 30 ? 'high' : ( $d <= 120 ? 'medium' : 'low' ); $out[] = $base + array( 'id' => 'eolsoon-' . $s['product'] . '-' . $s['branch'], 'severity' => $sev, /* translators: 1: product, 2: branch, 3: days, 4: date */ 'title' => sprintf( _n( '%1$s %2$s reaches end-of-life in %3$d day (%4$s)', '%1$s %2$s reaches end-of-life in %3$d days (%4$s)', $d, 'sunsetly' ), $s['label'], $s['branch'], $d, $s['security_until'] ), 'detail' => __( 'It already receives security fixes only — no bug fixes. After the date above it receives nothing.', 'sunsetly' ), 'fix' => $to, 'days' => $d, ); } if ( $s['patch_behind'] && 'eol' !== $s['state'] ) { $sev = 'low'; foreach ( $cves as $c ) { if ( Sunsetly_Util::severity_rank( $c['severity'] ) < Sunsetly_Util::severity_rank( $sev ) ) { $sev = $c['severity']; } } $cve_ids = array_filter( wp_list_pluck( $cves, 'cve' ) ); $out[] = $base + array( 'id' => 'patch-' . $s['product'] . '-' . $s['installed'], 'severity' => $sev, /* translators: 1: product, 2: installed, 3: latest */ 'title' => sprintf( __( '%1$s %2$s is missing security patches (latest is %3$s)', 'sunsetly' ), $s['label'], $s['installed'], $s['latest_patch'] ), 'detail' => $cves /* translators: 1: count, 2: list */ ? sprintf( _n( '%1$d known vulnerability affects your exact version: %2$s', '%1$d known vulnerabilities affect your exact version: %2$s', count( $cves ), 'sunsetly' ), count( $cves ), implode( ', ', array_slice( $cve_ids, 0, 6 ) ) ) : __( 'Patch releases contain bug and security fixes. Hosts can usually apply them with no code changes.', 'sunsetly' ), /* translators: 1: product, 2: version */ 'fix' => sprintf( __( 'Ask your host to apply %1$s %2$s (same branch — no compatibility risk).', 'sunsetly' ), $s['label'], $s['latest_patch'] ), ); } return $out; } /** * Vulnerability finding. * * @param string $type Type. * @param string $slug Slug. * @param string $name Name. * @param string $installed Installed version. * @param bool $active Active? * @param array $v Vulnerability. * @param string $new_version Available update. * @return array */ public static function vuln_finding( $type, $slug, $name, $installed, $active, $v, $new_version ) { $fixes = $new_version && $v['patched_in'] && version_compare( $new_version, $v['patched_in'], '>=' ); if ( $fixes ) { /* translators: %s: version */ $fix = sprintf( __( 'Security fix available — update to %s now.', 'sunsetly' ), $new_version ); } elseif ( $v['patched_in'] ) { /* translators: %s: version */ $fix = sprintf( __( 'Fixed in %s. Update as soon as it is offered (premium plugins: download from the vendor).', 'sunsetly' ), $v['patched_in'] ); } else { $fix = __( 'No patch exists yet. Deactivate and remove it, or replace it, until the vendor releases a fix.', 'sunsetly' ); } if ( ! $active && 'core' !== $type ) { $fix .= ' ' . __( 'It is inactive — but its files can still be attacked. Delete it if unused.', 'sunsetly' ); } return array( 'id' => 'vuln-' . $type . '-' . $slug . '-' . md5( $v['id'] ), 'severity' => $v['severity'], 'category' => 'vulnerability', 'type' => $type, 'slug' => $slug, 'name' => $name, 'installed' => $installed, 'title' => ( 0 === stripos( $v['title'], $name ) ? '' : $name . ': ' ) . $v['title'], /* translators: %s: vulnerability data source name. */ 'detail' => trim( ( $v['cve'] ? $v['cve'] . ' · ' : '' ) . ( null !== $v['score'] ? 'CVSS ' . $v['score'] . ' · ' : '' ) . sprintf( __( 'Source: %s', 'sunsetly' ), $v['source'] ), " \t\n\r\0\x0B" ), 'fix' => $fix, 'link' => $v['link'], 'cve' => $v['cve'], 'patched_in' => $v['patched_in'], 'security_update' => (bool) $fixes, 'active' => (bool) $active, ); } /** * Routine (non-security) update finding. * * @param string $type Type. * @param string $slug Slug. * @param string $name Name. * @param string $from From. * @param string $to To. * @param string $url URL. * @return array */ public static function update_finding( $type, $slug, $name, $from, $to, $url ) { return array( 'id' => 'update-' . $type . '-' . $slug . '-' . $to, 'severity' => 'info', 'category' => 'update', 'type' => $type, 'slug' => $slug, 'name' => $name, 'installed' => $from, /* translators: 1: name, 2: from, 3: to */ 'title' => sprintf( __( '%1$s %2$s → %3$s (routine update)', 'sunsetly' ), $name, $from, $to ), 'detail' => __( 'No known security issue in your version. Safe to schedule in your normal maintenance window.', 'sunsetly' ), 'fix' => __( 'Back up, then update during your next maintenance window.', 'sunsetly' ), 'link' => $url, 'security_update' => false, ); } /** * Upcoming maintenance calendar (next 18 months). * * @param array $php PHP status. * @param array $db DB status. * @return array[] */ public static function calendar( $php, $db ) { $events = array(); foreach ( array( $php, $db ) as $s ) { if ( $s['active_until'] && null !== $s['days_active'] && $s['days_active'] >= 0 ) { $events[] = array( 'date' => $s['active_until'], 'key' => 'active-' . $s['product'] . '-' . $s['branch'], /* translators: 1: product 2: branch */ 'title' => sprintf( __( '%1$s %2$s: active support ends (security fixes only after this)', 'sunsetly' ), $s['label'], $s['branch'] ), 'severity' => 'low', ); } if ( $s['security_until'] && null !== $s['days_security'] && $s['days_security'] >= 0 ) { $events[] = array( 'date' => $s['security_until'], 'key' => 'eol-' . $s['product'] . '-' . $s['branch'], /* translators: 1: product 2: branch */ 'title' => sprintf( __( '%1$s %2$s: END OF LIFE — no more security fixes', 'sunsetly' ), $s['label'], $s['branch'] ), 'severity' => 'high', ); } } $events = apply_filters( 'sunsetly_calendar', $events, $php, $db ); $limit = time() + 548 * DAY_IN_SECONDS; $events = array_values( array_filter( $events, function ( $e ) use ( $limit ) { $ts = strtotime( $e['date'] ); return $ts && $ts <= $limit; } ) ); foreach ( $events as &$e ) { $e['days'] = Sunsetly_Util::days_until( $e['date'] ); } unset( $e ); usort( $events, function ( $a, $b ) { return strcmp( $a['date'], $b['date'] ); } ); return $events; } /** * Keep a 90-point score history for trend charts / client reports. * * @param array $report Report. */ private static function push_history( $report ) { $h = get_option( self::HISTORY, array() ); $h = is_array( $h ) ? $h : array(); $h[] = array( 't' => $report['generated'], 'score' => $report['score'], 'counts' => $report['counts'], ); update_option( self::HISTORY, array_slice( $h, -90 ), false ); } }