/.json * - themes: md5 of every file in downloads.wordpress.org/theme/..zip * * Results are cached in the plugin table for 90 days; "not available" for 7 days. * * @package SafetyBis\Security */ namespace SafetyBis\Security; defined( 'ABSPATH' ) || exit; /** * Reference checksum provider. */ final class Reference { const TTL_FOUND = 7776000; // 90 days. const TTL_MISSING = 604800; // 7 days. /** * Core checksums: path => md5. * * @param string $version WordPress version. * @param string $locale Package locale. * @return array|null Null when WordPress.org has no checksums for this version/locale. * @throws \RuntimeException When WordPress.org cannot be reached. */ public static function core( $version, $locale ) { $key = 'core:' . $version . ':' . $locale; $cached = Refs::get( $key ); if ( null !== $cached ) { return ! empty( $cached['files'] ) ? $cached['files'] : null; } $url = add_query_arg( array( 'version' => $version, 'locale' => $locale, ), 'https://api.wordpress.org/core/checksums/1.0/' ); $body = self::fetch( $url ); $data = json_decode( (string) $body, true ); if ( ! is_array( $data ) || ! array_key_exists( 'checksums', $data ) ) { throw new \RuntimeException( esc_html__( 'WordPress.org returned an unexpected answer for core checksums.', 'safetybis-security' ) ); } $files = is_array( $data['checksums'] ) ? $data['checksums'] : array(); if ( isset( $files[ $version ] ) && is_array( $files[ $version ] ) ) { $files = $files[ $version ]; } Refs::set( $key, array( 'files' => $files ), $files ? self::TTL_FOUND : self::TTL_MISSING ); return $files ? $files : null; } /** * Plugin checksums: path => list of md5. * * @param string $slug Plugin slug. * @param string $version Plugin version. * @return array|null Null when the plugin or version is not in the directory. * @throws \RuntimeException When WordPress.org cannot be reached. */ public static function plugin( $slug, $version ) { if ( ! self::valid( $slug, $version ) ) { return null; } $key = 'plugin:' . $slug . ':' . $version; $cached = Refs::get( $key ); if ( null !== $cached ) { return ! empty( $cached['files'] ) ? $cached['files'] : null; } $url = 'https://downloads.wordpress.org/plugin-checksums/' . rawurlencode( $slug ) . '/' . rawurlencode( $version ) . '.json'; $body = self::fetch( $url, array( 404 ) ); $data = null === $body ? null : json_decode( $body, true ); $files = array(); if ( is_array( $data ) && isset( $data['files'] ) && is_array( $data['files'] ) ) { foreach ( $data['files'] as $path => $sums ) { if ( is_array( $sums ) && isset( $sums['md5'] ) ) { $files[ $path ] = array_values( (array) $sums['md5'] ); } } } Refs::set( $key, array( 'files' => $files ), $files ? self::TTL_FOUND : self::TTL_MISSING ); return $files ? $files : null; } /** * Theme checksums built from the directory zip: path => list of md5. * * @param string $slug Theme slug (folder name). * @param string $version Theme version. * @return array|null Null when the theme or version is not in the directory. * @throws \RuntimeException When the package cannot be downloaded or read. */ public static function theme( $slug, $version ) { if ( ! self::valid( $slug, $version ) ) { return null; } $key = 'theme2:' . $slug . ':' . $version; $cached = Refs::get( $key ); if ( null !== $cached ) { return ! empty( $cached['files'] ) ? $cached['files'] : null; } require_once ABSPATH . 'wp-admin/includes/file.php'; $url = 'https://downloads.wordpress.org/theme/' . rawurlencode( $slug ) . '.' . rawurlencode( $version ) . '.zip'; $tmp = download_url( $url, 60 ); if ( is_wp_error( $tmp ) ) { $code = $tmp->get_error_data( 'http_404' ); if ( 'http_404' === $tmp->get_error_code() || ( is_array( $code ) && isset( $code['code'] ) && 404 === (int) $code['code'] ) ) { Refs::set( $key, array( 'files' => array() ), self::TTL_MISSING ); return null; } /* translators: %s: error message. */ throw new \RuntimeException( esc_html( sprintf( __( 'Could not download the theme package from WordPress.org: %s', 'safetybis-security' ), $tmp->get_error_message() ) ) ); } try { $files = Zip::hashes( $tmp, $slug ); } finally { wp_delete_file( $tmp ); } Refs::set( $key, array( 'files' => $files ), $files ? self::TTL_FOUND : self::TTL_MISSING ); return $files ? $files : null; } /** * Add the copies shipped inside the WordPress package (Akismet, Twenty* themes) as * accepted alternatives: a bundled theme can differ from the directory zip of the * same version (for example readme.txt or minified CSS). * * @param array|null $ref path => [md5] from the directory, or null. * @param string $kind plugins|themes. * @param string $slug Folder name. * @return array|null */ public static function with_bundled( $ref, $kind, $slug ) { list( $version, $locale ) = Scan\CoreModule::installed(); try { $core = self::core( $version, $locale ); } catch ( \RuntimeException $e ) { return $ref; } if ( ! is_array( $core ) ) { return $ref; } $prefix = 'wp-content/' . $kind . '/' . $slug . '/'; $bundled = array(); foreach ( $core as $path => $md5 ) { if ( 0 === strpos( $path, $prefix ) ) { $bundled[ substr( $path, strlen( $prefix ) ) ] = (string) $md5; } } if ( ! $bundled ) { return $ref; } if ( null === $ref ) { $ref = array(); foreach ( $bundled as $path => $md5 ) { $ref[ $path ] = array( $md5 ); } return $ref; } foreach ( $bundled as $path => $md5 ) { if ( isset( $ref[ $path ] ) && ! in_array( $md5, $ref[ $path ], true ) ) { $ref[ $path ][] = $md5; } } return $ref; } /** * Directory information about a plugin or theme (cached 24 hours). * * @param string $type plugin|theme. * @param string $slug Slug. * @return array{found: bool, closed: bool, closed_date: string, reason: string, last_updated: string, version: string} * @throws \RuntimeException When WordPress.org cannot be reached. */ public static function info( $type, $slug ) { $key = 'info:' . $type . ':' . $slug; $cached = Refs::get( $key ); if ( null !== $cached ) { return $cached; } $base = 'plugin' === $type ? 'https://api.wordpress.org/plugins/info/1.2/' : 'https://api.wordpress.org/themes/info/1.2/'; $url = add_query_arg( array( 'action' => $type . '_information', 'request' => array( 'slug' => $slug, 'fields' => array( 'sections' => false, 'versions' => false, 'reviews' => false, 'banners' => false, 'icons' => false, 'screenshots' => false, 'contributors' => false, 'description' => false, 'tags' => false, ), ), ), $base ); $body = self::fetch( $url, array( 404 ) ); $data = null === $body ? null : json_decode( $body, true ); $info = array( 'found' => false, 'closed' => false, 'closed_date' => '', 'reason' => '', 'last_updated' => '', 'version' => '', ); if ( is_array( $data ) ) { if ( ! empty( $data['closed'] ) || ( isset( $data['error'] ) && 'closed' === $data['error'] ) ) { $info['found'] = true; $info['closed'] = true; $info['closed_date'] = isset( $data['closed_date'] ) ? (string) $data['closed_date'] : ''; $info['reason'] = isset( $data['reason_text'] ) && is_string( $data['reason_text'] ) ? $data['reason_text'] : ''; } elseif ( ! isset( $data['error'] ) && isset( $data['version'] ) ) { $info['found'] = true; $info['last_updated'] = isset( $data['last_updated'] ) ? (string) $data['last_updated'] : ''; $info['version'] = (string) $data['version']; } } Refs::set( $key, $info, DAY_IN_SECONDS ); return $info; } /** * GET a WordPress.org URL. * * @param string $url URL. * @param int[] $not_found HTTP codes that mean "not available" (returns null). * @return string|null Body. * @throws \RuntimeException On network errors and unexpected codes. */ private static function fetch( $url, array $not_found = array() ) { $response = wp_remote_get( $url, array( 'timeout' => 20 ) ); if ( is_wp_error( $response ) ) { /* translators: %s: error message. */ throw new \RuntimeException( esc_html( sprintf( __( 'Could not connect to WordPress.org: %s', 'safetybis-security' ), $response->get_error_message() ) ) ); } $code = (int) wp_remote_retrieve_response_code( $response ); if ( in_array( $code, $not_found, true ) ) { return null; } if ( 200 !== $code ) { /* translators: %d: HTTP status code. */ throw new \RuntimeException( esc_html( sprintf( __( 'WordPress.org answered with HTTP %d.', 'safetybis-security' ), $code ) ) ); } return (string) wp_remote_retrieve_body( $response ); } /** * Slugs and versions go into URLs: allow only safe characters. * * @param string $slug Slug. * @param string $version Version. * @return bool */ private static function valid( $slug, $version ) { return (bool) preg_match( '/^[a-z0-9][a-z0-9._-]{0,190}$/i', $slug ) && (bool) preg_match( '/^[a-z0-9][a-z0-9._+-]{0,60}$/i', $version ); } }