is_safe( self::OPT_CORE ) ) { return $query; } $allowed = array( 'version', 'php', 'locale', 'mysql', 'channel' ); $query = array_intersect_key( $query, array_flip( $allowed ) ); if ( isset( $query['mysql'] ) && is_string( $query['mysql'] ) ) { // Reduce the raw server version to "x.y.z-Variant", dropping // distribution and logging suffixes (e.g. "11.8.8-MariaDB-ubu2404-log"). $query['mysql'] = preg_replace( '/^([^-]+-[^-]+).*$/', '$1', $query['mysql'] ); } return $query; } /** * Filter update check request arguments. * * @param array $args HTTP request arguments. * @param string $url Request URL. * @return array */ public function filter_request_args( $args, $url ) { $parsed = wp_parse_url( $url ); if ( ! isset( $parsed['host'] ) || 'api.wordpress.org' !== strtolower( $parsed['host'] ) ) { return $args; } $path = isset( $parsed['path'] ) ? $parsed['path'] : ''; if ( 0 === stripos( $path, '/core/version-check' ) ) { return $this->filter_core_request( $args ); } if ( 0 === stripos( $path, '/themes/update-check' ) ) { return $this->filter_themes_request( $args ); } if ( 0 === stripos( $path, '/plugins/update-check' ) ) { return $this->filter_plugins_request( $args ); } return $args; } /** * Strip site-identifying data from the core version check in safe mode. * * Removes the wp_install/wp_blog headers and reduces the translations * payload to its project dates. * * @param array $args HTTP request arguments. * @return array */ private function filter_core_request( $args ) { if ( ! $this->is_safe( self::OPT_CORE ) ) { return $args; } unset( $args['headers']['wp_install'], $args['headers']['wp_blog'] ); if ( isset( $args['body']['translations'] ) && is_string( $args['body']['translations'] ) ) { $args['body']['translations'] = $this->filter_translations( $args['body']['translations'] ); } return $args; } /** * Limit the themes update check payload in safe mode. * * @param array $args HTTP request arguments. * @return array */ private function filter_themes_request( $args ) { if ( ! $this->is_safe( self::OPT_THEMES ) ) { return $args; } if ( ! isset( $args['body']['themes'] ) || ! is_string( $args['body']['themes'] ) ) { return $args; } $data = json_decode( $args['body']['themes'], true ); if ( ! is_array( $data ) || empty( $data['themes'] ) || ! is_array( $data['themes'] ) ) { return $args; } $themes = array(); foreach ( $data['themes'] as $stylesheet => $fields ) { if ( is_array( $fields ) ) { $themes[ $stylesheet ] = $this->hash_update_uri( $this->keep_fields( $fields, array( 'name', 'version', 'updateuri', 'template', 'stylesheet' ) ) ); } } if ( empty( $themes ) ) { return $args; } $args['body'] = array( 'themes' => wp_json_encode( array( 'themes' => $themes ) ), 'translations' => $this->filter_translations( $this->body_field( $args, 'translations', '' ) ), 'locale' => $this->body_field( $args, 'locale', '[]' ), ); return $args; } /** * Limit the plugins update check payload in safe mode. * * @param array $args HTTP request arguments. * @return array */ private function filter_plugins_request( $args ) { if ( ! $this->is_safe( self::OPT_PLUGINS ) ) { return $args; } if ( ! isset( $args['body']['plugins'] ) || ! is_string( $args['body']['plugins'] ) ) { return $args; } $data = json_decode( $args['body']['plugins'], true ); if ( ! is_array( $data ) || empty( $data['plugins'] ) || ! is_array( $data['plugins'] ) ) { return $args; } $plugins = array(); foreach ( $data['plugins'] as $basename => $fields ) { if ( is_array( $fields ) ) { $plugins[ $basename ] = $this->hash_update_uri( $this->keep_fields( $fields, array( 'updateuri', 'version', 'requireswp', 'requiresphp', 'requiresplugins' ) ) ); } } if ( empty( $plugins ) ) { return $args; } $args['body'] = array( 'plugins' => wp_json_encode( array( 'plugins' => $plugins ) ), 'translations' => $this->filter_translations( $this->body_field( $args, 'translations', '' ) ), 'locale' => $this->body_field( $args, 'locale', '[]' ), 'all' => $this->body_field( $args, 'all', 'true' ), ); return $args; } /** * Read a string field from the request body arguments. * * @param array $args HTTP request arguments. * @param string $key Body field name. * @param string $fallback_value Default value when missing. * @return string */ private function body_field( $args, $key, $fallback_value ) { if ( isset( $args['body'][ $key ] ) && is_string( $args['body'][ $key ] ) ) { return $args['body'][ $key ]; } return $fallback_value; } /** * Reduce translations payloads to empty entries. * * Only the project and locale identifiers are kept (the API needs * them to know which translations to check); all the translation * metadata (dates, generators, versions) is removed. * * @param string $json Encoded translations payload. * @return string */ private function filter_translations( $json ) { $data = json_decode( $json, true ); if ( ! is_array( $data ) ) { return $json; } $filtered = array(); foreach ( $data as $slug => $locales ) { if ( ! is_array( $locales ) ) { continue; } foreach ( $locales as $locale => $fields ) { if ( is_array( $fields ) ) { $filtered[ $slug ][ $locale ] = new stdClass(); } } } if ( empty( $filtered ) ) { return $json; } return wp_json_encode( $filtered ); } /** * Keep only the allowed fields of a payload entry. * * Matching is case-insensitive; original keys are preserved. * * @param array $fields Fields of the entry. * @param array $allowed Lowercase allowed field names. * @return array */ private function keep_fields( $fields, $allowed ) { $kept = array(); foreach ( $fields as $key => $value ) { if ( in_array( strtolower( (string) $key ), $allowed, true ) ) { $kept[ $key ] = $value; } } return $kept; } /** * Hash Update URI values hosted outside wordpress.org. * * Update URIs pointing at wordpress.org are kept as-is (they are * public slugs). Any other host is replaced with a deterministic * SHA-256 truncation of the URI, so the plugin or theme source is * no longer identifiable while staying consistent across requests. * * @param array $fields Fields of a plugin or theme entry. * @return array */ private function hash_update_uri( $fields ) { foreach ( $fields as $key => $value ) { if ( 'updateuri' !== strtolower( (string) $key ) || ! is_string( $value ) || '' === $value ) { continue; } $host = wp_parse_url( $value, PHP_URL_HOST ); if ( ! is_string( $host ) || false === strpos( strtolower( $host ), 'wordpress.org' ) ) { $fields[ $key ] = 'https://' . substr( hash( 'sha256', $value ), 0, 16 ); } } return $fields; } /** * Whether a check is configured to run in safe mode. * * @param string $option Option name. * @return bool */ private function is_safe( $option ) { return 'original' !== Robotstxt_Telemetry_Network::get_setting( $option, 'safe' ); } } }