35 lines
2.4 KiB
Python
35 lines
2.4 KiB
Python
"""Describe exactly the request being approved; never trust model risk prose."""
|
||
import json
|
||
import re
|
||
from .approval_store import ApprovalError
|
||
|
||
|
||
def describe_action(action):
|
||
if (not isinstance(action,dict)or set(action)!={'kind','method','path','params','body'}
|
||
or action['kind']!='work-api-v1' or action['method']not in ('GET','POST','PUT','PATCH','DELETE')
|
||
or not isinstance(action['path'],str)or len(action['path'])>1500
|
||
or not re.fullmatch(r'/open-apis/[A-Za-z0-9_./@!:$-]+',action['path'])
|
||
or '..'in action['path']or '//'in action['path']
|
||
or not isinstance(action['params'],dict)or not isinstance(action['body'],dict)):
|
||
raise ApprovalError('unsupported_approval_action')
|
||
encoded=json.dumps(action,sort_keys=True,ensure_ascii=True,separators=(',',':'),allow_nan=False).encode()
|
||
if len(encoded)>32000:raise ApprovalError('operation_budget_exceeded')
|
||
parts=action['path'].split('/');namespace=parts[2]
|
||
targets={'calendar':'日历','contact':'通讯录','search':'搜索','drive':'云盘',
|
||
'docx':'文档','wiki':'知识库','bitable':'多维表格','task':'任务','sheets':'电子表格','vc':'会议'}
|
||
if namespace not in targets:raise ApprovalError('unsupported_approval_action')
|
||
method=action['method'];label=targets[namespace]
|
||
verb={'GET':'查询','POST':'提交','PUT':'更新','PATCH':'修改','DELETE':'删除'}[method]
|
||
subject=next((action['body'][key].strip()[:80]for key in ('summary','title','name')
|
||
if isinstance(action['body'].get(key),str)and action['body'][key].strip()),'')
|
||
if any(ord(char)<32 for char in subject):subject=''
|
||
summary=verb+label+'中的指定数据'+(':'+subject if subject else'')
|
||
resource='/'.join(parts[4:])
|
||
if len(resource)>300:raise ApprovalError('approval_target_display_budget_exceeded')
|
||
impact=('本次是查询,但该接口尚未列入自动放行范围。'if method=='GET'else
|
||
'可能修改平台数据或触发通知;已经发生的操作不能保证自动撤销。')
|
||
if method=='DELETE':impact='会删除指定数据或移除关联,可能影响其他人,且不一定能够恢复。'
|
||
if re.search(r'/(permissions|members|collaborators|share)(/|$)',action['path']):
|
||
impact='可能改变其他人的访问权限或成员关系,请核对授权范围。'
|
||
return summary,impact+'\n目标范围:'+label+';目标标识:'+resource
|