luri / anothercsrflib
一个用于NO中间件项目的简单易用的Another CSRF php库,带有到期令牌。如果你想使用中间件CSRF库,请查看slim/csrf
v1.0.RC1
2020-11-08 17:30 UTC
Requires
- php: ^7.2|^8.0
Requires (Dev)
- phpunit/phpunit: ^8 | ^9
This package is auto-updated.
Last update: 2024-09-09 01:02:39 UTC
README
一个小型简单易用的Another CSRF库。
我们为每个表单生成一个令牌,并在处理请求之前验证该令牌。
令牌仅有效一次。它可以被限制为单个URL、单个持续时间、单个表单、访问者的IP和用户代理。
结构
src/ For source sode
tests/ For phpunit test code
安装
通过Composer
$ composer require luri/anothercsrflib
使用方法
以下示例中,以下保护措施是激活的
- 仅对生成表单的URL有效
- 仅对访问者的IP有效
- 仅对访问者的用户代理有效
- 有效期为10分钟
use Luri\ACSRFLib\{
Protect,
ServerWrapper
};
$protectLib = new Protect($_SESSION, new ServerWrapper());
//
// Part 1 - Before html form generation
//
$token = $protectLib->generateToken();
//You must include the token into html form like this :
echo '<input type="hidden" name="' . key($token) . '" value="' . current($token) . '" />';
//
// Part 2 - on process form data
// (same page here beacause default url protection, but you can change this and use an
// antoher page for process you form data.)
//
if ($protectLib->isValidRequest($_POST)) { // This for example. For security, you must filter and validate user entry.
//Token is valid, so you can process the request
}
您可以更改或禁用URL/时间持续时间。
您可以添加表单保护
您可以禁用IP和用户代理保护。
请参阅tests/UsageTest.php以获取更多示例。
请参阅src/Protect.php中的注释。
变更日志
请参阅CHANGELOG了解最近更改的更多信息。
测试
$ composer test
安全
如果您发现任何与安全相关的问题,请通过电子邮件luri@e.email联系,而不是使用问题跟踪器。
鸣谢
- Luri
许可证
本程序是自由软件:您可以在[GNU通用公共许可证](LICENSE.md)的条款下重新分发和/或修改它,该许可证由[自由软件基金会](https://www.fsf.org/)发布,许可证版本为3,或任何更高版本。
本程序分发时附带“希望它有用”,但没有任何保证;甚至没有关于其适销性或适用于特定用途的暗示保证。有关详细信息,请参阅[GNU通用公共许可证](LICENSE.md)。
版权所有 2020,Luri及以下作者[鸣谢] (#Credits)。