Skip to content

Commit

Permalink
Add Stats to XML-RPC Errors (#13335)
Browse files Browse the repository at this point in the history
* Add `xmlrpc_errors` to Jetpack (non-compact) Options

[not verified]

* Send XML-RPC errors via heartbeat.

Track unique error codes in `xmlrpc_errors` option.
  • Loading branch information
mdawaffe authored and jeherve committed Aug 27, 2019
1 parent d6821bc commit 4e1a49a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions class.jetpack-heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ public static function generate_stats_array( $prefix = '' ) {
$return["{$prefix}plugins"] = implode( ',', Jetpack::get_active_plugins() );
$return["{$prefix}manage-enabled"] = true;

$xmlrpc_errors = Jetpack_Options::get_option( 'xmlrpc_errors', array() );
if ( $xmlrpc_errors ) {
$return["{$prefix}xmlrpc-errors"] = implode( ',', array_keys( $xmlrpc_errors ) );
Jetpack_Options::delete_option( 'xmlrpc_errors' );
}

// Missing the connection owner?
$connection_manager = new Manager();
$return["{$prefix}missing-owner"] = $connection_manager->is_missing_connection_owner();
Expand Down
31 changes: 31 additions & 0 deletions class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ private function __construct() {
};
} );

add_action( 'jetpack_verify_signature_error', array( $this, 'track_xmlrpc_error' ) );

$this->connection_manager = new Connection_Manager();
$this->connection_manager->init();

Expand Down Expand Up @@ -4306,6 +4308,35 @@ function admin_notices() {
<?php endif;
}

/**
* We can't always respond to a signed XML-RPC request with a
* helpful error message. In some circumstances, doing so could
* leak information.
*
* Instead, track that the error occurred via a Jetpack_Option,
* and send that data back in the heartbeat.
* All this does is increment a number, but it's enough to find
* trends.
*
* @param WP_Error $xmlrpc_error The error produced during
* signature validation.
*/
function track_xmlrpc_error( $xmlrpc_error ) {
$code = is_wp_error( $xmlrpc_error )
? $xmlrpc_error->get_error_code()
: 'should-not-happen';

$xmlrpc_errors = Jetpack_Options::get_option( 'xmlrpc_errors', array() );
if ( isset( $xmlrpc_errors[ $code ] ) && $xmlrpc_errors[ $code ] ) {
// No need to update the option if we already have
// this code stored.
return;
}
$xmlrpc_errors[ $code ] = true;

Jetpack_Options::update_option( 'xmlrpc_errors', $xmlrpc_errors, false );
}

/**
* Record a stat for later output. This will only currently output in the admin_footer.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/options/legacy/class.jetpack-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static function get_option_names( $type = 'compact' ) {
'static_asset_cdn_files', // (array) An nested array of files that we can swap out for cdn versions.
'mapbox_api_key', // (string) Mapbox API Key, for use with Map block.
'mailchimp', // (string) Mailchimp keyring data, for mailchimp block.
'xmlrpc_errors', // (array) Keys are XML-RPC signature error codes. Values are truthy.
);

case 'private':
Expand Down

0 comments on commit 4e1a49a

Please sign in to comment.