From 5110e427869b4c5a660d69968909ca7e98e4af2b Mon Sep 17 00:00:00 2001 From: Knut Sveidqvist Date: Tue, 28 Jun 2022 18:50:41 +0200 Subject: [PATCH] Better balance check --- src/utils.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/utils.js b/src/utils.js index 805556dd6..be28ef8df 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1057,7 +1057,7 @@ export const directiveSanitizer = (args) => { for (let i = 0; i < kArr.length; i++) { const k = kArr[i]; const val = args.themeVariables[k]; - if (!val.match(/^[a-zA-Z0-9#,";()%. ]+$/)) { + if (val && val.match && !val.match(/^[a-zA-Z0-9#,";()%. ]+$/)) { args.themeVariables[k] = ''; } } @@ -1065,9 +1065,19 @@ export const directiveSanitizer = (args) => { log.debug('After sanitization', args); }; export const sanitizeCss = (str) => { - const stringsearch = 'o'; - const startCnt = (str.match(/\{/g) || []).length; - const endCnt = (str.match(/\}/g) || []).length; + let startCnt = 0; + let endCnt = 0; + + for (let i = 0; i < str.length; i++) { + if (startCnt < endCnt) { + return '{ /* ERROR: Unbalanced CSS */ }'; + } + if (str[i] === '{') { + startCnt++; + } else if (str[i] === '}') { + endCnt++; + } + } if (startCnt !== endCnt) { return '{ /* ERROR: Unbalanced CSS */ }'; }