diff options
| author | Greg Roach <greg@subaqua.co.uk> | 2025-07-30 12:52:48 +0100 |
|---|---|---|
| committer | Greg Roach <greg@subaqua.co.uk> | 2025-07-30 12:52:48 +0100 |
| commit | 72e3e616fccbe9722129b9f1c11f53b0a686556d (patch) | |
| tree | 7c96a877099d5053f98d570afeea43ea92362676 | |
| parent | 9d93998d2c92f912ae14d9d4e5189b335695ec8f (diff) | |
| download | webtrees-72e3e616fccbe9722129b9f1c11f53b0a686556d.tar.gz webtrees-72e3e616fccbe9722129b9f1c11f53b0a686556d.tar.bz2 webtrees-72e3e616fccbe9722129b9f1c11f53b0a686556d.zip | |
Show reasons when blocking bots
| -rw-r--r-- | app/Http/Middleware/AuthNotRobot.php | 2 | ||||
| -rw-r--r-- | app/Http/Middleware/BadBotBlocker.php | 19 |
2 files changed, 11 insertions, 10 deletions
diff --git a/app/Http/Middleware/AuthNotRobot.php b/app/Http/Middleware/AuthNotRobot.php index c56c0f3ee8..57e7c7f5a9 100644 --- a/app/Http/Middleware/AuthNotRobot.php +++ b/app/Http/Middleware/AuthNotRobot.php @@ -41,7 +41,7 @@ class AuthNotRobot implements MiddlewareInterface public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { if ($request->getAttribute(BadBotBlocker::ROBOT_ATTRIBUTE_NAME) !== null) { - return response('Not acceptable', StatusCodeInterface::STATUS_NOT_ACCEPTABLE); + return response('Not acceptable: routing', StatusCodeInterface::STATUS_NOT_ACCEPTABLE); } return $handler->handle($request); diff --git a/app/Http/Middleware/BadBotBlocker.php b/app/Http/Middleware/BadBotBlocker.php index a6c24dbfc9..051a1a4903 100644 --- a/app/Http/Middleware/BadBotBlocker.php +++ b/app/Http/Middleware/BadBotBlocker.php @@ -244,12 +244,12 @@ class BadBotBlocker implements MiddlewareInterface assert($address instanceof AddressInterface); if ($ua === '') { - return $this->response(); + return $this->response('Not acceptable: no-ua'); } foreach (self::BAD_ROBOTS as $robot) { if (str_contains($ua, $robot)) { - return $this->response(); + return $this->response('Not acceptable: bad-ua'); } } @@ -260,7 +260,7 @@ class BadBotBlocker implements MiddlewareInterface if ($this->checkRobotDNS($ip, $valid_domains, false)) { $validated_bot = true; } else { - return $this->response(); + return $this->response('Not acceptable: bad-dns'); } } } @@ -270,7 +270,7 @@ class BadBotBlocker implements MiddlewareInterface if ($this->checkRobotDNS($ip, $valid_domains, true)) { $validated_bot = true; } else { - return $this->response(); + return $this->response('Not acceptable: bad-dns'); } } } @@ -287,7 +287,7 @@ class BadBotBlocker implements MiddlewareInterface } } - return $this->response(); + return $this->response('Not acceptable: bad-dns'); } } } @@ -299,7 +299,7 @@ class BadBotBlocker implements MiddlewareInterface foreach ($matches[1] as $asn) { foreach ($this->fetchIpRangesForAsn($asn) as $range) { if ($range->contains($address)) { - return $this->response(); + return $this->response('Not acceptable: bad-asn'); } } } @@ -331,7 +331,8 @@ class BadBotBlocker implements MiddlewareInterface '<body>Cookie check</body>' . '</html>'; - return $this->response($content)->withHeader('set-cookie', 'x=y; HttpOnly; SameSite=Strict'); + return $this->response($content) + ->withHeader('set-cookie', 'x=y; HttpOnly; SameSite=Strict'); } // Bots get restricted access @@ -344,7 +345,7 @@ class BadBotBlocker implements MiddlewareInterface $path = $request->getUri()->getPath(); if (str_starts_with($path, '/xmlrpc.php') || str_starts_with($path, '/wp-')) { - return $this->response(); + return $this->response('Not acceptable: not-wp'); } return $handler->handle($request); @@ -386,7 +387,7 @@ class BadBotBlocker implements MiddlewareInterface }, random_int(self::WHOIS_TTL_MIN, self::WHOIS_TTL_MAX)); } - private function response(string $content = 'Not acceptable'): ResponseInterface + private function response(string $content): ResponseInterface { return response($content, StatusCodeInterface::STATUS_NOT_ACCEPTABLE); } |
