From b1a26ba256f1d4a9618446fad86bbcc1d6a10a08 Mon Sep 17 00:00:00 2001 From: Chris Portscheller Date: Fri, 13 Nov 2026 19:12:16 -0600 Subject: [PATCH 1/2] Count denied WordPress requests separately from detection findings --- admin/partials/dashboard-widget.php | 58 ++++-- admin/partials/detections-page.php | 6 +- admin/partials/statistics-page.php | 47 ++++- docs/plugin-stack-compatibility-2026-11-13.md | 5 + docs/request-counting-validation.json | 41 ++++ docs/request-counting.md | 87 ++++++++ includes/class-webdecoy-activator.php | 40 +++- includes/class-webdecoy-request-counts.php | 189 ++++++++++++++++++ readme.txt | 1 + tests/RequestCountsTest.php | 78 ++++++++ tests/wp-compat/plugin-stack/README.md | 29 +++ .../plugin-stack/check-request-counts.py | 129 ++++++++++++ tests/wp-compat/plugin-stack/client.php | 11 +- tests/wp-compat/plugin-stack/control.php | 2 +- .../wp-compat/plugin-stack/request-counts.php | 44 ++++ tests/wp-compat/plugin-stack/router.php | 2 +- tests/wp-compat/plugin-stack/run.py | 6 +- uninstall.php | 4 + webdecoy.php | 7 + 19 files changed, 752 insertions(+), 34 deletions(-) create mode 100644 docs/request-counting-validation.json create mode 100644 docs/request-counting.md create mode 100644 includes/class-webdecoy-request-counts.php create mode 100644 tests/RequestCountsTest.php create mode 100644 tests/wp-compat/plugin-stack/check-request-counts.py create mode 100644 tests/wp-compat/plugin-stack/request-counts.php diff --git a/admin/partials/dashboard-widget.php b/admin/partials/dashboard-widget.php index b0a41d7..67e1a81 100644 --- a/admin/partials/dashboard-widget.php +++ b/admin/partials/dashboard-widget.php @@ -10,20 +10,12 @@ exit; } -// Get local stats (cached for 5 minutes to avoid DB queries on every admin page) -$stats = get_transient('webdecoy_dashboard_stats'); -if ($stats === false) { - $detector = new WebDecoy_Detector(); - $stats = $detector->get_stats(7); - set_transient('webdecoy_dashboard_stats', $stats, 5 * MINUTE_IN_SECONDS); -} - -$blocked_stats = get_transient('webdecoy_dashboard_blocked_stats'); -if ($blocked_stats === false) { - $blocker = new WebDecoy_Blocker(); - $blocked_stats = $blocker->get_stats(); - set_transient('webdecoy_dashboard_blocked_stats', $blocked_stats, 5 * MINUTE_IN_SECONDS); -} +// Keep the displayed findings and active-IP totals current with request counters. +// These are admin-only reads; no cache invalidation writes are added to bot requests. +$detector = new WebDecoy_Detector(); +$stats = $detector->get_stats(7); +$blocker = new WebDecoy_Blocker(); +$blocked_stats = $blocker->get_stats(); // Known crawlers, AI agents and bot tools seen in the last 24 hours. Counted // locally on every install, connected or not (WebDecoy_Crawler_Visits). @@ -35,6 +27,8 @@ set_transient('webdecoy_dashboard_crawlers', $crawlers, 5 * MINUTE_IN_SECONDS); } +$request_counts_today = WebDecoy_Request_Counts::summary(1); + // Checkout bot signals in the last 24 hours: velocity, card-testing patterns, // bot scores at checkout and the decoy coupon, all recorded as detections // with a woocommerce_ source by WebDecoy_WooCommerce. @@ -132,10 +126,42 @@

+
+

+ +
+ __('Detection rows', 'webdecoy'), 'blocked' => __('Blocked requests', 'webdecoy'), 'withheld' => __('Enforcement withheld', 'webdecoy')] as $metric => $label) : ?> +
+ + +
+ +
+

+ +

+

+ + + +

+ +

+ +
+
- +
@@ -143,7 +169,7 @@
- +
diff --git a/admin/partials/detections-page.php b/admin/partials/detections-page.php index c1fe59b..9cd98bb 100644 --- a/admin/partials/detections-page.php +++ b/admin/partials/detections-page.php @@ -151,11 +151,15 @@

+

+ + +

- +
diff --git a/admin/partials/statistics-page.php b/admin/partials/statistics-page.php index 7b8da51..243cf54 100644 --- a/admin/partials/statistics-page.php +++ b/admin/partials/statistics-page.php @@ -126,6 +126,7 @@ // Known crawlers, AI agents and bot tools over 7 days (counters only). $crawler_summary = class_exists('WebDecoy_Crawler_Visits') ? WebDecoy_Crawler_Visits::summary(7 * 24) : null; +$request_windows = [1 => WebDecoy_Request_Counts::summary(1), 7 => WebDecoy_Request_Counts::summary(7), 30 => WebDecoy_Request_Counts::summary(30)]; $options = get_option('webdecoy_options', []); ?> @@ -136,22 +137,60 @@
- +
- +
- +
- +
+
+

+

+ + + + + + + + + __('Detection rows', 'webdecoy'), + 'blocked' => __('Blocked requests (403)', 'webdecoy'), + 'throttled' => __('Throttled requests (429)', 'webdecoy'), + 'challenged' => __('Challenges served', 'webdecoy'), + 'withheld' => __('Requests with enforcement withheld', 'webdecoy'), + ] as $metric => $label) : ?> + + $counts) : ?> + + + + + +
+

+ +

+ +

