[ISSUE #3103] Fix can't create properties config (#3483)

* Fix browser Uncaught TypeError when create a Properties type configuration

* Change Properties content verification method

* Update front end resources
This commit is contained in:
ljhrot 2020-07-31 19:16:56 +08:00 committed by GitHub
parent 43f606519b
commit 4310b3ee47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 174 additions and 13 deletions

View File

@ -58,7 +58,6 @@ request.middleWare((_config = {}) => {
if (res.code === 403 && !hasAlert) {
hasAlert = true;
window.Dialog.alert({
language: aliwareIntl.currentLanguageCode || 'zh-cn',
style: { width: 400 },
content: res.message,
onOk: () => {
@ -81,7 +80,6 @@ request.middleWare((_config = {}) => {
hasAlert = true;
window.Dialog.alert({
language: aliwareIntl.currentLanguageCode || 'zh-cn',
style: { width: 400 },
content: aliwareIntl.get('com.alibaba.nacos.pubshow'), // '子账号没有权限请联系主账号负责人RAM上授权',
onOk: () => {

View File

@ -266,7 +266,6 @@ class NewConfig extends React.Component {
} else {
Dialog.confirm({
content: locale.confirmSyanx,
language: aliwareIntl.currentLanguageCode || 'zh-cn',
onOk: () => {
this.publicConfigBeforeCheck(content);
},
@ -352,7 +351,6 @@ class NewConfig extends React.Component {
error: res => {
this.closeLoading();
Dialog.alert({
language: aliwareIntl.currentLanguageCode || 'zh-cn',
content: locale.publishFailed,
});
},

View File

@ -16,6 +16,92 @@
import * as yamljs from 'yamljs';
/**
* 校验一个配置项
*/
function validateProperty(property) {
let { length } = property;
let keyLen = 0;
let valueStart = length;
let hasSep = false;
let precedingBackslash = false;
let c;
// 解析 key
while (keyLen < length) {
c = property[keyLen];
if ((c === '=' || c === ':') && !precedingBackslash) {
valueStart = keyLen + 1;
hasSep = true;
break;
}
if ((c === ' ' || c === '\t' || c === '\f') && !precedingBackslash) {
valueStart = keyLen + 1;
break;
}
if (c === '\\') {
precedingBackslash = !precedingBackslash;
} else {
precedingBackslash = false;
}
keyLen++;
}
// 解析 value
while (valueStart < length) {
c = property[valueStart];
if (c !== ' ' && c !== '\t' && c !== '\f') {
if (!hasSep && (c === '=' || c === ':')) {
hasSep = true;
} else {
break;
}
}
valueStart++;
}
return (
validateKeyOrValueForProperty(property, 0, keyLen) &&
validateKeyOrValueForProperty(property, valueStart, length)
);
}
function validateKeyOrValueForProperty(property, start, end) {
// check null
if (start >= end) {
return false;
}
let index = 0;
let c;
while (index < property.length) {
c = property[index++];
if (c !== '\\') {
continue;
}
c = property[index++];
// check backslash
if (!isPropertyEscape(c)) {
return false;
}
// check Unicode
if (c === 'u') {
let unicode = property.slice(index, index + 4).join('');
if (unicode.match(/^[a-f0-9]{4}$/i) === null) {
return false;
}
index += 4;
}
}
return true;
}
function isPropertyEscape(c = '') {
return 'abfnrt\\"\'0! #:=u'.includes(c);
}
export default {
/**
* 检测json是否合法
@ -65,12 +151,91 @@ export default {
* 检测属性是否正确
*/
validateProperties(str = '') {
const reg = /^[^=]+=.+$/;
return str
.replace('\n\r', '\n')
.split('\n')
.filter(_str => _str)
.every(_str => reg.test(_str.trim()));
let isNewLine = true;
let isCommentLine = false;
let isSkipWhiteSpace = true;
let precedingBackslash = false;
let appendedLineBegin = false;
let skipLF = false;
let hasProperty = false;
let property = [];
for (let i = 0; i < str.length; i++) {
let c = str[i];
if (skipLF) {
skipLF = false;
if (c === '\n') {
continue;
}
}
// 跳过行首空白字符
if (isSkipWhiteSpace) {
if (c === ' ' || c === '\t' || c === '\f') {
continue;
}
if (!appendedLineBegin && (c === '\r' || c === '\n')) {
continue;
}
appendedLineBegin = false;
isSkipWhiteSpace = false;
}
// 判断注释行
if (isNewLine) {
isNewLine = false;
if (c === '#' || c === '!') {
isCommentLine = true;
continue;
}
}
if (c !== '\n' && c !== '\r') {
property.push(c);
if (c === '\\') {
precedingBackslash = !precedingBackslash;
} else {
precedingBackslash = false;
}
continue;
}
// 跳过注释行
if (isCommentLine || property.length === 0) {
isNewLine = true;
isCommentLine = false;
isSkipWhiteSpace = true;
property = [];
continue;
}
// 处理转移字符
if (precedingBackslash) {
property.pop();
precedingBackslash = false;
isSkipWhiteSpace = true;
appendedLineBegin = true;
if (c === '\r') {
skipLF = true;
}
continue;
}
// 解析出配置项
// 进行校验
if (!validateProperty(property)) {
return false;
}
hasProperty = true;
property = [];
isNewLine = true;
isSkipWhiteSpace = true;
}
// 校验最后一行
if (property.length > 0 && !isCommentLine) {
return validateProperty(property);
}
return hasProperty;
},
/**

File diff suppressed because one or more lines are too long