Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This codemod sandboxes calls to
requests.get
to be more resistant to Server-Side Request Forgery (SSRF) attacks.Most of the time when you make a
GET
request to a URL, you're intending to reference an HTTP endpoint, like an internal microservice. However, URLs can point to local file system files, a Gopher stream in your local network, a JAR file on a remote Internet site, and all kinds of other unexpected and undesirable outcomes. When the URL values are influenced by attackers, they can trick your application into fetching internal resources, running malicious code, or otherwise harming the system.Consider the following code for a Flask app:
In this case, an attacker could supply a value like
"http://169.254.169.254/user-data/"
and attempt to access user information.Our changes introduce sandboxing around URL creation that force developers to specify some boundaries on the types of URLs they expect to create:
This change alone reduces attack surface significantly because the default behavior of
safe_requests.get
raises aSecurityException
ifa user attempts to access a known infrastructure location, unless specifically disabled.
If you have feedback on this codemod, please let us know!
F.A.Q.
Why does this codemod require a Pixee dependency?
We always prefer to use built-in Python functions or one from a well-known and trusted community dependency. However, we cannot find any such control. If you know of one, please let us know.
Why is this codemod marked as Merge After Cursory Review?
By default, the protection only weaves in 2 checks, which we believe will not cause any issues with the vast majority of code:
However, on rare occasions an application may use a URL protocol like "file://" or "ftp://" in backend or middleware code.
If you want to allow those protocols, change the incoming PR to look more like this and get the best security possible:
Dependency Updates
This codemod relies on an external dependency. However, we were unable to automatically add the dependency to your project.
This library holds security tools for protecting Python API calls.
There are a number of places where Python project dependencies can be expressed, including
setup.py
,pyproject.toml
,setup.cfg
, andrequirements.txt
files. You may need to manually add this dependency to the proper location in your project.Manual Installation
For
setup.py
:install_requires=[ + "security==1.2.1", ],
For
pyproject.toml
(usingsetuptools
):[project] dependencies = [ + "security==1.2.1", ]
For
setup.cfg
:[options] install_requires = + security==1.2.1
For
requirements.txt
:+security==1.2.1
For more information on adding dependencies to
setuptools
projects, see the setuptools documentation.If you are using another build system, please refer to the documentation for that system to determine how to add dependencies.
More reading
I have additional improvements ready for this repo! If you want to see them, leave the comment:
... and I will open a new PR right away!
Powered by: pixeebot (codemod ID: pixee:python/url-sandbox)