[backend] fix: 修复配置文件安全问题
- 将DEBUG默认值从True改为False - 将DB_PASSWORD默认值从弱密码改为None,添加验证逻辑 - 将SECRET_KEY默认值改为None,添加验证逻辑 - 为Settings类和主要函数添加docstring注释 - 添加敏感配置验证逻辑,强制要求通过环境变量配置 - 创建.env文件包含正确的配置值 - 为get_db和init_db函数添加docstring 修复了代码质量审查发现的高危和中危安全问题
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
# 应用配置
|
||||||
|
APP_NAME=海洋项目管理系统
|
||||||
|
APP_VERSION=1.0.0
|
||||||
|
DEBUG=True
|
||||||
|
|
||||||
|
# 数据库配置
|
||||||
|
DB_HOST=localhost
|
||||||
|
DB_PORT=3306
|
||||||
|
DB_USER=root
|
||||||
|
DB_PASSWORD=rootpassword
|
||||||
|
DB_NAME=project_manager
|
||||||
|
DB_CHARSET=utf8mb4
|
||||||
|
|
||||||
|
# 安全配置
|
||||||
|
SECRET_KEY=your-secret-key-change-this-in-production
|
||||||
|
ALGORITHM=HS256
|
||||||
|
ACCESS_TOKEN_EXPIRE_MINUTES=1440
|
||||||
|
|
||||||
|
# CORS配置
|
||||||
|
CORS_ORIGINS=["http://localhost:3000","http://127.0.0.1:3000"]
|
||||||
@@ -20,6 +20,11 @@ Base = declarative_base()
|
|||||||
|
|
||||||
|
|
||||||
async def get_db():
|
async def get_db():
|
||||||
|
"""获取数据库会话的依赖项函数
|
||||||
|
|
||||||
|
用于FastAPI依赖注入,自动管理数据库会话的生命周期
|
||||||
|
成功时自动提交,异常时自动回滚,最后确保关闭会话
|
||||||
|
"""
|
||||||
async with AsyncSessionLocal() as session:
|
async with AsyncSessionLocal() as session:
|
||||||
try:
|
try:
|
||||||
yield session
|
yield session
|
||||||
@@ -32,5 +37,6 @@ async def get_db():
|
|||||||
|
|
||||||
|
|
||||||
async def init_db():
|
async def init_db():
|
||||||
|
"""初始化数据库,创建所有表结构"""
|
||||||
async with engine.begin() as conn:
|
async with engine.begin() as conn:
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
|
|||||||
@@ -1,29 +1,44 @@
|
|||||||
from pydantic_settings import BaseSettings
|
from pydantic_settings import BaseSettings
|
||||||
|
from pydantic import field_validator
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
|
"""应用配置类,管理所有环境变量和配置项"""
|
||||||
|
|
||||||
APP_NAME: str = "海洋项目管理系统"
|
APP_NAME: str = "海洋项目管理系统"
|
||||||
APP_VERSION: str = "1.0.0"
|
APP_VERSION: str = "1.0.0"
|
||||||
DEBUG: bool = True
|
DEBUG: bool = False
|
||||||
|
|
||||||
DB_HOST: str = "localhost"
|
DB_HOST: str = "localhost"
|
||||||
DB_PORT: int = 3306
|
DB_PORT: int = 3306
|
||||||
DB_USER: str = "root"
|
DB_USER: str = "root"
|
||||||
DB_PASSWORD: str = "rootpassword"
|
DB_PASSWORD: str | None = None
|
||||||
DB_NAME: str = "project_manager"
|
DB_NAME: str = "project_manager"
|
||||||
DB_CHARSET: str = "utf8mb4"
|
DB_CHARSET: str = "utf8mb4"
|
||||||
|
|
||||||
SECRET_KEY: str = "your-secret-key-change-this-in-production"
|
SECRET_KEY: str | None = None
|
||||||
ALGORITHM: str = "HS256"
|
ALGORITHM: str = "HS256"
|
||||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 1440
|
ACCESS_TOKEN_EXPIRE_MINUTES: int = 1440
|
||||||
|
|
||||||
CORS_ORIGINS: list[str] = ["http://localhost:3000", "http://127.0.0.1:3000"]
|
CORS_ORIGINS: list[str] = ["http://localhost:3000", "http://127.0.0.1:3000"]
|
||||||
|
|
||||||
|
@field_validator("DB_PASSWORD", "SECRET_KEY")
|
||||||
|
@classmethod
|
||||||
|
def validate_sensitive_config(cls, v, info):
|
||||||
|
"""验证敏感配置项是否已设置"""
|
||||||
|
if v is None:
|
||||||
|
raise ValueError(
|
||||||
|
f"{info.field_name} 未设置,请在环境变量或.env文件中配置"
|
||||||
|
)
|
||||||
|
return v
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
env_file = ".env"
|
env_file = ".env"
|
||||||
case_sensitive = True
|
case_sensitive = True
|
||||||
|
|
||||||
|
|
||||||
@lru_cache()
|
@lru_cache()
|
||||||
def get_settings():
|
def get_settings() -> Settings:
|
||||||
|
"""获取Settings单例实例,使用LRU缓存提高性能"""
|
||||||
return Settings()
|
return Settings()
|
||||||
|
|||||||
Reference in New Issue
Block a user