diff --git a/README.md b/README.md index 2493f24..46b8aca 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,8 @@ Settings → **ALTCHA**. Both **mark suspicious submissions as spam** (via `gform_entry_is_spam`) rather than rejecting them — a real visitor is never blocked or shown an error, and a false positive stays recoverable in the entry *Spam* view. They apply to every Gravity Form, independent of whether ALTCHA -itself is enabled. +itself is enabled. Each flag records its reason with Gravity Forms' +`set_spam_filter()` (filter "ALTCHA"), so the entry's spam note says why. * **Rate limiting** — flags submissions once an IP exceeds a per-form allowance within a window (default 3 per hour). The IP is resolved independently of Gravity @@ -110,7 +111,8 @@ content_filter: spam {"form":7,"score":3,"signals":["keyword"]} email_validation: blocked {"form":7,"status":"undeliverable","disposable":false,"reason":"rejected_email","domain":"example.com"} ``` -`reason` for ALTCHA is `missing` / `invalid` / `replay`. **No PII is logged** — +`reason` for ALTCHA is `missing` / `malformed` / `expired` / +`invalid_signature` / `invalid_solution` / `invalid` / `replay`. **No PII is logged** — IPs only ever appear as a salted hash, and emails as the domain only. Every decision also fires the `genero/gravityforms_altcha/log` action, so you diff --git a/gravityforms-altcha.php b/gravityforms-altcha.php index 8d44322..31a5545 100644 --- a/gravityforms-altcha.php +++ b/gravityforms-altcha.php @@ -6,7 +6,7 @@ * Plugin Name: ALTCHA for Gravity Forms * Plugin URI: https://github.com/generoi/gravityforms-altcha * Description: Invisible ALTCHA spam protection for Gravity Forms — uses the MIT-licensed altcha-org/altcha PHP library and the ALTCHA widget web component to proof-of-work-verify every form submission with no user interaction. - * Version: 0.6.2 + * Version: 0.7.0 * Requires at least: 6.0 * Requires PHP: 8.2 * Author: Genero diff --git a/src/Plugin.php b/src/Plugin.php index 4150bdc..f599c25 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -4,7 +4,7 @@ class Plugin { - public const VERSION = '0.6.2'; + public const VERSION = '0.7.0'; public const SLUG = 'gravityforms-altcha'; diff --git a/src/SpamFilter.php b/src/SpamFilter.php index deb275b..1992ee5 100644 --- a/src/SpamFilter.php +++ b/src/SpamFilter.php @@ -47,6 +47,7 @@ public function flag($isSpam, $form, $entry): bool if (Settings::rateLimitEnabled()) { if ($this->exceedsRateLimit($form)) { Logger::record('rate_limit', 'blocked', ['form' => $formId]); + self::recordReason($formId, __('The submission rate limit for this IP address was exceeded.', 'gravityforms-altcha')); return true; } @@ -63,6 +64,11 @@ public function flag($isSpam, $form, $entry): bool 'score' => $report['score'], 'signals' => $report['signals'], ]); + self::recordReason($formId, sprintf( + /* translators: %s: comma-separated content signals, e.g. "keyword, link" */ + __('The content filter matched: %s.', 'gravityforms-altcha'), + implode(', ', $report['signals']) + )); return true; } @@ -72,6 +78,18 @@ public function flag($isSpam, $form, $entry): bool return (bool) $isSpam; } + /** + * Records why the entry was flagged, so its spam note and anything reading + * the submission (a monitor, an admin) show a reason instead of a bare + * "Spam Filter". + */ + private static function recordReason(mixed $formId, string $reason): void + { + if (method_exists('GFCommon', 'set_spam_filter')) { + \GFCommon::set_spam_filter((int) $formId, __('ALTCHA', 'gravityforms-altcha'), $reason); + } + } + /** * Per-IP, per-form sliding-ish counter. Allows up to the limit within the * window and flags anything beyond it. An unresolvable IP is never diff --git a/test/integration/SpamFilterIntegrationTest.php b/test/integration/SpamFilterIntegrationTest.php index 354b793..f2c054e 100644 --- a/test/integration/SpamFilterIntegrationTest.php +++ b/test/integration/SpamFilterIntegrationTest.php @@ -60,6 +60,27 @@ public function test_keyword_in_message_is_flagged(): void $this->assertTrue($this->isSpam($this->makeForm(), ['2' => 'cheap VIAGRA, best price'])); } + public function test_flagged_content_records_its_reason(): void + { + $form = $this->makeForm(); + unset(\GFFormDisplay::$submission[$form['id']]); + + $this->assertTrue($this->isSpam($form, ['2' => 'cheap VIAGRA, best price'])); + $this->assertSame( + ['filter' => 'ALTCHA', 'reason' => 'The content filter matched: keyword.'], + \GFFormDisplay::$submission[$form['id']]['spam_filter'] ?? null, + ); + } + + public function test_clean_submission_records_no_reason(): void + { + $form = $this->makeForm(); + unset(\GFFormDisplay::$submission[$form['id']]); + + $this->assertFalse($this->isSpam($form, ['1.3' => 'Matti', '1.6' => 'M', '2' => 'Kiitos!'])); + $this->assertArrayNotHasKey('spam_filter', \GFFormDisplay::$submission[$form['id']] ?? []); + } + public function test_url_in_name_field_is_flagged(): void { $this->assertTrue($this->isSpam($this->makeForm(), [ @@ -93,6 +114,10 @@ public function test_rate_limit_flags_beyond_the_allowance(): void // Default 3/hour → first three pass, the rest are flagged. $this->assertSame([false, false, false, true, true], $results); + $this->assertSame( + ['filter' => 'ALTCHA', 'reason' => 'The submission rate limit for this IP address was exceeded.'], + \GFFormDisplay::$submission[$form['id']]['spam_filter'] ?? null, + ); unset($_SERVER['REMOTE_ADDR']); }