yggdrasil/src/api/mod.rs
xfy 82a3c12940 feat(security): add Origin-based CSRF protection for write endpoints
对所有 POST/PUT/PATCH/DELETE 请求校验 Origin(回退 Referer)等于本站,
堵住 login CSRF 与未来 GET 化写接口的盲区(SameSite=Lax 覆盖不到)。

- 新增 src/api/csrf.rs:纯函数 origin 解析(不引入 url crate)+ axum 中间件
- 挂载到 upload 路由与 Dioxus app 路由(最外层,先于超时/压缩)
- APP_BASE_URL 配置可信域名;未设置时回退 Host + X-Forwarded-Proto
- GET/OPTIONS 放行;拿不到本站 origin 时放行避免误杀
- 9 个单测覆盖写方法识别、origin 标准化、默认端口省略、头解析回退
2026-06-18 13:22:59 +08:00

31 lines
818 B
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! API 层根模块。
//!
//! 按职责划分子模块,包含两类接口:
//! - Dioxus server function`#[server(Name, "/api")]`),如 `auth`、`posts`
//! - Axum 手动路由处理器,如 `upload`、`image`。
/// 认证相关的 Dioxus server function。
pub mod auth;
/// CSRF 防护中间件。
pub mod csrf;
/// 评论相关接口。
pub mod comments;
/// 应用错误类型与转换。
pub mod error;
/// 图片服务的 Axum 处理器。
pub mod image;
/// Markdown 渲染与 HTML 清理。
pub mod markdown;
/// 文章 CRUD 相关接口。
pub mod posts;
/// 限流工具。
pub mod rate_limit;
/// HTML 消毒器。
pub mod sanitizer;
/// 回收站与站点配置接口。
pub mod settings;
/// URL slug 生成与校验。
pub mod slug;
/// 图片上传的 Axum 处理器。
pub mod upload;