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'] : ''; // The translations endpoints need the real locale to work. if ( $this->mask_locale_enabled() && 0 !== stripos( $path, '/translations' ) ) { $args = $this->mask_locale_in_body( $args ); } 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' ) ) { $args = $this->exclude_hidden_plugins( $args ); 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; } /** * Remove the hidden plugins from the update check payload. * * Applies in both Safe and Original modes: the checked plugins are * never reported to WordPress.org (neither their plugin data, nor * the active list, nor their translation projects), which also * means they stop receiving WordPress.org update notifications. * * @param array $args HTTP request arguments. * @return array */ private function exclude_hidden_plugins( $args ) { $hidden = Robotstxt_Telemetry_Network::get_hidden_plugins(); if ( empty( $hidden ) || empty( $args['body']['plugins'] ) || ! is_string( $args['body']['plugins'] ) ) { return $args; } $data = json_decode( $args['body']['plugins'], true ); if ( ! is_array( $data ) ) { return $args; } $hidden_slugs = array(); foreach ( $hidden as $basename ) { $slug = dirname( $basename ); $hidden_slugs[ $slug ] = true; unset( $data['plugins'][ $basename ], $data['active'][ $basename ] ); } if ( isset( $args['body']['translations'] ) && is_string( $args['body']['translations'] ) ) { $translations = json_decode( $args['body']['translations'], true ); if ( is_array( $translations ) ) { foreach ( array_keys( $translations ) as $slug ) { if ( isset( $hidden_slugs[ $slug ] ) ) { unset( $translations[ $slug ] ); } } $args['body']['translations'] = wp_json_encode( $translations ); } } $args['body']['plugins'] = wp_json_encode( $data ); return $args; } /** * Whether the installation language should be masked as en_US. * * @return bool */ private function mask_locale_enabled() { return (bool) Robotstxt_Telemetry_Network::get_setting( 'robotstxt_telemetry_mask_locale', false ); } /** * Replace the locale field of a WordPress.org request body. * * Handles the JSON-encoded locale arrays of the update checks * (for example `["es_ES","en_US"]`) and plain locale strings. * * @param array $args HTTP request arguments. * @return array */ private function mask_locale_in_body( $args ) { if ( ! isset( $args['body']['locale'] ) || ! is_array( $args['body'] ) ) { return $args; } $locale = $args['body']['locale']; if ( is_string( $locale ) ) { $decoded = json_decode( $locale, true ); if ( is_array( $decoded ) ) { $masked = array(); foreach ( $decoded as $single_locale ) { $masked[] = 'en_US'; } $args['body']['locale'] = wp_json_encode( array_values( array_unique( $masked ) ) ); return $args; } $args['body']['locale'] = 'en_US'; } 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 the translation revision date. * * Only the project and locale identifiers plus the PO-Revision-Date * are kept: the API needs the revision date to decide whether a * translation update is available. The rest of the translation * metadata (creation 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 ) ) { $kept = $this->keep_fields( $fields, array( 'po-revision-date' ) ); if ( ! empty( $kept ) ) { $filtered[ $slug ][ $locale ] = $kept; } } } } 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' ); } } }