Skip to content

Commit a7b023e

Browse files
bandit warnings pass 1 (microsoft#13194)
1 parent 30b3035 commit a7b023e

6 files changed

Lines changed: 23 additions & 20 deletions

File tree

‎pythonFiles/vscode_datascience_helpers/daemon/daemon_python.py‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,6 @@ def exec_file():
134134
with change_exec_context(args, cwd, env):
135135
return self._execute_and_capture_output(exec_file)
136136

137-
@error_decorator
138-
def m_exec_code(self, code):
139-
self.log.info("Exec code %s", code)
140-
141-
def exec_code():
142-
eval(code, globals())
143-
144-
return self._execute_and_capture_output(exec_code)
145-
146137
@error_decorator
147138
def m_exec_file_observable(self, file_name, args=[], cwd=None, env=None):
148139
args = [] if args is None else args

‎pythonFiles/vscode_datascience_helpers/dataframes/vscodeDataFrameHelpers.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ def _VSCODE_convertToDataFrame(df):
1313
elif hasattr(df, "toPandas"):
1414
df = df.toPandas()
1515
else:
16+
""" Disabling bandit warning for try, except, pass. We want to swallow all exceptions here to not crash on
17+
variable fetching """
1618
try:
1719
temp = _VSCODE_pd.DataFrame(df)
1820
df = temp
19-
except:
21+
except: # nosec
2022
pass
2123
return df
2224

‎pythonFiles/vscode_datascience_helpers/getJupyterVariableShape.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Query Jupyter server for defined variables list
22
# Tested on 2.7 and 3.6
33
import json as _VSCODE_json
4+
import ast as _VSCODE_ast
45

56
# In IJupyterVariables.getValue this '_VSCode_JupyterTestValue' will be replaced with the json stringified value of the target variable
67
# Indexes off of _VSCODE_targetVariable need to index types that are part of IJupyterVariable
78
_VSCODE_targetVariable = _VSCODE_json.loads("""_VSCode_JupyterTestValue""")
89

9-
_VSCODE_evalResult = eval(_VSCODE_targetVariable["name"])
10+
_VSCODE_evalResult = _VSCODE_ast.literal_eval(_VSCODE_targetVariable["name"])
1011

1112
# Find shape and count if available
1213
if hasattr(_VSCODE_evalResult, "shape"):

‎pythonFiles/vscode_datascience_helpers/getServerInfo.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424

2525
print(json.dumps(server_info_list))
2626
except Exception:
27-
import subprocess
27+
""" Usage of subprocess is safe here as we are using run and are in control of all the arguments passed to it
28+
flagging for execution of partial path is also not correct as it is a command, not a path """
29+
import subprocess # nosec
2830
import sys
2931

30-
result = subprocess.run(
32+
result = subprocess.run( # nosec
3133
["jupyter", "notebook", "list", "--jsonlist"], capture_output=True
3234
)
3335
encoding = os.getenv("PYTHONIOENCODING", "utf-8")

‎pythonFiles/vscode_datascience_helpers/jupyter_daemon.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import json
55
import logging
66
import os
7-
import subprocess
7+
8+
# See comment at our point of usage of subprocess on why this usage is ok
9+
import subprocess # nosec
810
import sys
911
from vscode_datascience_helpers.daemon.daemon_python import (
1012
error_decorator,
@@ -53,7 +55,8 @@ def m_exec_module(self, module_name, args=[], cwd=None, env=None):
5355
from notebook import notebookapp as app
5456

5557
return {"stdout": ".".join(list(str(v) for v in app.version_info))}
56-
except Exception:
58+
""" We specifically don't want to bubble up an error from --version so pass exception here """
59+
except Exception: # nosec
5760
pass
5861
# kernelspec, nbconvert are subcommands of jupyter.
5962
# python -m jupyter kernelspec, python -m jupyter nbconvert,
@@ -71,9 +74,9 @@ def m_exec_module(self, module_name, args=[], cwd=None, env=None):
7174
return super().m_exec_module(module_name, args, cwd, env)
7275

7376
def _exec_with_subprocess(self, module_name, args=[], cwd=None, env=None):
74-
# # result = subprocess.run([sys.executable, "-m"] + args, stdout=sys.stdout, stderr=sys.stderr)
75-
# return self._execute_and_capture_output(lambda: subprocess.run([sys.executable, "-m", module_name] + args, stdout=sys.stdout, stderr=sys.stderr))
76-
result = subprocess.run(
77+
# The usage here traced up to our execModule function on the daemon and only goes down this path for jupyter subcommands
78+
# in m_exec_module in this class if module_name == jupyter and we are in control of the args we know that we are safe here
79+
result = subprocess.run( # nosec
7780
[sys.executable, "-m", module_name] + args, capture_output=True
7881
)
7982
encoding = os.getenv("PYTHONIOENCODING", "utf-8")

‎pythonFiles/vscode_datascience_helpers/kernel_launcher_daemon.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import os.path
88
import signal
99
import sys
10-
import subprocess
10+
11+
# Only subprocess.PIPE is used in this file so we can ignore the subprocess warning for this file
12+
# The actual POpen is in kernel_launcher.py
13+
import subprocess # nosec
1114
import threading
1215
import time
1316
from vscode_datascience_helpers.daemon.daemon_python import (
@@ -30,7 +33,8 @@ def close(self):
3033
"""
3134
try:
3235
self.m_kill_kernel()
33-
except:
36+
""" We don't care about exceptions coming back from killing the kernel, so pass here """
37+
except: # nosec
3438
pass
3539
super().close()
3640

0 commit comments

Comments
 (0)