psecio/csrf
Composer 安装命令:
composer require psecio/csrf
包简介
CSRF Token Generation Library
README 文档
README
Cross-site request forgery (CSRF) tokens provide protection for your request submissions to help ensure they came from your application. The most common implementation in PHP uses the current session to store the token value and makes a comparison once the form is submitted. This library makes it simpler by handling this common task for you buy default. It:
- Provides effective random token generation via either OpenSSL or the PHP 7 csprng
- Handles the session storage and evaluation once the data is submitted.
Here's an example of it in use:
<?php require_once 'vendor/autoload.php'; $manager = new \Psecio\Csrf\Manager(); if (isset($_POST['sub'])) { $result = $manager->verify($_POST['csrf-token']); if ($result === false) { echo 'Bad token! Shame on you...'; } } ?> <form action="/csrf/index.php" method="POST"> <input type="submit" value="submit" name="sub"/> <input type="hidden" name="csrf-token" value="<?php echo $manager->generate(); ?>"/> </form> ?>
As you can see there's three main parts:
- The
Managerinstance - The call to the
generatemethod for creating and storing the token - The call to the
verifymethod to check the submitted token against what's been stored
The way the library stores tokens, a unique hash is created and the token is then stored with this as the key. This prevents the usual problem of only being able to store one token at a time and resulting in false errors if more than one tab is open for the same application.
Double-Submit Tokens
There's also a concept in web application security called double-submit cookies where the CSRF value is sent as a cookies as well as a part of the POST data. This cookie is set to HTTP Only meaning that no Javascript can access the cookie value, preventing theft from something like a cross-site scripting (XSS) attack.
To enable the automatic double-submit feature in this Csrf library, just pass true to the Manager constructor:
<?php $manager = new \Psecio\Csrf\Manager(true); ?>
That's it - the storage and evaluation of the cookie is include in the verification process along with the value stored in the session.
Extending with your own storage method
The Manager class also includes an optional parameter that lets you define your own storage method if for some reason the default session method doesn't work for your application. You just define a class that extends the \Psecio\Csrf\Storage class and pass it in as a second parameter:
<?php class DbStorage { public function save($key, $code) { // Save to the database here... } public abstract function get($key) { // Read from the database here } public abstract function delete($key) { // Delete from the database here } } $dbStorage = new DbStorage(); $manager = new Manager(false, $dbStorage); ?>
The Csrf library will then use this storage method instead of the session method. Note: If you change the first parameter to true it will still use the cookie method for double-submit handling.
psecio/csrf 适用场景与选型建议
psecio/csrf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 301 次下载、GitHub Stars 达 12, 最近一次更新时间为 2013 年 10 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「token」 「csrf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 psecio/csrf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 psecio/csrf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 psecio/csrf 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony bundle which provides a simple way to add CSRF tokens to routes
Provide a way to secure accesses to all routes of an symfony application.
A package for validating Google Cloud's JWT provided by webhooks
JSON Web Token Authentication for Laravel and Lumen
It's a barebone security class written on PHP
Stk CSRF service and middleware
统计信息
- 总下载量: 301
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-10-23