This commit is contained in:
Javier Casares 2026-08-18 16:32:08 +00:00
commit 515af45d09

View file

@ -31,11 +31,14 @@ if ( ! class_exists( 'Robotstxt_Telemetry' ) ) {
add_action( 'init', array( 'Robotstxt_Telemetry_DB', 'maybe_upgrade' ) );
add_action( 'init', array( $this, 'schedule_cleanup' ) );
add_action( 'robotstxt_telemetry_cleanup', array( 'Robotstxt_Telemetry_DB', 'cleanup' ) );
add_filter( 'http_request_args', array( $this, 'hash_useragent_url' ), 9, 2 );
add_filter( 'http_request_args', array( $this, 'filter_useragent' ), 9, 2 );
$wordpress_api = new Robotstxt_Telemetry_WordPress_Api();
$wordpress_api->register();
$endpoints = new Robotstxt_Telemetry_Endpoints();
$endpoints->register();
$logger = new Robotstxt_Telemetry_Logger();
$logger->register();
@ -49,13 +52,14 @@ if ( ! class_exists( 'Robotstxt_Telemetry' ) ) {
}
/**
* Replace the site URL in the outbound User-Agent with a fixed hash.
* Filter the outbound User-Agent.
*
* The default outbound User-Agent is "WordPress/<version>; <site URL>",
* and the WordPress.org update checks send an explicit one with the
* same shape. When the setting is enabled, the URL part is replaced
* with a deterministic SHA-256 hash so the site is no longer
* identifiable while staying consistent across requests.
* same shape. Depending on the settings, the site URL is sent as-is,
* replaced with a deterministic SHA-256 hash, or removed, and the
* WordPress version can be reduced to its major version ("7.2.n")
* or nulled ("0.0.0").
*
* Runs at priority 9 so the telemetry logger records the value that
* is actually sent on the wire.
@ -64,30 +68,72 @@ if ( ! class_exists( 'Robotstxt_Telemetry' ) ) {
* @param string $url Request URL.
* @return array
*/
public function hash_useragent_url( $args, $url ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter -- Second argument required by the http_request_args filter signature.
if ( ! (bool) Robotstxt_Telemetry_Network::get_setting( 'robotstxt_telemetry_hash_useragent_url', true ) ) {
public function filter_useragent( $args, $url ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter -- Second argument required by the http_request_args filter signature.
$url_mode = Robotstxt_Telemetry_Network::get_useragent_url_mode();
$version_mode = Robotstxt_Telemetry_Network::get_wp_version_mode();
if ( 'url' === $url_mode && 'actual' === $version_mode ) {
return $args;
}
$site_url = get_bloginfo( 'url' );
if ( '' === $site_url ) {
return $args;
if ( isset( $args['user-agent'] ) && is_string( $args['user-agent'] ) ) {
$args['user-agent'] = $this->mask_user_agent( $args['user-agent'], $url_mode, $version_mode, $site_url );
}
$hash = substr( hash( 'sha256', $site_url ), 0, 16 );
if ( isset( $args['user-agent'] ) && is_string( $args['user-agent'] ) && false !== strpos( $args['user-agent'], $site_url ) ) {
$args['user-agent'] = str_replace( $site_url, $hash, $args['user-agent'] );
}
if ( isset( $args['headers']['User-Agent'] ) && is_string( $args['headers']['User-Agent'] ) && false !== strpos( $args['headers']['User-Agent'], $site_url ) ) {
$args['headers']['User-Agent'] = str_replace( $site_url, $hash, $args['headers']['User-Agent'] );
foreach ( array( 'User-Agent', 'user-agent' ) as $header ) {
if ( isset( $args['headers'][ $header ] ) && is_string( $args['headers'][ $header ] ) ) {
$args['headers'][ $header ] = $this->mask_user_agent( $args['headers'][ $header ], $url_mode, $version_mode, $site_url );
}
}
return $args;
}
/**
* Apply the URL and version masking to a User-Agent string.
*
* @param string $user_agent User-Agent value.
* @param string $url_mode 'url', 'hash', or 'none'.
* @param string $version_mode 'actual', 'major', or 'nulled'.
* @param string $site_url Site URL.
* @return string
*/
private function mask_user_agent( $user_agent, $url_mode, $version_mode, $site_url ) {
// Core sends the site URL with (update checks) and without (HTTP
// API) a trailing slash; the slashed form must be replaced first
// so its slash does not leak, then the bare form.
$bare = untrailingslashit( $site_url );
$hash = substr( hash( 'sha256', $bare ), 0, 16 );
$url_forms = array( $bare . '/', $bare );
$url_forms = array_unique( $url_forms );
foreach ( $url_forms as $url_form ) {
if ( false === strpos( $user_agent, $url_form ) ) {
continue;
}
if ( 'hash' === $url_mode ) {
$user_agent = str_replace( $url_form, $hash, $user_agent );
} elseif ( 'none' === $url_mode ) {
$user_agent = str_replace( '; ' . $url_form, '', $user_agent );
$user_agent = str_replace( $url_form, '', $user_agent );
}
}
$user_agent = trim( $user_agent );
if ( 'actual' !== $version_mode && preg_match( '#^WordPress/(\d+[^\s;]*)#', $user_agent, $matches ) ) {
$user_agent = preg_replace(
'#^WordPress/[^\s;]+#',
'WordPress/' . Robotstxt_Telemetry_Network::mask_wp_version( $matches[1], $version_mode ),
$user_agent
);
}
return $user_agent;
}
/**
* Schedule the log cleanup event if it is not scheduled yet.
*