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
58 changes: 42 additions & 16 deletions admin/partials/dashboard-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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.
Expand Down Expand Up @@ -132,18 +126,50 @@
</p>
</div>

<div class="webdecoy-widget-request-counts">
<h4><?php esc_html_e('Detection rows and requests today (UTC)', 'webdecoy'); ?></h4>
<?php if ($request_counts_today['available']) : ?>
<div class="webdecoy-widget-stats">
<?php foreach (['detections' => __('Detection rows', 'webdecoy'), 'blocked' => __('Blocked requests', 'webdecoy'), 'withheld' => __('Enforcement withheld', 'webdecoy')] as $metric => $label) : ?>
<div class="webdecoy-stat">
<span class="webdecoy-stat-value" data-webdecoy-count="<?php echo esc_attr($metric); ?>"><?php echo esc_html(number_format($request_counts_today[$metric])); ?></span>
<span class="webdecoy-stat-label"><?php echo esc_html($label); ?></span>
</div>
<?php endforeach; ?>
</div>
<p>
<?php echo esc_html(sprintf(
/* translators: 1: requests throttled, 2: requests challenged */
__('Throttled requests: %1$s. Challenges served: %2$s.', 'webdecoy'),
number_format($request_counts_today['throttled']), number_format($request_counts_today['challenged'])
)); ?>
</p>
<p class="description">
<?php esc_html_e('Repeat blocks are included. Detection rows are saved findings; one request can create several. Withheld enforcement is not a block.', 'webdecoy'); ?>
<?php echo esc_html(sprintf(
/* translators: %s: UTC date and time when request tracking started */
__('Request tracking started %s UTC; earlier request totals are unavailable.', 'webdecoy'),
gmdate('Y-m-d H:i', $request_counts_today['started_at'])
)); ?>
<a href="<?php echo esc_url(admin_url('admin.php?page=webdecoy-statistics')); ?>"><?php esc_html_e('Compare request counts', 'webdecoy'); ?></a>
</p>
<?php else : ?>
<p class="description"><?php esc_html_e('Request counters are not available yet. Reload after the database update finishes.', 'webdecoy'); ?></p>
<?php endif; ?>
</div>

<div class="webdecoy-widget-stats">
<div class="webdecoy-stat">
<span class="webdecoy-stat-value"><?php echo esc_html(number_format($stats['total'])); ?></span>
<span class="webdecoy-stat-label"><?php esc_html_e('Detections (7 days)', 'webdecoy'); ?></span>
<span class="webdecoy-stat-label"><?php esc_html_e('Detection rows (rolling 7 days)', 'webdecoy'); ?></span>
</div>
<div class="webdecoy-stat">
<span class="webdecoy-stat-value webdecoy-stat-danger"><?php echo esc_html(number_format($stats['high_risk'])); ?></span>
<span class="webdecoy-stat-label"><?php esc_html_e('High Risk', 'webdecoy'); ?></span>
</div>
<div class="webdecoy-stat">
<span class="webdecoy-stat-value"><?php echo esc_html(number_format($blocked_stats['active'])); ?></span>
<span class="webdecoy-stat-label"><?php esc_html_e('Blocked IPs', 'webdecoy'); ?></span>
<span class="webdecoy-stat-label"><?php esc_html_e('Active blocked IPs', 'webdecoy'); ?></span>
</div>
</div>

Expand Down
6 changes: 5 additions & 1 deletion admin/partials/detections-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,15 @@

<div class="wrap">
<h1><?php esc_html_e('Bot Detections', 'webdecoy'); ?></h1>
<p class="description">
<?php esc_html_e('These are saved findings, not a count of blocked requests. One request can create several rows; repeat IP blocks may create none.', 'webdecoy'); ?>
<a href="<?php echo esc_url(admin_url('admin.php?page=webdecoy-statistics')); ?>"><?php esc_html_e('See request counts', 'webdecoy'); ?></a>
</p>

<div class="webdecoy-detection-stats">
<div class="webdecoy-stat-box">
<span class="value"><?php echo esc_html(number_format($stats['total'])); ?></span>
<span class="label"><?php esc_html_e('Total (7 days)', 'webdecoy'); ?></span>
<span class="label"><?php esc_html_e('Detection rows (rolling 7 days)', 'webdecoy'); ?></span>
</div>
<div class="webdecoy-stat-box webdecoy-stat-danger">
<span class="value"><?php echo esc_html(number_format($stats['high_risk'])); ?></span>
Expand Down
49 changes: 44 additions & 5 deletions admin/partials/statistics-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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', []);
?>

