enlivenapp/flight-csrf 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

enlivenapp/flight-csrf

最新稳定版本:0.2.1

Composer 安装命令:

composer require enlivenapp/flight-csrf

包简介

CSRF protection middleware and helpers for FlightPHP

README 文档

README

Stable? Not Quite Yet License PHP Version Monthly Downloads Total Downloads GitHub Issues Contributors Latest Release Contributions Welcome

flight-csrf

I noticed folks downloading some of these packages. I'm super grateful, Thank You! I would like to let folks know until this notice disappears I'm doing a lot of breaking changes without worrying about them. Once versions are up around 0.5.x things should settle down.

CSRF protection middleware and helpers for FlightPHP, built for the flight-school plugin system.

Requirements

  • PHP 8.1+
  • enlivenapp/flight-school ^0.2

Installation

composer require enlivenapp/flight-csrf

Flight School auto-discovers the package via its flightphp-plugin composer type. Enable it in your app config:

// app/config/config.php
return [
    // ...
    'plugins' => [
        'enlivenapp/flight-csrf' => [
            'enabled' => true,
        ],
    ],
];

Usage

Forms

Use csrf_field() to output a hidden input in any HTML form. The token is generated and stored in the session automatically.

<form method="POST" action="/login">
    <?= csrf_field() ?>
    <input type="text" name="username">
    <button type="submit">Log in</button>
</form>

AJAX requests

Use csrf_token() to retrieve the raw token value. The most common pattern is to embed it in a meta tag and read it from JavaScript.

<meta name="csrf-token" content="<?= csrf_token() ?>">
fetch('/api/resource', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
    },
    body: JSON.stringify({ foo: 'bar' }),
});

The middleware accepts the token from either the _csrf_token POST field or the X-CSRF-TOKEN request header (POST field is checked first).

Protecting routes

Apply CsrfMiddleware to any route or route group that should be protected:

use Enlivenapp\FlightCsrf\Middlewares\CsrfMiddleware;

Flight::route('POST /login', function () {
    // ...
})->addMiddleware(new CsrfMiddleware(Flight::app()));

// Or on a group
Flight::group('/admin', function () {
    Flight::route('POST /save', function () { /* ... */ });
    Flight::route('DELETE /remove/@id', function ($id) { /* ... */ });
}, [new CsrfMiddleware(Flight::app())]);

When validation fails the middleware halts the request with a 403 response. The body is a JSON-encoded error payload ({"error":"CSRF token validation failed."}); note that no Content-Type header is set on the halted response.

Configuration

Configuration is stored under the enlivenapp.flight-csrf key. The plugin's own defaults live in src/Config/Config.php; override them per-app via the plugins array in app/config/config.php (values are merged on top of the defaults with array_replace_recursive).

Key Default Description
field_name _csrf_token Name of the hidden form field
session_key _csrf_token Session key used to store the token
protected_methods ['POST','PUT','PATCH','DELETE'] HTTP methods that trigger validation
exclude_routes [] Request paths to skip (exact URI match, no query string)
token_lifetime 7200 Absolute seconds from creation before the token expires (default: 2 hours). 0 disables time-based expiry (token lives for the session). Expired tokens fail validation (403) and are rotated on the next csrf_token() / csrf_field() call.

Overriding defaults

On first load the plugin scaffolds token_lifetime and exclude_routes into its block in app/config/config.php so you can edit them in place. Any other config key can also be added manually to the same block — values are merged on top of the plugin defaults.

Example after scaffolding, with a shorter token lifetime and exclude routes filled in:

// app/config/config.php
return [
    // ...
    'plugins' => [
        'enlivenapp/flight-csrf' => [
            'enabled' => true,
            'token_lifetime' => 3600,   // 1 hour instead of the 2-hour default
            'exclude_routes' => [
                '/webhooks/stripe',
                '/api/public/callback',
            ],
        ],
    ],
];

License

MIT

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固