inventor96/mako-csrf

GitHub: inventor96/mako-csrf

这是一个专为 Mako 框架设计的 CSRF 防护中间件,通过包装 Session Token 有效防止跨站请求伪造攻击。

Stars: 0 | Forks: 0

# Mako CSRF 这是一个围绕 Mako [session tokens](https://makoframework.com/docs/10.0/learn-more:sessions#usage:security) 的中间件包装器,用作反 CSRF 机制。 ## 安装说明 1. 安装 composer 包: composer require inventor96/mako-csrf 2. 在 Mako 中启用该包: `app/config/application.php`: [ 'packages' => [ 'web' => [ \inventor96\MakoCSRF\CSRFPackage::class ], ], ]; 3. 注册中间件: `app/http/routing/middleware.php`: $dispatcher->registerGlobalMiddleware(\inventor96\MakoCSRF\CSRFMiddleware::class); 还建议您将优先级设置为低于 Mako 默认值 100: `app/http/routing/middleware.php`: $dispatcher->setMiddlewarePriority(\inventor96\MakoCSRF\CSRFMiddleware::class, 25); 最低限度是,它应该在可能导致应用程序发生更改的任何其他中间件之前处理。 ## 配置 默认配置开箱即用,但为了混淆起见,您可能需要更改这些值。如果您想覆盖默认配置,请在 `app/config/packages/csrf/csrf.php` 创建一个新文件。 以下配置项及其默认值如下: ``` return [ /* * --------------------------------------------------------- * Form name * --------------------------------------------------------- * * The name of the form field that will contain the CSRF token. * This applies to both the generated HTML element, as well as the field that is checked in the middleware. */ 'form_name' => 'mako_csrf_token', /* * --------------------------------------------------------- * View variable name * --------------------------------------------------------- * * The name of the variable that will be made available in views to contain the CSRF token. */ 'view_var_name' => 'mako_csrf_token', /* * --------------------------------------------------------- * Missing token message * --------------------------------------------------------- * * The message of the `BadRequestException` when the CSRF token is missing. */ 'missing_token_message' => 'The CSRF token is missing.', /* * --------------------------------------------------------- * Bad token message * --------------------------------------------------------- * * The message of the `BadRequestException` when the CSRF token is invalid. */ 'bad_token_message' => 'The CSRF token is invalid.', ]; ``` ## 使用方法 ### 中间件 中间件将自动要求任何状态更改的 HTTP 动词(例如 `POST`、`DELETE` 等)提供有效的 CSRF token,并作为其他动词(例如 `GET`、`HEAD` 等)的透传通道。要在任一方向覆盖此行为,您需要使用 `$required` 参数在每个路由的基础上覆盖配置。 ``` $routes->post('/articles/{id}', [Articles::class, 'update']) ->middleware(CSRFMiddleware::class, required: false); ``` ### 视图 视图中提供了两个变量,它们都基于 `view_var_name` 配置选项。如果您更改了配置选项,请在以下示例中将 `mako_csrf_token` 替换为您的新值: - `$mako_csrf_token`:CSRF token 值本身。 - `$mako_csrf_token_input`:包含 CSRF token 的 HTML 隐藏输入元素,其 `name` 属性设置为 `form_name` 配置选项。这可以直接在 HTML 表单中使用。
{{ raw:$mako_csrf_token_input }}
标签:Composer包, CSRF防护, Mako框架, OpenVAS, PHP, Session安全, Syscall, Web安全, Web开发, 中间件, 会话令牌, 后端开发, 安全中间件, 蓝队分析, 表单安全, 防跨站请求伪造