! empty( $v['uuid'] ) ? (string) $v['uuid'] : md5( $title . $cve ), 'title' => html_entity_decode( wp_strip_all_tags( $title ), ENT_QUOTES, 'UTF-8' ), 'severity' => Sunsetly_Util::normalise_severity( $sev, $score ), 'score' => $score, 'cve' => $cve, 'link' => $link, 'patched_in' => $patched_in, 'unfixed' => $unfixed, 'source' => 'WPVulnerability', ); } return $out; } /** * Apply a WPVulnerability operator block. * * @param array $op Operator. * @param string $version Version. * @return bool */ public static function wpv_applies( $op, $version ) { $version = Sunsetly_Util::clean_version( $version ); if ( '' === $version ) { return false; } $allowed = array( 'lt', 'le', 'gt', 'ge', 'eq', '<', '<=', '>', '>=', '=', '==' ); $min = isset( $op['min_version'] ) ? (string) $op['min_version'] : ''; $max = isset( $op['max_version'] ) ? (string) $op['max_version'] : ''; $min_op = isset( $op['min_operator'] ) && in_array( $op['min_operator'], $allowed, true ) ? $op['min_operator'] : 'ge'; $max_op = isset( $op['max_operator'] ) && in_array( $op['max_operator'], $allowed, true ) ? $op['max_operator'] : 'le'; if ( '' === $min && '' === $max ) { return false; } if ( '' !== $min && ! version_compare( $version, $min, $min_op ) ) { return false; } if ( '' !== $max && ! version_compare( $version, $max, $max_op ) ) { return false; } return true; } /* --------------------------------------------------------------------- * Wordfence Intelligence v3 * ------------------------------------------------------------------- */ /** * Get (and refresh when due) the slim Wordfence index for installed software. * * @param array $inventory [ ['type'=>'plugin','slug'=>'x'], ... ]. * @return array|null */ public static function wordfence_index( $inventory ) { $meta = get_option( self::WF_META, array() ); $index = get_option( self::WF_INDEX, null ); $want = self::inventory_signature( $inventory ); $fresh = is_array( $index ) && ! empty( $meta['fetched'] ) && ( time() - (int) $meta['fetched'] ) < 12 * HOUR_IN_SECONDS; $same = isset( $meta['signature'] ) && $meta['signature'] === $want; // Feed allows one download per 30 minutes — never go faster than that. $can_fetch = empty( $meta['attempted'] ) || ( time() - (int) $meta['attempted'] ) > 31 * MINUTE_IN_SECONDS; if ( ( $fresh && $same ) || ! $can_fetch ) { return is_array( $index ) ? $index : null; } $meta['attempted'] = time(); update_option( self::WF_META, $meta, false ); $built = self::download_wordfence( $inventory ); if ( is_array( $built ) ) { update_option( self::WF_INDEX, $built, false ); $meta['fetched'] = time(); $meta['signature'] = $want; $meta['error'] = ''; update_option( self::WF_META, $meta, false ); return $built; } return is_array( $index ) ? $index : null; } /** * Hash of installed software list. * * @param array $inventory Inventory. * @return string */ private static function inventory_signature( $inventory ) { $keys = array(); foreach ( $inventory as $i ) { $keys[] = $i['type'] . ':' . $i['slug']; } sort( $keys ); return md5( implode( '|', $keys ) ); } /** * Download the feed to a temp file and reduce it to the installed slugs. * * @param array $inventory Inventory. * @return array|null */ private static function download_wordfence( $inventory ) { if ( ! Sunsetly_Settings::remote_allowed() ) { return null; } if ( ! function_exists( 'wp_tempnam' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $tmp = wp_tempnam( 'sunsetly-wf' ); $response = wp_remote_get( self::WORDFENCE_FEED, array( 'timeout' => 120, 'stream' => true, 'filename' => $tmp, 'user-agent' => Sunsetly_HTTP::user_agent(), 'headers' => array( 'Authorization' => 'Bearer ' . Sunsetly_Settings::get( 'wordfence_api_key' ), 'Accept' => 'application/json', ), ) ); $code = is_wp_error( $response ) ? 0 : (int) wp_remote_retrieve_response_code( $response ); if ( 200 !== $code ) { $msg = is_wp_error( $response ) ? $response->get_error_message() : 'HTTP ' . $code; if ( 401 === $code || 403 === $code ) { $msg = __( 'Wordfence rejected the API key. Check it under Sunsetly → Settings.', 'sunsetly' ); } elseif ( 429 === $code ) { $msg = __( 'Wordfence rate limit reached (1 download per 30 minutes). Will retry automatically.', 'sunsetly' ); } self::record_wf_error( $msg ); wp_delete_file( $tmp ); return null; } $wanted = array( 'core:wordpress' => true ); foreach ( $inventory as $i ) { $wanted[ $i['type'] . ':' . $i['slug'] ] = true; } if ( function_exists( 'wp_raise_memory_limit' ) ) { wp_raise_memory_limit( 'admin' ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $raw = file_get_contents( $tmp ); wp_delete_file( $tmp ); $feed = json_decode( (string) $raw, true ); unset( $raw ); if ( ! is_array( $feed ) ) { self::record_wf_error( __( 'Wordfence feed could not be parsed (possibly too large for this server memory limit). Falling back to WPVulnerability.', 'sunsetly' ) ); return null; } $index = self::reduce_wordfence( $feed, $wanted ); unset( $feed ); return $index; } /** * Reduce a decoded Wordfence feed to the wanted "type:slug" keys. * * @param array $feed Decoded feed (object map or list). * @param array $wanted Wanted keys. * @return array */ public static function reduce_wordfence( $feed, $wanted ) { $index = array(); foreach ( $feed as $record ) { if ( empty( $record['software'] ) || ! is_array( $record['software'] ) ) { continue; } foreach ( $record['software'] as $sw ) { $key = ( $sw['type'] ?? '' ) . ':' . ( $sw['slug'] ?? '' ); if ( isset( $wanted[ $key ] ) ) { $index[ $key ][] = array( 'id' => $record['id'] ?? '', 'title' => $record['title'] ?? '', 'cve' => $record['cve'] ?? '', 'cvss' => $record['cvss'] ?? array(), 'references' => array_slice( (array) ( $record['references'] ?? array() ), 0, 1 ), 'informational' => ! empty( $record['informational'] ), 'software' => array( $sw ), ); } } } return $index; } /** * Match slim Wordfence records against an installed version. * * @param array $records Records. * @param string $type Type. * @param string $slug Slug. * @param string $version Version. * @return array[] */ public static function match_wordfence( $records, $type, $slug, $version ) { $out = array(); foreach ( (array) $records as $r ) { if ( ! empty( $r['informational'] ) ) { continue; } foreach ( (array) $r['software'] as $sw ) { if ( ( $sw['type'] ?? '' ) !== $type || ( $sw['slug'] ?? '' ) !== $slug ) { continue; } $hit = false; foreach ( (array) ( $sw['affected_versions'] ?? array() ) as $range ) { if ( Sunsetly_Util::in_range( $version, $range['from_version'] ?? '*', ! empty( $range['from_inclusive'] ), $range['to_version'] ?? '*', ! empty( $range['to_inclusive'] ) ) ) { $hit = true; break; } } if ( ! $hit ) { continue; } $score = isset( $r['cvss']['score'] ) ? (float) $r['cvss']['score'] : null; $rating = isset( $r['cvss']['rating'] ) ? (string) $r['cvss']['rating'] : ''; $patched = ! empty( $sw['patched'] ); $cve = (string) ( $r['cve'] ?? '' ); $link = ! empty( $r['references'][0] ) ? (string) $r['references'][0] : ( $r['id'] ? 'https://www.wordfence.com/threat-intel/vulnerabilities/id/' . rawurlencode( $r['id'] ) : '' ); $out[] = array( 'id' => (string) $r['id'], 'title' => html_entity_decode( wp_strip_all_tags( (string) $r['title'] ), ENT_QUOTES, 'UTF-8' ), 'severity' => Sunsetly_Util::normalise_severity( $rating, $score ), 'score' => $score, 'cve' => $cve, 'link' => $link, 'patched_in' => $patched ? Sunsetly_Util::lowest_fix( $sw['patched_versions'] ?? array(), $version ) : '', 'unfixed' => ! $patched, 'source' => 'Wordfence Intelligence', ); } } return $out; } /** * Store a Wordfence error for display. * * @param string $msg Message. */ private static function record_wf_error( $msg ) { $meta = get_option( self::WF_META, array() ); $meta['error'] = $msg; update_option( self::WF_META, $meta, false ); Sunsetly_HTTP::$errors[] = $msg; } }