Skip to content

Commit ecd53cc

Browse files
committed
Fix nosec for nested dicts
Before this commit nosec was searched from the begnning of the expression's context, which may be broader than the exact piece of code that a developer wants to skip. This caused, that for the below example: 1. example = { 2. 'S3_CONFIG_PARAMS': dict( # nosec B106 3. ... 4. ), 5. 'LOCALFS_BASEDIR': '/var/tmp/herp', # nosec B108 6. } for line 5, nosec from line 2 was returned. Thus `nosec B108` was ignored. This commit changes the algorithm that search for nosec for an expression and nosec from the exact line of the expression is preferred. Resolves: PyCQA#1003
1 parent 02d73e9 commit ecd53cc

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

bandit/core/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ def check_ast_node(name):
373373

374374

375375
def get_nosec(nosec_lines, context):
376-
for lineno in context["linerange"]:
377-
nosec = nosec_lines.get(lineno, None)
376+
for lineno in [context["lineno"], *context["linerange"]]:
377+
nosec = nosec_lines.get(lineno)
378378
if nosec is not None:
379379
return nosec
380380
return None

examples/nosec.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@
1313
subprocess.Popen('/bin/ls *', shell=True) # type: ... # noqa: E501 ; pylint: disable=line-too-long # nosec
1414
subprocess.Popen('#nosec', shell=True) # nosec B607, B101
1515
subprocess.Popen('#nosec', shell=True) # nosec B602, subprocess_popen_with_shell_equals_true
16+
# check that nosec in nested dict does not cause "higher" annotations to be ignored
17+
# reproduction of https://github.com/PyCQA/bandit/issues/1003
18+
example = {
19+
'S3_CONFIG_PARAMS': dict( # nosec B106
20+
aws_access_key_id='key_goes_here',
21+
aws_secret_access_key='secret_goes_here',
22+
endpoint_url='s3.amazonaws.com',
23+
),
24+
'LOCALFS_BASEDIR': '/var/tmp/herp', # nosec B108
25+
'ALPINE_APORTS_DIR': '/tmp/derp', # nosec B108
26+
}

0 commit comments

Comments
 (0)