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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gravityforms-altcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Plugin
{
public const VERSION = '0.6.2';
public const VERSION = '0.7.0';

public const SLUG = 'gravityforms-altcha';

Expand Down
18 changes: 18 additions & 0 deletions src/SpamFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/integration/SpamFilterIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(), [
Expand Down Expand Up @@ -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']);
}
Expand Down
Loading