httpx-secure 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- httpx_secure-1.0.0/.editorconfig +16 -0
- httpx_secure-1.0.0/.envrc +3 -0
- httpx_secure-1.0.0/.github/workflows/workflow.yaml +99 -0
- httpx_secure-1.0.0/.gitignore +183 -0
- httpx_secure-1.0.0/.vscode/settings.json +3 -0
- httpx_secure-1.0.0/LICENSE +12 -0
- httpx_secure-1.0.0/PKG-INFO +114 -0
- httpx_secure-1.0.0/README.md +79 -0
- httpx_secure-1.0.0/httpx_secure/__init__.py +260 -0
- httpx_secure-1.0.0/httpx_secure/py.typed +0 -0
- httpx_secure-1.0.0/pyproject.toml +95 -0
- httpx_secure-1.0.0/shell.nix +76 -0
- httpx_secure-1.0.0/tests/conftest.py +11 -0
- httpx_secure-1.0.0/tests/test___init__.py +165 -0
- httpx_secure-1.0.0/uv.lock +254 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
root = true
|
|
2
|
+
|
|
3
|
+
[*]
|
|
4
|
+
charset = utf-8
|
|
5
|
+
end_of_line = lf
|
|
6
|
+
indent_style = space
|
|
7
|
+
indent_size = 2
|
|
8
|
+
max_line_length = 88
|
|
9
|
+
insert_final_newline = true
|
|
10
|
+
trim_trailing_whitespace = true
|
|
11
|
+
|
|
12
|
+
[*.{py,pyi}]
|
|
13
|
+
indent_size = 4
|
|
14
|
+
|
|
15
|
+
[*.md]
|
|
16
|
+
max_line_length = off
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
tags:
|
|
8
|
+
- "*"
|
|
9
|
+
pull_request:
|
|
10
|
+
branches:
|
|
11
|
+
- main
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
test:
|
|
15
|
+
strategy:
|
|
16
|
+
matrix:
|
|
17
|
+
os: [ubuntu-latest, macos-latest]
|
|
18
|
+
|
|
19
|
+
runs-on: ${{ matrix.os }}
|
|
20
|
+
steps:
|
|
21
|
+
- uses: actions/checkout@v4
|
|
22
|
+
- uses: nixbuild/nix-quick-install-action@v32
|
|
23
|
+
|
|
24
|
+
- name: Generate cache key
|
|
25
|
+
run: |
|
|
26
|
+
nixpkgs_hash=$(grep -Eom1 'archive/[0-9a-f]{40}\.tar\.gz' shell.nix | cut -d'/' -f2 | cut -d'.' -f1)
|
|
27
|
+
echo "NIXPKGS_HASH=$nixpkgs_hash" >> $GITHUB_ENV
|
|
28
|
+
echo "CACHE_KEY=${{ runner.os }}-$nixpkgs_hash" >> $GITHUB_ENV
|
|
29
|
+
|
|
30
|
+
- uses: nix-community/cache-nix-action@v6
|
|
31
|
+
with:
|
|
32
|
+
primary-key: nix-${{ env.CACHE_KEY }}
|
|
33
|
+
|
|
34
|
+
- name: Setup NIX_PATH
|
|
35
|
+
run: |
|
|
36
|
+
path=$(nix eval --impure --expr "(import (fetchTarball \"https://github.com/NixOS/nixpkgs/archive/${{ env.NIXPKGS_HASH }}.tar.gz\") {}).path")
|
|
37
|
+
echo "NIX_PATH=nixpkgs=$path" >> $GITHUB_ENV
|
|
38
|
+
|
|
39
|
+
- name: Get cache directories
|
|
40
|
+
id: cache-dirs
|
|
41
|
+
run: |
|
|
42
|
+
nix-shell -p uv --run "
|
|
43
|
+
echo \"UV_CACHE=\$(uv cache dir)\"
|
|
44
|
+
" | tee -a $GITHUB_OUTPUT
|
|
45
|
+
|
|
46
|
+
- name: Cache files and packages
|
|
47
|
+
uses: actions/cache@v4
|
|
48
|
+
with:
|
|
49
|
+
key: pkg-${{ env.CACHE_KEY }}-${{ hashFiles('uv.lock') }}
|
|
50
|
+
path: |
|
|
51
|
+
${{ steps.cache-dirs.outputs.UV_CACHE }}
|
|
52
|
+
.venv
|
|
53
|
+
|
|
54
|
+
- name: Assert tag name matches version
|
|
55
|
+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
|
56
|
+
run: |
|
|
57
|
+
version=$(nix-shell \
|
|
58
|
+
--pure --run "hatch version" 2>/dev/null | tail -n1)
|
|
59
|
+
tag_name=${{ github.ref_name }}
|
|
60
|
+
if [ "$version" != "$tag_name" ]; then
|
|
61
|
+
echo "Tag name mismatch: $version != $tag_name"
|
|
62
|
+
exit 1
|
|
63
|
+
fi
|
|
64
|
+
|
|
65
|
+
- name: Run tests
|
|
66
|
+
run: nix-shell
|
|
67
|
+
--pure --run "run-tests"
|
|
68
|
+
|
|
69
|
+
- name: Build distribution
|
|
70
|
+
run: nix-shell
|
|
71
|
+
--pure --run "uv build"
|
|
72
|
+
|
|
73
|
+
- name: Upload artifacts
|
|
74
|
+
uses: actions/upload-artifact@v4
|
|
75
|
+
with:
|
|
76
|
+
name: dist-${{ runner.os }}
|
|
77
|
+
path: dist/
|
|
78
|
+
|
|
79
|
+
publish-pypi:
|
|
80
|
+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
|
|
81
|
+
name: Publish to PyPI
|
|
82
|
+
needs: test
|
|
83
|
+
runs-on: ubuntu-latest
|
|
84
|
+
|
|
85
|
+
environment:
|
|
86
|
+
name: pypi
|
|
87
|
+
url: https://pypi.org/p/httpx-secure
|
|
88
|
+
permissions:
|
|
89
|
+
id-token: write
|
|
90
|
+
|
|
91
|
+
steps:
|
|
92
|
+
- name: Download artifacts
|
|
93
|
+
uses: actions/download-artifact@v4
|
|
94
|
+
with:
|
|
95
|
+
name: dist-${{ runner.os }}
|
|
96
|
+
path: dist/
|
|
97
|
+
|
|
98
|
+
- name: Publish to PyPI
|
|
99
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig
|
|
2
|
+
# Created by https://www.toptal.com/developers/gitignore/api/direnv,python
|
|
3
|
+
# Edit at https://www.toptal.com/developers/gitignore?templates=direnv,python
|
|
4
|
+
|
|
5
|
+
### direnv ###
|
|
6
|
+
.direnv
|
|
7
|
+
# .envrc
|
|
8
|
+
|
|
9
|
+
### Python ###
|
|
10
|
+
# Byte-compiled / optimized / DLL files
|
|
11
|
+
__pycache__/
|
|
12
|
+
*.py[cod]
|
|
13
|
+
*$py.class
|
|
14
|
+
|
|
15
|
+
# C extensions
|
|
16
|
+
*.so
|
|
17
|
+
|
|
18
|
+
# Distribution / packaging
|
|
19
|
+
.Python
|
|
20
|
+
build/
|
|
21
|
+
develop-eggs/
|
|
22
|
+
dist/
|
|
23
|
+
downloads/
|
|
24
|
+
eggs/
|
|
25
|
+
.eggs/
|
|
26
|
+
lib/
|
|
27
|
+
lib64/
|
|
28
|
+
parts/
|
|
29
|
+
sdist/
|
|
30
|
+
var/
|
|
31
|
+
wheels/
|
|
32
|
+
share/python-wheels/
|
|
33
|
+
*.egg-info/
|
|
34
|
+
.installed.cfg
|
|
35
|
+
*.egg
|
|
36
|
+
MANIFEST
|
|
37
|
+
|
|
38
|
+
# PyInstaller
|
|
39
|
+
# Usually these files are written by a python script from a template
|
|
40
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
41
|
+
*.manifest
|
|
42
|
+
*.spec
|
|
43
|
+
|
|
44
|
+
# Installer logs
|
|
45
|
+
pip-log.txt
|
|
46
|
+
pip-delete-this-directory.txt
|
|
47
|
+
|
|
48
|
+
# Unit test / coverage reports
|
|
49
|
+
htmlcov/
|
|
50
|
+
.tox/
|
|
51
|
+
.nox/
|
|
52
|
+
.coverage
|
|
53
|
+
.coverage.*
|
|
54
|
+
.cache
|
|
55
|
+
nosetests.xml
|
|
56
|
+
coverage.xml
|
|
57
|
+
*.cover
|
|
58
|
+
*.py,cover
|
|
59
|
+
.hypothesis/
|
|
60
|
+
.pytest_cache/
|
|
61
|
+
cover/
|
|
62
|
+
|
|
63
|
+
# Translations
|
|
64
|
+
*.mo
|
|
65
|
+
*.pot
|
|
66
|
+
|
|
67
|
+
# Django stuff:
|
|
68
|
+
*.log
|
|
69
|
+
local_settings.py
|
|
70
|
+
db.sqlite3
|
|
71
|
+
db.sqlite3-journal
|
|
72
|
+
|
|
73
|
+
# Flask stuff:
|
|
74
|
+
instance/
|
|
75
|
+
.webassets-cache
|
|
76
|
+
|
|
77
|
+
# Scrapy stuff:
|
|
78
|
+
.scrapy
|
|
79
|
+
|
|
80
|
+
# Sphinx documentation
|
|
81
|
+
docs/_build/
|
|
82
|
+
|
|
83
|
+
# PyBuilder
|
|
84
|
+
.pybuilder/
|
|
85
|
+
target/
|
|
86
|
+
|
|
87
|
+
# Jupyter Notebook
|
|
88
|
+
.ipynb_checkpoints
|
|
89
|
+
|
|
90
|
+
# IPython
|
|
91
|
+
profile_default/
|
|
92
|
+
ipython_config.py
|
|
93
|
+
|
|
94
|
+
# pyenv
|
|
95
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
96
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
97
|
+
# .python-version
|
|
98
|
+
|
|
99
|
+
# pipenv
|
|
100
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
101
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
102
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
103
|
+
# install all needed dependencies.
|
|
104
|
+
#Pipfile.lock
|
|
105
|
+
|
|
106
|
+
# poetry
|
|
107
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
108
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
109
|
+
# commonly ignored for libraries.
|
|
110
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
111
|
+
#poetry.lock
|
|
112
|
+
|
|
113
|
+
# pdm
|
|
114
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
115
|
+
#pdm.lock
|
|
116
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
117
|
+
# in version control.
|
|
118
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
119
|
+
.pdm.toml
|
|
120
|
+
|
|
121
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
122
|
+
__pypackages__/
|
|
123
|
+
|
|
124
|
+
# Celery stuff
|
|
125
|
+
celerybeat-schedule
|
|
126
|
+
celerybeat.pid
|
|
127
|
+
|
|
128
|
+
# SageMath parsed files
|
|
129
|
+
*.sage.py
|
|
130
|
+
|
|
131
|
+
# Environments
|
|
132
|
+
.env
|
|
133
|
+
.venv
|
|
134
|
+
env/
|
|
135
|
+
venv/
|
|
136
|
+
ENV/
|
|
137
|
+
env.bak/
|
|
138
|
+
venv.bak/
|
|
139
|
+
|
|
140
|
+
# Spyder project settings
|
|
141
|
+
.spyderproject
|
|
142
|
+
.spyproject
|
|
143
|
+
|
|
144
|
+
# Rope project settings
|
|
145
|
+
.ropeproject
|
|
146
|
+
|
|
147
|
+
# mkdocs documentation
|
|
148
|
+
/site
|
|
149
|
+
|
|
150
|
+
# mypy
|
|
151
|
+
.mypy_cache/
|
|
152
|
+
.dmypy.json
|
|
153
|
+
dmypy.json
|
|
154
|
+
|
|
155
|
+
# Pyre type checker
|
|
156
|
+
.pyre/
|
|
157
|
+
|
|
158
|
+
# pytype static type analyzer
|
|
159
|
+
.pytype/
|
|
160
|
+
|
|
161
|
+
# Cython debug symbols
|
|
162
|
+
cython_debug/
|
|
163
|
+
|
|
164
|
+
# PyCharm
|
|
165
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
166
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
167
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
168
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
169
|
+
#.idea/
|
|
170
|
+
|
|
171
|
+
### Python Patch ###
|
|
172
|
+
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
|
|
173
|
+
poetry.toml
|
|
174
|
+
|
|
175
|
+
# ruff
|
|
176
|
+
.ruff_cache/
|
|
177
|
+
|
|
178
|
+
# LSP config files
|
|
179
|
+
pyrightconfig.json
|
|
180
|
+
|
|
181
|
+
# End of https://www.toptal.com/developers/gitignore/api/direnv,python
|
|
182
|
+
|
|
183
|
+
# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
BSD Zero Clause License
|
|
2
|
+
|
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
4
|
+
purpose with or without fee is hereby granted.
|
|
5
|
+
|
|
6
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
7
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
8
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
9
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
10
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
11
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
12
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: httpx-secure
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Drop-in SSRF protection for httpx
|
|
5
|
+
Project-URL: Repository, https://github.com/Zaczero/httpx-secure
|
|
6
|
+
Project-URL: Issues, https://github.com/Zaczero/httpx-secure/issues
|
|
7
|
+
Author-email: Kamil Monicz <kamil@monicz.dev>
|
|
8
|
+
License-Expression: 0BSD
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: anyio,async,asyncio,client,dns,dns-cache,http,httpx,security,ssrf,ssrf-protection,trio,web-security
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Framework :: AnyIO
|
|
13
|
+
Classifier: Framework :: AsyncIO
|
|
14
|
+
Classifier: Framework :: Trio
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: Zero-Clause BSD (0BSD)
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
25
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
26
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
27
|
+
Classifier: Topic :: Security
|
|
28
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
29
|
+
Classifier: Topic :: System :: Networking
|
|
30
|
+
Classifier: Typing :: Typed
|
|
31
|
+
Requires-Python: >=3.9
|
|
32
|
+
Requires-Dist: anyio
|
|
33
|
+
Requires-Dist: httpx
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
# httpx-secure
|
|
37
|
+
|
|
38
|
+
[](https://pypi.org/p/httpx-secure)
|
|
39
|
+
[](https://liberapay.com/Zaczero/)
|
|
40
|
+
[](https://github.com/sponsors/Zaczero)
|
|
41
|
+
|
|
42
|
+
Drop-in SSRF protection for httpx.
|
|
43
|
+
|
|
44
|
+
## Why Use This?
|
|
45
|
+
|
|
46
|
+
- **SSRF Protection**: Block requests to private/internal IP addresses
|
|
47
|
+
- **Custom Validation**: Extend with your own validation logic
|
|
48
|
+
- **Minimal Overhead**: Efficient implementation with built-in DNS caching
|
|
49
|
+
- **Broad Python Support**: Compatible with Python 3.9+
|
|
50
|
+
- [**Semantic Versioning**](https://semver.org): Predictable, reliable updates
|
|
51
|
+
- [**Zero-Clause BSD**](https://choosealicense.com/licenses/0bsd/): Public domain, use freely anywhere
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install httpx-secure
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Quick Start
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import httpx
|
|
63
|
+
from httpx_secure import httpx_ssrf_protection
|
|
64
|
+
|
|
65
|
+
client = httpx_ssrf_protection(
|
|
66
|
+
httpx.AsyncClient(),
|
|
67
|
+
dns_cache_size=1000, # Cache up to 1000 DNS resolutions
|
|
68
|
+
dns_cache_ttl=300, # Cache for 5 minutes
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
await client.get("https://public.domain") # Allowed
|
|
72
|
+
await client.get("https://private.domain") # Blocked
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Custom Validation
|
|
76
|
+
|
|
77
|
+
For example, implement a simple domain whitelist to restrict requests to specific hosts:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
import httpx
|
|
81
|
+
from httpx_secure import httpx_ssrf_protection
|
|
82
|
+
from ipaddress import IPv4Address, IPv6Address
|
|
83
|
+
|
|
84
|
+
def custom_validator(
|
|
85
|
+
hostname: str,
|
|
86
|
+
ip: IPv4Address | IPv6Address,
|
|
87
|
+
port: int
|
|
88
|
+
) -> bool:
|
|
89
|
+
return hostname in {
|
|
90
|
+
"whitelisted.domain",
|
|
91
|
+
"webhook.partner.com",
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
client = httpx_ssrf_protection(
|
|
95
|
+
httpx.AsyncClient(),
|
|
96
|
+
custom_validator=custom_validator,
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
await client.get("https://whitelisted.domain") # Allowed
|
|
100
|
+
await client.get("https://unknown.domain") # Blocked
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## How It Works
|
|
104
|
+
|
|
105
|
+
1. **Cache Lookup**: First checks if the host has been recently validated and cached
|
|
106
|
+
2. **DNS Resolution**: If not cached, resolves the hostname to an IP address
|
|
107
|
+
3. **Validation**: Verifies the IP is globally routable, blocking private/internal addresses
|
|
108
|
+
4. **Custom Validation**: If provided, your custom validator is called for additional checks
|
|
109
|
+
5. **Request Modification**: Rewrites the request to use the validated IP directly
|
|
110
|
+
|
|
111
|
+
The DNS cache significantly reduces latency for repeated requests, while per-host locking ensures efficient concurrent resolution of parallel requests.
|
|
112
|
+
|
|
113
|
+
> [!TIP]
|
|
114
|
+
> The SSRF protection applies to all HTTP methods (GET, POST, PUT, DELETE, etc.) and automatically validates redirects to prevent SSRF attacks through redirect chains.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# httpx-secure
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/p/httpx-secure)
|
|
4
|
+
[](https://liberapay.com/Zaczero/)
|
|
5
|
+
[](https://github.com/sponsors/Zaczero)
|
|
6
|
+
|
|
7
|
+
Drop-in SSRF protection for httpx.
|
|
8
|
+
|
|
9
|
+
## Why Use This?
|
|
10
|
+
|
|
11
|
+
- **SSRF Protection**: Block requests to private/internal IP addresses
|
|
12
|
+
- **Custom Validation**: Extend with your own validation logic
|
|
13
|
+
- **Minimal Overhead**: Efficient implementation with built-in DNS caching
|
|
14
|
+
- **Broad Python Support**: Compatible with Python 3.9+
|
|
15
|
+
- [**Semantic Versioning**](https://semver.org): Predictable, reliable updates
|
|
16
|
+
- [**Zero-Clause BSD**](https://choosealicense.com/licenses/0bsd/): Public domain, use freely anywhere
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install httpx-secure
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
import httpx
|
|
28
|
+
from httpx_secure import httpx_ssrf_protection
|
|
29
|
+
|
|
30
|
+
client = httpx_ssrf_protection(
|
|
31
|
+
httpx.AsyncClient(),
|
|
32
|
+
dns_cache_size=1000, # Cache up to 1000 DNS resolutions
|
|
33
|
+
dns_cache_ttl=300, # Cache for 5 minutes
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
await client.get("https://public.domain") # Allowed
|
|
37
|
+
await client.get("https://private.domain") # Blocked
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Custom Validation
|
|
41
|
+
|
|
42
|
+
For example, implement a simple domain whitelist to restrict requests to specific hosts:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
import httpx
|
|
46
|
+
from httpx_secure import httpx_ssrf_protection
|
|
47
|
+
from ipaddress import IPv4Address, IPv6Address
|
|
48
|
+
|
|
49
|
+
def custom_validator(
|
|
50
|
+
hostname: str,
|
|
51
|
+
ip: IPv4Address | IPv6Address,
|
|
52
|
+
port: int
|
|
53
|
+
) -> bool:
|
|
54
|
+
return hostname in {
|
|
55
|
+
"whitelisted.domain",
|
|
56
|
+
"webhook.partner.com",
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
client = httpx_ssrf_protection(
|
|
60
|
+
httpx.AsyncClient(),
|
|
61
|
+
custom_validator=custom_validator,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
await client.get("https://whitelisted.domain") # Allowed
|
|
65
|
+
await client.get("https://unknown.domain") # Blocked
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## How It Works
|
|
69
|
+
|
|
70
|
+
1. **Cache Lookup**: First checks if the host has been recently validated and cached
|
|
71
|
+
2. **DNS Resolution**: If not cached, resolves the hostname to an IP address
|
|
72
|
+
3. **Validation**: Verifies the IP is globally routable, blocking private/internal addresses
|
|
73
|
+
4. **Custom Validation**: If provided, your custom validator is called for additional checks
|
|
74
|
+
5. **Request Modification**: Rewrites the request to use the validated IP directly
|
|
75
|
+
|
|
76
|
+
The DNS cache significantly reduces latency for repeated requests, while per-host locking ensures efficient concurrent resolution of parallel requests.
|
|
77
|
+
|
|
78
|
+
> [!TIP]
|
|
79
|
+
> The SSRF protection applies to all HTTP methods (GET, POST, PUT, DELETE, etc.) and automatically validates redirects to prevent SSRF attacks through redirect chains.
|