From 3a9579bb1e6d50b65686ab7630757a318b31d895 Mon Sep 17 00:00:00 2001 From: maxkrapp1 Date: Fri, 17 Nov 2023 08:38:02 +0100 Subject: [PATCH] Added check for illegal characters in file names and paths thanks to @ilka-schulz for the hint and the solution suggestion. Path and file names are checked for the permitted characters from the remote manual: https://doc.zahner.de/manuals/remote2.pdf --- thales_remote/script_wrapper.py | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/thales_remote/script_wrapper.py b/thales_remote/script_wrapper.py index 50da363..6cf5e95 100644 --- a/thales_remote/script_wrapper.py +++ b/thales_remote/script_wrapper.py @@ -776,6 +776,7 @@ def setEISOutputPath(self, path: str) -> str: :returns: reponse string from the device """ path = path.lower() # c: has to be lower case + self._checkForForbiddenCharactersInPath(path) return self.setValue("EIS_PATH", path) def setEISOutputFileName(self, name: str) -> str: @@ -791,6 +792,7 @@ def setEISOutputFileName(self, name: str) -> str: :param name: basic name of the file :returns: reponse string from the device """ + self._checkForForbiddenCharactersInFilename(name) return self.setValue("EIS_ROOT", name) def measureEIS(self) -> str: @@ -1033,6 +1035,7 @@ def setCVOutputPath(self, path: str) -> str: :returns: reponse string from the device """ path = path.lower() # c: has to be lower case + self._checkForForbiddenCharactersInPath(path) return self.setValue("CV_PATH", path) def setCVOutputFileName(self, name: str) -> str: @@ -1048,6 +1051,7 @@ def setCVOutputFileName(self, name: str) -> str: :param name: basic name of the file to set :returns: reponse string from the device """ + self._checkForForbiddenCharactersInFilename(name) return self.setValue("CV_ROOT", name) def checkCVSetup(self) -> str: @@ -1372,6 +1376,7 @@ def setIEOutputPath(self, path: str) -> str: :returns: reponse string from the device """ path = path.lower() # c: has to be lower case + self._checkForForbiddenCharactersInPath(path) return self.setValue("IE_PATH", path) def setIEOutputFileName(self, name: str) -> str: @@ -1387,6 +1392,7 @@ def setIEOutputFileName(self, name: str) -> str: :param name: The basic name of the file. :returns: reponse string from the device """ + self._checkForForbiddenCharactersInFilename(name) return self.setValue("IE_ROOT", name) def checkIESetup(self) -> str: @@ -1503,6 +1509,7 @@ def setSequenceOutputPath(self, path: str) -> str: :returns: reponse string from the device """ path = path.lower() # c: has to be lower case + self._checkForForbiddenCharactersInPath(path) return self.setValue("SEQ_PATH", path) def setSequenceOutputFileName(self, name: str) -> str: @@ -1518,6 +1525,7 @@ def setSequenceOutputFileName(self, name: str) -> str: :param name: The basic name of the file. :returns: reponse string from the device """ + self._checkForForbiddenCharactersInFilename(name) return self.setValue("SEQ_ROOT", name) def enableSequenceAcqGlobal(self, state: bool = True) -> str: @@ -2149,3 +2157,29 @@ def _requestValueAndParseUsingRegexp(self, command: str, pattern: str) -> float: ) match = re.search(pattern, reply) return float(match.group(1)) + + def _checkForForbiddenCharactersInPath(self, string:str): + """ + This function ensures that only the permitted characters appear in the path to: + https://doc.zahner.de/manuals/remote2.pdf + + :param string: string to check. + """ + pattern = re.compile(r"(^[a-k]{1}[:]{1}[\/\\0-9a-zA-Z_\+-]+$)") + match = pattern.fullmatch(string) + if match is None: + raise ValueError(f"invalid path: \"{string}\"") + return + + def _checkForForbiddenCharactersInFilename(self, string:str): + """ + This function ensures that only the permitted characters appear in the filename to: + https://doc.zahner.de/manuals/remote2.pdf + + :param string: string to check. + """ + pattern = re.compile(r"(^[0-9a-zA-Z_\+-]+$)") + match = pattern.fullmatch(string) + if match is None: + raise ValueError(f"invalid filename: \"{string}\"") + return