Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions includes/class-webdecoy-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ public static function uninstall(): void
wp_clear_scheduled_hook('webdecoy_sync_blocked_ips');
wp_clear_scheduled_hook('webdecoy_flush_violations');
wp_clear_scheduled_hook('webdecoy_flush_ai_referrals');
wp_clear_scheduled_hook('webdecoy_checkin');
wp_clear_scheduled_hook('webdecoy_sync_entitlements');
wp_clear_scheduled_hook('webdecoy_sync_actor_feed');
}
Expand Down
87 changes: 87 additions & 0 deletions includes/class-webdecoy-checkin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Hourly check-in with WebDecoy Cloud: "installed, key works, running this
* version". Nothing about visitors or requests.
*
* A correct install on a quiet site sends no detections, so without this it
* looked the same as a broken one in the dashboard, and nobody could tell which
* plugin version a site ran. Sent only when an API key is set, and skipped
* while WebDecoy is asking clients to back off.
*
* @package WebDecoy
*/

if (!defined('ABSPATH')) {
exit;
}

require_once __DIR__ . '/class-webdecoy-http.php';

class WebDecoy_Checkin
{
public const CRON_HOOK = 'webdecoy_checkin';

private const ENDPOINT = 'https://in.webdecoy.com/api/v1/sdk/checkin';

/**
* Schedule the hourly check-in when an API key is set; remove it when the
* key is gone, so a disconnected site stops calling.
*/
public static function register(string $apiKey): void
{
if ($apiKey === '') {
if (function_exists('wp_next_scheduled') && wp_next_scheduled(self::CRON_HOOK)) {
wp_clear_scheduled_hook(self::CRON_HOOK);
}
return;
}
add_action(self::CRON_HOOK, static function () use ($apiKey): void {
self::send($apiKey);
});
if (function_exists('wp_next_scheduled') && !wp_next_scheduled(self::CRON_HOOK)) {
wp_schedule_event(time() + 60, 'hourly', self::CRON_HOOK);
}
}

/**
* What a check-in says. Every field is reduced to the characters the
* endpoint accepts, so a host with an unusual PHP version string still
* checks in instead of being refused.
*
* @return array{source:string,version:string,runtime:string}
*/
public static function payload(string $pluginVersion, string $wpVersion, string $phpVersion): array
{
$version = substr((string) preg_replace('/[^0-9A-Za-z.+-]/', '', $pluginVersion), 0, 32);
$runtime = 'wordpress ' . $wpVersion . '; php ' . $phpVersion;
$runtime = substr((string) preg_replace('/[^0-9A-Za-z .;:+_-]/', '', $runtime), 0, 64);
return [
'source' => 'wordpress_plugin',
'version' => $version !== '' ? $version : 'unknown',
'runtime' => $runtime,
];
}

/** Send one check-in. Failures are ignored: the next hour tries again. */
public static function send(string $apiKey): void
{
if ($apiKey === '' || !function_exists('wp_remote_post')) {
return;
}
if (class_exists('WebDecoy_Detection_Sender') && WebDecoy_Detection_Sender::backing_off()) {
return;
}
$wp = function_exists('get_bloginfo') ? (string) get_bloginfo('version') : '';
$body = self::payload(defined('WEBDECOY_VERSION') ? WEBDECOY_VERSION : '', $wp, PHP_VERSION);
WebDecoy_HTTP::post(self::ENDPOINT, [
// phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout -- WebDecoy_HTTP caps this to 3 seconds on VIP; retains the non-VIP caller budget.
'timeout' => 5,
'blocking' => false,
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $apiKey,
],
'body' => (string) wp_json_encode($body),
]);
}
}
38 changes: 38 additions & 0 deletions tests/CheckinTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* What the hourly check-in says. Scheduling is not tested here: the wp-cron
* stubs are shared by every test file, so one file cannot observe its own calls.
*
* Run: php tests/run.php
*/

