security: CORS 白名单移除 localhost 并改为 config.ini 驱动

扫描发现生产 CORS 含 http://localhost:9200(硬编码于 fsys.py),且 config.ini 的 cors 段(通配符 *)从未被读取。白名单改为从 config.ini 读取,生产值仅 qsont.me/yhdet.top,localhost 仅限本地开发显式配置。
This commit is contained in:
qing committed 2026-07-28 11:40:19 +08:00
1 parent 7726857d6b
commit 4d16bc32fc
2 files changed
+11 -3

No files matched your search

+4 -1
View File
@@ -11,4 +11,7 @@ cert_file = cert.pem
key_file = key.pem
[cors]
allowed_origins = *
# 允许跨域的 Origin 白名单(逗号分隔)
# 注意:生产环境请勿包含 localhost / 127.0.0.1 / 通配符 *
# 本地开发联调时可临时追加 http://localhost:9200,调试完务必移除
allowed_origins = https://qsont.me,https://yhdet.top
+7 -2
View File
@@ -38,7 +38,6 @@ parser.add_argument('--config', type=str, default='config.ini', help='Configurat
args = parser.parse_args()
app = Flask(__name__)
CORS(app, origins=["https://qsont.me", "https://yhdet.top", "http://localhost:9200"])
apply_security_headers(app)
# ==================== 配置文件加载 ====================
@@ -50,17 +49,23 @@ file_config = {
'server': {'host': '0.0.0.0', 'port': '5000'},
'directory': {'root': './public'},
'ssl': {'enabled': 'false', 'cert_file': 'cert.pem', 'key_file': 'key.pem'},
# CORS 白名单(逗号分隔),生产环境默认值;localhost 仅限本地开发时显式写入 config.ini
'cors': {'allowed_origins': 'https://qsont.me,https://yhdet.top'},
}
if os.path.exists(CONFIG_FILE):
config_parser = configparser.ConfigParser()
config_parser.read(CONFIG_FILE, encoding='utf-8')
for section in ['server', 'directory', 'ssl']:
for section in ['server', 'directory', 'ssl', 'cors']:
if section in config_parser:
for key, value in config_parser[section].items():
if key in file_config[section]:
file_config[section][key] = value
# ==================== CORS(配置加载完成后初始化) ====================
_cors_origins = [o.strip() for o in file_config['cors']['allowed_origins'].split(',') if o.strip()]
CORS(app, origins=_cors_origins)
HTML_ROOT_DIR = file_config['directory']['root']
if HTML_ROOT_DIR and not os.path.isabs(HTML_ROOT_DIR):
HTML_ROOT_DIR = os.path.join(BASE_DIR, HTML_ROOT_DIR)