secure 2.0.0rc1__tar.gz → 2.0.2__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.
- secure-2.0.2/MANIFEST.in +4 -0
- secure-2.0.2/PKG-INFO +304 -0
- secure-2.0.2/README.md +276 -0
- {secure-2.0.0rc1 → secure-2.0.2}/pyproject.toml +30 -5
- secure-2.0.2/secure/_internal/__init__.py +1 -0
- secure-2.0.2/secure/_internal/configured_headers.py +122 -0
- secure-2.0.2/secure/_internal/constants.py +39 -0
- secure-2.0.2/secure/_internal/emit.py +138 -0
- secure-2.0.2/secure/_internal/normalize.py +164 -0
- secure-2.0.2/secure/_internal/policy.py +167 -0
- secure-2.0.2/secure/_internal/presets.py +112 -0
- secure-2.0.2/secure/_internal/types.py +21 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/custom_header.py +1 -1
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/server.py +3 -3
- {secure-2.0.0rc1 → secure-2.0.2}/secure/middleware/asgi.py +1 -1
- {secure-2.0.0rc1 → secure-2.0.2}/secure/middleware/wsgi.py +1 -1
- secure-2.0.2/secure/secure.py +503 -0
- secure-2.0.2/secure.egg-info/PKG-INFO +304 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure.egg-info/SOURCES.txt +9 -0
- secure-2.0.0rc1/PKG-INFO +0 -703
- secure-2.0.0rc1/README.md +0 -675
- secure-2.0.0rc1/secure/secure.py +0 -983
- secure-2.0.0rc1/secure.egg-info/PKG-INFO +0 -703
- {secure-2.0.0rc1 → secure-2.0.2}/LICENSE +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/__init__.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/__init__.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/_validation.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/base_header.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/cache_control.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/content_security_policy.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/cross_origin_embedder_policy.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/cross_origin_opener_policy.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/cross_origin_resource_policy.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/permissions_policy.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/referrer_policy.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/strict_transport_security.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/x_content_type_options.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/x_dns_prefetch_control.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/x_frame_options.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/headers/x_permitted_cross_domain_policies.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/middleware/__init__.py +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure/py.typed +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure.egg-info/dependency_links.txt +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/secure.egg-info/top_level.txt +0 -0
- {secure-2.0.0rc1 → secure-2.0.2}/setup.cfg +0 -0
secure-2.0.2/MANIFEST.in
ADDED
secure-2.0.2/PKG-INFO
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: secure
|
|
3
|
+
Version: 2.0.2
|
|
4
|
+
Summary: A lightweight package that adds security headers for Python web frameworks.
|
|
5
|
+
Author-email: Caleb Kinney <caleb@typeerror.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/TypeError/secure
|
|
8
|
+
Project-URL: Documentation, https://github.com/TypeError/secure/tree/main/docs
|
|
9
|
+
Project-URL: Repository, https://github.com/TypeError/secure
|
|
10
|
+
Project-URL: Issue Tracker, https://github.com/TypeError/secure/issues
|
|
11
|
+
Keywords: security,headers,web,framework,HTTP
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# secure
|
|
30
|
+
|
|
31
|
+
[](https://pypi.org/project/secure/) [](https://pypi.org/project/secure/) [](https://github.com/TypeError/secure/blob/main/LICENSE)
|
|
32
|
+
|
|
33
|
+
Define HTTP security headers once. Apply them consistently across Python web apps.
|
|
34
|
+
|
|
35
|
+
`secure` provides a small, dependency-free API for configuring modern security headers across common Python web frameworks through ASGI middleware, WSGI middleware, or framework response hooks.
|
|
36
|
+
|
|
37
|
+
Use it when you want to avoid copy-pasted header strings spread across handlers, hooks, and middleware.
|
|
38
|
+
|
|
39
|
+
<p align="center">
|
|
40
|
+
<img src="https://typeerror.com/assets/secure-hex.png" alt="secure hex" width="200">
|
|
41
|
+
</p>
|
|
42
|
+
|
|
43
|
+
Quick links: [Quick start](#quick-start) · [Headers](#what-headers-are-applied-by-default) · [Middleware](#middleware)
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Why use `secure`
|
|
48
|
+
|
|
49
|
+
Setting headers manually is fine for one endpoint. It gets harder to review when values are copied across routes, response hooks, reverse-proxy settings, and different framework integrations.
|
|
50
|
+
|
|
51
|
+
`secure` helps you:
|
|
52
|
+
|
|
53
|
+
- Keep one `Secure` policy object instead of scattered header strings
|
|
54
|
+
- Apply the same policy through ASGI middleware, WSGI middleware, or response hooks
|
|
55
|
+
- Start with practical presets, then customize headers for your application
|
|
56
|
+
- Use builders for complex headers such as Content Security Policy and Permissions Policy
|
|
57
|
+
- Make duplicate handling, header overwrites, and validation explicit
|
|
58
|
+
|
|
59
|
+
The defaults are a reasonable starting point, not a substitute for application-specific review. Content Security Policy in particular should be adjusted for the scripts, styles, assets, and third-party services your app actually uses.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Installation
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
uv add secure
|
|
67
|
+
# or
|
|
68
|
+
pip install secure
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Quick start
|
|
74
|
+
|
|
75
|
+
### FastAPI / ASGI middleware
|
|
76
|
+
|
|
77
|
+
Use middleware when you can attach `secure` once and cover the whole application.
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from fastapi import FastAPI
|
|
81
|
+
from secure import Secure
|
|
82
|
+
from secure.middleware import SecureASGIMiddleware
|
|
83
|
+
|
|
84
|
+
app = FastAPI()
|
|
85
|
+
secure_headers = Secure.with_default_headers()
|
|
86
|
+
|
|
87
|
+
app.add_middleware(SecureASGIMiddleware, secure=secure_headers)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Response hooks and handlers
|
|
91
|
+
|
|
92
|
+
Use the same policy object in framework hooks, middleware callbacks, or handlers that expose a response object with headers support.
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from secure import Secure
|
|
96
|
+
|
|
97
|
+
secure_headers = Secure.with_default_headers()
|
|
98
|
+
|
|
99
|
+
secure_headers.set_headers(response)
|
|
100
|
+
# or
|
|
101
|
+
await secure_headers.set_headers_async(response)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`Secure.with_default_headers()` maps to `Preset.BALANCED`, a practical default designed to be customized.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## What headers are applied by default
|
|
109
|
+
|
|
110
|
+
```http
|
|
111
|
+
Cross-Origin-Opener-Policy: same-origin
|
|
112
|
+
Cross-Origin-Resource-Policy: same-origin
|
|
113
|
+
Content-Security-Policy: default-src 'self'; base-uri 'self'; font-src 'self' https: data:; form-action 'self'; frame-ancestors 'self'; img-src 'self' data:; object-src 'none'; script-src 'self'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; upgrade-insecure-requests
|
|
114
|
+
Strict-Transport-Security: max-age=31536000; includeSubDomains
|
|
115
|
+
Permissions-Policy: geolocation=(), microphone=(), camera=()
|
|
116
|
+
Referrer-Policy: strict-origin-when-cross-origin
|
|
117
|
+
X-Content-Type-Options: nosniff
|
|
118
|
+
X-Frame-Options: SAMEORIGIN
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
This preset reflects modern browser guidance from MDN and OWASP. It reduces cross-origin risk, prevents MIME sniffing, and provides a conservative Content Security Policy you can extend.
|
|
122
|
+
|
|
123
|
+
The default CSP avoids unsafe script execution by default, but CSP is context-sensitive. Interactive apps, dashboards, CDNs, analytics, embedded content, or other third-party integrations may require additional configuration.
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## Presets
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
from secure import Preset, Secure
|
|
131
|
+
|
|
132
|
+
Secure.from_preset(Preset.BALANCED)
|
|
133
|
+
Secure.from_preset(Preset.BASIC)
|
|
134
|
+
Secure.from_preset(Preset.STRICT)
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
- **BALANCED**: practical default for many applications
|
|
138
|
+
- **BASIC**: Helmet-style compatibility
|
|
139
|
+
- **STRICT**: tighter CSP and isolation
|
|
140
|
+
|
|
141
|
+
Start with BALANCED, review the generated headers, and move stricter only when needed.
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## Middleware
|
|
146
|
+
|
|
147
|
+
`secure` provides both ASGI and WSGI middleware.
|
|
148
|
+
|
|
149
|
+
- Overwrites headers by default
|
|
150
|
+
- Allows controlled duplication via `multi_ok`
|
|
151
|
+
- Only modifies HTTP responses in ASGI apps
|
|
152
|
+
|
|
153
|
+
### ASGI
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from secure import Secure
|
|
157
|
+
from secure.middleware import SecureASGIMiddleware
|
|
158
|
+
|
|
159
|
+
secure_headers = Secure.with_default_headers()
|
|
160
|
+
app = SecureASGIMiddleware(app, secure=secure_headers)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### WSGI
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
from secure import Secure
|
|
167
|
+
from secure.middleware import SecureWSGIMiddleware
|
|
168
|
+
|
|
169
|
+
secure_headers = Secure.with_default_headers()
|
|
170
|
+
app = SecureWSGIMiddleware(app, secure=secure_headers)
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Django example
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
from secure import Secure
|
|
177
|
+
|
|
178
|
+
class SecureHeadersMiddleware:
|
|
179
|
+
def __init__(self, get_response):
|
|
180
|
+
self.get_response = get_response
|
|
181
|
+
self.secure = Secure.with_default_headers()
|
|
182
|
+
|
|
183
|
+
def __call__(self, request):
|
|
184
|
+
response = self.get_response(request)
|
|
185
|
+
self.secure.set_headers(response)
|
|
186
|
+
return response
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## Framework integration examples
|
|
192
|
+
|
|
193
|
+
These examples show common integration paths. See the full [framework integration guide](https://github.com/TypeError/secure/tree/main/docs/frameworks.md) for additional frameworks and notes about first-class middleware versus minimal fallback examples.
|
|
194
|
+
|
|
195
|
+
### FastAPI
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
from secure.middleware import SecureASGIMiddleware
|
|
199
|
+
|
|
200
|
+
app.add_middleware(SecureASGIMiddleware, secure=secure_headers)
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Flask
|
|
204
|
+
|
|
205
|
+
```python
|
|
206
|
+
@app.after_request
|
|
207
|
+
def add_security_headers(response):
|
|
208
|
+
secure_headers.set_headers(response)
|
|
209
|
+
return response
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Shiny for Python
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
from shiny import App
|
|
216
|
+
from secure.middleware import SecureASGIMiddleware
|
|
217
|
+
|
|
218
|
+
app = SecureASGIMiddleware(App(), secure=secure_headers)
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Interactive apps often need CSP configuration for their script, style, and asset loading patterns.
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
## Policy builders
|
|
226
|
+
|
|
227
|
+
### Content Security Policy
|
|
228
|
+
|
|
229
|
+
```python
|
|
230
|
+
from secure.headers import ContentSecurityPolicy
|
|
231
|
+
|
|
232
|
+
csp = (
|
|
233
|
+
ContentSecurityPolicy()
|
|
234
|
+
.default_src("'self'")
|
|
235
|
+
.script_src("'self'", "cdn.example.com")
|
|
236
|
+
)
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Permissions Policy
|
|
240
|
+
|
|
241
|
+
```python
|
|
242
|
+
from secure.headers import PermissionsPolicy
|
|
243
|
+
|
|
244
|
+
permissions = (
|
|
245
|
+
PermissionsPolicy()
|
|
246
|
+
.geolocation("'self'")
|
|
247
|
+
.camera("'none'")
|
|
248
|
+
)
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
## Header pipeline and validation
|
|
254
|
+
|
|
255
|
+
Use the optional pipeline when you need stricter control:
|
|
256
|
+
|
|
257
|
+
```python
|
|
258
|
+
secure_headers = (
|
|
259
|
+
Secure.with_default_headers()
|
|
260
|
+
.allowlist_headers(...)
|
|
261
|
+
.deduplicate_headers(...)
|
|
262
|
+
.validate_and_normalize_headers(...)
|
|
263
|
+
)
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
This makes header allowlists, deduplication, normalization, and validation explicit instead of leaving those choices spread across application code.
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
## Requirements
|
|
271
|
+
|
|
272
|
+
- Python 3.10+
|
|
273
|
+
- No external dependencies
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
|
|
277
|
+
## Documentation
|
|
278
|
+
|
|
279
|
+
Read the full [documentation](https://github.com/TypeError/secure/tree/main/docs).
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## Learn more
|
|
284
|
+
|
|
285
|
+
- [MDN HTTP Headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)
|
|
286
|
+
- [OWASP Secure Headers Project](https://owasp.org/www-project-secure-headers/)
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
## License
|
|
291
|
+
|
|
292
|
+
MIT License
|
|
293
|
+
|
|
294
|
+
---
|
|
295
|
+
|
|
296
|
+
## Contributing
|
|
297
|
+
|
|
298
|
+
Issues and pull requests are welcome.
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
## Acknowledgements
|
|
303
|
+
|
|
304
|
+
Built using guidance from MDN and OWASP secure headers recommendations.
|
secure-2.0.2/README.md
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
# secure
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/secure/) [](https://pypi.org/project/secure/) [](https://github.com/TypeError/secure/blob/main/LICENSE)
|
|
4
|
+
|
|
5
|
+
Define HTTP security headers once. Apply them consistently across Python web apps.
|
|
6
|
+
|
|
7
|
+
`secure` provides a small, dependency-free API for configuring modern security headers across common Python web frameworks through ASGI middleware, WSGI middleware, or framework response hooks.
|
|
8
|
+
|
|
9
|
+
Use it when you want to avoid copy-pasted header strings spread across handlers, hooks, and middleware.
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<img src="https://typeerror.com/assets/secure-hex.png" alt="secure hex" width="200">
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
Quick links: [Quick start](#quick-start) · [Headers](#what-headers-are-applied-by-default) · [Middleware](#middleware)
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Why use `secure`
|
|
20
|
+
|
|
21
|
+
Setting headers manually is fine for one endpoint. It gets harder to review when values are copied across routes, response hooks, reverse-proxy settings, and different framework integrations.
|
|
22
|
+
|
|
23
|
+
`secure` helps you:
|
|
24
|
+
|
|
25
|
+
- Keep one `Secure` policy object instead of scattered header strings
|
|
26
|
+
- Apply the same policy through ASGI middleware, WSGI middleware, or response hooks
|
|
27
|
+
- Start with practical presets, then customize headers for your application
|
|
28
|
+
- Use builders for complex headers such as Content Security Policy and Permissions Policy
|
|
29
|
+
- Make duplicate handling, header overwrites, and validation explicit
|
|
30
|
+
|
|
31
|
+
The defaults are a reasonable starting point, not a substitute for application-specific review. Content Security Policy in particular should be adjusted for the scripts, styles, assets, and third-party services your app actually uses.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
uv add secure
|
|
39
|
+
# or
|
|
40
|
+
pip install secure
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Quick start
|
|
46
|
+
|
|
47
|
+
### FastAPI / ASGI middleware
|
|
48
|
+
|
|
49
|
+
Use middleware when you can attach `secure` once and cover the whole application.
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from fastapi import FastAPI
|
|
53
|
+
from secure import Secure
|
|
54
|
+
from secure.middleware import SecureASGIMiddleware
|
|
55
|
+
|
|
56
|
+
app = FastAPI()
|
|
57
|
+
secure_headers = Secure.with_default_headers()
|
|
58
|
+
|
|
59
|
+
app.add_middleware(SecureASGIMiddleware, secure=secure_headers)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Response hooks and handlers
|
|
63
|
+
|
|
64
|
+
Use the same policy object in framework hooks, middleware callbacks, or handlers that expose a response object with headers support.
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from secure import Secure
|
|
68
|
+
|
|
69
|
+
secure_headers = Secure.with_default_headers()
|
|
70
|
+
|
|
71
|
+
secure_headers.set_headers(response)
|
|
72
|
+
# or
|
|
73
|
+
await secure_headers.set_headers_async(response)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`Secure.with_default_headers()` maps to `Preset.BALANCED`, a practical default designed to be customized.
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## What headers are applied by default
|
|
81
|
+
|
|
82
|
+
```http
|
|
83
|
+
Cross-Origin-Opener-Policy: same-origin
|
|
84
|
+
Cross-Origin-Resource-Policy: same-origin
|
|
85
|
+
Content-Security-Policy: default-src 'self'; base-uri 'self'; font-src 'self' https: data:; form-action 'self'; frame-ancestors 'self'; img-src 'self' data:; object-src 'none'; script-src 'self'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; upgrade-insecure-requests
|
|
86
|
+
Strict-Transport-Security: max-age=31536000; includeSubDomains
|
|
87
|
+
Permissions-Policy: geolocation=(), microphone=(), camera=()
|
|
88
|
+
Referrer-Policy: strict-origin-when-cross-origin
|
|
89
|
+
X-Content-Type-Options: nosniff
|
|
90
|
+
X-Frame-Options: SAMEORIGIN
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
This preset reflects modern browser guidance from MDN and OWASP. It reduces cross-origin risk, prevents MIME sniffing, and provides a conservative Content Security Policy you can extend.
|
|
94
|
+
|
|
95
|
+
The default CSP avoids unsafe script execution by default, but CSP is context-sensitive. Interactive apps, dashboards, CDNs, analytics, embedded content, or other third-party integrations may require additional configuration.
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Presets
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
from secure import Preset, Secure
|
|
103
|
+
|
|
104
|
+
Secure.from_preset(Preset.BALANCED)
|
|
105
|
+
Secure.from_preset(Preset.BASIC)
|
|
106
|
+
Secure.from_preset(Preset.STRICT)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
- **BALANCED**: practical default for many applications
|
|
110
|
+
- **BASIC**: Helmet-style compatibility
|
|
111
|
+
- **STRICT**: tighter CSP and isolation
|
|
112
|
+
|
|
113
|
+
Start with BALANCED, review the generated headers, and move stricter only when needed.
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## Middleware
|
|
118
|
+
|
|
119
|
+
`secure` provides both ASGI and WSGI middleware.
|
|
120
|
+
|
|
121
|
+
- Overwrites headers by default
|
|
122
|
+
- Allows controlled duplication via `multi_ok`
|
|
123
|
+
- Only modifies HTTP responses in ASGI apps
|
|
124
|
+
|
|
125
|
+
### ASGI
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from secure import Secure
|
|
129
|
+
from secure.middleware import SecureASGIMiddleware
|
|
130
|
+
|
|
131
|
+
secure_headers = Secure.with_default_headers()
|
|
132
|
+
app = SecureASGIMiddleware(app, secure=secure_headers)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### WSGI
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from secure import Secure
|
|
139
|
+
from secure.middleware import SecureWSGIMiddleware
|
|
140
|
+
|
|
141
|
+
secure_headers = Secure.with_default_headers()
|
|
142
|
+
app = SecureWSGIMiddleware(app, secure=secure_headers)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Django example
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
from secure import Secure
|
|
149
|
+
|
|
150
|
+
class SecureHeadersMiddleware:
|
|
151
|
+
def __init__(self, get_response):
|
|
152
|
+
self.get_response = get_response
|
|
153
|
+
self.secure = Secure.with_default_headers()
|
|
154
|
+
|
|
155
|
+
def __call__(self, request):
|
|
156
|
+
response = self.get_response(request)
|
|
157
|
+
self.secure.set_headers(response)
|
|
158
|
+
return response
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## Framework integration examples
|
|
164
|
+
|
|
165
|
+
These examples show common integration paths. See the full [framework integration guide](https://github.com/TypeError/secure/tree/main/docs/frameworks.md) for additional frameworks and notes about first-class middleware versus minimal fallback examples.
|
|
166
|
+
|
|
167
|
+
### FastAPI
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
from secure.middleware import SecureASGIMiddleware
|
|
171
|
+
|
|
172
|
+
app.add_middleware(SecureASGIMiddleware, secure=secure_headers)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Flask
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
@app.after_request
|
|
179
|
+
def add_security_headers(response):
|
|
180
|
+
secure_headers.set_headers(response)
|
|
181
|
+
return response
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Shiny for Python
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
from shiny import App
|
|
188
|
+
from secure.middleware import SecureASGIMiddleware
|
|
189
|
+
|
|
190
|
+
app = SecureASGIMiddleware(App(), secure=secure_headers)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Interactive apps often need CSP configuration for their script, style, and asset loading patterns.
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## Policy builders
|
|
198
|
+
|
|
199
|
+
### Content Security Policy
|
|
200
|
+
|
|
201
|
+
```python
|
|
202
|
+
from secure.headers import ContentSecurityPolicy
|
|
203
|
+
|
|
204
|
+
csp = (
|
|
205
|
+
ContentSecurityPolicy()
|
|
206
|
+
.default_src("'self'")
|
|
207
|
+
.script_src("'self'", "cdn.example.com")
|
|
208
|
+
)
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### Permissions Policy
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
from secure.headers import PermissionsPolicy
|
|
215
|
+
|
|
216
|
+
permissions = (
|
|
217
|
+
PermissionsPolicy()
|
|
218
|
+
.geolocation("'self'")
|
|
219
|
+
.camera("'none'")
|
|
220
|
+
)
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
## Header pipeline and validation
|
|
226
|
+
|
|
227
|
+
Use the optional pipeline when you need stricter control:
|
|
228
|
+
|
|
229
|
+
```python
|
|
230
|
+
secure_headers = (
|
|
231
|
+
Secure.with_default_headers()
|
|
232
|
+
.allowlist_headers(...)
|
|
233
|
+
.deduplicate_headers(...)
|
|
234
|
+
.validate_and_normalize_headers(...)
|
|
235
|
+
)
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
This makes header allowlists, deduplication, normalization, and validation explicit instead of leaving those choices spread across application code.
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## Requirements
|
|
243
|
+
|
|
244
|
+
- Python 3.10+
|
|
245
|
+
- No external dependencies
|
|
246
|
+
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
## Documentation
|
|
250
|
+
|
|
251
|
+
Read the full [documentation](https://github.com/TypeError/secure/tree/main/docs).
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## Learn more
|
|
256
|
+
|
|
257
|
+
- [MDN HTTP Headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)
|
|
258
|
+
- [OWASP Secure Headers Project](https://owasp.org/www-project-secure-headers/)
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## License
|
|
263
|
+
|
|
264
|
+
MIT License
|
|
265
|
+
|
|
266
|
+
---
|
|
267
|
+
|
|
268
|
+
## Contributing
|
|
269
|
+
|
|
270
|
+
Issues and pull requests are welcome.
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
274
|
+
## Acknowledgements
|
|
275
|
+
|
|
276
|
+
Built using guidance from MDN and OWASP secure headers recommendations.
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["setuptools>=
|
|
2
|
+
requires = ["setuptools>=84.2.1", "wheel>=0.48.0"]
|
|
3
3
|
build-backend = "setuptools.build_meta"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "secure"
|
|
7
|
-
version = "2.0.
|
|
7
|
+
version = "2.0.2"
|
|
8
8
|
description = "A lightweight package that adds security headers for Python web frameworks."
|
|
9
9
|
readme = { file = "README.md", content-type = "text/markdown" }
|
|
10
10
|
license = "MIT"
|
|
@@ -13,7 +13,7 @@ authors = [{ name = "Caleb Kinney", email = "caleb@typeerror.com" }]
|
|
|
13
13
|
requires-python = ">=3.10"
|
|
14
14
|
keywords = ["security", "headers", "web", "framework", "HTTP"]
|
|
15
15
|
classifiers = [
|
|
16
|
-
"Development Status ::
|
|
16
|
+
"Development Status :: 5 - Production/Stable",
|
|
17
17
|
"Intended Audience :: Developers",
|
|
18
18
|
"Operating System :: OS Independent",
|
|
19
19
|
"Programming Language :: Python :: 3",
|
|
@@ -42,13 +42,17 @@ exclude = ["tests*", "docs*"]
|
|
|
42
42
|
[tool.setuptools.package-data]
|
|
43
43
|
secure = ["py.typed"]
|
|
44
44
|
|
|
45
|
+
[tool.pytest.ini_options]
|
|
46
|
+
pythonpath = ["."]
|
|
47
|
+
testpaths = ["tests"]
|
|
48
|
+
|
|
45
49
|
# --- Ruff (formatter + linter) ---
|
|
46
50
|
[tool.ruff]
|
|
47
51
|
target-version = "py310"
|
|
48
52
|
line-length = 120
|
|
49
53
|
src = ["secure"]
|
|
50
54
|
exclude = ["build", "dist", ".venv", "**/__pycache__"]
|
|
51
|
-
fix =
|
|
55
|
+
fix = false
|
|
52
56
|
|
|
53
57
|
[tool.ruff.format]
|
|
54
58
|
quote-style = "preserve"
|
|
@@ -72,7 +76,7 @@ ignore = ["TRY003", "EM101"]
|
|
|
72
76
|
|
|
73
77
|
|
|
74
78
|
[tool.ruff.lint.per-file-ignores]
|
|
75
|
-
"tests/**" = ["S101","S311","T20","PLR2004", "ANN001", "ANN202", "ANN401", "ANN201"]
|
|
79
|
+
"tests/**" = ["S101","S311","T20","PLR2004", "PLW0108", "ANN001", "ANN202", "ANN401", "ANN201"]
|
|
76
80
|
"examples/**" = ["T20"]
|
|
77
81
|
|
|
78
82
|
[tool.ruff.lint.flake8-annotations]
|
|
@@ -93,3 +97,24 @@ force-sort-within-sections = true
|
|
|
93
97
|
|
|
94
98
|
[tool.ruff.lint.pydocstyle]
|
|
95
99
|
convention = "google"
|
|
100
|
+
|
|
101
|
+
[dependency-groups]
|
|
102
|
+
test = [
|
|
103
|
+
"pytest>=9.2.0",
|
|
104
|
+
]
|
|
105
|
+
lint = [
|
|
106
|
+
"ruff>=0.17.5",
|
|
107
|
+
]
|
|
108
|
+
build = [
|
|
109
|
+
"build>=1.6.1",
|
|
110
|
+
"twine>=7.0.0",
|
|
111
|
+
]
|
|
112
|
+
docs = [
|
|
113
|
+
"great-docs>=0.17.0 ; python_full_version >= '3.11'",
|
|
114
|
+
]
|
|
115
|
+
dev = [
|
|
116
|
+
{ include-group = "test" },
|
|
117
|
+
{ include-group = "lint" },
|
|
118
|
+
{ include-group = "build" },
|
|
119
|
+
{ include-group = "docs" },
|
|
120
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Private implementation details for the secure package."""
|