|
14 | 14 | from pyaedt.generic.general_methods import is_ironpython
|
15 | 15 | from pyaedt.generic.settings import is_linux
|
16 | 16 | from pyaedt import is_windows
|
| 17 | +from pyaedt.misc.misc import is_safe_path |
17 | 18 |
|
18 | 19 | if is_linux and is_ironpython:
|
19 |
| - import subprocessdotnet as subprocess |
| 20 | + import subprocessdotnet as subprocess # nosec |
20 | 21 | else:
|
21 |
| - import subprocess |
| 22 | + import subprocess # nosec |
22 | 23 |
|
23 | 24 | if not is_ironpython:
|
24 | 25 | import rpyc
|
@@ -270,24 +271,37 @@ def exposed_run_script(self, script, aedt_version="2021.2", ansysem_path=None, n
|
270 | 271 | elif os.path.exists(script):
|
271 | 272 | script_file = script
|
272 | 273 | else:
|
273 |
| - return "File wrong or wrong commands." |
| 274 | + return "Wrong file or wrong commands." |
| 275 | + if not is_safe_path(script_file): |
| 276 | + return "Script file {} not safe.".format(script_file) |
274 | 277 | executable = "ansysedt.exe"
|
275 | 278 | if is_linux and not ansysem_path and not env_path(aedt_version):
|
276 | 279 | ansysem_path = os.getenv("PYAEDT_SERVER_AEDT_PATH", "")
|
277 | 280 | if env_path(aedt_version) or ansysem_path:
|
278 | 281 | if not ansysem_path:
|
279 | 282 | ansysem_path = env_path(aedt_version)
|
280 |
| - |
281 |
| - ng_feature = " -features=SF159726_SCRIPTOBJECT" |
| 283 | + exe_path = os.path.join(ansysem_path, executable) |
| 284 | + if not is_safe_path(exe_path): |
| 285 | + return "Ansys EM path not safe." |
| 286 | + command = [exe_path] |
| 287 | + if non_graphical: |
| 288 | + command.append("-ng") |
| 289 | + ng_feature = "-features=SF159726_SCRIPTOBJECT" |
282 | 290 | if self._beta_options:
|
283 | 291 | for opt in range(self._beta_options.__len__()):
|
284 | 292 | if self._beta_options[opt] not in ng_feature:
|
285 | 293 | ng_feature += "," + self._beta_options[opt]
|
286 | 294 | if non_graphical:
|
287 |
| - ng_feature += ",SF6694_NON_GRAPHICAL_COMMAND_EXECUTION -ng" |
288 |
| - command = os.path.join(ansysem_path, executable) + ng_feature + " -RunScriptAndExit " + script_file |
289 |
| - p = subprocess.Popen(command) |
290 |
| - p.wait() |
| 295 | + ng_feature += ",SF6694_NON_GRAPHICAL_COMMAND_EXECUTION" |
| 296 | + command.append(ng_feature) |
| 297 | + command = [exe_path, ng_feature, "-RunScriptAndExit", script_file] |
| 298 | + try: |
| 299 | + p = subprocess.Popen(command) # nosec |
| 300 | + p.wait() |
| 301 | + except subprocess.CalledProcessError as e: |
| 302 | + msg = "Command failed with error: {}".format(e) |
| 303 | + logger.error(msg) |
| 304 | + return msg |
291 | 305 | return "Script Executed."
|
292 | 306 |
|
293 | 307 | else:
|
@@ -871,7 +885,13 @@ def aedt_grpc(port=None, beta_options=None, use_aedt_relative_path=False, non_gr
|
871 | 885 | from pyaedt.generic.general_methods import grpc_active_sessions
|
872 | 886 | sessions = grpc_active_sessions()
|
873 | 887 | if not port:
|
874 |
| - port = check_port(random.randint(18500, 20000)) |
| 888 | + # TODO: Remove once IronPython is deprecated |
| 889 | + if is_ironpython: |
| 890 | + port = check_port(random.randint(18500, 20000)) # nosec |
| 891 | + else: |
| 892 | + import secrets |
| 893 | + secure_random = secrets.SystemRandom() |
| 894 | + port = check_port(secure_random.randint(18500, 20000)) |
875 | 895 |
|
876 | 896 | if port == 0:
|
877 | 897 | print("Error. No ports are available.")
|
@@ -1097,7 +1117,7 @@ def on_disconnect(self, connection):
|
1097 | 1117 | try:
|
1098 | 1118 | edb.close_edb()
|
1099 | 1119 | except Exception:
|
1100 |
| - pass |
| 1120 | + logger.warning("Error when trying to close EDB.") |
1101 | 1121 |
|
1102 | 1122 | def start_service(self, port):
|
1103 | 1123 | """Connect to remove service manager and run a new server on specified port.
|
@@ -1157,5 +1177,11 @@ def exposed_stop_service(self, port):
|
1157 | 1177 |
|
1158 | 1178 | @staticmethod
|
1159 | 1179 | def exposed_check_port():
|
1160 |
| - port_number = random.randint(18500, 20000) |
1161 |
| - return check_port(port_number) |
| 1180 | + # TODO: Remove once IronPython is deprecated |
| 1181 | + if is_ironpython: # nosec |
| 1182 | + port = check_port(random.randint(18500, 20000)) # nosec |
| 1183 | + else: |
| 1184 | + import secrets |
| 1185 | + secure_random = secrets.SystemRandom() |
| 1186 | + port = check_port(secure_random.randint(18500, 20000)) |
| 1187 | + return port |
0 commit comments