Skip to content

Commit

Permalink
Enhancement 205 - Slicing shall be blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
namsonx committed Mar 22, 2024
1 parent 13b58d5 commit 48eae0f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
39 changes: 26 additions & 13 deletions JsonPreprocessor/CJsonPreprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class CNameMangling(Enum):
DUPLICATEDKEY_01 = "__handleDuplicatedKey__"
STRINGCONVERT = "__ConvertParameterToString__"
LISTINDEX = "__IndexOfList__"
SLICEINDEX = "__SlicingIndex__"

class CPythonJSONDecoder(json.JSONDecoder):
"""
Expand Down Expand Up @@ -526,6 +527,10 @@ def __handleDotInNestedParam(sNestedParam : str) -> str:
if sInputStr==sLoopCheck1:
self.__reset()
raise Exception(f"Invalid expression while handling the parameter '{sNestedParam}'.")
elif re.search(r"\[\s*\-\d+\s*\]", sInputStr):
errorMsg = f"Slicing is currently not supported! Please update the expression '{sNestedParam}'."
self.__reset()
raise Exception(errorMsg)
if sInputStr==sLoopCheck:
self.__reset()
raise Exception(f"Invalid expression while handling the parameter '{sNestedParam}'.")
Expand Down Expand Up @@ -706,7 +711,7 @@ def __jsonUpdated(k, v, oJson, bNested, keyNested = '', bDuplicatedHandle=False,

def __loadNestedValue(initValue: str, sInputStr: str, bKey=False, key=''):
varPattern = rf"\${{\s*[^{re.escape(self.specialCharacters)}]*\s*}}"
indexPattern = r"\[[\s-]*\d*\s*\]"
indexPattern = r"\[[\s\-\+\d]*\]"
dictPattern = r"(\[+\s*'[^\$\[\]\(\)]+'\s*\]+|\[+\s*\d+\s*\]+|\[+\s*" + varPattern + r".*\]+)*|" + indexPattern
pattern = varPattern + dictPattern
bValueConvertString = False
Expand Down Expand Up @@ -890,7 +895,7 @@ def __recursiveNestedHandling(sInputStr: str, lNestedParam: list) -> str:
return sInputStr

variablePattern = rf"[^{re.escape(self.specialCharacters)}]+"
indexPattern = r"\[[\s-]*\d*\s*\]"
indexPattern = r"\[[\s\-\+\d]*\]"
dictPattern = r"\[+\s*'.+'\s*\]+|\[+\s*\d+\s*\]+|\[+\s*\${\s*" + variablePattern + r"\s*}.*\]+|" + indexPattern
nestedPattern = r"\${\s*" + variablePattern + r"(\${\s*" + variablePattern + r"\s*})*" + r"\s*}(" + dictPattern + r")*"
valueStrPattern = r"[\"|\']\s*[0-9A-Za-z_\-\s*]+[\"|\']"
Expand Down Expand Up @@ -991,7 +996,7 @@ def __checkNestedParam(self, sInput : str, bKey=False) -> bool:
"""
pattern = rf"^\${{\s*[^{re.escape(self.specialCharacters)}]+\s*}}(\[.*\])+$"
pattern1 = rf"\${{.+}}(\[.+\])*[^\[]*\${{"
pattern2 = r"\${.*\${.*}"
pattern2 = r"\[[a-zA-Z0-9\.\-\+\${}'\s]*:[a-zA-Z0-9\.\-\+\${}'\s]*\]" # Slicing pattern
if "${" not in sInput:
return True
if re.search(rf"\${{\s*[^{re.escape(self.specialCharacters)}]+\['*.+'*\].*}}", sInput, re.UNICODE):
Expand All @@ -1004,23 +1009,27 @@ def __checkNestedParam(self, sInput : str, bKey=False) -> bool:
errorMsg = f"Invalid syntax! A sub-element in {sInput.strip()} has to enclosed in quotes."
self.__reset()
raise Exception(errorMsg)
elif re.search(r'\[\s*\]', sInput):
elif re.search(r'\[[!@#$%^&*()+=[\]{}|;:\s\-\+\'",<>?/`~]*\]', sInput):
if CNameMangling.STRINGCONVERT.value not in sInput or \
re.match(pattern, sInput.replace(CNameMangling.STRINGCONVERT.value, "")):
errorMsg = f"Expression '{sInput.replace(CNameMangling.STRINGCONVERT.value, '')}' cannot be evaluated. \
Reason: Empty pair of square brackets detected."
Reason: Empty or special character is detected in pair of square brackets."
self.__reset()
raise Exception(errorMsg)
else:
return True
elif re.search(r"\${\s*}", sInput):
elif re.search(r'\${[!@#$%^&*()+=[\]{}|;:\s\-\+\'",<>?/`~]*}', sInput):
if CNameMangling.STRINGCONVERT.value not in sInput:
errorMsg = f"Expression '{sInput.replace(CNameMangling.STRINGCONVERT.value, '')}' cannot be evaluated. \
Reason: Empty pair of curly brackets detected."
Reason: Empty or special character is detected pair of curly brackets."
self.__reset()
raise Exception(errorMsg)
else:
return True
elif re.search(pattern2, sInput) or re.search(r"\[\s*\-\s*\d+\]", sInput):
errorMsg = f"Slicing is currently not supported! Please update the expression '{sInput}'."
self.__reset()
raise Exception(errorMsg)
elif CNameMangling.STRINGCONVERT.value in sInput:
if sInput.count("${") > sInput.count("}"):
sInput = re.sub(CNameMangling.STRINGCONVERT.value, "", sInput)
Expand Down Expand Up @@ -1181,9 +1190,12 @@ def __checkKeynameFormat(oJson : dict):
if nestedVar[0].isdigit():
self.__reset()
raise Exception(f"Invalid parameter format in line: {line.strip()}")
tmpList = re.findall(r"(\"[^\"]+\")", line)
tmpList01 = re.findall(r"(\"[^\"]+\")", line)
line = re.sub(r"(\"[^\"]+\")", CNameMangling.COLONS.value, line)
indexPattern = r"\[[\s-]*\d*\s*\]"
slicingPattern = r"\[[a-zA-Z0-9\.\-\+\${}'\s]*:[a-zA-Z0-9\.\-\+\${}'\s]*\]"
tmpList02 = re.findall(slicingPattern, line)
line = re.sub(slicingPattern, CNameMangling.SLICEINDEX.value, line)
indexPattern = r"\[[\s\-\+\d]*\]"
if re.search(indexPattern, line):
indexList = re.findall(indexPattern, line)
line = re.sub("(" + indexPattern + ")", CNameMangling.LISTINDEX.value, line)
Expand All @@ -1197,12 +1209,13 @@ def __checkKeynameFormat(oJson : dict):
nestedKey = True
if CNameMangling.COLONS.value in item:
while CNameMangling.COLONS.value in item:
item = re.sub(CNameMangling.COLONS.value, tmpList[0], item, count=1)
tmpList.pop(0)
item = re.sub(CNameMangling.COLONS.value, tmpList01.pop(0), item, count=1)
elif CNameMangling.LISTINDEX.value in item:
while CNameMangling.LISTINDEX.value in item:
item = re.sub(CNameMangling.LISTINDEX.value, indexList[0], item, count=1)
indexList.pop(0)
item = re.sub(CNameMangling.LISTINDEX.value, indexList.pop(0), item, count=1)
elif CNameMangling.SLICEINDEX.value in item:
while CNameMangling.SLICEINDEX.value in item:
item = re.sub(CNameMangling.SLICEINDEX.value, tmpList02.pop(0), item, count=1)
i+=1
newSubItem = ""
if re.search(r"^\s*\[.+\][\s,]*$", item) and item.count('[')==item.count(']'):
Expand Down
8 changes: 4 additions & 4 deletions test/testconfig/TestConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2792,9 +2792,9 @@
dictUsecase['HINT'] = None
dictUsecase['COMMENT'] = None
dictUsecase['JSONFILE'] = r"..\testfiles\jpp-test_config_1350.jsonp"
dictUsecase['EXPECTEDEXCEPTION'] = None # !!! expectation TODO / something like: "slicing not supported"!!!
dictUsecase['EXPECTEDEXCEPTION'] = "Slicing is currently not supported!"
dictUsecase['EXPECTEDRETURN'] = None
# # # listofdictUsecases.append(dictUsecase)
listofdictUsecases.append(dictUsecase)
del dictUsecase
# --------------------------------------------------------------------------------------------------------------
dictUsecase = {}
Expand Down Expand Up @@ -2988,9 +2988,9 @@
dictUsecase['HINT'] = None
dictUsecase['COMMENT'] = None
dictUsecase['JSONFILE'] = r"..\testfiles\jpp-test_config_1364.jsonp"
dictUsecase['EXPECTEDEXCEPTION'] = None # # !!! expectation TODO / something like: "slicing not supported"!!!
dictUsecase['EXPECTEDEXCEPTION'] = "Slicing is currently not supported!"
dictUsecase['EXPECTEDRETURN'] = None
# # # listofdictUsecases.append(dictUsecase)
listofdictUsecases.append(dictUsecase)
del dictUsecase
# --------------------------------------------------------------------------------------------------------------
dictUsecase = {}
Expand Down

0 comments on commit 48eae0f

Please sign in to comment.