diff --git a/spug_api/apps/app/views.py b/spug_api/apps/app/views.py index d1c69ef..0e52b5d 100644 --- a/spug_api/apps/app/views.py +++ b/spug_api/apps/app/views.py @@ -12,6 +12,7 @@ from apps.setting.utils import AppSetting import subprocess import json import os +import re class AppView(View): @@ -30,7 +31,9 @@ class AppView(View): Argument('desc', required=False) ).parse(request.body) if error is None: - form.name = form.name.replace("'", '') + if not re.fullmatch(r'[-\w]+', form.key, re.ASCII): + return json_response(error='标识符必须为字母、数字、-和下划线的组合') + app = App.objects.filter(key=form.key).first() if app and app.id != form.id: return json_response(error=f'唯一标识符 {form.key} 已存在,请更改后重试') diff --git a/spug_api/apps/config/views.py b/spug_api/apps/config/views.py index 9f39a55..863ad14 100644 --- a/spug_api/apps/config/views.py +++ b/spug_api/apps/config/views.py @@ -7,6 +7,7 @@ from libs import json_response, JsonParser, Argument from apps.app.models import Deploy from apps.config.models import * import json +import re class EnvironmentView(View): @@ -25,7 +26,9 @@ class EnvironmentView(View): Argument('desc', required=False) ).parse(request.body) if error is None: - form.key = form.key.replace("'", '') + if not re.fullmatch(r'[-\w]+', form.key, re.ASCII): + return json_response(error='标识符必须为字母、数字、-和下划线的组合') + env = Environment.objects.filter(key=form.key).first() if env and env.id != form.id: return json_response(error=f'唯一标识符 {form.key} 已存在,请更改后重试') @@ -83,6 +86,9 @@ class ServiceView(View): Argument('desc', required=False) ).parse(request.body) if error is None: + if not re.fullmatch(r'[-\w]+', form.key, re.ASCII): + return json_response(error='标识符必须为字母、数字、-和下划线的组合') + service = Service.objects.filter(key=form.key).first() if service and service.id != form.id: return json_response(error=f'唯一标识符 {form.key} 已存在,请更改后重试') diff --git a/spug_api/libs/ssh.py b/spug_api/libs/ssh.py index 56f721f..4dc7071 100644 --- a/spug_api/libs/ssh.py +++ b/spug_api/libs/ssh.py @@ -62,7 +62,7 @@ class SSH: chan.settimeout(timeout) chan.set_combine_stderr(True) if environment: - str_env = ' '.join(f"{k}='{self._handle_env(v)}'" for k, v in environment.items()) + str_env = ' '.join(self._handle_env(k, v) for k, v in environment.items()) command = f'export {str_env} && {command}' chan.exec_command(command) stdout = chan.makefile("rb", -1) @@ -75,7 +75,7 @@ class SSH: chan.settimeout(timeout) chan.set_combine_stderr(True) if environment: - str_env = ' '.join(f"{k}='{self._handle_env(v)}'" for k, v in environment.items()) + str_env = ' '.join(self._handle_env(k, v) for k, v in environment.items()) command = f'export {str_env} && {command}' chan.exec_command(command) stdout = chan.makefile("rb", -1) @@ -106,10 +106,11 @@ class SSH: except UnicodeDecodeError: return out.decode('GBK') - def _handle_env(self, value): + def _handle_env(self, key, value): + key = key.replace('-', '_') if isinstance(value, str): value = value.replace("'", "'\"'\"'") - return value + return f"{key}='{value}'" def __enter__(self): if self.client is not None: diff --git a/spug_web/src/pages/config/app/Form.js b/spug_web/src/pages/config/app/Form.js index 7e1dcaf..c2eb2c5 100644 --- a/spug_web/src/pages/config/app/Form.js +++ b/spug_web/src/pages/config/app/Form.js @@ -37,7 +37,12 @@ export default observer(function () {