diff --git a/README.md b/README.md index 46b8aca..eea9618 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Integration.php b/src/Integration.php index 7c234e8..118ebe9 100644 --- a/src/Integration.php +++ b/src/Integration.php @@ -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; @@ -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 $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 diff --git a/test/mocked/MonitoringReportTest.php b/test/mocked/MonitoringReportTest.php new file mode 100644 index 0000000..9b21c73 --- /dev/null +++ b/test/mocked/MonitoringReportTest.php @@ -0,0 +1,76 @@ +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']); + } +}