spencer14420/sp-anti-csrf
Composer 安装命令:
composer require spencer14420/sp-anti-csrf
包简介
Can be used to generate and validate anti-CSRF tokens
README 文档
README
SPAntiCSRF is a lightweight PHP package that secures web applications against CSRF attacks using token-based validation.
Features
- Session-based CSRF token management: Tokens are stored securely in PHP sessions.
- Token expiration: Tokens expire after a configurable duration for enhanced security.
- Validation and replay protection: Ensures tokens are valid and prevents token reuse.
- Session regeneration: Provides a method to regenerate session IDs, helping to mitigate session fixation attacks when used appropriately.
- One-time token usage: Tokens are invalidated after successful validation to prevent reuse.
Installation
You can install SPAntiCSRF using Composer:
composer require spencer14420/sp-anti-csrf
Usage
Generate a CSRF Token
Generate a token when rendering forms or making requests that require CSRF protection:
use spencer14420\SpAntiCsrf\AntiCsrf; $csrf = new AntiCsrf(); $token = $csrf->generateToken();
Use the token in your HTML form:
<input type="hidden" id="csrf_token" name="csrf_token" value="<?php echo $token ?>" />
Validate the Token
Validate the token on the server side when processing the form submission:
use spencer14420\SpAntiCsrf\AntiCsrf; $csrf = new AntiCsrf(); try { $token = $_POST['csrf_token'] ?? ''; if (!$csrf->tokenIsValid($token)) { throw new Exception('Invalid CSRF token.'); } // Proceed with processing the form } catch (Exception $e) { // Handle invalid or expired token echo 'Error: ' . $e->getMessage(); }
Regenerate the Session
For added security, you can regenerate the session ID periodically or after certain actions:
$csrf->regenerateSession();
- Consider calling
regenerateSession()after sensitive actions like user login, logout, or privilege escalation to protect against session fixation attacks.
API Reference
generateToken(int $expirySeconds = 3600): string
Generates a new CSRF token, and stores it in a session variable with an expiry time (default: 1 hour).
- Parameters:
$expirySeconds: The token's lifetime in seconds.
- Returns: The generated token as a string.
tokenIsValid(string $tokenToCheck): bool
Validates a CSRF token.
- Parameters
$tokenToCheck: The token to validate.
- Returns:
trueif the token is valid and has not expired;falseotherwise.
regenerateSession(): void
Regenerates the PHP session ID to mitigate session fixation attacks.
tokenIsNotExpired(): bool
Checks if the stored token has expired.
- Returns:
trueif the token has not expired;falseotherwise.
spencer14420/sp-anti-csrf 适用场景与选型建议
spencer14420/sp-anti-csrf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 613 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 spencer14420/sp-anti-csrf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spencer14420/sp-anti-csrf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 613
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-16