Expand All @@ -136,22 +137,60 @@
<div class="webdecoy-stats-summary">
<div class="webdecoy-stat-box">
<span class="value"><?php echo esc_html(number_format($total_today)); ?></span>
<span class="label"><?php esc_html_e('Today', 'webdecoy'); ?></span>
<span class="label"><?php esc_html_e('Detection rows today', 'webdecoy'); ?></span>
</div>
<div class="webdecoy-stat-box">
<span class="value"><?php echo esc_html(number_format((int) $total_7d)); ?></span>
<span class="label"><?php esc_html_e('Last 7 Days', 'webdecoy'); ?></span>
<span class="label"><?php esc_html_e('Detection rows (rolling 7 days)', 'webdecoy'); ?></span>
</div>
<div class="webdecoy-stat-box">
<span class="value"><?php echo esc_html(number_format($total_30d)); ?></span>
<span class="label"><?php esc_html_e('Last 30 Days', 'webdecoy'); ?></span>
<span class="label"><?php esc_html_e('Detection rows (30 days)', 'webdecoy'); ?></span>
</div>
<div class="webdecoy-stat-box webdecoy-stat-success">
<span class="value"><?php echo esc_html(number_format((int) $active_blocks)); ?></span>
<span class="label"><?php esc_html_e('Active Blocks', 'webdecoy'); ?></span>
<span class="label"><?php esc_html_e('Active blocked IPs', 'webdecoy'); ?></span>
</div>
</div>

<div class="webdecoy-chart-card">
<h2><?php esc_html_e('Detection rows and request outcomes', 'webdecoy'); ?></h2>
<p class="description"><?php esc_html_e('Detection rows are saved findings, not a request total. One request can create several findings, while a repeat IP block may create no new finding. Each request is counted once under its strongest outcome below.', 'webdecoy'); ?></p>
<table class="wp-list-table widefat striped">
<thead><tr>
<th scope="col"><?php esc_html_e('Metric', 'webdecoy'); ?></th>
<th scope="col"><?php esc_html_e('Today (UTC)', 'webdecoy'); ?></th>
<th scope="col"><?php esc_html_e('7 calendar days (UTC)', 'webdecoy'); ?></th>
<th scope="col"><?php esc_html_e('30 calendar days (UTC)', 'webdecoy'); ?></th>
</tr></thead>
<tbody>
<?php foreach ([
'detections' => __('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) : ?>
<tr><th scope="row"><?php echo esc_html($label); ?></th>
<?php foreach ($request_windows as $days => $counts) : ?>
<td data-webdecoy-outcome="<?php echo esc_attr($metric); ?>" data-window="<?php echo esc_attr($days); ?>"><?php echo $counts['available'] ? esc_html(number_format($counts[$metric])) : '&mdash;'; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p class="description"><?php esc_html_e('Calendar windows include today. Blocked and throttled counts require an actual denial from WebDecoy; challenges are separate. Withheld enforcement includes monitor mode and is not a block. Setup-test responses, decoy pages, form-validation notices and requests stopped earlier by another firewall or cache are excluded from request-denial counts.', 'webdecoy'); ?></p>
<?php if ($request_windows[1]['available']) : ?>
<p class="description"><?php echo esc_html(sprintf(
/* translators: %s: UTC date and time when request tracking started */
__('Request tracking started %s UTC. Earlier request totals cannot be reconstructed from detection rows. Request counters retain 30 calendar days.', 'webdecoy'),
gmdate('Y-m-d H:i', $request_windows[1]['started_at'])
)); ?></p>
<?php else : ?>
<p class="description"><?php esc_html_e('Request counters are not available yet. Reload after the database update finishes.', 'webdecoy'); ?></p>
<?php endif; ?>
</div>

<!-- Charts Grid -->
<div class="webdecoy-charts-grid">
<!-- Detection Trend -->
Expand Down
5 changes: 5 additions & 0 deletions docs/plugin-stack-compatibility-2026-11-13.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 42 additions & 0 deletions docs/request-counting-validation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"unit_tests_passed": 235,
"focused_checks_passed": 14,
"focused_http_requests": 60,
"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 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",
"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."
}
90 changes: 90 additions & 0 deletions docs/request-counting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# 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. 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.

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.
- 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,
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.
- 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
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).
Loading
Loading