A library that overrides the default behaviors of the requests library, and adds new security features.
Project description
requests-hardened is a library that overrides the default behaviors of the requests library, and adds new security features.
Installation
The project is available on PyPI:
pip install requests-hardened
Features
SSRF Filters: blocks private and loopback IP ranges.
HTTP Redirects: can be used safely alongside the SSRF filter feature.
Proxy Support: proxies can be used in combination with SSRF Filters for a defense in depth.
Handy Overrides of Defaults: allows to enforce secure defaults globally, such as to mitigate DoS attacks.
Overrides of Defaults
This library allows to override some default values from the requests library that can have a security impact:
Config.never_redirect = False always reject HTTP redirects
Config.default_timeout = (2, 10) sets the default timeout value when no value or None is passed
Config.user_agent_override = None optional config to override User-Agent header. When set to None, requests library will set its default user-agent.
SSRF Filters
A SSRF IP filter can be used to reject HTTP(S) requests targeting private and loopback IP addresses.
Settings:
Config.ip_filter_enable whether or not to filter the IP addresses
ip_filter_allow_loopback_ips whether or not to allow loopback IP addresses
Proxy Support
The SSRF IP filter’s behavior with proxies are as follows:
Proxy’s IP Address: does not block private and loopback IP addresses (no filtering). Instead, the filter assumes that the proxy URL is never tainted with untrusted user input.
Target IP Address (Tunneled HTTP Requests): by default, the tunneled requests are filtered for potential SSRF attacks.
Protocols Supported: SOCKS4, SOCKS5, HTTP, and HTTPS proxy server protocols are supported.
Example Usage:
from requests_hardened import Config, Manager
http_manager = Manager(
Config(
default_timeout=(2, 10),
never_redirect=False,
# Enable SSRF IP filter
ip_filter_enable=True,
ip_filter_allow_loopback_ips=False,
)
)
# List of proxies
proxies = {
"https": "socks5://127.0.0.1:8888",
"http": "socks5://127.0.0.1:8888",
}
# Sends the HTTP request using the proxy
resp = http_manager.send_request("GET", "https://example.com", proxies=proxies)
print(resp)
Full Example
from requests_hardened import Config, Manager
# Creates a global "manager" that can be used to create ``requests.Session``
# objects with hardening in place.
http_manager = Manager(
Config(
default_timeout=(2, 10),
never_redirect=False,
ip_filter_enable=True,
ip_filter_allow_loopback_ips=False,
user_agent_override=None
)
)
# Sends an HTTP request without re-using ``requests.Session``:
resp = http_manager.send_request("GET", "https://example.com")
print(resp)
# Sends HTTP requests with reusable ``requests.Session``:
with http_manager.get_session() as sess:
sess.request("GET", "https://example.com")
sess.request("POST", "https://example.com", json={"foo": "bar"})
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file requests_hardened-1.2.0.tar.gz.
File metadata
- Download URL: requests_hardened-1.2.0.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24ff13c798a22afc3465c24ff955b003c81f605e2ec30cbdbd40f28389cfca72
|
|
| MD5 |
bc3738b668cfbd8e5549442d8e05d660
|
|
| BLAKE2b-256 |
0dab3206848b4657be7902bb10af5686f71da450d9135340ecd6ee80da718557
|
Provenance
The following attestation bundles were made for requests_hardened-1.2.0.tar.gz:
Publisher:
publish-pypi.yaml on saleor/requests-hardened
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
requests_hardened-1.2.0.tar.gz -
Subject digest:
24ff13c798a22afc3465c24ff955b003c81f605e2ec30cbdbd40f28389cfca72 - Sigstore transparency entry: 563389791
- Sigstore integration time:
-
Permalink:
saleor/requests-hardened@416ebea59c7e92378f91e8c144988b3c879c16d7 -
Branch / Tag:
refs/tags/v1.2.0 - Owner: https://github.com/saleor
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yaml@416ebea59c7e92378f91e8c144988b3c879c16d7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file requests_hardened-1.2.0-py3-none-any.whl.
File metadata
- Download URL: requests_hardened-1.2.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d70b38bbfdea3f1d27d9149a5967f8c350b3496d232b1d4b031b7d0f2590ba9
|
|
| MD5 |
b38db3d25f37ab517cbc67b3a59ecf2d
|
|
| BLAKE2b-256 |
8d0eb521e2034f0984b3a446009223e8ec67bfae5e3d4a11b0066951d2df6515
|
Provenance
The following attestation bundles were made for requests_hardened-1.2.0-py3-none-any.whl:
Publisher:
publish-pypi.yaml on saleor/requests-hardened
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
requests_hardened-1.2.0-py3-none-any.whl -
Subject digest:
7d70b38bbfdea3f1d27d9149a5967f8c350b3496d232b1d4b031b7d0f2590ba9 - Sigstore transparency entry: 563389792
- Sigstore integration time:
-
Permalink:
saleor/requests-hardened@416ebea59c7e92378f91e8c144988b3c879c16d7 -
Branch / Tag:
refs/tags/v1.2.0 - Owner: https://github.com/saleor
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yaml@416ebea59c7e92378f91e8c144988b3c879c16d7 -
Trigger Event:
release
-
Statement type: