-
Notifications
You must be signed in to change notification settings - Fork 13
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
✨ Enable Ruff security rules (bandit) #1686
Conversation
Signed-off-by: zethson <[email protected]>
Great! Let's discuss this tomorrow! |
Signed-off-by: zethson <[email protected]>
Signed-off-by: zethson <[email protected]>
Signed-off-by: zethson <[email protected]>
…into feature/bandit
Signed-off-by: zethson <[email protected]>
Signed-off-by: zethson <[email protected]>
Signed-off-by: zethson <[email protected]>
Signed-off-by: zethson <[email protected]>
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1686 +/- ##
==========================================
- Coverage 92.12% 91.59% -0.53%
==========================================
Files 50 50
Lines 5496 5566 +70
==========================================
+ Hits 5063 5098 +35
- Misses 433 468 +35 ☔ View full report in Codecov by Sentry. |
Signed-off-by: zethson <[email protected]>
How does bandit compare to GitHub's CodeQL (see one of the GA jobs)? |
I don't really see any or a lot of overlap. CodeQL seems to be pretty superficial for Python and only checks the most obvious issues. The advantages of CodeQL are that we can also use it for other languages and can use it's query language if we ever need to but I don't see a use-case for us. |
Signed-off-by: zethson <[email protected]>
(coverage may go down because we have a couple of untested exceptions now). I can add coverage for these in a followup PR quickly tomorrow. |
Thanks for clarifying! For me to understand: The overlap is so small because CodeQL doesn't cover as many security issues as bandit for Python? The nature of the tools is the same in that they're both static code analyzers that detect security issues, correct? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is overall great!
The only "bad thing" concerns the few cases where exceptions now need to be triggered in tests, but it's very hard to.
I used assert
in those instances because I consciously wanted to avoid having to test these exceptions.
Because assert is only used for consistency checks that are meant to provide feedback to users/devs in some edge cases, I thought it'd be OK security-wise.
If somebody attempted to bypass these assert statements, it'd be OK: the "attacker" would get a more cryptic error behavior downstream.
Hence: In those hard-to-test cases, I'd stick with the assert rather than introducing an additional line of code and an additional test with essentially no value to users & devs. I consider maintaining that additional line and test more harmful that beneficial; in particular in a phase of this project where the code overall can hopefully still much consolidated (I mean, the code works, but it needs to be refactored to become more maintainable).
In cases where there is value to the user (say the allowed path suffixes for the vitessce export), there should never be an assert.
Correct. CodeQL can also test for unreachable code and other things but we have all of that covered with other tools already. I am currently not impressed by CodeQL but enabling it doesn't hurt.
Yeah that's fair. However, either we:
I vote for option 2. |
I think option 1 with a I care much more about simplicity than about the exact codecov coverage number. If there is no or minimal value add through additional complexity, I'd always go with what's simpler. In this case, I'd say there is zero value add in a few instances where assert was replaced. Introducing complexity both within the code and the coverage CI doesn't seem to merit it. We also need to keep in mind that whatever convention is introduced in this PR will be mandated for future contributors. Explaining people how to "not count a specific except block in coverage" seems like a lot of luggage. It's also error prone because some people might take it as an excuse to not test triggering the exception. Currently, we have this beautiful & simple rule that every exception needs to be triggered in tests. There can't be a single try except block that's not triggered. If what you suggest means starting to distinguish between "meaningful exceptions" and "exceptions we add to make bandit happy" my reaction is: ohhhhh, let's please not do this. Nobody will get it. So, unless I miss something, I vote for option 1. It's just a tiny bit of work to revert some of these exceptions back to asserts and add the |
I didn't really agree with all of your arguments because they can be made both ways, but this one is good. I'll revert to asserts where reasonable and then merge. |
How can any of the other arguments made both ways? 🤔
I think I somewhat get this. Thanks! |
Signed-off-by: zethson <[email protected]>
…into feature/bandit
Now we have to explain how to "circumvent the Bandit rule". We could ignore it in the config, but I actually think that it can be useful if developers think twice about whether this should be an internal |
Signed-off-by: zethson <[email protected]>
I fully agree with you. I think each assert statement should have a |
Signed-off-by: zethson <[email protected]>
Signed-off-by: zethson <[email protected]>
Signed-off-by: zethson <[email protected]>
Signed-off-by: zethson <[email protected]>
@@ -223,8 +223,10 @@ def set_abbr(self, value: str): | |||
else: | |||
try: | |||
self.add_synonym(value, save=False) | |||
except Exception: # pragma: no cover | |||
pass | |||
except Exception as e: # pragma: no cover |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sunnyosun - why do we need this? This seems like it should error.
@@ -209,8 +209,10 @@ def safer_read_partial(elem, indices): | |||
try: | |||
ds = CSRDataset(elem) | |||
result = _subset_sparse(ds, indices) | |||
except Exception: | |||
pass | |||
except Exception as e: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the only way this can be done, @Koncopd?
@@ -307,8 +309,10 @@ def safer_read_partial(elem, indices): # noqa | |||
try: | |||
ds = CSRDataset(elem) | |||
return _subset_sparse(ds, indices) | |||
except Exception: | |||
pass | |||
except Exception as e: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, @Koncopd.
Enabling security rules in Ruff:
Just a show off for now! We can discuss later