StarrySkyWorld commited on
Commit
cf78e0e
·
verified ·
1 Parent(s): 128775e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ from fastapi import FastAPI, HTTPException
3
+ from pydantic import BaseModel, Field
4
+ from typing import Optional
5
+ import subprocess
6
+ import textwrap
7
+ import ast
8
+ import resource
9
+ import os
10
+
11
+ app = FastAPI(title="Python Exec Service (PES) on HuggingFace")
12
+
13
+ # ----------------- 配置 -----------------
14
+ MAX_CODE_LENGTH = 20_000 # 代码最大长度
15
+ MAX_TIMEOUT = 20 # 固定最长 20 秒(由 PES 控制)
16
+ MAX_MEMORY_MB = 256 # 子进程最大内存
17
+ MAX_OUTPUT_SIZE = 4 * 1024 * 1024 # 4MB 输出限制
18
+
19
+ # ----------------- 请求 / 响应模型 -----------------
20
+ class ExecRequest(BaseModel):
21
+ code: str = Field(..., description="要执行的 Python 代码")
22
+ stdin: Optional[str] = Field("", description="传给程序的标准输入")
23
+
24
+ class ExecResponse(BaseModel):
25
+ stdout: str
26
+ stderr: str
27
+ return_code: Optional[int]
28
+ timeout: bool
29
+
30
+ # ----------------- 安全检查:AST 审计 -----------------
31
+ DANGEROUS_NAMES = {
32
+ "__import__", "eval", "exec", "open", "compile",
33
+ "input", "globals", "locals", "vars",
34
+ "os", "sys", "subprocess", "socket",
35
+ "shutil", "pathlib"
36
+ }
37
+
38
+ DANGEROUS_MODULES = {
39
+ "os", "sys", "subprocess", "socket", "shutil",
40
+ "pathlib", "threading", "multiprocessing"
41
+ }
42
+
43
+ class SafeVisitor(ast.NodeVisitor):
44
+ def visit_Import(self, node):
45
+ for alias in node.names:
46
+ if alias.name.split('.')[0] in DANGEROUS_MODULES:
47
+ raise ValueError(f"禁止导入模块: {alias.name}")
48
+ self.generic_visit(node)
49
+
50
+ def visit_ImportFrom(self, node):
51
+ if node.module and node.module.split('.')[0] in DANGEROUS_MODULES:
52
+ raise ValueError(f"禁止 from 导入模块: {node.module}")
53
+ self.generic_visit(node)
54
+
55
+ def visit_Attribute(self, node):
56
+ # 禁止访问 __dict__ / __class__ / __globals__ 等双下划线属性
57
+ if isinstance(node.attr, str) and node.attr.startswith("__"):
58
+ raise ValueError(f"禁止访问特殊属性: {node.attr}")
59
+ self.generic_visit(node)
60
+
61
+ def visit_Name(self, node):
62
+ if node.id in DANGEROUS_NAMES:
63
+ raise ValueError(f"禁止使用名称: {node.id}")
64
+ self.generic_visit(node)
65
+
66
+ def static_security_check(code: str):
67
+ try:
68
+ tree = ast.parse(code, mode="exec")
69
+ except SyntaxError as e:
70
+ raise ValueError(f"语法错误: {e}")
71
+ SafeVisitor().visit(tree)
72
+
73
+ # ----------------- 资源限制(子进程 preexec_fn) -----------------
74
+ def set_limits():
75
+ # CPU 时间限制(秒)—— hard limit MAX_TIMEOUT
76
+ resource.setrlimit(resource.RLIMIT_CPU, (MAX_TIMEOUT, MAX_TIMEOUT))
77
+ # 地址空间限制(内存)
78
+ max_bytes = MAX_MEMORY_MB * 1024 * 1024
79
+ resource.setrlimit(resource.RLIMIT_AS, (max_bytes, max_bytes))
80
+ # 文件大小限制(防止写超大文件)
81
+ resource.setrlimit(resource.RLIMIT_FSIZE, (MAX_OUTPUT_SIZE, MAX_OUTPUT_SIZE))
82
+
83
+ # ----------------- 核心执行 API -----------------
84
+ @app.post("/execute", response_model=ExecResponse)
85
+ def execute(req: ExecRequest):
86
+ # 1. 限制代码长度
87
+ if len(req.code) > MAX_CODE_LENGTH:
88
+ raise HTTPException(status_code=400, detail="代码过长,超过限制")
89
+
90
+ # 2. 去掉多余缩进
91
+ code = textwrap.dedent(req.code)
92
+
93
+ # 3. 静态安全检查(AST)
94
+ try:
95
+ static_security_check(code)
96
+ except ValueError as e:
97
+ raise HTTPException(status_code=400, detail=f"安全检查不通过: {e}")
98
+
99
+ # 4. 执行:PES 仅提供命令参数,不允许外部控制 timeout
100
+ try:
101
+ proc = subprocess.run(
102
+ ["python", "-c", code],
103
+ input=req.stdin.encode("utf-8") if req.stdin else None,
104
+ capture_output=True,
105
+ timeout=MAX_TIMEOUT, # 固定 20 秒
106
+ preexec_fn=set_limits # 资源限制
107
+ )
108
+ stdout = proc.stdout[:MAX_OUTPUT_SIZE].decode("utf-8", errors="replace")
109
+ stderr = proc.stderr[:MAX_OUTPUT_SIZE].decode("utf-8", errors="replace")
110
+
111
+ return ExecResponse(
112
+ stdout=stdout,
113
+ stderr=stderr,
114
+ return_code=proc.returncode,
115
+ timeout=False
116
+ )
117
+ except subprocess.TimeoutExpired as e:
118
+ # 子进程超时
119
+ stdout = (e.stdout or b"")[:MAX_OUTPUT_SIZE].decode("utf-8", errors="replace")
120
+ stderr = (e.stderr or b"")[:MAX_OUTPUT_SIZE].decode("utf-8", errors="replace")
121
+ if not stderr:
122
+ stderr = "Process timed out"
123
+ return ExecResponse(
124
+ stdout=stdout,
125
+ stderr=stderr,
126
+ return_code=None,
127
+ timeout=True
128
+ )
129
+ except Exception as e:
130
+ raise HTTPException(status_code=500, detail=f"执行失败: {e}")
131
+
132
+ # 本地调试用,HF Space 也可以用这个启动命令
133
+ if __name__ == "__main__":
134
+ import uvicorn
135
+ port = int(os.environ.get("PORT", 7860))
136
+ uvicorn.run("app:app", host="0.0.0.0", port=port)