perf: block add or update connection when name contains illegal characters

This commit is contained in:
tiny-craft 2023-08-25 01:11:34 +08:00
parent 96a11bdb9c
commit 397c31d502
4 changed files with 23 additions and 7 deletions

View File

@ -101,11 +101,15 @@ func (c *connectionService) GetConnection(name string) (resp types.JSResp) {
// SaveConnection save connection config to local profile
func (c *connectionService) SaveConnection(name string, param types.ConnectionConfig) (resp types.JSResp) {
var err error
if len(name) > 0 {
// update connection
err = c.conns.UpdateConnection(name, param)
if strings.ContainsAny(param.Name, "/") {
err = errors.New("connection name contains illegal characters")
} else {
err = c.conns.CreateConnection(param)
if len(name) > 0 {
// update connection
err = c.conns.UpdateConnection(name, param)
} else {
err = c.conns.CreateConnection(param)
}
}
if err != nil {
resp.Msg = err.Error()

View File

@ -1,5 +1,5 @@
<script setup>
import { get, isEmpty, map } from 'lodash'
import { every, get, includes, isEmpty, map } from 'lodash'
import { computed, nextTick, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import { TestConnection } from 'wailsjs/go/services/connectionService.js'
@ -19,8 +19,18 @@ const editName = ref('')
const generalForm = ref(null)
const generalFormRules = () => {
const requiredMsg = i18n.t('field_required')
const illegalChars = ['/', '\\']
return {
name: { required: true, message: requiredMsg, trigger: 'input' },
name: [
{ required: true, message: requiredMsg, trigger: 'input' },
{
validator: (rule, value) => {
return every(illegalChars, (c) => !includes(value, c))
},
message: i18n.t('illegal_characters'),
trigger: 'input',
},
],
addr: { required: true, message: requiredMsg, trigger: 'input' },
defaultFilter: { required: true, message: requiredMsg, trigger: 'input' },
keySeparator: { required: true, message: requiredMsg, trigger: 'input' },
@ -232,7 +242,7 @@ const onClose = () => {
v-if="showTestResult"
:title="isEmpty(testResult) ? '' : $t('conn_test_fail')"
:type="isEmpty(testResult) ? 'success' : 'error'">
<template v-if="isEmpty(testResult)"> {{ $t('conn_test_succ') }}</template>
<template v-if="isEmpty(testResult)">{{ $t('conn_test_succ') }}</template>
<template v-else>{{ testResult }}</template>
</n-alert>
</n-spin>

View File

@ -132,6 +132,7 @@
"reload_succ": "Reloaded!",
"field_required": "This item should not be blank",
"spec_field_required": "\"{key}\" should not be blank",
"illegal_characters": "Include illegal characters",
"nonexist_tab_content": "Selected key does not exist. Please retry",
"empty_server_content": "Select and open a connection from the left",
"empty_server_list": "No redis server",

View File

@ -132,6 +132,7 @@
"reload_succ": "已重新载入",
"field_required": "此项不能为空",
"spec_field_required": "{key} 不能为空",
"illegal_characters": "包含非法字符",
"nonexist_tab_content": "所选键不存在,请尝试刷新重试",
"empty_server_content": "可以从左边选择并打开连接",
"empty_server_list": "还没添加Redis服务器",