eliepse/argile-honeypot

用于保护表单免受机器人垃圾邮件攻击的 HoneyPot,适用于 Argile 框架

2.0.1 2021-01-23 14:34 UTC

This package is auto-updated.

Last update: 2024-09-23 22:16:36 UTC


README

Packagist Version Packagist PHP Version Support Packagist License Framework: Argile

Argile Honeypot 是一个简单的防止机器人垃圾邮件攻击的公共表单保护方案。它可用于各种项目,但专为与简单的 Argile 框架配合使用而设计。

它是如何工作的?

HoneyPot 通过对 HTML 响应中的输入名称进行散列(由 HoneypotResponseMiddleware 处理),并添加具有原始名称的虚假输入(HoneyPot)。机器人垃圾邮件发送者可能会填写所有字段,尤其是那些看起来像真实字段的字段。

HoneypotRequestMiddleware 中间件检查来自客户端的 POST 请求,并检查是否有虚假字段被填写。如果是这样,则请求被阻止,并发送 403 响应。它还检查表单是否在特定延迟(默认为 5 秒)内被填写。

如何使用它?

首先,通过将其添加到您的 composer.json 或通过命令行要求它来安装此包。

composer require eliepse/argile-honeypot

您还必须添加 CSS 类 "onipat" 来隐藏虚假输入。CSS 文件位于 /resources/css/honeypot.css

.onipat {
    opacity: 0;
    position: absolute;
    top: 0;
    left: 0;
    height: 0;
    width: 0;
    z-index: -1;
}

1. 准备表单

然后,将 HoneypotResponseMiddleware 添加到包含要保护的表单的路由中。以下是一个示例

$router->get('/', WelcomeController::class)
	->addMiddleware(new HoneypotResponseMiddleware());

为了让中间件正常工作,您必须指示一些常见的字段作为 HoneyPot 进行复制。只需在输入名称前添加前缀 honeypot: 即可。示例

<input type="email" name="honeypot:email" placeholder="Type your email here...">

中间件将自动将真实字段的名称更改为散列,并生成一个虚假字段。

<!-- The original field with hashed name -->
<input type="email" name="jh87dd4h88rjk8h448dfa" placeholder="Type your email here...">
<!-- The fake field generated -->
<input type="email" name="email" class="onipat" autocomplete="off" tabindex="-1">

2. 处理 POST 请求

现在,我们必须处理请求以阻止垃圾邮件并将输入名称更改为原始名称(这样其余代码就不必处理散列名称)。只需将请求中间件添加到您的路由中,如下所示。

$router->post("/contact", [SendContactMailController::class, 'send'])
	->addMiddleware(new HoneypotRequestMiddleware());

许可证

此软件包在 MIT 许可证 下。

Élie Meignan 维护。