v8.1.7: Update service action routing and validation logic

Revised the action route to use a restricted list of allowed actions via the `any` converter. Improved service name validation by refining the regex pattern and adjusted logging message formatting for consistency.
pull/418/head v8.1.7
Aidaho 2025-04-10 15:45:14 +03:00
parent 60044a8cff
commit c153da9842
2 changed files with 3 additions and 5 deletions

View File

@ -100,7 +100,7 @@ def action_service(action: str, service: str) -> str:
'stop': 'disable --now',
'restart': 'restart',
}
if not re.match(r'^[a-zA-Z0-9\.\-]+$', service):
if not re.match(r'^[a-zA-Z0-9._-]+$', service):
return f"Invalid service name: {service}. Only alphanumeric characters, dots, and hyphens are allowed."
cmd = f"sudo systemctl {actions[action]} {service}"
if not roxy_sql.get_user().Status:
@ -109,7 +109,7 @@ def action_service(action: str, service: str) -> str:
if is_in_docker:
cmd = f"sudo supervisorctl {action} {service}"
os.system(cmd)
roxywi_common.logging('Roxy-WI server', f' The service {service} has been {action}ed', roxywi=1, login=1)
roxywi_common.logging('Roxy-WI server', f'The service {service} has been {action}ed', roxywi=1, login=1)
return 'ok'

View File

@ -85,11 +85,9 @@ def update_tools(service):
return f'error: {e}'
@bp.route('/tools/action/<service>/<action>')
@bp.route('/tools/action/<service>/<any(start, stop, restart):action>')
def action_tools(service, action):
roxywi_auth.page_for_admin()
if action not in ('start', 'stop', 'restart'):
return 'error: wrong action'
return roxy.action_service(action, service)