From 6e14ee12a3e9c39fe887b9073cd8df186e931426 Mon Sep 17 00:00:00 2001 From: xiaoli-cli Date: Sat, 25 Apr 2026 21:28:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=B2=99=E7=AE=B1?= =?UTF-8?q?=E5=9C=A8=E6=97=A0=E5=AE=9E=E4=BE=8B=E8=B0=83=E7=94=A8=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E5=B1=9E=E6=80=A7=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - _execute_code_safely: 用getattr安全访问self属性 - _execute_code_safely: 导入traceback为_traceback避免命名冲突 - _sandbox_set_limits: 改用AICLI类方法引用 --- ai_cli.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ai_cli.py b/ai_cli.py index ad59396..7a202ab 100644 --- a/ai_cli.py +++ b/ai_cli.py @@ -2066,6 +2066,7 @@ class AICLI: """ import subprocess import json as _json + import traceback as _traceback # 安全检查 safe, violations = _sandbox_checker.check(code) @@ -2078,7 +2079,8 @@ class AICLI: } vars_json = _json.dumps(variables or {}, ensure_ascii=False, default=str) - whitelist_json = _json.dumps(self.code_execution_whitelist or _SAFE_WHITELIST) + wl = getattr(self, 'code_execution_whitelist', None) or _SAFE_WHITELIST + whitelist_json = _json.dumps(wl) script = _make_sandbox_worker(code, vars_json, whitelist_json, _sandbox_work_dir) safe_env = { @@ -2099,7 +2101,7 @@ class AICLI: [sys.executable, '-c', script], capture_output=True, timeout=timeout, cwd=_sandbox_work_dir, env=safe_env, - preexec_fn=self._sandbox_set_limits if _HAS_RESOURCE else None, + preexec_fn=AICLI._sandbox_set_limits if _HAS_RESOURCE else None, ) elapsed = time.time() - start_time stdout = proc.stdout.decode('utf-8', errors='replace')[:1000000] @@ -2134,7 +2136,7 @@ class AICLI: return { 'success': False, 'result': f'代码执行异常: {str(e)}', - 'stdout': '', 'stderr': traceback.format_exc(), + 'stdout': '', 'stderr': _traceback.format_exc(), 'execution_time': time.time() - start_time, 'memory_usage': 0 }