if (!defined('ABSPATH')) {
define('ABSPATH', '/tmp/');
}
require_once dirname(__DIR__) . '/includes/class-webdecoy-checkin.php';

$t = ['TestRunner', 'test'];
$same = ['TestRunner', 'assertSame'];
$true = ['TestRunner', 'assertTrue'];

echo "\nCheck-in\n";

$t('says which plugin, WordPress and PHP version it runs', function () use ($same) {
$same(
['source' => 'wordpress_plugin', 'version' => '2.17.5', 'runtime' => 'wordpress 6.7.1; php 8.2.12'],
WebDecoy_Checkin::payload('2.17.5', '6.7.1', '8.2.12')
);
});

$t('reduces an unusual PHP version to what the endpoint accepts', function () use ($same, $true) {
$p = WebDecoy_Checkin::payload('2.17.5', '6.7.1', '8.2.12-1+ubuntu22.04.1+deb.sury.org+1 (cli) <x>');
$true(strlen($p['runtime']) <= 64, 'runtime fits the column');
$same(1, preg_match('/^[0-9A-Za-z .;:+_-]{0,64}$/', $p['runtime']), 'runtime uses only accepted characters');
});

$t('never sends an empty version', function () use ($same) {
$same('unknown', WebDecoy_Checkin::payload('', '6.7.1', '8.2.12')['version']);
});
2 changes: 2 additions & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
wp_clear_scheduled_hook('webdecoy_sync_entitlements');
wp_clear_scheduled_hook('webdecoy_sync_actor_feed');
wp_clear_scheduled_hook('webdecoy_flush_ai_referrals');
wp_clear_scheduled_hook('webdecoy_checkin');
}

// Delete all transients with webdecoy_ prefix
Expand Down Expand Up @@ -132,6 +133,7 @@
wp_clear_scheduled_hook('webdecoy_sync_entitlements');
wp_clear_scheduled_hook('webdecoy_sync_actor_feed');
wp_clear_scheduled_hook('webdecoy_flush_ai_referrals');
wp_clear_scheduled_hook('webdecoy_checkin');

// Delete transients for this site
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Explicit uninstall cleanup of plugin-owned tables/transients; mutation cannot use a cached result.
Expand Down
10 changes: 10 additions & 0 deletions webdecoy.php
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ private function init_hooks(): void
// Traffic page when connected. Wired on
// plugins_loaded because its class is loaded there by load_includes().
add_action('plugins_loaded', [$this, 'register_ai_referrals'], 20);
add_action('plugins_loaded', [$this, 'register_checkin'], 20);

// Load text domain

Expand Down Expand Up @@ -1082,6 +1083,7 @@ public function load_includes(): void
require_once WEBDECOY_PLUGIN_DIR . 'includes/class-webdecoy-detection-sender.php';
require_once WEBDECOY_PLUGIN_DIR . 'includes/class-webdecoy-browser-report.php';
require_once WEBDECOY_PLUGIN_DIR . 'includes/class-webdecoy-ai-referrals.php';
require_once WEBDECOY_PLUGIN_DIR . 'includes/class-webdecoy-checkin.php';
require_once WEBDECOY_PLUGIN_DIR . 'includes/class-webdecoy-crawler-visits.php';
require_once WEBDECOY_PLUGIN_DIR . 'includes/class-webdecoy-detection-summary.php';
require_once WEBDECOY_PLUGIN_DIR . 'includes/class-webdecoy-robots-txt.php';
Expand Down Expand Up @@ -1458,6 +1460,14 @@ public function register_ai_referrals(): void
WebDecoy_AI_Referrals::register((string) ($this->options['api_key'] ?? ''));
}

/**
* Register the hourly check-in with WebDecoy Cloud, when an API key is set.
*/
public function register_checkin(): void
{
WebDecoy_Checkin::register((string) ($this->options['api_key'] ?? ''));
}

/**
* Register the WordPress-native query/REST traps (author enumeration),
* wired to record synthetic tripwire violations.
Expand Down
Loading