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
25 changes: 24 additions & 1 deletion includes/class-webdecoy-violation-reporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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,
]);
Expand All @@ -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<string,mixed> $payload
* @return array<string,mixed>
*/
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.
*/
Expand Down
51 changes: 51 additions & 0 deletions tests/ViolationEventIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* Violation event ids (WebDecoy/app#1945).
*
* A batch can reach ingest and be saved while its response is lost; the drain
* then retries it. Ingest stores each event id once per organization, so the
* retry is harmless only if the resent row carries the id it was given when
* it was spooled.
*
* Run: php tests/run.php
*/

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

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

echo "\nViolation event ids\n";

$t('a spooled event gets the id it was given', function () use ($same) {
$payload = WebDecoy_Violation_Reporter::with_event_id(['rule' => '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');
});
Loading