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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ add_action('genero/gravityforms_altcha/log', function (string $event, string $ou
}, 10, 3);
```

With [gds-monitoring](https://github.com/generoi/gds-monitoring) installed, an
`expired` or `invalid_signature` rejection is also reported to it, every one,
since both take a real, signed challenge and so point to a person turned away
(`report altcha blog 2: rejected form 4 (expired): 125 s past expiry`). Bots'
reasons (missing, malformed, replayed proofs) aren't. Without it the call does
nothing.

## How it works

1. **Form render** — when enabled for the form, the plugin injects a hidden
Expand Down
30 changes: 29 additions & 1 deletion src/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public function validate(array $result): array
$reason = self::REASON_REPLAY;
}

Logger::record('altcha', 'fail', $this->failureContext($formId, $reason, $payload, $challenge));
$context = $this->failureContext($formId, $reason, $payload, $challenge);
Logger::record('altcha', 'fail', $context);
$this->reportToMonitoring($context);

$result['is_valid'] = false;
$this->rejected[(int) $formId] = true;
Expand Down Expand Up @@ -175,6 +177,32 @@ private function failureContext(mixed $formId, string $reason, string $payload,
return $context;
}

/**
* Tells gds-monitoring (when installed) about rejections that take a real,
* signed challenge: it expired before the visitor submitted, or its
* signature no longer matches (the HMAC key changed). Those are real
* people being turned away, and nothing is saved for a refused
* submission. A missing, malformed or replayed proof is what bots send,
* so it isn't reported. Every one is reported (no throttle), since each
* is a person.
*
* @param array<string, mixed> $context
*/
private function reportToMonitoring(array $context): void
{
$reason = (string) $context['reason'];
if (! in_array($reason, [Challenge::REASON_EXPIRED, Challenge::REASON_INVALID_SIGNATURE], true)) {
return;
}

$message = sprintf('rejected form %d (%s)', (int) $context['form'], $reason);
if (isset($context['expired_for'])) {
$message .= sprintf(': %d s past expiry', (int) $context['expired_for']);
}

do_action('gds/monitoring/report', 'altcha', $message, false);
}

/**
* One-time-use enforcement. A signed challenge stays verifiable until it
* expires, so without this a bot could solve the proof once and replay the
Expand Down
76 changes: 76 additions & 0 deletions test/mocked/MonitoringReportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Genero\GravityFormsAltcha\Tests\Mocked;

use AltchaOrg\Altcha\Algorithm\Pbkdf2;
use AltchaOrg\Altcha\Altcha;
use AltchaOrg\Altcha\Payload;
use AltchaOrg\Altcha\SolveChallengeOptions;
use Brain\Monkey\Actions;
use Brain\Monkey\Functions;
use Genero\GravityFormsAltcha\Challenge;
use Genero\GravityFormsAltcha\Integration;
use Genero\GravityFormsAltcha\Plugin;

/**
* Exercises the report to gds-monitoring: only rejections that took a real,
* signed challenge reach it, since those turn real people away.
*/
class MonitoringReportTest extends MockedTestCase
{
protected function setUp(): void
{
parent::setUp();

Functions\when('apply_filters')->alias(
fn (string $hook, $value = null) => $hook === 'genero/gravityforms_altcha/should_protect'
? true
: $value
);
Functions\when('plugin_dir_path')->returnArg(1);
Functions\when('plugin_dir_url')->returnArg(1);
Functions\when('get_option')->justReturn('0123456789abcdef0123456789abcdef');
Functions\when('sanitize_text_field')->returnArg(1);
Functions\when('wp_unslash')->returnArg(1);
Functions\when('get_transient')->justReturn(false);
Functions\when('set_transient')->justReturn(true);

Plugin::getInstance(dirname(__DIR__, 2).'/gravityforms-altcha.php');
}

protected function tearDown(): void
{
unset($_POST[Integration::POST_FIELD]);
parent::tearDown();
}

public function test_a_missing_proof_is_not_reported(): void
{
Actions\expectDone('gds/monitoring/report')->never();

$this->validate(45);
}

public function test_a_proof_signed_with_another_key_is_reported(): void
{
$altcha = new Altcha('old-key');
$foreign = (new Challenge('old-key', cost: 1000))->create();
$solution = $altcha->solveChallenge(new SolveChallengeOptions(algorithm: new Pbkdf2, challenge: $foreign));
$_POST[Integration::POST_FIELD] = (new Payload($foreign, $solution))->toBase64();

Actions\expectDone('gds/monitoring/report')
->once()
->with('altcha', 'rejected form 45 (invalid_signature)', false);

$this->validate(45);
}

private function validate(int $formId): void
{
$result = (new Integration)->validate(['is_valid' => true, 'form' => ['id' => $formId]]);

$this->assertFalse($result['is_valid']);
}
}
Loading