-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[flake8-bandit
] Fix S412
false negative
#19009
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
Conversation
|
I think the documentation might just be wrong. I tried importing the >>> import twisted.web.twcgi.CGIScript
Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
import twisted.web.twcgi.CGIScript
ModuleNotFoundError: No module named 'twisted.web.twcgi.CGIScript'; 'twisted.web.twcgi' is not a package
>>> import twisted.web.twcgi
>>> twisted.web.twcgi.CGIScript
<class 'twisted.web.twcgi.CGIScript'> I didn't try |
Looking at the docs, it looks like you are right: from twisted.web.twcgi import CGIScript # S412
import twisted.web.twcgi
twisted.web.twcgi.CGIScript # no lint
from wsgiref.handlers import CGIHandler # S412
import wsgiref.handlers
wsgiref.handlers.CGIHandler # no lint Note This only applies to For fixing this, I see two possible paths:
|
Could you check what the upstream linter does? It's not the most satisfying answer because I think we could be more robust here by using either of your suggestions, but I'm tempted just to update the docs and leave the code alone if this is how the upstream linter works. I think checking the qualified name at the usage site rather than at the import is how the airflow rules work, for example, which I think aligns with your second suggestion. That would probably be the best solution, but I think we'd need to apply it to all of these rules. |
In import requests
import wsgiref.handlers
def application(environ, start_response):
r = requests.get('https://192.168.0.42/private/api/foobar', timeout=30)
start_response('200 OK', [('Content-Type', 'text/plain')])
return [r.content]
if __name__ == '__main__':
wsgiref.handlers.CGIHandler().run(application) from twisted.internet import reactor
from twisted.web import static, server, twcgi
root = static.File("/root")
root.putChild("login.cgi", twcgi.CGIScript("/var/www/cgi-bin/login.py"))
reactor.listenTCP(80, server.Site(root))
reactor.run() I think the functionality is coming from https://github.com/PyCQA/bandit, where it defines sets.append(
utils.build_conf_dict(
"import_httpoxy",
"B412",
issue.Cwe.IMPROPER_ACCESS_CONTROL,
[
"wsgiref.handlers.CGIHandler",
"twisted.web.twcgi.CGIScript",
"twisted.web.twcgi.CGIDirectory",
],
"Consider possible security implications associated with "
"{name} module.",
"HIGH",
)
) And checks for it in imports, import froms, and calls: |
Ah nice, I made it to Testing empirically, it looks like they do flag both imports and calls: $ cat <<EOF | uvx --with flake8-bandit flake8 --select S -
from twisted.web.twcgi import CGIScript # S412
import twisted.web.twcgi # ok
from wsgiref.handlers import CGIHandler # S412
import wsgiref.handlers # ok
twisted.web.twcgi.CGIScript() # S412
wsgiref.handlers.CGIHandler() # S412
CGIScript() # S412
EOF
Unable to find qualified name for module: stdin
stdin:1:1: S412 Consider possible security implications associated with CGIScript module.
stdin:3:1: S412 Consider possible security implications associated with CGIHandler module.
stdin:7:1: S412 Consider possible security implications associated with twisted.web.twcgi.CGIScript module.
stdin:8:1: S412 Consider possible security implications associated with wsgiref.handlers.CGIHandler module.
stdin:9:1: S412 Consider possible security implications associated with twisted.web.twcgi.CGIScript module. |
It looks like
Using things from inside the module don't get the lint (assuming they aren't part of another lint)
|
My read of the thread here is that the changes in this PR aren't really correct, and we should instead revise the rule more thoroughly. Is that right? If that's the case, we might want to close this PR and open a follow-up issue summarizing our findings here, unless you'd rather repurpose this PR. |
Sounds good, do you want to open it or should I? I don't think I have the capacity/understanding to make the needed changes. |
I'll try to write one up, but let me know if I get anything wrong! |
<!-- Thank you for contributing to Ruff/ty! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? --> ## Summary <!-- What's the purpose of the change? What does it do, and why? --> Part of #18972 This PR makes [suspicious-httpoxy-import (S412)](https://docs.astral.sh/ruff/rules/suspicious-httpoxy-import/#suspicious-httpoxy-import-s412)'s example error out-of-the-box. Since the checked imports are classes instead of modules, the example isn't valid. See #19009 for more details ``` PS ~>py -c "import wsgiref.handlers.CGIHandler" Traceback (most recent call last): File "<string>", line 1, in <module> import wsgiref.handlers.CGIHandler ModuleNotFoundError: No module named 'wsgiref.handlers.CGIHandler'; 'wsgiref.handlers' is not a package PS ~>py -c "from wsgiref.handlers import CGIHandler" PS ~> ``` [Old example](https://play.ruff.rs/bf48c901-6a46-4795-ba1d-c6af79d5c96e) ```py import wsgiref.handlers.CGIHandler ``` [New example](https://play.ruff.rs/1f0e1e60-1f0f-484a-9a17-2d0290a68f2a) ```py from wsgiref.handlers import CGIHandler ``` ## Test Plan <!-- How was it tested? --> N/A, no functionality/tests affected
Summary
Part of #18972
This PR fixes a false negative in suspicious-httpoxy-import (S412) if the suspicious import is imported directly, ie
import wsgiref.handlers.CGIHandler
andimport twisted.web.twcgi.CGIScript
Test Plan
Added additional cases to test file