Skip to content

Commit

Permalink
[ISSUE #3103] Fix can't create properties config (#3483)
Browse files Browse the repository at this point in the history
* Fix browser Uncaught TypeError when create a Properties type configuration

* Change Properties content verification method

* Update front end resources
  • Loading branch information
ljhrot authored Jul 31, 2020
1 parent 43f6065 commit 4310b3e
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 13 deletions.
2 changes: 0 additions & 2 deletions console/src/main/resources/static/console-fe/src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => {
Expand All @@ -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: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ class NewConfig extends React.Component {
} else {
Dialog.confirm({
content: locale.confirmSyanx,
language: aliwareIntl.currentLanguageCode || 'zh-cn',
onOk: () => {
this.publicConfigBeforeCheck(content);
},
Expand Down Expand Up @@ -352,7 +351,6 @@ class NewConfig extends React.Component {
error: res => {
this.closeLoading();
Dialog.alert({
language: aliwareIntl.currentLanguageCode || 'zh-cn',
content: locale.publishFailed,
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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是否合法
Expand Down Expand Up @@ -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;
},

/**
Expand Down
6 changes: 3 additions & 3 deletions console/src/main/resources/static/js/main.js

Large diffs are not rendered by default.

0 comments on commit 4310b3e

Please sign in to comment.