Skip to content

fix: security improvements and error handling fixes#105

Open
hobostay wants to merge 1 commit into666ghj:mainfrom
hobostay:fix/security-improvements
Open

fix: security improvements and error handling fixes#105
hobostay wants to merge 1 commit into666ghj:mainfrom
hobostay:fix/security-improvements

Conversation

@hobostay
Copy link

@hobostay hobostay commented Mar 9, 2026

问题概述

这个PR修复了项目中发现的多个安全问题和代码质量问题。

安全修复

  1. 硬编码的SECRET_KEY - backend/app/config.py

    • 之前:使用硬编码的'mirofish-secret-key'作为默认值
    • 现在:如果未设置环境变量,会生成随机密钥并发出警告
  2. DEBUG模式默认为True - backend/app/config.py

    • 之前:DEBUG默认为True
    • 现在:DEBUG默认为False,生产环境更安全
  3. CORS配置允许所有来源 - backend/app/__init__.py

    • 之前:CORS(app, resources={r"/api/*": {"origins": "*"}})
    • 现在:通过环境变量CORS_ALLOWED_ORIGINS配置,默认只允许localhost
  4. 错误响应泄露traceback - 所有API文件

    • 之前:所有API错误响应都包含完整的traceback
    • 现在:只在DEBUG模式下返回traceback

代码质量改进

  • 新增backend/app/utils/error_handler.py - 统一的错误处理工具
  • 修复file_parser.py中的空异常处理器,添加了适当的日志
  • 更新.env.example添加新的安全配置选项

文件修改

.env.example                       | 11 +++++++
backend/app/__init__.py            | 18 +++++++++--
backend/app/api/graph.py           | 40 ++++++++++--------------
backend/app/api/report.py          | 34 ++++++++++----------
backend/app/api/simulation.py      | 61 ++++++++++++++++++------------------
backend/app/config.py              | 20 +++++++++---
backend/app/utils/__init__.py      |  3 +-
backend/app/utils/error_handler.py | 63 ++++++++++++++++++++++++++++++++++++++
backend/app/utils/file_parser.py   | 13 +++++---
9 files changed, 182 insertions(+), 81 deletions(-)

检查清单

  • 代码遵循项目风格
  • 修改已测试
  • 更新了相关文档(.env.example)
  • 提交信息清晰描述了变更

This commit addresses several security and code quality issues:

**Security Fixes:**
- Fix hardcoded SECRET_KEY in config.py - now generates a random key if not set, with a warning
- Fix DEBUG mode default from True to False for production safety
- Add proper CORS origin validation instead of allowing all origins
- Remove traceback from API error responses to prevent information disclosure
- Add CORS_ALLOWED_ORIGINS configuration to .env.example

**Code Quality Improvements:**
- Add new error_handler.py utility for consistent error responses
- Replace empty exception handlers in file_parser.py with proper logging
- Update all API routes to use centralized error handling
- Only expose tracebacks in DEBUG mode

**Files Changed:**
- backend/app/config.py: SECRET_KEY and DEBUG defaults
- backend/app/__init__.py: CORS configuration improvements
- backend/app/utils/error_handler.py: new centralized error handling
- backend/app/utils/file_parser.py: improved exception logging
- backend/app/api/*.py: removed traceback from client responses
- .env.example: added new security configuration options

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. documentation Improvements or additions to documentation labels Mar 9, 2026
@JasonOA888
Copy link

代码审查反馈

优秀的PR!这些安全修复非常关键,特别是生产环境部署时。

几个建议:

  1. SECRET_KEY随机生成 - 建议添加日志记录生成的key,方便调试但不要泄露到错误响应中

  2. CORS配置 - 考虑添加CORS_ALLOW_METHODSCORS_ALLOW_HEADERS配置,提供更细粒度的控制

  3. error_handler.py - 建议添加自定义异常类型,让API可以抛出特定错误而不是通用Exception

  4. DEBUG模式 - 建议在config.py中添加assert not (DEBUG and not os.getenv("FLASK_ENV") == "development")防止误操作

这些都是小改进,整体PR质量很高,可以直接合并!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants