Skip to content
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

Disable individual tests #597

Merged
merged 3 commits into from Feb 4, 2022

Conversation

mikespallino
Copy link
Contributor

This PR allows for disabling specific tests by ID (e.g. B602, B607). It also supports using test name (e.g. subprocess_popen_with_shell_equals_true) as requested in #418 .

the nosec comment behaves nicely with others and can be present anywhere in a string of comments.

If nosec is used without specific tests, it acts as it did before by blanket ignoring all tests for that line.
If nosec is used with specific tests and a test not indicated in the comments is triggered, the line will be caught and reported.

@particledecay
Copy link

Would love to see this make it in. It's especially useful as a guide in the code for others, because seeing # nosec in a large codebase likely encourages other developers to simply use the same strategy when bandit complains, rather than skip a single check.

We use the pre-commit framework and have bandit in use with it, so skipping one check on one line is way better than skipping all checks for a single line, or one check for the entire run.

@sbrunner
Copy link

sbrunner commented Mar 3, 2021

Isn't cleaner to have nosec: B602 or as pylint nosec: disable=B602?

@mikespallino
Copy link
Contributor Author

@sbrunner I think it's probably best to follow your first suggestion and be in line with noqa comments and use a colon. Using a nosec comment is meant for disabling the tests already, so I don't see the need for including the word disabled like pylint. If the comment was actually bandit instead of nosec I could see your point though because we could potentially do multiple things for bandit besides just disabling tests.

@lukehinds or @ericwb thoughts?

@pdunnigan
Copy link

+1 request a review on this PR

@naltun
Copy link

naltun commented Aug 20, 2021

Devs, please rereview this PR.

Thanks @mikespallino for the patch.

@mjeg
Copy link

mjeg commented Aug 25, 2021

👍 please add this, it would help us out.

@mikespallino mikespallino force-pushed the disable-individual-tests branch 2 times, most recently from e73f5f9 to f67796f Compare August 25, 2021 14:50
@jenstroeger
Copy link

jenstroeger commented Dec 15, 2021

Second this PR, I was looking for this exact feature! @lukehinds @ericwb @ghugo @sigmavirus24 can you please review and merge?

@sigmavirus24
Copy link
Member

This is presently not mergeable. I'm happy to review once it's up-to-date

@mikespallino mikespallino force-pushed the disable-individual-tests branch 4 times, most recently from b9d406c to 433fb15 Compare January 7, 2022 00:14
@mikespallino
Copy link
Contributor Author

Should be ready for review now @sigmavirus24

bandit/core/manager.py Outdated Show resolved Hide resolved
bandit/core/manager.py Outdated Show resolved Hide resolved
bandit/core/manager.py Outdated Show resolved Hide resolved
bandit/core/metrics.py Outdated Show resolved Hide resolved
Comment on lines 42 to 43
'nosecs_by_tests': 0,
'failed_nosecs_by_test': 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we adding these here? We don't report nosec here from what I can see this file, we only use that to exclude those lines from checks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sort of shoehorned this in here because I couldn't think of any existing structures that were better suited for tracking these until we make it to metric collection. The trouble being that we check for test failures in this module, but we report the metrics separately and I needed a way to tie these results over to metrics so that they could be reported. This is complicated by the way that this method is called within BanditNodeVisitor, so here I'm pretty much calculating these metrics on the fly while visiting the AST.

Very open to ideas on changing this around.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So our tester class has other attributes on it. In all of the visitor functions, we update scores but we could also update metrics too because we have the tester and the metrics gatherer:

self.tester = b_tester.BanditTester(
self.testset, self.debug, nosec_lines)
# in some cases we can't determine a qualified name
try:
self.namespace = b_utils.get_module_qualname_from_path(fname)
except b_utils.InvalidModulePath:
LOG.info('Unable to find qualified name for module: %s',
self.fname)
self.namespace = ""
LOG.debug('Module qualified name: %s', self.namespace)
self.metrics = metrics

So if we changed our tester to store these metrics you care about, perhaps in a metrics = {} default dictionary, we could update metrics from there before/after lines like

self.update_scores(self.tester.run_tests(self.context, 'FunctionDef'))
and add a method to the tester to "zero" them after we've consumed them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up just passing metrics into BanditTester's constructor when we instantiate it in BanditNodeVisitor's constructor so we can just call the metric function there directly, is this okay? I originally did it this way to avoid changing any return types or function signatures.

bandit/core/tester.py Outdated Show resolved Hide resolved
examples/nosec.py Outdated Show resolved Hide resolved
@mikespallino mikespallino force-pushed the disable-individual-tests branch 4 times, most recently from b4b7222 to 3595ab0 Compare January 11, 2022 01:46
@mikespallino mikespallino force-pushed the disable-individual-tests branch 2 times, most recently from 4cc9247 to abb233d Compare January 26, 2022 03:33
Copy link
Member

@sigmavirus24 sigmavirus24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So some of these comments I meant to post weeks ago but are still valid I think. Some may be duplicates because the GitHub UI is confusing in this matter and I can't tell how to remove old duplicates

bandit/core/manager.py Outdated Show resolved Hide resolved
Comment on lines 42 to 43
'nosecs_by_tests': 0,
'failed_nosecs_by_test': 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So our tester class has other attributes on it. In all of the visitor functions, we update scores but we could also update metrics too because we have the tester and the metrics gatherer:

self.tester = b_tester.BanditTester(
self.testset, self.debug, nosec_lines)
# in some cases we can't determine a qualified name
try:
self.namespace = b_utils.get_module_qualname_from_path(fname)
except b_utils.InvalidModulePath:
LOG.info('Unable to find qualified name for module: %s',
self.fname)
self.namespace = ""
LOG.debug('Module qualified name: %s', self.namespace)
self.metrics = metrics

So if we changed our tester to store these metrics you care about, perhaps in a metrics = {} default dictionary, we could update metrics from there before/after lines like

self.update_scores(self.tester.run_tests(self.context, 'FunctionDef'))
and add a method to the tester to "zero" them after we've consumed them.

bandit/core/tester.py Outdated Show resolved Hide resolved
bandit/core/manager.py Outdated Show resolved Hide resolved
bandit/core/metrics.py Outdated Show resolved Hide resolved
bandit/formatters/text.py Outdated Show resolved Hide resolved
bandit/formatters/text.py Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants