安全:增加测试批次上下文硬校验

This commit is contained in:
焦龙言
2026-09-22 15:11:53 +08:00
parent 17bfa2c5fe
commit 763ea1efcc
9 changed files with 666 additions and 4 deletions
@@ -4,6 +4,7 @@ from typing import Literal
from exceptions.exception import LoginException
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_business.domain.test_batch_scope import TestBatchScope
# 定义上下文变量
# 存储当前请求的编译后的排除路由模式列表
@@ -12,6 +13,8 @@ current_exclude_patterns: ContextVar[
] = ContextVar('current_exclude_patterns', default=None)
# 存储当前用户信息
current_user: ContextVar[CurrentUserModel | None] = ContextVar('current_user', default=None)
# 存储当前请求已校验的测试批次作用域
current_test_batch_scope: ContextVar[TestBatchScope | None] = ContextVar('current_test_batch_scope', default=None)
class RequestContext:
@@ -69,6 +72,21 @@ class RequestContext:
raise LoginException(data='', message='当前用户信息为空,请检查是否已登录')
return _current_user
@staticmethod
def set_current_test_batch_scope(scope: TestBatchScope) -> Token:
"""
设置当前请求已校验的测试批次作用域
:param scope: 测试批次作用域
:return: 上下文变量令牌,用于重置
"""
return current_test_batch_scope.set(scope)
@staticmethod
def get_current_test_batch_scope() -> TestBatchScope | None:
"""获取当前请求已校验的测试批次作用域。"""
return current_test_batch_scope.get()
@staticmethod
def reset_current_exclude_patterns(token: Token) -> None:
"""
@@ -87,6 +105,11 @@ class RequestContext:
"""
current_user.reset(token)
@staticmethod
def reset_current_test_batch_scope(token: Token) -> None:
"""重置当前请求的测试批次作用域。"""
current_test_batch_scope.reset(token)
@staticmethod
def clear_all() -> None:
"""
@@ -94,3 +117,4 @@ class RequestContext:
"""
current_exclude_patterns.set(None)
current_user.set(None)
current_test_batch_scope.set(None)