Skip to content

Commit 9a772f4

Browse files
committed
refac
1 parent 8c71897 commit 9a772f4

3 files changed

Lines changed: 24 additions & 20 deletions

File tree

backend/open_webui/routers/terminals.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from open_webui.models.users import Users
2121
from open_webui.utils.access_control import has_connection_access
2222
from open_webui.utils.auth import get_verified_user
23+
from open_webui.utils.terminals import get_terminal_server_url
2324
from open_webui.utils.tools import bearer_auth_header, normalize_bearer_token
2425
from starlette.background import BackgroundTask
2526

@@ -100,7 +101,7 @@ async def proxy_terminal(
100101
if not await has_connection_access(user, connection, user_group_ids):
101102
return JSONResponse({'error': 'Access denied'}, status_code=403)
102103

103-
base_url = (connection.get('url') or '').rstrip('/')
104+
base_url = get_terminal_server_url(connection)
104105
if not base_url:
105106
return JSONResponse({'error': 'Terminal server URL not configured'}, status_code=503)
106107

@@ -110,11 +111,6 @@ async def proxy_terminal(
110111

111112
target_url = f'{base_url}/{safe_path}'
112113

113-
# Route through orchestrator policy endpoint if policy_id is set
114-
policy_id = connection.get('policy_id')
115-
if policy_id:
116-
target_url = f'{base_url}/p/{policy_id}/{safe_path}'
117-
118114
if request.query_params:
119115
target_url += f'?{request.query_params}'
120116

@@ -272,16 +268,14 @@ async def ws_terminal(
272268
return
273269
user, connection = result
274270

275-
base_url = (connection.get('url') or '').rstrip('/')
271+
base_url = get_terminal_server_url(connection)
276272
if not base_url:
277273
await ws.close(code=4003, reason='Terminal server URL not configured')
278274
return
279275

280276
# Build upstream WebSocket URL (no token in URL)
281277
ws_base = base_url.replace('https://', 'wss://').replace('http://', 'ws://')
282278

283-
# Route through orchestrator policy endpoint if policy_id is set
284-
policy_id = connection.get('policy_id')
285279
upstream_params = {}
286280
# For orchestrator-backed servers, pass user_id
287281
upstream_params['user_id'] = user.id
@@ -292,10 +286,7 @@ async def ws_terminal(
292286
# decode depth) and inject an attacker-chosen user_id ahead of the one appended below.
293287
safe_session_id = urllib.parse.quote(session_id, safe='')
294288

295-
if policy_id:
296-
upstream_url = f'{ws_base}/p/{policy_id}/api/terminals/{safe_session_id}'
297-
else:
298-
upstream_url = f'{ws_base}/api/terminals/{safe_session_id}'
289+
upstream_url = f'{ws_base}/api/terminals/{safe_session_id}'
299290
if upstream_params:
300291
upstream_url += f'?{urllib.parse.urlencode(upstream_params)}'
301292

backend/open_webui/utils/automations.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from open_webui.utils.auth import create_token
3737
from open_webui.utils.misc import parse_duration
3838
from open_webui.utils.task import prompt_template
39+
from open_webui.utils.terminals import get_terminal_server_url
3940
from starlette.datastructures import Headers
4041

4142
log = logging.getLogger(__name__)
@@ -323,16 +324,11 @@ async def _set_terminal_cwd(app, server_id: str, user, cwd: str, chat_id: str) -
323324
log.warning(f'Terminal server {server_id} not found for CWD set')
324325
return
325326

326-
base_url = (connection.get('url') or '').rstrip('/')
327+
base_url = get_terminal_server_url(connection)
327328
if not base_url:
328329
return
329330

330-
# Build target URL — route through orchestrator policy if configured
331-
policy_id = connection.get('policy_id')
332-
if connection.get('server_type') == 'orchestrator' and policy_id:
333-
target_url = f'{base_url}/p/{policy_id}/files/cwd'
334-
else:
335-
target_url = f'{base_url}/files/cwd'
331+
target_url = f'{base_url}/files/cwd'
336332

337333
headers = {'Content-Type': 'application/json', 'X-User-Id': user.id}
338334
if chat_id:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Shared routing helpers for admin-configured terminal servers."""
2+
3+
from urllib.parse import quote
4+
5+
6+
def get_terminal_server_url(connection: dict) -> str:
7+
"""Return the upstream base URL for a terminal connection.
8+
9+
Only orchestrator connections with an explicit policy use the named-policy
10+
route. Direct Open Terminal connections and legacy unscoped orchestrator
11+
connections keep their existing root route.
12+
"""
13+
base_url = str(connection.get('url') or '').rstrip('/')
14+
policy_id = str(connection.get('policy_id') or '').strip()
15+
if connection.get('server_type') == 'orchestrator' and policy_id:
16+
return f'{base_url}/p/{quote(policy_id, safe="")}'
17+
return base_url

0 commit comments

Comments
 (0)