From 541a8fae7b12abe0a78321b3b61b6105b639fd9d Mon Sep 17 00:00:00 2001 From: ehooo Date: Thu, 27 Feb 2020 23:51:29 +0100 Subject: [PATCH 1/2] Improve detection of "nosec" clause --- bandit/core/manager.py | 9 +++++++-- tests/functional/test_runtime.py | 8 +------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/bandit/core/manager.py b/bandit/core/manager.py index 956d0673b..298812c2d 100644 --- a/bandit/core/manager.py +++ b/bandit/core/manager.py @@ -264,9 +264,14 @@ def _parse_file(self, fname, fdata, new_files_list): lines = data.splitlines() self.metrics.begin(fname) self.metrics.count_locs(lines) - if self.ignore_nosec: - nosec_lines = set() + + nosec_lines = set() + if not six.PY2 and isinstance(data, bytes): + has_nosec = b'nosec' in data else: + has_nosec = 'nosec' in data + + if not self.ignore_nosec and has_nosec: try: fdata.seek(0) if six.PY2: diff --git a/tests/functional/test_runtime.py b/tests/functional/test_runtime.py index b77a606b9..d581c7109 100644 --- a/tests/functional/test_runtime.py +++ b/tests/functional/test_runtime.py @@ -5,7 +5,6 @@ import os import subprocess -import six import testtools @@ -103,12 +102,7 @@ def test_example_nonsense2(self): ) self.assertEqual(0, retcode) self.assertIn("Files skipped (1):", output) - if six.PY2: - self.assertIn("nonsense2.py (exception while scanning file)", - output) - else: - self.assertIn("nonsense2.py (syntax error while parsing AST", - output) + self.assertIn("nonsense2.py (exception while scanning file)", output) def test_example_imports(self): (retcode, output) = self._test_example(['bandit', ], ['imports.py', ]) From 1e74f6282209f4e008a34874cce072b3aa9d2193 Mon Sep 17 00:00:00 2001 From: Eric Brown Date: Mon, 7 Feb 2022 19:14:15 -0800 Subject: [PATCH 2/2] Update manager.py --- bandit/core/manager.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bandit/core/manager.py b/bandit/core/manager.py index 298812c2d..e0309196d 100644 --- a/bandit/core/manager.py +++ b/bandit/core/manager.py @@ -266,10 +266,7 @@ def _parse_file(self, fname, fdata, new_files_list): self.metrics.count_locs(lines) nosec_lines = set() - if not six.PY2 and isinstance(data, bytes): - has_nosec = b'nosec' in data - else: - has_nosec = 'nosec' in data + has_nosec = b'nosec' in data if not self.ignore_nosec and has_nosec: try: