Skip to content

Commit

Permalink
Python scan speed improvements (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
Prabhu Subramanian authored Oct 23, 2020
1 parent 617d0e0 commit 4c2f116
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 20 deletions.
1 change: 1 addition & 0 deletions lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
"examples",
"tutorials",
"samples",
"migrations",
"db_migrations",
"unittests",
"unittests_legacy",
Expand Down
2 changes: 1 addition & 1 deletion lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def convert_to_findings(src_dir, repo_context, reports_dir, sarif_files):
"""
app_name = find_app_name(src_dir, repo_context)
findings_fname = utils.get_report_file(
"inspect", reports_dir, True, ext_name="findings.json"
"ngsast", reports_dir, True, ext_name="findings.json"
)
# Exclude any ng sast sarif files
sarif_files = [f for f in sarif_files if "ng-sast" not in f]
Expand Down
14 changes: 8 additions & 6 deletions lib/pyt/cfg_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def deep_analysis(src, files):
directory = os.path.dirname(path)
project_modules = get_modules(directory, prepend_module_root=False)
local_modules = get_directory_modules(directory)
LOG.debug(f"Generating AST for {path}")

LOG.debug(f"Generating AST and CFG for {path}")
try:
tree = generate_ast(path)
if not tree:
Expand All @@ -49,15 +50,16 @@ def deep_analysis(src, files):
path,
allow_local_directory_imports=True,
)
cfg_list = [cfg]
FrameworkAdaptor(
cfg_list, project_modules, local_modules, framework_route_criteria
)
cfg_list.append(cfg)
except Exception as e:
LOG.debug(e)

# Add all the route functions to the cfg_list
try:
# Taint all possible entry points
LOG.debug("Determining taints")
FrameworkAdaptor(
cfg_list, project_modules, local_modules, framework_route_criteria
)
LOG.debug("Building constraints table")
initialize_constraint_table(cfg_list)
LOG.debug("About to begin deep analysis")
Expand Down
3 changes: 3 additions & 0 deletions lib/pyt/core/project_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
The module finds all python modules and generates an ast for them.
"""
import os
from functools import lru_cache

_local_modules = list()


@lru_cache()
def get_directory_modules(directory):
"""Return a list containing tuples of
e.g. ('__init__', 'example/import_test_project/__init__.py')
Expand All @@ -31,6 +33,7 @@ def get_directory_modules(directory):
return _local_modules


@lru_cache()
def get_modules(path, prepend_module_root=True):
"""Return a list containing tuples of
e.g. ('test_project.utils', 'example/test_project/utils.py')
Expand Down
4 changes: 3 additions & 1 deletion lib/pyt/helper_visitors/label_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ def slicev(self, node):
for d in node.dims:
self.visit(d)
else:
self.visit(node.value)
self.visit(
node.value
) # This should be changed to self.visit(node) for python 3.9

# operator = Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift | RShift | BitOr | BitXor | BitAnd | FloorDiv
def visit_Add(self, node):
Expand Down
4 changes: 2 additions & 2 deletions lib/pyt/web_frameworks/framework_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def find_route_functions_taint_args(self):
def run(self):
"""Run find_route_functions_taint_args on each CFG."""
function_cfgs = list()
for _ in self.cfg_list:
function_cfgs.extend(self.find_route_functions_taint_args())
# for _ in self.cfg_list:
function_cfgs.extend(self.find_route_functions_taint_args())
self.cfg_list.extend(function_cfgs)


Expand Down
11 changes: 1 addition & 10 deletions test/integration/test_bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,7 @@ def test_context():
os.environ["BITBUCKET_PR_ID"] = "pr-123"
os.environ["BITBUCKET_PR_DESTINATION_BRANCH"] = "main"
context = bitbucket.Bitbucket().get_context({"foo": "bar"})
assert context == {
"foo": "bar",
"repoOwner": "test",
"repoFullname": "test/bar",
"prID": "pr-123",
"prTargetBranch": "main",
"repoUUID": "uuid123",
"repoWorkspace": "foo",
"bitbucketToken": None,
}
assert context["foo"] == "bar"


def test_reports_url():
Expand Down

0 comments on commit 4c2f116

Please sign in to comment.