Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whitelist lines ending in # nosec #121

Merged
merged 22 commits into from
Apr 28, 2018
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions pyt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def parse_args(args):
'(only JSON-formatted files are accepted)',
type=str,
default=False)
parser.add_argument('--ignore-nosec', dest='ignore_nosec', action='store_true',
help='do not skip lines with # nosec comments')

save_parser = subparsers.add_parser('save', help='Save menu.')
save_parser.set_defaults(which='save')
Expand Down Expand Up @@ -192,7 +194,7 @@ def parse_args(args):
return parser.parse_args(args)


def analyse_repo(args, github_repo, analysis_type, ui_mode):
def analyse_repo(args, github_repo, analysis_type, ui_mode, nosec_lines):
cfg_list = list()
directory = os.path.dirname(github_repo.path)
project_modules = get_modules(directory)
Expand All @@ -215,7 +217,8 @@ def analyse_repo(args, github_repo, analysis_type, ui_mode):
VulnerabilityFiles(
args.blackbox_mapping_file,
args.trigger_word_file
)
),
nosec_lines
)
return vulnerabilities

Expand All @@ -235,12 +238,23 @@ def main(command_line_args=sys.argv[1:]):
elif args.trim_reassigned_in:
ui_mode = UImode.TRIM

path = os.path.normpath(args.filepath)
cfg_list = list()
if args.ignore_nosec:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i moved the code this place and deleted nosec_lines = set() .Because its already created in if-else statement.

nosec_lines = set()
else:
file = open(path, "r")
lines = file.readlines()
nosec_lines = set(
lineno for
(lineno, line) in enumerate(lines, start=1)
if '#nosec' in line or '# nosec' in line)

if args.git_repos:
repos = get_repos(args.git_repos)
for repo in repos:
repo.clone()
vulnerabilities = analyse_repo(args, repo, analysis, ui_mode)
vulnerabilities = analyse_repo(args, repo, analysis, ui_mode, nosec_lines)
if args.json:
json.report(vulnerabilities, sys.stdout)
else:
Expand All @@ -263,8 +277,6 @@ def main(command_line_args=sys.argv[1:]):
)
exit()

path = os.path.normpath(args.filepath)

directory = None
if args.project_root:
directory = os.path.normpath(args.project_root)
Expand Down Expand Up @@ -305,8 +317,10 @@ def main(command_line_args=sys.argv[1:]):
VulnerabilityFiles(
args.blackbox_mapping_file,
args.trigger_word_file
)
),
nosec_lines
)

if args.baseline:
vulnerabilities = get_vulnerabilities_not_in_baseline(vulnerabilities, args.baseline)

Expand Down
27 changes: 16 additions & 11 deletions pyt/vulnerabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def identify_triggers(
cfg,
sources,
sinks,
lattice
lattice,
nosec_lines=set()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So identify_triggers( is never called in tests, so I don't think a default arg is needed.

):
"""Identify sources, sinks and sanitisers in a CFG.

Expand All @@ -89,12 +90,12 @@ def identify_triggers(
tainted_nodes = filter_cfg_nodes(cfg, TaintedNode)
tainted_trigger_nodes = [TriggerNode('Framework function URL parameter', None,
node) for node in tainted_nodes]
sources_in_file = find_triggers(assignment_nodes, sources)
sources_in_file = find_triggers(assignment_nodes, sources, nosec_lines)
sources_in_file.extend(tainted_trigger_nodes)

find_secondary_sources(assignment_nodes, sources_in_file, lattice)

sinks_in_file = find_triggers(cfg.nodes, sinks)
sinks_in_file = find_triggers(cfg.nodes, sinks, nosec_lines)

sanitiser_node_dict = build_sanitiser_node_dict(cfg, sinks_in_file)

Expand Down Expand Up @@ -170,7 +171,8 @@ def append_node_if_reassigned(

def find_triggers(
nodes,
trigger_words
trigger_words,
nosec_lines=set()
):
"""Find triggers from the trigger_word_list in the nodes.

Expand All @@ -183,7 +185,8 @@ def find_triggers(
"""
trigger_nodes = list()
for node in nodes:
trigger_nodes.extend(iter(label_contains(node, trigger_words)))
if node.line_number not in nosec_lines:
trigger_nodes.extend(iter(label_contains(node, trigger_words)))
return trigger_nodes


Expand Down Expand Up @@ -466,7 +469,8 @@ def find_vulnerabilities_in_cfg(
lattice,
ui_mode,
blackbox_mapping,
vulnerabilities_list
vulnerabilities_list,
nosec_lines
):
"""Find vulnerabilities in a cfg.

Expand All @@ -482,7 +486,8 @@ def find_vulnerabilities_in_cfg(
cfg,
definitions.sources,
definitions.sinks,
lattice
lattice,
nosec_lines
)
for sink in triggers.sinks:
for source in triggers.sources:
Expand All @@ -503,7 +508,8 @@ def find_vulnerabilities(
cfg_list,
analysis_type,
ui_mode,
vulnerability_files
vulnerability_files,
nosec_lines=set()
):
"""Find vulnerabilities in a list of CFGs from a trigger_word_file.

Expand All @@ -518,7 +524,6 @@ def find_vulnerabilities(
"""
vulnerabilities = list()
definitions = parse(vulnerability_files.triggers)

with open(vulnerability_files.blackbox_mapping) as infile:
blackbox_mapping = json.load(infile)
for cfg in cfg_list:
Expand All @@ -528,9 +533,9 @@ def find_vulnerabilities(
Lattice(cfg.nodes, analysis_type),
ui_mode,
blackbox_mapping,
vulnerabilities
vulnerabilities,
nosec_lines
)
with open(vulnerability_files.blackbox_mapping, 'w') as outfile:
json.dump(blackbox_mapping, outfile, indent=4)

return vulnerabilities
1 change: 1 addition & 0 deletions tests/command_line_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_no_args(self):
[-m BLACKBOX_MAPPING_FILE] [-py2] [-l LOG_LEVEL]
[-a ADAPTOR] [-db] [-dl DRAW_LATTICE [DRAW_LATTICE ...]]
[-j] [-li | -re | -rt] [-ppm] [-b BASELINE]
[--ignore-nosec]
{save,github_search} ...\n""" + \
"python -m pyt: error: one of the arguments " + \
"-f/--filepath -gr/--git-repos is required\n"
Expand Down
1 change: 0 additions & 1 deletion tests/vulnerabilities_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from pyt.node_types import Node
from pyt.reaching_definitions_taint import ReachingDefinitionsTaintAnalysis


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, sorry.

class EngineTest(BaseTestCase):
def run_empty(self):
return
Expand Down