Skip to content

Commit 3a9579b

Browse files
committed
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
1 parent c987711 commit 3a9579b

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

thales_remote/script_wrapper.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ def setEISOutputPath(self, path: str) -> str:
776776
:returns: reponse string from the device
777777
"""
778778
path = path.lower() # c: has to be lower case
779+
self._checkForForbiddenCharactersInPath(path)
779780
return self.setValue("EIS_PATH", path)
780781

781782
def setEISOutputFileName(self, name: str) -> str:
@@ -791,6 +792,7 @@ def setEISOutputFileName(self, name: str) -> str:
791792
:param name: basic name of the file
792793
:returns: reponse string from the device
793794
"""
795+
self._checkForForbiddenCharactersInFilename(name)
794796
return self.setValue("EIS_ROOT", name)
795797

796798
def measureEIS(self) -> str:
@@ -1033,6 +1035,7 @@ def setCVOutputPath(self, path: str) -> str:
10331035
:returns: reponse string from the device
10341036
"""
10351037
path = path.lower() # c: has to be lower case
1038+
self._checkForForbiddenCharactersInPath(path)
10361039
return self.setValue("CV_PATH", path)
10371040

10381041
def setCVOutputFileName(self, name: str) -> str:
@@ -1048,6 +1051,7 @@ def setCVOutputFileName(self, name: str) -> str:
10481051
:param name: basic name of the file to set
10491052
:returns: reponse string from the device
10501053
"""
1054+
self._checkForForbiddenCharactersInFilename(name)
10511055
return self.setValue("CV_ROOT", name)
10521056

10531057
def checkCVSetup(self) -> str:
@@ -1372,6 +1376,7 @@ def setIEOutputPath(self, path: str) -> str:
13721376
:returns: reponse string from the device
13731377
"""
13741378
path = path.lower() # c: has to be lower case
1379+
self._checkForForbiddenCharactersInPath(path)
13751380
return self.setValue("IE_PATH", path)
13761381

13771382
def setIEOutputFileName(self, name: str) -> str:
@@ -1387,6 +1392,7 @@ def setIEOutputFileName(self, name: str) -> str:
13871392
:param name: The basic name of the file.
13881393
:returns: reponse string from the device
13891394
"""
1395+
self._checkForForbiddenCharactersInFilename(name)
13901396
return self.setValue("IE_ROOT", name)
13911397

13921398
def checkIESetup(self) -> str:
@@ -1503,6 +1509,7 @@ def setSequenceOutputPath(self, path: str) -> str:
15031509
:returns: reponse string from the device
15041510
"""
15051511
path = path.lower() # c: has to be lower case
1512+
self._checkForForbiddenCharactersInPath(path)
15061513
return self.setValue("SEQ_PATH", path)
15071514

15081515
def setSequenceOutputFileName(self, name: str) -> str:
@@ -1518,6 +1525,7 @@ def setSequenceOutputFileName(self, name: str) -> str:
15181525
:param name: The basic name of the file.
15191526
:returns: reponse string from the device
15201527
"""
1528+
self._checkForForbiddenCharactersInFilename(name)
15211529
return self.setValue("SEQ_ROOT", name)
15221530

15231531
def enableSequenceAcqGlobal(self, state: bool = True) -> str:
@@ -2149,3 +2157,29 @@ def _requestValueAndParseUsingRegexp(self, command: str, pattern: str) -> float:
21492157
)
21502158
match = re.search(pattern, reply)
21512159
return float(match.group(1))
2160+
2161+
def _checkForForbiddenCharactersInPath(self, string:str):
2162+
"""
2163+
This function ensures that only the permitted characters appear in the path to:
2164+
https://doc.zahner.de/manuals/remote2.pdf
2165+
2166+
:param string: string to check.
2167+
"""
2168+
pattern = re.compile(r"(^[a-k]{1}[:]{1}[\/\\0-9a-zA-Z_\+-]+$)")
2169+
match = pattern.fullmatch(string)
2170+
if match is None:
2171+
raise ValueError(f"invalid path: \"{string}\"")
2172+
return
2173+
2174+
def _checkForForbiddenCharactersInFilename(self, string:str):
2175+
"""
2176+
This function ensures that only the permitted characters appear in the filename to:
2177+
https://doc.zahner.de/manuals/remote2.pdf
2178+
2179+
:param string: string to check.
2180+
"""
2181+
pattern = re.compile(r"(^[0-9a-zA-Z_\+-]+$)")
2182+
match = pattern.fullmatch(string)
2183+
if match is None:
2184+
raise ValueError(f"invalid filename: \"{string}\"")
2185+
return

0 commit comments

Comments
 (0)