From e49c96d6152dddffc973a4301ea8f996f5deb9d9 Mon Sep 17 00:00:00 2001 From: Chris Portscheller Date: Fri, 27 Nov 2026 12:46:17 -0600 Subject: [PATCH] Give spooled violations an event id so retries are stored once --- .../class-webdecoy-violation-reporter.php | 25 ++++++++- tests/ViolationEventIdTest.php | 51 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/ViolationEventIdTest.php diff --git a/includes/class-webdecoy-violation-reporter.php b/includes/class-webdecoy-violation-reporter.php index 4720409..4a7fad6 100644 --- a/includes/class-webdecoy-violation-reporter.php +++ b/includes/class-webdecoy-violation-reporter.php @@ -21,6 +21,9 @@ * 2. As a safety net, by a cron job (webdecoy_flush_violations) that drains * anything left behind if the ingest service was briefly unreachable. * + * Each event gets an id when it is spooled, so a retried batch that ingest + * already saved is stored once (WebDecoy/app#1945). + * * Delivery is retried once then dropped, and the queue is hard-capped, so * reporting can never become a reliability or storage liability (best-effort, * matching @webdecoy/node). @@ -107,7 +110,7 @@ public function report(array $events): void foreach ($events as $event) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Persist a security event or state mutation in a plugin-owned table; a cached result cannot replace this write. $wpdb->insert($table, [ - 'payload' => wp_json_encode($event->toApiPayload()), + 'payload' => wp_json_encode(self::with_event_id($event->toApiPayload(), wp_generate_uuid4())), 'attempts' => 0, 'created_at' => $now, ]); @@ -116,6 +119,26 @@ public function report(array $events): void $this->ensureShutdownDrain(); } + /** + * Give a spooled violation its event id. + * + * The id is stamped once, when the event is spooled, and travels with the + * stored payload, so every resend of the row carries the same id. Ingest + * stores an event id once per organization, so a batch that was saved + * before its response was lost (a timeout, then a retry) is not stored + * twice and does not trip a second deny. + * + * @param array $payload + * @return array + */ + public static function with_event_id(array $payload, string $eventId): array + { + if (!isset($payload['eventId']) && $eventId !== '') { + $payload['eventId'] = $eventId; + } + return $payload; + } + /** * Register the shutdown drain exactly once. */ diff --git a/tests/ViolationEventIdTest.php b/tests/ViolationEventIdTest.php new file mode 100644 index 0000000..596043f --- /dev/null +++ b/tests/ViolationEventIdTest.php @@ -0,0 +1,51 @@ + 'tripwire'], '0b7d3f3e-5c1a-4c39-9a51-6f2f6a4b8e11'); + $same('0b7d3f3e-5c1a-4c39-9a51-6f2f6a4b8e11', $payload['eventId']); + $same('tripwire', $payload['rule']); +}); + +$t('an id already on the payload is never replaced', function () use ($same) { + $payload = WebDecoy_Violation_Reporter::with_event_id(['eventId' => 'first'], 'second'); + $same('first', $payload['eventId']); +}); + +$t('the id survives the trip through the spool unchanged', function () use ($same) { + $stored = json_encode(WebDecoy_Violation_Reporter::with_event_id(['rule' => 'rate_limit'], 'abc')); + $resent = json_decode($stored, true); + $same('abc', $resent['eventId'], 'a retry resends the stored payload, id included'); +}); + +$t('report() stamps the id at spool time, not at send time', function () use ($true) { + $src = (string) file_get_contents(dirname(__DIR__) . '/includes/class-webdecoy-violation-reporter.php'); + $true( + (bool) preg_match('/\'payload\'\s*=>\s*wp_json_encode\(self::with_event_id\(\$event->toApiPayload\(\),\s*wp_generate_uuid4\(\)\)\)/', $src), + 'the spool insert must carry a generated eventId' + ); + $true(substr_count($src, 'wp_generate_uuid4(') === 1, 'ids are generated in one place only'); +});