Skip to content

Commit

Permalink
Added check for illegal characters in file names and paths
Browse files Browse the repository at this point in the history
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
  • Loading branch information
maxkrapp1 committed Nov 17, 2023
1 parent c987711 commit 3a9579b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions thales_remote/script_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 3a9579b

Please sign in to comment.