-
Notifications
You must be signed in to change notification settings - Fork 244
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
Changes from 4 commits
4cd3ea5
ae9e8e9
1735db7
fb88051
d0e8ef0
8569aa4
e8cddd0
3d5867b
1502ee7
bed2f77
69d0193
3cb5186
ec6d23a
6f09912
9c4dea6
f4ebbff
7c872a0
b943310
175c235
c79161c
ed13514
0928700
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,6 +142,9 @@ def parse_args(args): | |
'(only JSON-formatted files are accepted)', | ||
type=str, | ||
default=False) | ||
parser.add_argument('-in', '--ignore-nosec', | ||
help='Ignoring nosec commands', | ||
action='store_true') | ||
|
||
save_parser = subparsers.add_parser('save', help='Save menu.') | ||
save_parser.set_defaults(which='save') | ||
|
@@ -298,15 +301,38 @@ def main(command_line_args=sys.argv[1:]): | |
|
||
analyse(cfg_list, analysis_type=analysis) | ||
|
||
nosec_lines = set() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this code if args.ignore_nosec:
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) to near the top, so we take into account |
||
vulnerabilities = find_vulnerabilities( | ||
cfg_list, | ||
analysis, | ||
ui_mode, | ||
VulnerabilityFiles( | ||
args.blackbox_mapping_file, | ||
args.trigger_word_file | ||
) | ||
), | ||
nosec_lines | ||
) | ||
|
||
if args.ignore_nosec: | ||
nosec_lines = set() | ||
else: | ||
file = open(path, "r") | ||
lines = file.readlines() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, I like this even more than .read()->.splitlines. |
||
nosec_lines = set( | ||
lineno for | ||
(lineno, line) in enumerate(lines, start=1) | ||
if '#nosec' in line or '# nosec' in line) | ||
vulnerabilities = find_vulnerabilities( | ||
cfg_list, | ||
analysis, | ||
ui_mode, | ||
VulnerabilityFiles( | ||
args.blackbox_mapping_file, | ||
args.trigger_word_file | ||
), | ||
nosec_lines | ||
) | ||
|
||
if args.baseline: | ||
vulnerabilities = get_vulnerabilities_not_in_baseline(vulnerabilities, args.baseline) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,8 @@ def identify_triggers( | |
cfg, | ||
sources, | ||
sinks, | ||
lattice | ||
lattice, | ||
nosec_lines | ||
): | ||
"""Identify sources, sinks and sanitisers in a CFG. | ||
|
||
|
@@ -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) | ||
|
||
|
@@ -170,7 +171,8 @@ def append_node_if_reassigned( | |
|
||
def find_triggers( | ||
nodes, | ||
trigger_words | ||
trigger_words, | ||
nosec_lines | ||
): | ||
"""Find triggers from the trigger_word_list in the nodes. | ||
|
||
|
@@ -183,7 +185,10 @@ 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))) | ||
else: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
pass | ||
return trigger_nodes | ||
|
||
|
||
|
@@ -466,7 +471,8 @@ def find_vulnerabilities_in_cfg( | |
lattice, | ||
ui_mode, | ||
blackbox_mapping, | ||
vulnerabilities_list | ||
vulnerabilities_list, | ||
nosec_lines | ||
): | ||
"""Find vulnerabilities in a cfg. | ||
|
||
|
@@ -482,7 +488,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: | ||
|
@@ -503,7 +510,8 @@ def find_vulnerabilities( | |
cfg_list, | ||
analysis_type, | ||
ui_mode, | ||
vulnerability_files | ||
vulnerability_files, | ||
nosec_lines | ||
): | ||
"""Find vulnerabilities in a list of CFGs from a trigger_word_file. | ||
|
||
|
@@ -518,19 +526,19 @@ 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: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I kind of liked how the newlines were in this function. |
||
find_vulnerabilities_in_cfg( | ||
cfg, | ||
definitions, | ||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of like how Bandit does this a little more https://github.com/openstack/bandit/blob/master/bandit/cli/main.py#L230
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i.e.
looks really nice.