fix: 全量代码审计修复—安全/死代码/错误处理/配置净化
P0 安全修复: - 支付宝回调验签 (alipay_notify BTreeMap + rsa2_verify) - JWT fallback 'default_secret' 改为 .expect() (panic保底) - 安全响应头中间件 (CSP/X-Frame-Options/HSTS) P1 死代码清理: - 移除孤儿文件 src/alipay.rs (284行, 无mod注册) - 移除Cargo未使用依赖 (actix-files/error/log/hex/digest) - log::info! → tracing::info! (auth.rs) - 移除 config.rs server_host + 3个TOML定义 P1 质量修复: - 修复 weather.rs unwrap() → unwrap_or - 修复 auth.rs+payment.rs 错误吞咽 (add tracing::warn) - 修复 main.rs 3x parse().unwrap → unwrap_or P3 运维: - 新增 scripts/backup-db.sh (定时备份用) - 新增 README.md (快速入门文档) - deploy.sh 集成 backup-db.sh 上传
This commit is contained in:
31
src/main.rs
31
src/main.rs
@@ -1,4 +1,4 @@
|
||||
use actix_web::middleware::from_fn;
|
||||
use actix_web::middleware::{from_fn, DefaultHeaders};
|
||||
use actix_web::{App, HttpServer, web};
|
||||
use tracing::{error, info};
|
||||
use openssl::ssl::{SslAcceptor, SslAcceptorBuilder, SslFiletype, SslMethod};
|
||||
@@ -62,6 +62,15 @@ fn create_server_config(
|
||||
.app_data(web::Data::new(pool))
|
||||
.app_data(web::Data::new(http_client))
|
||||
.app_data(web::Data::new(app_state))
|
||||
// 安全响应头(全局中间件)
|
||||
.wrap(
|
||||
actix_web::middleware::DefaultHeaders::new()
|
||||
.add(("X-Content-Type-Options", "nosniff"))
|
||||
.add(("X-Frame-Options", "DENY"))
|
||||
.add(("Content-Security-Policy", "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdn.jsdelivr.net; style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; img-src 'self' data:; font-src 'self' https://cdn.jsdelivr.net; connect-src 'self'"))
|
||||
.add(("Referrer-Policy", "no-referrer-when-downgrade"))
|
||||
.add(("Permissions-Policy", "geolocation=(), microphone=(), camera=()"))
|
||||
)
|
||||
// 根路径(无需认证)
|
||||
.service(root) // #[get("/")] - 返回服务信息
|
||||
// 支付页面(无需认证,外部浏览器访问)
|
||||
@@ -144,12 +153,26 @@ async fn main() -> std::io::Result<()> {
|
||||
// 保持文件 guard 存活(使用 Box 泄漏)
|
||||
std::mem::forget(_guard);
|
||||
|
||||
use std::str::FromStr;
|
||||
let default_directive = tracing_subscriber::filter::Directive::from_str("info").unwrap();
|
||||
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
tracing_subscriber::EnvFilter::from_default_env()
|
||||
.add_directive(format!("rust_backend={}", app_config.rust_log).parse().unwrap())
|
||||
.add_directive(format!("actix_web={}", app_config.rust_log).parse().unwrap())
|
||||
.add_directive("sqlx=warn".parse().unwrap())
|
||||
.add_directive(
|
||||
format!("rust_backend={}", app_config.rust_log)
|
||||
.parse()
|
||||
.unwrap_or(default_directive.clone())
|
||||
)
|
||||
.add_directive(
|
||||
format!("actix_web={}", app_config.rust_log)
|
||||
.parse()
|
||||
.unwrap_or(default_directive)
|
||||
)
|
||||
.add_directive(
|
||||
"sqlx=warn".parse()
|
||||
.expect("sqlx=warn 是合法的日志指令")
|
||||
)
|
||||
)
|
||||
.with_target(true)
|
||||
.with_thread_ids(false) // 生产环境可开启
|
||||
|
||||
Reference in New Issue
Block a user