Files
debt-manager/backend/app/main.py
OP 498d29dfa9 security: 全面安全加固 v2.1
严重修复:
- C1: JWT密钥生成64字节随机字符串替代硬编码占位符
- C2: 数据库密码改为随机强密码,移除宿主机端口映射
- C3: 管理员默认密码改为强密码

高危修复:
- H1: 移除数据库54326端口映射,仅内部网络访问
- H3: 添加X-Frame-Options/CSP/XSS-Protection等安全头
- H4: 前端所有innerHTML拼接处添加escapeHtml转义防XSS
- H5: SMTP密码在API响应中脱敏显示
- H6: 债务列表接口添加用户隔离,普通用户只能看自己的数据
- H7: 债务详情/计划/记录接口添加归属校验
- H8: 账户级暴力破解锁定(5次失败锁定5分钟)
- H9: 速率限制数据增加过期清理机制防内存泄漏

中危修复:
- M1: 注册/重置密码接口添加后端密码强度校验(最少8位)
- M4: 添加全局异常处理器,500错误不再暴露堆栈
- M5: Docker容器改为非root用户运行
- M6: 添加.dockerignore排除pyc和敏感文件
- M7: 管理员操作写入审计日志
- M8: CORS限制为实际使用的HTTP方法和头

其他:
- Nginx隐藏版本号(server_tokens off)
- 管理员角色修改添加枚举校验
- 密码重置验证码接口返回统一错误信息
2026-06-28 22:46:24 +08:00

157 lines
5.4 KiB
Python

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from contextlib import asynccontextmanager
from app.core.config import get_settings
from app.core.database import engine, async_session
from app.models.models import Base, User, Debt, RepaymentPlan, RepaymentRecord, Group
from app.core.security import hash_password
from app.api import auth, debts, repayments, admin, loans, notifications
from sqlalchemy import select, text
settings = get_settings()
METHOD_REV = {"equal_installment": "e", "interest_first": "p", "lump_sum": "i"}
async def seed_admin():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async with async_session() as db:
result = await db.execute(select(User))
existing = result.scalars().all()
if not existing:
admin_user = User(
email="admin",
password_hash=hash_password(settings.ADMIN_PASSWORD),
nickname="管理员",
role="admin",
)
db.add(admin_user)
test_user = User(
email="test",
password_hash=hash_password("test123"),
nickname="测试用户",
role="user",
)
db.add(test_user)
await db.commit()
async def migrate_account_names():
async with async_session() as db:
result = await db.execute(select(User))
users = result.scalars().all()
for u in users:
if "@" in u.email:
new_email = u.email.split("@")[0]
existing = await db.execute(select(User).where(User.email == new_email, User.id != u.id))
if not existing.scalar_one_or_none():
u.email = new_email
await db.commit()
async def migrate_add_real_email():
async with engine.begin() as conn:
await conn.execute(text(
"ALTER TABLE users ADD COLUMN IF NOT EXISTS real_email VARCHAR(255) DEFAULT ''"
))
async def migrate_debt_data():
async with async_session() as db:
result = await db.execute(select(Debt).where(Debt.schedule_data == None))
debts = result.scalars().all()
for debt in debts:
plans_result = await db.execute(
select(RepaymentPlan).where(RepaymentPlan.debt_id == debt.id).order_by(RepaymentPlan.due_date)
)
plans = plans_result.scalars().all()
schedule = []
paid_periods = []
for p in plans:
item = {
"p": p.principal_due / 100,
"date": p.due_date.strftime("%Y-%m-%d") if p.due_date else "",
"pay": p.total_due / 100,
"pr": p.principal_due / 100,
"it": p.interest_due / 100,
"rp": 0,
}
schedule.append(item)
if p.status == "paid":
paid_periods.append(len(schedule))
remaining = debt.amount / 100
for item in schedule:
item["rp"] = round(remaining, 2)
remaining -= item["pr"]
debt.schedule_data = schedule
debt.paid_periods = paid_periods
await db.commit()
async def migrate_account_names():
async with async_session() as db:
result = await db.execute(select(User))
users = result.scalars().all()
for u in users:
if "@" in u.email:
u.email = u.email.split("@")[0]
await db.commit()
@asynccontextmanager
async def lifespan(app: FastAPI):
await seed_admin()
await migrate_add_real_email()
await migrate_account_names()
await migrate_debt_data()
from app.services.scheduler import start_scheduler, check_upcoming_repayments
start_scheduler()
await check_upcoming_repayments()
yield
from app.services.scheduler import scheduler
scheduler.shutdown()
app = FastAPI(title=settings.PROJECT_NAME, lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS,
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE", "PATCH"],
allow_headers=["Authorization", "Content-Type"],
)
@app.middleware("http")
async def rate_limit_middleware(request: Request, call_next):
from app.core.rate_limit import is_ip_blocked, get_remaining_seconds
ip = request.client.host if request.client else "unknown"
if is_ip_blocked(ip):
remaining = get_remaining_seconds(ip)
return JSONResponse(status_code=429, content={"detail": f"登录失败次数过多,请 {remaining} 秒后再试", "retry_after": remaining})
return await call_next(request)
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
return JSONResponse(status_code=500, content={"detail": "服务器内部错误"})
app.include_router(auth.router, prefix=settings.API_V1_PREFIX)
app.include_router(loans.router, prefix=settings.API_V1_PREFIX)
app.include_router(debts.router, prefix=settings.API_V1_PREFIX)
app.include_router(repayments.router, prefix=settings.API_V1_PREFIX)
app.include_router(admin.router, prefix=settings.API_V1_PREFIX)
app.include_router(notifications.router, prefix=settings.API_V1_PREFIX)
@app.get("/health")
async def health():
return {"status": "ok"}