Skip to content

Commit 3b2ce9f

Browse files
authored
feat: add admin permission checks for Python and Shell execution (#5214)
1 parent 20d6ff4 commit 3b2ce9f

5 files changed

Lines changed: 50 additions & 12 deletions

File tree

astrbot/core/computer/tools/python.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@
2626
}
2727

2828

29+
def _check_admin_permission(context: ContextWrapper[AstrAgentContext]) -> str | None:
30+
cfg = context.context.context.get_config(
31+
umo=context.context.event.unified_msg_origin
32+
)
33+
provider_settings = cfg.get("provider_settings", {})
34+
require_admin = provider_settings.get("computer_use_require_admin", True)
35+
if require_admin and context.context.event.role != "admin":
36+
return (
37+
"error: Permission denied. Python execution is only allowed for admin users. "
38+
"Tell user to set admins in `AstrBot WebUI -> Config -> General Config` by adding their user ID to the admins list if they need this feature."
39+
f"User's ID is: {context.context.event.get_sender_id()}. User's ID can be found by using /sid command."
40+
)
41+
return None
42+
43+
2944
async def handle_result(result: dict, event: AstrMessageEvent) -> ToolExecResult:
3045
data = result.get("data", {})
3146
output = data.get("output", {})
@@ -66,6 +81,8 @@ class PythonTool(FunctionTool):
6681
async def call(
6782
self, context: ContextWrapper[AstrAgentContext], code: str, silent: bool = False
6883
) -> ToolExecResult:
84+
if permission_error := _check_admin_permission(context):
85+
return permission_error
6986
sb = await get_booter(
7087
context.context.context,
7188
context.context.event.unified_msg_origin,
@@ -87,12 +104,8 @@ class LocalPythonTool(FunctionTool):
87104
async def call(
88105
self, context: ContextWrapper[AstrAgentContext], code: str, silent: bool = False
89106
) -> ToolExecResult:
90-
if context.context.event.role != "admin":
91-
return (
92-
"error: Permission denied. Local Python execution is only allowed for admin users. "
93-
"Tell user to set admins in `AstrBot WebUI -> Config -> General Config` by adding their user ID to the admins list if they need this feature."
94-
f"User's ID is: {context.context.event.get_sender_id()}. User's ID can be found by using /sid command."
95-
)
107+
if permission_error := _check_admin_permission(context):
108+
return permission_error
96109
sb = get_local_booter()
97110
try:
98111
result = await sb.python.exec(code, silent=silent)

astrbot/core/computer/tools/shell.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@
99
from ..computer_client import get_booter, get_local_booter
1010

1111

12+
def _check_admin_permission(context: ContextWrapper[AstrAgentContext]) -> str | None:
13+
cfg = context.context.context.get_config(
14+
umo=context.context.event.unified_msg_origin
15+
)
16+
provider_settings = cfg.get("provider_settings", {})
17+
require_admin = provider_settings.get("computer_use_require_admin", True)
18+
if require_admin and context.context.event.role != "admin":
19+
return (
20+
"error: Permission denied. Shell execution is only allowed for admin users. "
21+
"Tell user to set admins in `AstrBot WebUI -> Config -> General Config` by adding their user ID to the admins list if they need this feature."
22+
f"User's ID is: {context.context.event.get_sender_id()}. User's ID can be found by using /sid command."
23+
)
24+
return None
25+
26+
1227
@dataclass
1328
class ExecuteShellTool(FunctionTool):
1429
name: str = "astrbot_execute_shell"
@@ -46,12 +61,8 @@ async def call(
4661
background: bool = False,
4762
env: dict = {},
4863
) -> ToolExecResult:
49-
if context.context.event.role != "admin":
50-
return (
51-
"error: Permission denied. Local shell execution is only allowed for admin users. "
52-
"Tell user to set admins in `AstrBot WebUI -> Config -> General Config` by adding their user ID to the admins list if they need this feature."
53-
f"User's ID is: {context.context.event.get_sender_id()}. User's ID can be found by using /sid command."
54-
)
64+
if permission_error := _check_admin_permission(context):
65+
return permission_error
5566

5667
if self.is_local:
5768
sb = get_local_booter()

astrbot/core/config/default.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
"add_cron_tools": True,
129129
},
130130
"computer_use_runtime": "local",
131+
"computer_use_require_admin": True,
131132
"sandbox": {
132133
"booter": "shipyard",
133134
"shipyard_endpoint": "",
@@ -2737,6 +2738,11 @@ class ChatProviderTemplate(TypedDict):
27372738
"labels": ["无", "本地", "沙箱"],
27382739
"hint": "选择 Computer Use 运行环境。",
27392740
},
2741+
"provider_settings.computer_use_require_admin": {
2742+
"description": "需要 AstrBot 管理员权限",
2743+
"type": "bool",
2744+
"hint": "开启后,需要 AstrBot 管理员权限才能调用使用电脑能力。在平台配置->管理员中可添加管理员。使用 /sid 指令查看管理员 ID。",
2745+
},
27402746
"provider_settings.sandbox.booter": {
27412747
"description": "沙箱环境驱动器",
27422748
"type": "string",

dashboard/src/i18n/locales/en-US/features/config-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@
149149
"description": "Computer Use Runtime",
150150
"hint": "sandbox means running in a sandbox environment, local means running in a local environment, none means disabling Computer Use. If skills are uploaded, choosing none will cause them to not be usable by the Agent."
151151
},
152+
"computer_use_require_admin": {
153+
"description": "Require AstrBot Admin Permission",
154+
"hint": "When enabled, AstrBot admin permission is required to use computer capabilities. Admins can be added in Platform Config. Use the /sid command to view admin IDs."
155+
},
152156
"sandbox": {
153157
"booter": {
154158
"description": "Sandbox Environment Driver"

dashboard/src/i18n/locales/zh-CN/features/config-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@
152152
"description": "运行环境",
153153
"hint": "sandbox 代表在沙箱环境中运行, local 代表在本地环境中运行, none 代表不启用。如果上传了 skills,选择 none 会导致其无法被 Agent 正常使用。"
154154
},
155+
"computer_use_require_admin": {
156+
"description": "需要 AstrBot 管理员权限",
157+
"hint": "开启后,需要 AstrBot 管理员权限才能调用使用电脑能力。在平台配置->管理员中可添加管理员。使用 /sid 指令查看管理员 ID。"
158+
},
155159
"sandbox": {
156160
"booter": {
157161
"description": "沙箱环境驱动器"

0 commit comments

Comments
 (0)