feat: deps越权防护(query/header与token不一致403) + resolve_entity_for_request + 任务⑥开发规范沉淀
This commit is contained in:
@@ -30,6 +30,14 @@ def get_entity_id(
|
||||
if auth.startswith("Bearer "):
|
||||
token_entity = get_token_entity_id(auth[7:])
|
||||
if token_entity is not None:
|
||||
# 越权防护:显式传入的 query/header entity_id 与 token 绑定不一致 → 403
|
||||
explicit = None
|
||||
if entity_id is not None:
|
||||
explicit = entity_id
|
||||
elif x_entity_id and x_entity_id.isdigit():
|
||||
explicit = int(x_entity_id)
|
||||
if explicit is not None and explicit != token_entity:
|
||||
raise HTTPException(403, f"无权访问企业 entity_id={explicit}(当前账套: {token_entity})")
|
||||
return token_entity
|
||||
# token存在但是旧格式/无entity → 账套模式下强制走白名单或默认(由require_auth拦截)
|
||||
# 这里不抛401:公开接口可能带旧token,交给require_auth统一处理
|
||||
@@ -58,3 +66,16 @@ def get_entity_id(
|
||||
raise HTTPException(403, f"企业 entity_id={candidate} 不存在或未激活")
|
||||
|
||||
return 1 # 默认酣客(无token、无参数时兜底,兼容存量公开接口)
|
||||
|
||||
|
||||
def resolve_entity_for_request(request: Request, fallback: int = 1) -> int:
|
||||
"""账套模式:请求级entity解析(供从body读取entity_id的接口使用)
|
||||
登录用户(带Bearer token)→ token绑定的entity(唯一来源)
|
||||
无token(Bot服务通道)→ 回退到调用方传入的fallback(body中的entity_id等)
|
||||
"""
|
||||
auth = request.headers.get("Authorization", "")
|
||||
if auth.startswith("Bearer "):
|
||||
token_entity = get_token_entity_id(auth[7:])
|
||||
if token_entity is not None:
|
||||
return token_entity
|
||||
return fallback
|
||||
|
||||
Reference in New Issue
Block a user