Skip to content

Commit 9750181

Browse files
authored
Fix AttributeError on detect of tuple assign condition (#931)
In a specific example where a tuple is assigned to a call such as a choice of options, Bandit throws a traceback due to an assumption the assign is to a value of another tuple instead of a call. This change will avoid the traceback, but not necessarily help in detection of an XSS in this example. Fixes #520 Signed-off-by: Eric Brown <[email protected]>
1 parent 6a00317 commit 9750181

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

bandit/plugins/django_xss.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def is_assigned(self, node):
7070
if isinstance(target, ast.Name):
7171
if target.id == self.var_name.id:
7272
assigned = node.value
73-
elif isinstance(target, ast.Tuple):
73+
elif isinstance(target, ast.Tuple) and isinstance(
74+
node.value, ast.Tuple
75+
):
7476
pos = 0
7577
for name in target.elts:
7678
if name.id == self.var_name.id:

examples/mark_safe_insecure.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,11 @@ def test_insecure_with_assign(str_arg=None):
157157
if not str_arg:
158158
str_arg = 'could be insecure'
159159
safestring.mark_safe(str_arg)
160+
161+
def test_insecure_tuple_assign():
162+
HTML_CHOICES = (
163+
(_('Donate'), 'https://example.org/donate/'),
164+
(_('More info'), 'https://example.org/'),
165+
)
166+
text, url = choice(HTML_CHOICES)
167+
safestring.mark_safe('<a href="{0}">{1}</a>'.format(url, text))

tests/functional/test_functional.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ def test_django_xss_secure(self):
547547
def test_django_xss_insecure(self):
548548
"""Test for Django XSS via django.utils.safestring"""
549549
expect = {
550-
"SEVERITY": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 28, "HIGH": 0},
551-
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 28},
550+
"SEVERITY": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 29, "HIGH": 0},
551+
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 29},
552552
}
553553
self.b_mgr.b_ts = b_test_set.BanditTestSet(
554554
config=self.b_mgr.b_conf, profile={"exclude": ["B308"]}

0 commit comments

Comments
 (0)