|
3 | 3 | #
|
4 | 4 | # SPDX-License-Identifier: Apache-2.0
|
5 | 5 | import os
|
| 6 | +from contextlib import contextmanager |
6 | 7 |
|
7 | 8 | import testtools
|
8 | 9 |
|
@@ -33,6 +34,18 @@ def setUp(self):
|
33 | 34 | self.b_mgr.b_conf._settings["plugins_dir"] = path
|
34 | 35 | self.b_mgr.b_ts = b_test_set.BanditTestSet(config=b_conf)
|
35 | 36 |
|
| 37 | + @contextmanager |
| 38 | + def with_test_set(self, ts): |
| 39 | + """A helper context manager to change the test set without |
| 40 | + side-effects for any follow-up tests. |
| 41 | + """ |
| 42 | + orig_ts = self.b_mgr.b_ts |
| 43 | + self.b_mgr.b_ts = ts |
| 44 | + try: |
| 45 | + yield |
| 46 | + finally: |
| 47 | + self.b_mgr.b_ts = orig_ts |
| 48 | + |
36 | 49 | def run_example(self, example_script, ignore_nosec=False):
|
37 | 50 | """A helper method to run the specified test
|
38 | 51 |
|
@@ -526,21 +539,25 @@ def test_django_xss_secure(self):
|
526 | 539 | "SEVERITY": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 0},
|
527 | 540 | "CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 0},
|
528 | 541 | }
|
529 |
| - self.b_mgr.b_ts = b_test_set.BanditTestSet( |
530 |
| - config=self.b_mgr.b_conf, profile={"exclude": ["B308"]} |
531 |
| - ) |
532 |
| - self.check_example("mark_safe_secure.py", expect) |
| 542 | + with self.with_test_set( |
| 543 | + b_test_set.BanditTestSet( |
| 544 | + config=self.b_mgr.b_conf, profile={"exclude": ["B308"]} |
| 545 | + ) |
| 546 | + ): |
| 547 | + self.check_example("mark_safe_secure.py", expect) |
533 | 548 |
|
534 | 549 | def test_django_xss_insecure(self):
|
535 | 550 | """Test for Django XSS via django.utils.safestring"""
|
536 | 551 | expect = {
|
537 | 552 | "SEVERITY": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 29, "HIGH": 0},
|
538 | 553 | "CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 29},
|
539 | 554 | }
|
540 |
| - self.b_mgr.b_ts = b_test_set.BanditTestSet( |
541 |
| - config=self.b_mgr.b_conf, profile={"exclude": ["B308"]} |
542 |
| - ) |
543 |
| - self.check_example("mark_safe_insecure.py", expect) |
| 555 | + with self.with_test_set( |
| 556 | + b_test_set.BanditTestSet( |
| 557 | + config=self.b_mgr.b_conf, profile={"exclude": ["B308"]} |
| 558 | + ) |
| 559 | + ): |
| 560 | + self.check_example("mark_safe_insecure.py", expect) |
544 | 561 |
|
545 | 562 | def test_xml(self):
|
546 | 563 | """Test xml vulnerabilities."""
|
@@ -876,3 +893,36 @@ def test_trojansource_latin1(self):
|
876 | 893 | "CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 0},
|
877 | 894 | }
|
878 | 895 | self.check_example("trojansource_latin1.py", expect)
|
| 896 | + |
| 897 | + def test_markupsafe_markup_xss(self): |
| 898 | + expect = { |
| 899 | + "SEVERITY": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 4, "HIGH": 0}, |
| 900 | + "CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 4}, |
| 901 | + } |
| 902 | + self.check_example("markupsafe_markup_xss.py", expect) |
| 903 | + |
| 904 | + def test_markupsafe_markup_xss_extend_markup_names(self): |
| 905 | + expect = { |
| 906 | + "SEVERITY": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 2, "HIGH": 0}, |
| 907 | + "CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 2}, |
| 908 | + } |
| 909 | + b_conf = b_config.BanditConfig() |
| 910 | + b_conf.config["markupsafe_xss"] = { |
| 911 | + "extend_markup_names": ["webhelpers.html.literal"] |
| 912 | + } |
| 913 | + with self.with_test_set(b_test_set.BanditTestSet(config=b_conf)): |
| 914 | + self.check_example( |
| 915 | + "markupsafe_markup_xss_extend_markup_names.py", expect |
| 916 | + ) |
| 917 | + |
| 918 | + def test_markupsafe_markup_xss_allowed_calls(self): |
| 919 | + expect = { |
| 920 | + "SEVERITY": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 1, "HIGH": 0}, |
| 921 | + "CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 1}, |
| 922 | + } |
| 923 | + b_conf = b_config.BanditConfig() |
| 924 | + b_conf.config["markupsafe_xss"] = {"allowed_calls": ["bleach.clean"]} |
| 925 | + with self.with_test_set(b_test_set.BanditTestSet(config=b_conf)): |
| 926 | + self.check_example( |
| 927 | + "markupsafe_markup_xss_allowed_calls.py", expect |
| 928 | + ) |
0 commit comments