+ +
+
diff --git a/docs/plugin-stack-compatibility-2026-11-13.md b/docs/plugin-stack-compatibility-2026-11-13.md index a6aeee3..ede84ac 100644 --- a/docs/plugin-stack-compatibility-2026-11-13.md +++ b/docs/plugin-stack-compatibility-2026-11-13.md @@ -5,6 +5,11 @@ with independent server-side access receipts. No conflict was observed in the tested navigation, login, redirect, AI-counter or WebDecoy blocking flows. No production detection-hook change is supported by this check. +A [request-counter follow-up](request-counting.md) corrects fixture admin-directory +routing and replaces an admin-bar-only assertion with a real `wp-admin` screen +check. All 122 cases passed again with that correction and the request-counter +implementation. The stronger UI checks supersede the initial admin-page evidence. + This follows the [14-case hook-order check](hook-order-compatibility.md). These tests use actual installed plugins, full WordPress bootstrap and a real MariaDB database. They do not use the earlier in-memory service adapters. diff --git a/docs/request-counting-validation.json b/docs/request-counting-validation.json new file mode 100644 index 0000000..8dc624f --- /dev/null +++ b/docs/request-counting-validation.json @@ -0,0 +1,41 @@ +{ + "unit_tests_passed": 235, + "focused_checks_passed": 13, + "focused_http_requests": 58, + "measurements": { + "monitor": { + "request_count": 1, + "withheld_actions": 3 + }, + "repeat_blocks": { + "initial_and_repeat_count": 2, + "concurrent_requests": 40, + "counter_delta": 40, + "detection_delta": 0, + "aggregate_rows": 1 + } + }, + "checks": [ + "new schema and tracking-start metadata", + "setup-test 403 excluded", + "multiple monitor decisions deduplicated per request", + "fresh and repeated IP blocks counted without duplicate findings", + "40 concurrent repeat blocks reconcile with one aggregate bucket", + "challenge responses separate from blocks", + "actual 429 throttles separate from 403 blocks", + "nonce rejection and CLI operations excluded", + "authenticated dashboard and Statistics show separate measured counts", + "failed upgrade preserves protection and retries without backfilling", + "30 UTC calendar-day retention preserves the boundary bucket", + "all HTTP results matched independent server receipts", + "actual WordPress uninstall removes counters and start metadata" + ], + "compatibility_cases": { + "matrix-waf-enabled-results.json": 28, + "matrix-results.json": 66, + "matrix-extended-results.json": 28 + }, + "compatibility_total": 122, + "scope": "Request accounting and admin presentation only; no blocking decision changes.", + "release_status": "Unreleased implementation; release and marketplace publication not performed." +} diff --git a/docs/request-counting.md b/docs/request-counting.md new file mode 100644 index 0000000..b4c8b24 --- /dev/null +++ b/docs/request-counting.md @@ -0,0 +1,87 @@ +# Detection rows and request counts + +Detection rows are saved findings. They are not a count of all requests, nor a +count of requests blocked. A request can create multiple findings. A request +from an already-blocked IP can return 403 before scoring, without a new finding. + +The WordPress dashboard now shows detection rows, blocked requests and withheld +enforcement for today in UTC. Statistics compares the same measures over today, +7 and 30 UTC calendar days, with throttles and challenges shown separately. +Active blocked IPs remains the size of the current block list, not a request +counter. Dashboard finding/IP totals are read fresh to avoid mixing stale +summaries with current request counts; no cache-invalidation writes are added +to front-end requests. + +## Definitions + +| Metric | Meaning | +| --- | --- | +| Detection rows | Findings saved in the existing detection table; unchanged | +| Blocked requests | WebDecoy explicitly denies the HTTP request and its final response is 403; includes repeat IP blocks | +| Throttled requests | WebDecoy explicitly throttles the request and its final response is 429 | +| Challenges served | WebDecoy serves its 403 challenge; not counted as a hard block | +| Enforcement withheld | A request where protection would act but is withheld, including monitor mode; never an actual block | + +One request is counted once under its strongest marked outcome. Multiple +withheld decisions in the same request do not inflate the count. Recording +happens at PHP shutdown after checking the final response, and does not change +which enforcement path runs. Nonce/permission failures, validation notices +returned with 200, decoy 200/404 responses and reserved setup-test traffic are +not counted as request denials. Wordfence, CDN and cache responses that end +before WebDecoy runs are outside its counters. + +A protected form action refused through `WP_Error` or a WooCommerce notice +is not necessarily a denied HTTP request. Existing checkout/form findings +remain available separately; these counters do not relabel all protection +actions as blocked requests. + +## Storage, upgrade and coverage + +Schema 2.11.1 adds `webdecoy_request_counts`. Its only columns are UTC hour, +outcome and count, with a primary key on hour/outcome. Each counted request +uses an atomic `INSERT ... ON DUPLICATE KEY UPDATE hits = hits + 1`, preventing +concurrent lost updates without writing another detection row. There are at +most four outcome buckets per hour. No new visitor IP, URL, User-Agent or +fingerprint is stored. + +The existing activation/admin-upgrade path installs the table. Schema version +advances only when that table exists; failed creation retries on the next +admin request. A stored tracking-start timestamp is shown in the UI. Earlier +blocked-request totals cannot be reconstructed and are never backfilled. +Calendar-day comparisons use the same UTC boundaries for findings/outcomes, +but the request history starts at deployment and cannot cover earlier days. +Counter retention is 30 calendar days, pruned by the existing cleanup hook. +Uninstall removes the new table and start metadata, including fallback and +per-site cleanup paths. No cloud reporting/API contract is changed. + +## Validation + +- 235 dependency-free tests passed, including outcome precedence, final-status + validation, setup-test exclusion, aggregation and UTC/leap-day boundaries. +- 13 focused checks passed across 58 actual HTTP requests, each reconciled with + an independent server receipt in full WordPress/MariaDB. +- One monitor-mode login produced 3 withheld actions and exactly 1 request count. +- 40 concurrent repeat-block requests produced exactly 40 counter increments, + zero new detection rows, and one hourly aggregate row in the observed run. +- Challenges and 429s increment their own counters; setup 403s, nonce failures + and CLI maintenance do not increment the blocked-request metric. +- Actual admin dashboard/Statistics HTML values matched stored measurements. +- A simulated CREATE-permission failure preserved blocking, left tracking + unavailable, and retried successfully after permissions were restored, + without backfilling past requests. Repeated upgrade preserved start metadata. +- Retention preserved the boundary bucket and removed older buckets; actual + WordPress uninstall removed the table and start metadata. +- The full 122-request installed-plugin matrix passed again, including + Wordfence manual enforcement and native prepend modes. Wordfence's own + denials did not increment WebDecoy blocked-request counts. + +The installed-plugin fixture now delegates directories to PHP's real admin +index and checks the `wp-admin` body class. Earlier admin-bar-only assertions +could mistake a logged-in front page for the admin dashboard; the corrected +matrix and focused UI checks above supersede that narrower evidence. + +These are synthetic fixtures, not customer traffic or protection-accuracy data. +Browser JavaScript, downloaded vendor signatures, real CDN/cache/proxy topology +and full multisite behavior remain outside this validation. + +See [the isolated test fixture](../tests/wp-compat/plugin-stack/README.md). diff --git a/includes/class-webdecoy-activator.php b/includes/class-webdecoy-activator.php index bb10b98..3070653 100644 --- a/includes/class-webdecoy-activator.php +++ b/includes/class-webdecoy-activator.php @@ -21,19 +21,21 @@ class WebDecoy_Activator /** * Database version for migrations */ - private const DB_VERSION = '2.11.0'; + private const DB_VERSION = '2.11.1'; /** * Plugin activation */ public static function activate(): void { - self::create_tables(); + $storage_ready = self::create_tables(); self::set_default_options(); self::schedule_cleanup(); - // Store DB version - update_option('webdecoy_db_version', self::DB_VERSION); + // Retry a failed schema install on the next admin request. + if ($storage_ready) { + update_option('webdecoy_db_version', self::DB_VERSION); + } // Flush rewrite rules flush_rewrite_rules(); @@ -57,7 +59,7 @@ public static function deactivate(): void /** * Create database tables */ - private static function create_tables(): void + private static function create_tables(): bool { global $wpdb; @@ -155,6 +157,14 @@ private static function create_tables(): void PRIMARY KEY (bucket, agent_id, verification) ) $charset_collate;"; + // At most four aggregate rows per UTC hour; no IP, URL or User-Agent. + $sql_request_counts = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}webdecoy_request_counts ( + bucket DATETIME NOT NULL, + outcome VARCHAR(16) NOT NULL, + hits BIGINT UNSIGNED NOT NULL DEFAULT 0, + PRIMARY KEY (bucket, outcome) + ) $charset_collate;"; + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta($sql_blocked); @@ -164,6 +174,13 @@ private static function create_tables(): void dbDelta($sql_violation_queue); dbDelta($sql_ai_referrals); dbDelta($sql_crawler_visits); + dbDelta($sql_request_counts); + $table = $wpdb->prefix . 'webdecoy_request_counts'; + $exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($table))) === $table; + if ($exists) { + add_option('webdecoy_request_counts_started_at', time(), '', false); + } + return $exists; } /** @@ -260,9 +277,10 @@ public static function maybe_upgrade(): void { $current_version = get_option('webdecoy_db_version', '0'); - if (version_compare($current_version, self::DB_VERSION, '<')) { - self::create_tables(); - update_option('webdecoy_db_version', self::DB_VERSION); + if (version_compare($current_version, self::DB_VERSION, '<') || !get_option('webdecoy_request_counts_started_at')) { + if (self::create_tables()) { + update_option('webdecoy_db_version', self::DB_VERSION); + } // Clear the dead sync-blocked-ips schedule left by older installs. wp_clear_scheduled_hook('webdecoy_sync_blocked_ips'); } @@ -284,6 +302,7 @@ public static function uninstall(): void $wpdb->prefix . 'webdecoy_violation_queue', $wpdb->prefix . 'webdecoy_ai_referrals', $wpdb->prefix . 'webdecoy_crawler_visits', + $wpdb->prefix . 'webdecoy_request_counts', ]; foreach ($tables as $table) { @@ -294,6 +313,7 @@ public static function uninstall(): void // Delete options delete_option('webdecoy_options'); delete_option('webdecoy_db_version'); + delete_option('webdecoy_request_counts_started_at'); delete_option('webdecoy_entitlements'); delete_option('webdecoy_actor_feed_cursor'); delete_option('webdecoy_critical_moment_last'); @@ -339,6 +359,10 @@ public static function uninstall(): void WebDecoy_Crawler_Visits::prune(); } + if (class_exists('WebDecoy_Request_Counts')) { + WebDecoy_Request_Counts::prune(); + } + // Clean up old checkout attempts (keep 7 days) $wpdb->query($wpdb->prepare( "DELETE FROM {$wpdb->prefix}webdecoy_checkout_attempts WHERE created_at < %s", diff --git a/includes/class-webdecoy-request-counts.php b/includes/class-webdecoy-request-counts.php new file mode 100644 index 0000000..80ebd2f --- /dev/null +++ b/includes/class-webdecoy-request-counts.php @@ -0,0 +1,189 @@ + 1, 'challenged' => 2, 'throttled' => 3, 'blocked' => 4]; + private static string $pending = ''; + private static bool $registered = false; + private static bool $flushed = false; + private static ?int $blog_id = null; + + /** Mark an actual enforcement path; repeated marks still produce one row increment. */ + public static function mark(string $outcome): void + { + if (PHP_SAPI === 'cli' || self::$flushed || (defined('WEBDECOY_DISABLE') && WEBDECOY_DISABLE)) { + return; + } + $ua = isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT'])) : ''; + if (self::is_setup_test($ua)) { + return; + } + $chosen = self::choose(self::$pending, $outcome); + if ($chosen !== self::$pending) { + self::$blog_id = get_current_blog_id(); + } + self::$pending = $chosen; + if (self::$pending !== '' && !self::$registered) { + self::$registered = true; + register_shutdown_function([self::class, 'flush']); + } + } + + /** Stronger enforcement supersedes a withheld/challenge decision in one request. */ + public static function choose(string $previous, string $next): string + { + if (!isset(self::PRIORITIES[$next])) { + return $previous; + } + return (self::PRIORITIES[$next] > (self::PRIORITIES[$previous] ?? 0)) ? $next : $previous; + } + + public static function is_setup_test(string $user_agent): bool + { + return stripos(ltrim($user_agent), 'WebDecoy-Test/') === 0; + } + + /** A marked response must actually finish with the matching HTTP status. */ + public static function matches_response(string $outcome, int $status): bool + { + if ($outcome === 'blocked' || $outcome === 'challenged') { + return $status === 403; + } + if ($outcome === 'throttled') { + return $status === 429; + } + // A withheld decision is not a denial, whatever later code returns. + return $outcome === 'withheld'; + } + + public static function table(): string + { + global $wpdb; + return esc_sql($wpdb->prefix . 'webdecoy_request_counts'); + } + + /** Final response accounting cannot interfere with the protection response. */ + public static function flush(): void + { + if (self::$flushed) { + return; + } + self::$flushed = true; + if (!self::matches_response(self::$pending, (int) http_response_code())) { + return; + } + $switched = false; + try { + // A later switch_to_blog must not attribute this request to another site. + if (self::$blog_id !== null && get_current_blog_id() !== self::$blog_id) { + switch_to_blog(self::$blog_id); + $switched = true; + } + if (!self::ready()) { + return; + } + global $wpdb; + $table = self::table(); + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- trusted table prefix; outcome is a closed enum + $wpdb->query($wpdb->prepare( + "INSERT INTO {$table} (bucket, outcome, hits) VALUES (%s, %s, 1) ON DUPLICATE KEY UPDATE hits = hits + 1", + gmdate('Y-m-d H:00:00'), + self::$pending + )); + } catch (\Throwable $error) { + // Observability must never turn a completed denial into an error page. + unset($error); + } finally { + if ($switched) { + try { + restore_current_blog(); + } catch (\Throwable $error) { + unset($error); + } + } + } + } + + /** UTC calendar days, including today. No rolling/calendar window mismatch. */ + public static function window(int $days, ?int $now = null): array + { + $now = $now ?? time(); + $days = max(1, min(self::RETENTION_DAYS, $days)); + return [ + 'from' => gmdate('Y-m-d 00:00:00', $now - ($days - 1) * 86400), + 'until' => gmdate('Y-m-d 00:00:00', $now + 86400), + ]; + } + + public static function summarize(array $rows): array + { + $totals = ['blocked' => 0, 'throttled' => 0, 'challenged' => 0, 'withheld' => 0]; + foreach ($rows as $row) { + $outcome = (string) ($row['outcome'] ?? ''); + if (isset($totals[$outcome]) && is_numeric($row['hits'] ?? null)) { + $totals[$outcome] += max(0, (int) $row['hits']); + } + } + return $totals; + } + + /** Saved findings and request outcomes use exactly the same date boundaries. */ + public static function summary(int $days): array + { + $window = self::window($days); + $result = array_merge(self::summarize([]), $window, [ + 'available' => false, + 'detections' => 0, + 'started_at' => (int) get_option('webdecoy_request_counts_started_at', 0), + ]); + if (!self::ready()) { + return $result; + } + global $wpdb; + $table = self::table(); + $detections = esc_sql($wpdb->prefix . 'webdecoy_detections'); + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- trusted table prefix + $rows = $wpdb->get_results($wpdb->prepare( + "SELECT outcome, SUM(hits) AS hits FROM {$table} WHERE bucket >= %s AND bucket < %s GROUP BY outcome", + $window['from'], $window['until'] + ), ARRAY_A); + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- trusted table prefix + $count = $wpdb->get_var($wpdb->prepare( + "SELECT COUNT(*) FROM {$detections} WHERE created_at >= %s AND created_at < %s", + $window['from'], $window['until'] + )); + if (!is_array($rows) || $count === null) { + return $result; + } + return array_merge($result, self::summarize($rows), ['available' => true, 'detections' => (int) $count]); + } + + public static function prune(): void + { + if (!self::ready()) { + return; + } + global $wpdb; + $table = self::table(); + $window = self::window(self::RETENTION_DAYS); + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- trusted table prefix + $wpdb->query($wpdb->prepare("DELETE FROM {$table} WHERE bucket < %s", $window['from'])); + } + + private static function ready(): bool + { + return version_compare((string) get_option('webdecoy_db_version', '0'), self::SINCE_DB_VERSION, '>=') + && (int) get_option('webdecoy_request_counts_started_at', 0) > 0; + } +} diff --git a/readme.txt b/readme.txt index 96a2d8a..2f00906 100644 --- a/readme.txt +++ b/readme.txt @@ -125,6 +125,7 @@ Registration spam and comment spam are the same disease: automation pointed at y **Local Dashboard & Analytics** * Detection log with threat scores and MITRE tactic mapping * Statistics page with 30-day trend charts +* Separate request counters: actual blocks, throttles, challenges and withheld enforcement, including repeat IP blocks * Blocked IPs management * Dashboard widget: bots seen in the last 24 hours, checkout bot signals, threat overview * CSV export diff --git a/tests/RequestCountsTest.php b/tests/RequestCountsTest.php new file mode 100644 index 0000000..e59922d --- /dev/null +++ b/tests/RequestCountsTest.php @@ -0,0 +1,78 @@ + 'withheld', 'hits' => 9]]); + $same(9, $totals['withheld']); + $same(0, $totals['blocked']); + $same(0, $totals['throttled']); +}); + +$t('setup user agents are excluded without excluding ordinary bots', function () use ($same) { + $same(true, WebDecoy_Request_Counts::is_setup_test(' WebDecoy-Test/1.0 Playground')); + $same(true, WebDecoy_Request_Counts::is_setup_test('webdecoy-test/1.0')); + $same(false, WebDecoy_Request_Counts::is_setup_test('python-requests/2.32.3')); + $same(false, WebDecoy_Request_Counts::is_setup_test('Mozilla/5.0 WebDecoy-Test/1.0')); +}); + +$t('calendar windows cross month and leap-day boundaries in UTC', function () use ($same) { + $now = gmmktime(23, 59, 59, 3, 1, 2024); + $same(['from' => '2024-03-01 00:00:00', 'until' => '2024-03-02 00:00:00'], WebDecoy_Request_Counts::window(1, $now)); + $same(['from' => '2024-02-24 00:00:00', 'until' => '2024-03-02 00:00:00'], WebDecoy_Request_Counts::window(7, $now)); + $same(['from' => '2024-02-01 00:00:00', 'until' => '2024-03-02 00:00:00'], WebDecoy_Request_Counts::window(30, $now)); + $same(WebDecoy_Request_Counts::window(1, $now), WebDecoy_Request_Counts::window(0, $now)); + $same(WebDecoy_Request_Counts::window(30, $now), WebDecoy_Request_Counts::window(900, $now)); +}); + +$t('stored outcome rows reconcile independently and ignore unknown dimensions', function () use ($same) { + $rows = [ + ['outcome' => 'blocked', 'hits' => '12'], + ['outcome' => 'blocked', 'hits' => 5], + ['outcome' => 'challenged', 'hits' => 3], + ['outcome' => 'throttled', 'hits' => 4], + ['outcome' => 'withheld', 'hits' => 9], + ['outcome' => 'unknown', 'hits' => 1000], + ['outcome' => 'blocked', 'hits' => -5], + ['outcome' => 'blocked', 'hits' => 'bad'], + ]; + $totals = WebDecoy_Request_Counts::summarize($rows); + $same(['blocked' => 17, 'throttled' => 4, 'challenged' => 3, 'withheld' => 9], $totals); + $same(33, array_sum($totals)); +}); diff --git a/tests/wp-compat/plugin-stack/README.md b/tests/wp-compat/plugin-stack/README.md index 1b68419..8fe6db0 100644 --- a/tests/wp-compat/plugin-stack/README.md +++ b/tests/wp-compat/plugin-stack/README.md @@ -74,3 +74,32 @@ The output directory contains only parsed measurements and can be retained. ```sh docker compose -p wd-plugin-stack down ``` + +## Request counters (implementation under test) + +Use a staged plugin build containing `WebDecoy_Request_Counts` (the original +public 2.11.4 package does not contain it). The counter-specific check uses a +second ordinary PHP worker for concurrent SQL increments: + +```sh +docker exec -d "$WP_COMPAT_CONTAINER" sh -c 'exec php -d display_errors=0 -d log_errors=1 -d error_log=/scratch/counter-worker-errors.log -d memory_limit=128M -d max_execution_time=10 -d session.save_path=/scratch -S 127.0.0.3:8080 -t /site /fixture/router.php > /scratch/counter-worker.log 2>&1' +python3 check-request-counts.py --uninstall +``` + +This is deliberately destructive to the **disposable test database**. It drops +only the fixture counter table to simulate an old release, temporarily revokes +CREATE from the dummy database user (restored in `finally`), verifies retry and +continued protection, checks retention, and optionally invokes actual plugin +uninstall last. It checks setup-test exclusion, multiple withheld decisions in +one login request, fresh/repeated blocks, 40 concurrent hits, challenges, 429s, +nonce/CLI exclusions and actual authenticated dashboard/Statistics values. +Fresh markers/cookie jars prevent results from earlier runs satisfying a check. + +The router delegates real directories to PHP, and admin assertions require the +`wp-admin` body class. A front-end admin bar is not proof of an admin screen. + +If the concurrency worker already occupies 127.0.0.3, a native-prepend listener +can use 127.0.0.4 instead; set `WP_COMPAT_PREPEND_IP=127.0.0.4` for `run.py --extended`. +The public-plugin matrix skips counter-specific assertions when the capability +is absent. Tracking starts with the schema update; no historical block counts +are inferred from detections. See [request-count definitions](../../../docs/request-counting.md). diff --git a/tests/wp-compat/plugin-stack/check-request-counts.py b/tests/wp-compat/plugin-stack/check-request-counts.py new file mode 100644 index 0000000..46da9c3 --- /dev/null +++ b/tests/wp-compat/plugin-stack/check-request-counts.py @@ -0,0 +1,129 @@ +import subprocess,json,time,sys,uuid,os +from pathlib import Path +ROOT=Path(os.environ['WP_COMPAT_OUTPUT']).resolve() +ROOT.mkdir(parents=True,exist_ok=True) +CONTAINER=os.environ['WP_COMPAT_CONTAINER'] +# Refuse unlabeled containers, writable roots, or exposed host ports. +probe=subprocess.run(['docker','inspect','--format','{{index .Config.Labels "webdecoy.task"}}|{{.HostConfig.ReadonlyRootfs}}|{{json .HostConfig.PortBindings}}|{{.HostConfig.NetworkMode}}',CONTAINER],capture_output=True,text=True,check=True,timeout=10).stdout.strip().split('|') +if not (probe[0].startswith('wp-stack-compat') and probe[1]=='true' and probe[2] in ('{}','null') and probe[3].startswith('container:')): + raise SystemExit('Use only a labeled, read-only, private-loopback compatibility container.') +db_id=probe[3][len('container:'):] +db_mode=subprocess.run(['docker','inspect','--format','{{.HostConfig.NetworkMode}}',db_id],capture_output=True,text=True,check=True,timeout=10).stdout.strip() +if db_mode!='none':raise SystemExit('The shared database namespace must use network none.') + +from concurrent.futures import ThreadPoolExecutor +RUN_ID=uuid.uuid4().hex[:8] +REQUESTS=[] +CHECKS=[] +MEASUREMENTS={} +BROWSER='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' +def php(file,*args): + result=subprocess.run(['docker','exec',CONTAINER,'php','-d','memory_limit=128M','-d','max_execution_time=15','/fixture/'+file,*args],capture_output=True,text=True,timeout=30) + if result.returncode:raise RuntimeError(result.stderr+' '+result.stdout[-700:]) + return json.loads(result.stdout) +def ctl(*args):return php('control.php',*args) +def action(name,*args):return ctl('eval','request-counts.php',name,*args) +def state(): + s=action('summary');assert s['db_error']=='',s + return s +def configure(**patch):return action('configure',json.dumps(patch)) +def request(name,path='/compat-target/',browser=False,**extra): + marker=RUN_ID+'-'+name + p={'path':path,'marker':marker,'jar':RUN_ID+'-browser' if browser else marker,'headers':['X-Real-IP: 192.0.2.42']} + if browser:p.update(browser=True,ua=BROWSER) + p.update(extra) + result=php('client.php',json.dumps(p));assert not result['critical_error'],result + REQUESTS.append(result) + return result +def check(name):CHECKS.append(name);print('PASS '+name,flush=True) +def reset():action('reset') +def same(expected,actual):assert expected==actual,(expected,actual) + +try: + ctl('plugins','webdecoy/webdecoy.php');ctl('configure','monitor','proxy');reset() + s=state();assert s['today']['available'];same('2.11.1',s['db_version']);assert s['today']['started_at']>0 + same([],s['rows']);same(['bucket','outcome','hits'],s['columns']);check('new schema and tracking-start metadata') + + # Reserved 403 setup tests create a labeled finding, never a request denial. + r=request('setup',ua='WebDecoy-Test/1.0 Compat-'+RUN_ID+'-setup');same(403,r['status']);assert r['test_receipt'] + s=state();same(0,s['today']['blocked']);same([],s['rows']);check('setup-test 403 excluded') + + # Login scoring and a honeypot can both withhold enforcement in one request. + reset();configure(monitor_mode=True,min_score_to_block=40,tripwire_response='block') + request('monitor-form','/wp-login.php',jar=RUN_ID+'-dedupe') + reset();before=state() + r=request('monitor','/wp-login.php',jar=RUN_ID+'-dedupe',post={'log':'compat-admin','pwd':'fixture-admin-password','testcookie':'1','webdecoy_hp_login':'synthetic-fixture'}) + same(302,r['status']);after=state() + same(1,after['today']['withheld']);same(0,after['today']['blocked']);assert after['legacy_withheld_actions']-before['legacy_withheld_actions']>=2 + MEASUREMENTS['monitor']={'request_count':after['today']['withheld'],'withheld_actions':after['legacy_withheld_actions']-before['legacy_withheld_actions']} + same(1,sum(int(row['hits']) for row in after['rows']));check('multiple monitor decisions deduplicated per request') + + reset();configure(monitor_mode=False,min_score_to_block=75) + r=request('first-block','/compat-trap/');same(403,r['status']);same(1,state()['today']['blocked']) + before=state();r=request('repeat-block');same(403,r['status']);after=state();same(2,after['today']['blocked']);same(before['today']['detections'],after['today']['detections']) + check('fresh and repeated IP blocks counted without duplicate findings') + + # Two server processes share the SQL table; no read-modify-write lost updates. + before=state() + def hit(index):return request('concurrent-'+str(index),extended=bool(index%2)) + with ThreadPoolExecutor(max_workers=4) as workers:responses=list(workers.map(hit,range(40))) + assert all(r['status']==403 for r in responses) + after=state();same(40,after['today']['blocked']-before['today']['blocked']);same(before['today']['detections'],after['today']['detections']) + same(42,sum(int(row['hits']) for row in after['rows'] if row['outcome']=='blocked')) + assert 1 <= len([row for row in after['rows'] if row['outcome']=='blocked']) <= 2 # UTC-hour boundary may split the run + MEASUREMENTS['repeat_blocks']={'initial_and_repeat_count':2,'concurrent_requests':40,'counter_delta':after['today']['blocked']-before['today']['blocked'],'detection_delta':after['today']['detections']-before['today']['detections'],'aggregate_rows':len([row for row in after['rows'] if row['outcome']=='blocked'])} + check('40 concurrent repeat blocks reconcile with hourly aggregates') + + reset();configure(tripwire_response='challenge',monitor_mode=False) + r=request('challenge','/compat-trap/');same(403,r['status']);s=state();same(1,s['today']['challenged']);same(0,s['today']['blocked']) + check('challenge responses separate from blocks') + + reset();configure(tripwire_enabled=False,tripwire_response='block',rate_limit_enabled=True,rate_limit_requests=1,rate_limit_window=60,rate_limit_dry_run=False,min_score_to_block=75) + same(200,request('rate-first')['status']);same(429,request('rate-second')['status']);same(429,request('rate-third')['status']) + s=state();same(2,s['today']['throttled']);same(0,s['today']['blocked']);same(0,s['today']['withheld']) + check('actual 429 throttles separate from 403 blocks') + + reset();configure(rate_limit_enabled=False,tripwire_enabled=True,monitor_mode=True) + r=request('ajax-no-nonce','/wp-admin/admin-ajax.php',post={'action':'webdecoy_client_detection'});same(403,r['status']);same([],state()['rows']) + action('cli-mark');same([],state()['rows']);check('nonce rejection and CLI operations excluded') + + # Authenticated screens must show the real counter, not a detection-row proxy. + configure(monitor_mode=False);same(403,request('ui-seed','/compat-trap/')['status']);configure(monitor_mode=True) + same(200,request('login-form','/wp-login.php',True)['status']) + r=request('login-submit','/wp-login.php',True,post={'log':'compat-admin','pwd':'fixture-admin-password','wp-submit':'Log In','redirect_to':'http://127.0.0.1:8080/wp-admin/','testcookie':'1'});same(302,r['status']) + expected=state()['today'];r=request('dashboard','/wp-admin/',True);same(200,r['status']);same(expected['blocked'],r['widget_counts']['blocked']);same(expected['detections'],r['widget_counts']['detections']);assert r['tracking_start_visible'] + r=request('statistics','/wp-admin/admin.php?page=webdecoy-statistics',True);same(200,r['status']) + for metric in ['detections','blocked','throttled','challenged','withheld']:same(expected[metric],r['request_count_cells'][metric]['1']) + check('authenticated dashboard and Statistics show separate measured counts') + + # Simulate an old install. Denials must survive unavailable storage, then retry. + before=state()['today']['detections'];action('pre-upgrade');configure(monitor_mode=False) + root=['docker','exec',db_id,'mariadb','-uroot','-pfixture-root-password','-e'] + try: + subprocess.run(root+["REVOKE CREATE ON wp.* FROM 'wp'@'%';"],check=True,capture_output=True,text=True,timeout=15) + r=request('failed-upgrade-ui','/wp-admin/admin.php?page=webdecoy-statistics',True);same(200,r['status']) + same('2.11.0',state()['db_version']);assert state()['today']['available'] is False + same(403,request('no-storage-block')['status']) + finally: + subprocess.run(root+["GRANT CREATE ON wp.* TO 'wp'@'%';"],check=True,capture_output=True,text=True,timeout=15) + configure(monitor_mode=True) + r=request('retry-upgrade','/wp-admin/admin.php?page=webdecoy-statistics',True);same(200,r['status']);s=state();same('2.11.1',s['db_version']);assert s['today']['available'];same(0,s['today']['blocked']);same(before,s['today']['detections']) + started=s['today']['started_at'];action('upgrade');same(started,state()['today']['started_at']) + check('failed upgrade preserves protection and retries without backfilling') + + reset();retention=action('retention');same(0,retention['old_rows']);same(7,retention['boundary']);same(7,retention['summary']['blocked']) + check('30 UTC calendar-day retention preserves the boundary bucket') + + access=php('read-access.php') + for response in REQUESTS: + receipts=[row for row in access if row['marker']==response['marker']] + same(1,len(receipts));same(response['status'],receipts[0]['status']) + check('all HTTP results matched independent server receipts') + if '--uninstall' in sys.argv: + removed=action('uninstall');same(False,removed['counter_table_present']);same(False,removed['start_metadata_present']) + check('actual WordPress uninstall removes counters and start metadata') +except Exception as error: + (ROOT/'request-counts-results.json').write_text(json.dumps({'complete':False,'checks':CHECKS,'measurements':MEASUREMENTS,'requests':REQUESTS,'error':str(error)},indent=2)+'\n') + raise +(ROOT/'request-counts-results.json').write_text(json.dumps({'complete':True,'checks':CHECKS,'measurements':MEASUREMENTS,'requests':REQUESTS,'final':state()},indent=2)+'\n') +print(f'{len(CHECKS)} checks passed; {len(REQUESTS)} HTTP requests reconciled',flush=True) diff --git a/tests/wp-compat/plugin-stack/client.php b/tests/wp-compat/plugin-stack/client.php index 2f42043..59a45e6 100644 --- a/tests/wp-compat/plugin-stack/client.php +++ b/tests/wp-compat/plugin-stack/client.php @@ -6,8 +6,15 @@ $ua=$p['ua']??('python-requests/2.32.3 Compat-'.$p['marker']); $c=curl_init('http://127.0.0.1:'.($p['port']??8080).$path);$responseHeaders=[]; curl_setopt_array($c,[CURLOPT_RETURNTRANSFER=>true,CURLOPT_FOLLOWLOCATION=>false,CURLOPT_CONNECTTIMEOUT=>2,CURLOPT_TIMEOUT=>15,CURLOPT_USERAGENT=>$ua,CURLOPT_HTTPHEADER=>$headers,CURLOPT_ENCODING=>'',CURLOPT_COOKIEFILE=>'/scratch/'.($p['jar']??'default').'.cookies',CURLOPT_COOKIEJAR=>'/scratch/'.($p['jar']??'default').'.cookies',CURLOPT_HEADERFUNCTION=>static function($c,$h)use(&$responseHeaders){$kv=explode(':',$h,2);if(count($kv)===2 && in_array(strtolower(trim($kv[0])),['location','x-redirect-by','content-type','retry-after'],true))$responseHeaders[strtolower(trim($kv[0]))]=trim($kv[1]);return strlen($h);}]); -if(!empty($p['extended']))curl_setopt($c,CURLOPT_CONNECT_TO,['127.0.0.1:8080:127.0.0.3:8080']); +if(!empty($p['extended'])){ $backend=$p['backend_ip']??'127.0.0.3';if(!in_array($backend,['127.0.0.3','127.0.0.4'],true))throw new Exception('Expected private fixture listener');curl_setopt($c,CURLOPT_CONNECT_TO,['127.0.0.1:8080:'.$backend.':8080']); } if(isset($p['post'])){curl_setopt($c,CURLOPT_POST,true);curl_setopt($c,CURLOPT_POSTFIELDS,http_build_query($p['post']));} $body=curl_exec($c);if($body===false)throw new Exception(curl_error($c)); -$summary=['marker'=>$p['marker'],'path'=>$path,'status'=>curl_getinfo($c,CURLINFO_RESPONSE_CODE),'headers'=>$responseHeaders,'body_bytes'=>strlen($body),'login_form'=>strpos($body,'name="log"')!==false,'target_content'=>strpos($body,'COMPAT_TARGET_CONTENT')!==false,'admin_screen'=>strpos($body,'wp-admin-bar-my-account')!==false,'critical_error'=>strpos($body,'There has been a critical error')!==false,'test_receipt'=>strpos($body,'"webdecoy_test":true')!==false,'wordfence_block'=>strpos($body,'Your access to this site has been limited')!==false || strpos($body,'Generated by Wordfence')!==false]; +$summary=['marker'=>$p['marker'],'path'=>$path,'status'=>curl_getinfo($c,CURLINFO_RESPONSE_CODE),'headers'=>$responseHeaders,'body_bytes'=>strlen($body),'login_form'=>strpos($body,'name="log"')!==false,'target_content'=>strpos($body,'COMPAT_TARGET_CONTENT')!==false,'admin_screen'=>(bool)preg_match('/]*class="[^"]*\bwp-admin\b/', $body),'critical_error'=>strpos($body,'There has been a critical error')!==false,'test_receipt'=>strpos($body,'"webdecoy_test":true')!==false,'wordfence_block'=>strpos($body,'Your access to this site has been limited')!==false || strpos($body,'Generated by Wordfence')!==false]; +$summary['request_count_cells']=[]; +preg_match_all('/data-webdecoy-outcome="([a-z]+)" data-window="([0-9]+)">([^<]*)<\/td>/', $body, $cells, PREG_SET_ORDER); +foreach($cells as $cell)$summary['request_count_cells'][$cell[1]][$cell[2]]=$cell[3]==='—'?null:(int)str_replace(',','',$cell[3]); +$summary['widget_counts']=[]; +preg_match_all('/data-webdecoy-count="([a-z]+)">([0-9,]+)<\/span>/', $body, $widgets, PREG_SET_ORDER); +foreach($widgets as $widget)$summary['widget_counts'][$widget[1]]=(int)str_replace(',','',$widget[2]); +$summary['tracking_start_visible']=strpos($body,'Request tracking started')!==false; file_put_contents('/scratch/client-results.jsonl',json_encode($summary)."\n",FILE_APPEND|LOCK_EX);echo json_encode($summary); diff --git a/tests/wp-compat/plugin-stack/control.php b/tests/wp-compat/plugin-stack/control.php index 4220c46..721b4b3 100644 --- a/tests/wp-compat/plugin-stack/control.php +++ b/tests/wp-compat/plugin-stack/control.php @@ -34,7 +34,7 @@ echo json_encode(['rows'=>$rows,'db_error'=>$wpdb->last_error]);exit; } if($cmd==='state'){ - global $wp_version;echo json_encode(['wp'=>$wp_version,'php'=>PHP_VERSION,'plugins'=>get_option('active_plugins',[]),'webdecoy'=>get_option('webdecoy_options',[]),'wordfence_api_key_configured'=>class_exists('wfConfig') ? (bool)wfConfig::get('apiKey','') : null]);exit; + global $wp_version;echo json_encode(['request_counts_supported'=>class_exists('WebDecoy_Request_Counts'),'wp'=>$wp_version,'php'=>PHP_VERSION,'plugins'=>get_option('active_plugins',[]),'webdecoy'=>get_option('webdecoy_options',[]),'wordfence_api_key_configured'=>class_exists('wfConfig') ? (bool)wfConfig::get('apiKey','') : null]);exit; } if($cmd==='eval'){ // Trusted fixture file supplied by the test runner, never an HTTP parameter. diff --git a/tests/wp-compat/plugin-stack/request-counts.php b/tests/wp-compat/plugin-stack/request-counts.php new file mode 100644 index 0000000..1a00097 --- /dev/null +++ b/tests/wp-compat/plugin-stack/request-counts.php @@ -0,0 +1,44 @@ +array_keys($patch)]);return; +} +if($action==='reset'){ + $wpdb->query("DELETE FROM {$wpdb->prefix}webdecoy_request_counts"); + $wpdb->query("DELETE FROM {$wpdb->prefix}webdecoy_blocked_ips WHERE ip_address IN ('127.0.0.1','192.0.2.42')"); + $wpdb->query("DELETE FROM {$wpdb->prefix}webdecoy_rate_limits"); + echo json_encode(['reset'=>true]);return; +} +if($action==='pre-upgrade'){ + $wpdb->query("DROP TABLE {$wpdb->prefix}webdecoy_request_counts"); + delete_option('webdecoy_request_counts_started_at');update_option('webdecoy_db_version','2.11.0'); + echo json_encode(['db_version'=>get_option('webdecoy_db_version')]);return; +} +if($action==='upgrade'){ + WebDecoy_Activator::maybe_upgrade(); + echo json_encode(['db_version'=>get_option('webdecoy_db_version'),'started_at'=>(int)get_option('webdecoy_request_counts_started_at',0)]);return; +} +if($action==='cli-mark'){ + WebDecoy_Request_Counts::mark('blocked');WebDecoy_Request_Counts::flush();echo json_encode(['marked_cli'=>true]);return; +} +if($action==='uninstall'){ + define('WP_UNINSTALL_PLUGIN','webdecoy/webdecoy.php'); + require '/site/wp-content/plugins/webdecoy/uninstall.php'; + $name=$wpdb->prefix.'webdecoy_request_counts'; + echo json_encode(['counter_table_present'=>$wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s',$wpdb->esc_like($name)))===$name,'start_metadata_present'=>get_option('webdecoy_request_counts_started_at',false)!==false]);return; +} +if($action==='retention'){ + $window=WebDecoy_Request_Counts::window(30);$table=WebDecoy_Request_Counts::table(); + $old=gmdate('Y-m-d H:00:00',strtotime($window['from'].' UTC')-3600); + $wpdb->query($wpdb->prepare("INSERT INTO {$table} (bucket,outcome,hits) VALUES (%s,'blocked',99),(%s,'blocked',7) ON DUPLICATE KEY UPDATE hits=VALUES(hits)",$old,$window['from'])); + WebDecoy_Request_Counts::prune(); + echo json_encode(['old_rows'=>(int)$wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$table} WHERE bucket < %s",$window['from'])),'boundary'=>(int)$wpdb->get_var($wpdb->prepare("SELECT hits FROM {$table} WHERE bucket=%s AND outcome='blocked'",$window['from'])),'summary'=>WebDecoy_Request_Counts::summary(30)]);return; +} +if($action!=='summary')throw new Exception('Unknown counter fixture action'); +$table=WebDecoy_Request_Counts::table(); +$summary=WebDecoy_Request_Counts::summary(1); +echo json_encode(['today'=>$summary,'week'=>WebDecoy_Request_Counts::summary(7),'month'=>WebDecoy_Request_Counts::summary(30),'columns'=>$summary['available']?$wpdb->get_col("SHOW COLUMNS FROM {$table}"):[],'rows'=>$summary['available']?$wpdb->get_results("SELECT bucket,outcome,hits FROM {$table} ORDER BY bucket,outcome",ARRAY_A):[],'legacy_withheld_actions'=>(int)(get_option('webdecoy_suppressed_actions',[])[gmdate('Y-m-d')]['count']??0),'db_version'=>get_option('webdecoy_db_version'),'db_error'=>$wpdb->last_error]); diff --git a/tests/wp-compat/plugin-stack/router.php b/tests/wp-compat/plugin-stack/router.php index 930cbf5..6ee4405 100644 --- a/tests/wp-compat/plugin-stack/router.php +++ b/tests/wp-compat/plugin-stack/router.php @@ -1,5 +1,5 @@ prefix . 'webdecoy_rate_limits', $wpdb->prefix . 'webdecoy_checkout_attempts', $wpdb->prefix . 'webdecoy_ai_referrals', + $wpdb->prefix . 'webdecoy_request_counts', $wpdb->prefix . 'webdecoy_crawler_visits', ]; @@ -44,6 +45,7 @@ // Delete plugin options delete_option('webdecoy_options'); delete_option('webdecoy_db_version'); + delete_option('webdecoy_request_counts_started_at'); delete_option('webdecoy_api_status'); delete_option('webdecoy_api_last_check'); delete_option('webdecoy_api_last_error'); @@ -87,6 +89,7 @@ $wpdb->prefix . 'webdecoy_rate_limits', $wpdb->prefix . 'webdecoy_checkout_attempts', $wpdb->prefix . 'webdecoy_ai_referrals', + $wpdb->prefix . 'webdecoy_request_counts', ]; foreach ($tables as $table) { @@ -97,6 +100,7 @@ // Delete options for this site delete_option('webdecoy_options'); delete_option('webdecoy_db_version'); + delete_option('webdecoy_request_counts_started_at'); delete_option('webdecoy_api_status'); delete_option('webdecoy_api_last_check'); delete_option('webdecoy_api_last_error'); diff --git a/webdecoy.php b/webdecoy.php index 5b5d119..dea7e5e 100644 --- a/webdecoy.php +++ b/webdecoy.php @@ -1070,6 +1070,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-ai-referrals.php'; require_once WEBDECOY_PLUGIN_DIR . 'includes/class-webdecoy-crawler-visits.php'; + require_once WEBDECOY_PLUGIN_DIR . 'includes/class-webdecoy-request-counts.php'; require_once WEBDECOY_PLUGIN_DIR . 'includes/class-webdecoy-honeytoken.php'; require_once WEBDECOY_PLUGIN_DIR . 'includes/class-webdecoy-ip-enrichment.php'; require_once WEBDECOY_PLUGIN_DIR . 'includes/class-webdecoy-decoy-response.php'; @@ -1600,6 +1601,7 @@ private function enforce_block(string $ip, string $reason, ?int $duration = null */ private function record_suppressed_action(string $suppression, string $reason): void { + WebDecoy_Request_Counts::mark('withheld'); $stats = get_option('webdecoy_suppressed_actions', []); if (!is_array($stats)) { $stats = []; @@ -1670,6 +1672,7 @@ private function handle_rule_decision(\WebDecoy\Rules\RuleEngineResult $result, $retryAfter = isset($meta['retryAfter']) ? max(1, intval($meta['retryAfter'])) : 60; nocache_headers(); status_header(429); + WebDecoy_Request_Counts::mark('throttled'); header('Retry-After: ' . $retryAfter); if (isset($meta['max'])) { header('X-RateLimit-Limit: ' . (int) $meta['max']); @@ -1907,6 +1910,7 @@ private function is_challenge_verified(string $ip): bool */ private function serve_challenge_page(string $ip): void { + WebDecoy_Request_Counts::mark('challenged'); nocache_headers(); status_header(403); @@ -1931,6 +1935,8 @@ private function serve_challenge_page(string $ip): void */ private function block_request(string $message): void { + // Includes repeat hits on an already-blocked IP; no detection rescore needed. + WebDecoy_Request_Counts::mark('blocked'); // Send no-cache headers to prevent caching of block pages // This ensures unblocking takes effect immediately nocache_headers(); @@ -2133,6 +2139,7 @@ public function filter_comment(array $commentdata): array // Reachable at default sensitivity and serves a literal 403 — same gate. $suppression = $this->suppression_reason(); if ($suppression === '') { + WebDecoy_Request_Counts::mark('blocked'); wp_die( esc_html__('Your comment has been blocked due to suspicious activity.', 'webdecoy'), esc_html__('Comment Blocked', 'webdecoy'), From 59001467047a25310ddeb814fce75d71b2609295 Mon Sep 17 00:00:00 2001 From: Chris Portscheller Date: Fri, 13 Nov 2026 19:21:07 -0600 Subject: [PATCH 2/2] Isolate counter storage failures from protection responses --- admin/partials/dashboard-widget.php | 2 +- admin/partials/detections-page.php | 2 +- admin/partials/statistics-page.php | 4 ++-- docs/request-counting-validation.json | 7 ++++--- docs/request-counting.md | 7 +++++-- includes/class-webdecoy-request-counts.php | 11 ++++++++++- tests/wp-compat/plugin-stack/README.md | 3 ++- tests/wp-compat/plugin-stack/check-request-counts.py | 11 +++++++++++ tests/wp-compat/plugin-stack/client.php | 1 + 9 files changed, 37 insertions(+), 11 deletions(-) diff --git a/admin/partials/dashboard-widget.php b/admin/partials/dashboard-widget.php index 67e1a81..0b4c11f 100644 --- a/admin/partials/dashboard-widget.php +++ b/admin/partials/dashboard-widget.php @@ -161,7 +161,7 @@
- +
diff --git a/admin/partials/detections-page.php b/admin/partials/detections-page.php index 9cd98bb..0d17cce 100644 --- a/admin/partials/detections-page.php +++ b/admin/partials/detections-page.php @@ -159,7 +159,7 @@
- +
diff --git a/admin/partials/statistics-page.php b/admin/partials/statistics-page.php index 243cf54..fb9a660 100644 --- a/admin/partials/statistics-page.php +++ b/admin/partials/statistics-page.php @@ -97,7 +97,7 @@ gmdate('Y-m-d H:i:s', strtotime('-7 days')) )); $total_today = $wpdb->get_var($wpdb->prepare( - "SELECT COUNT(*) FROM {$detections_table} WHERE created_at > %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input + "SELECT COUNT(*) FROM {$detections_table} WHERE created_at >= %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix, not user input gmdate('Y-m-d 00:00:00') )); // expires_at is stored in UTC, so it must be compared against a UTC value. MySQL's @@ -141,7 +141,7 @@
- +
diff --git a/docs/request-counting-validation.json b/docs/request-counting-validation.json index 8dc624f..f7c5868 100644 --- a/docs/request-counting-validation.json +++ b/docs/request-counting-validation.json @@ -1,7 +1,7 @@ { "unit_tests_passed": 235, - "focused_checks_passed": 13, - "focused_http_requests": 58, + "focused_checks_passed": 14, + "focused_http_requests": 60, "measurements": { "monitor": { "request_count": 1, @@ -20,9 +20,10 @@ "setup-test 403 excluded", "multiple monitor decisions deduplicated per request", "fresh and repeated IP blocks counted without duplicate findings", - "40 concurrent repeat blocks reconcile with one aggregate bucket", + "40 concurrent repeat blocks reconcile with hourly aggregates", "challenge responses separate from blocks", "actual 429 throttles separate from 403 blocks", + "counter write failure preserves denial and recovery resumes counting", "nonce rejection and CLI operations excluded", "authenticated dashboard and Statistics show separate measured counts", "failed upgrade preserves protection and retries without backfilling", diff --git a/docs/request-counting.md b/docs/request-counting.md index b4c8b24..1207f56 100644 --- a/docs/request-counting.md +++ b/docs/request-counting.md @@ -40,7 +40,8 @@ actions as blocked requests. Schema 2.11.1 adds `webdecoy_request_counts`. Its only columns are UTC hour, outcome and count, with a primary key on hour/outcome. Each counted request uses an atomic `INSERT ... ON DUPLICATE KEY UPDATE hits = hits + 1`, preventing -concurrent lost updates without writing another detection row. There are at +concurrent lost updates without writing another detection row. Counter-write errors are suppressed and +do not alter the protection response; a failed write is not invented or backfilled. There are at most four outcome buckets per hour. No new visitor IP, URL, User-Agent or fingerprint is stored. @@ -58,7 +59,7 @@ per-site cleanup paths. No cloud reporting/API contract is changed. - 235 dependency-free tests passed, including outcome precedence, final-status validation, setup-test exclusion, aggregation and UTC/leap-day boundaries. -- 13 focused checks passed across 58 actual HTTP requests, each reconciled with +- 14 focused checks passed across 60 actual HTTP requests, each reconciled with an independent server receipt in full WordPress/MariaDB. - One monitor-mode login produced 3 withheld actions and exactly 1 request count. - 40 concurrent repeat-block requests produced exactly 40 counter increments, @@ -69,6 +70,8 @@ per-site cleanup paths. No cloud reporting/API contract is changed. - A simulated CREATE-permission failure preserved blocking, left tracking unavailable, and retried successfully after permissions were restored, without backfilling past requests. Repeated upgrade preserved start metadata. +- A database trigger rejecting counter-only writes preserved the 403 response + without leaking database errors; removing the trigger resumed counting. - Retention preserved the boundary bucket and removed older buckets; actual WordPress uninstall removed the table and start metadata. - The full 122-request installed-plugin matrix passed again, including diff --git a/includes/class-webdecoy-request-counts.php b/includes/class-webdecoy-request-counts.php index 80ebd2f..76c5531 100644 --- a/includes/class-webdecoy-request-counts.php +++ b/includes/class-webdecoy-request-counts.php @@ -84,7 +84,10 @@ public static function flush(): void return; } $switched = false; + $previous_errors = null; try { + global $wpdb; + $previous_errors = $wpdb->suppress_errors(true); // A later switch_to_blog must not attribute this request to another site. if (self::$blog_id !== null && get_current_blog_id() !== self::$blog_id) { switch_to_blog(self::$blog_id); @@ -93,7 +96,6 @@ public static function flush(): void if (!self::ready()) { return; } - global $wpdb; $table = self::table(); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- trusted table prefix; outcome is a closed enum $wpdb->query($wpdb->prepare( @@ -112,6 +114,13 @@ public static function flush(): void unset($error); } } + if ($previous_errors !== null) { + try { + $wpdb->suppress_errors($previous_errors); + } catch (\Throwable $error) { + unset($error); + } + } } } diff --git a/tests/wp-compat/plugin-stack/README.md b/tests/wp-compat/plugin-stack/README.md index 8fe6db0..4ba763f 100644 --- a/tests/wp-compat/plugin-stack/README.md +++ b/tests/wp-compat/plugin-stack/README.md @@ -88,7 +88,8 @@ python3 check-request-counts.py --uninstall This is deliberately destructive to the **disposable test database**. It drops only the fixture counter table to simulate an old release, temporarily revokes -CREATE from the dummy database user (restored in `finally`), verifies retry and +CREATE from the dummy database user (restored in `finally`), rejects a counter +insert with a temporary database trigger (also removed in `finally`), verifies retry and continued protection, checks retention, and optionally invokes actual plugin uninstall last. It checks setup-test exclusion, multiple withheld decisions in one login request, fresh/repeated blocks, 40 concurrent hits, challenges, 429s, diff --git a/tests/wp-compat/plugin-stack/check-request-counts.py b/tests/wp-compat/plugin-stack/check-request-counts.py index 46da9c3..8d1561f 100644 --- a/tests/wp-compat/plugin-stack/check-request-counts.py +++ b/tests/wp-compat/plugin-stack/check-request-counts.py @@ -83,6 +83,17 @@ def hit(index):return request('concurrent-'+str(index),extended=bool(index%2)) s=state();same(2,s['today']['throttled']);same(0,s['today']['blocked']);same(0,s['today']['withheld']) check('actual 429 throttles separate from 403 blocks') + # A DB trigger rejects just counter inserts. Detection writes must still work. + reset();configure(rate_limit_enabled=False,tripwire_enabled=True,tripwire_response='block',monitor_mode=False) + root=['docker','exec',db_id,'mariadb','-uroot','-pfixture-root-password','-e'] + try: + subprocess.run(root+["CREATE TRIGGER wp.fixture_counter_failure BEFORE INSERT ON wp.wp_webdecoy_request_counts FOR EACH ROW SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='fixture counter write rejected';"],check=True,capture_output=True,text=True,timeout=15) + r=request('counter-write-failure','/compat-trap/');same(403,r['status']);assert not r['counter_error_visible'];same(0,state()['today']['blocked']) + finally: + subprocess.run(root+["DROP TRIGGER IF EXISTS wp.fixture_counter_failure;"],check=True,capture_output=True,text=True,timeout=15) + same(403,request('counter-write-recovery')['status']);same(1,state()['today']['blocked']) + check('counter write failure preserves denial and recovery resumes counting') + reset();configure(rate_limit_enabled=False,tripwire_enabled=True,monitor_mode=True) r=request('ajax-no-nonce','/wp-admin/admin-ajax.php',post={'action':'webdecoy_client_detection'});same(403,r['status']);same([],state()['rows']) action('cli-mark');same([],state()['rows']);check('nonce rejection and CLI operations excluded') diff --git a/tests/wp-compat/plugin-stack/client.php b/tests/wp-compat/plugin-stack/client.php index 59a45e6..82295b5 100644 --- a/tests/wp-compat/plugin-stack/client.php +++ b/tests/wp-compat/plugin-stack/client.php @@ -16,5 +16,6 @@ $summary['widget_counts']=[]; preg_match_all('/data-webdecoy-count="([a-z]+)">([0-9,]+)<\/span>/', $body, $widgets, PREG_SET_ORDER); foreach($widgets as $widget)$summary['widget_counts'][$widget[1]]=(int)str_replace(',','',$widget[2]); +$summary['counter_error_visible']=strpos($body,'fixture counter write rejected')!==false || strpos($body,'WordPress database error')!==false; $summary['tracking_start_visible']=strpos($body,'Request tracking started')!==false; file_put_contents('/scratch/client-results.jsonl',json_encode($summary)."\n",FILE_APPEND|LOCK_EX);echo json_encode($summary);