secure 2.0.0__tar.gz → 2.0.0rc1__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.
Files changed (32) hide show
  1. {secure-2.0.0/secure.egg-info → secure-2.0.0rc1}/PKG-INFO +38 -28
  2. {secure-2.0.0 → secure-2.0.0rc1}/README.md +35 -26
  3. {secure-2.0.0 → secure-2.0.0rc1}/pyproject.toml +3 -3
  4. {secure-2.0.0 → secure-2.0.0rc1}/secure/secure.py +22 -16
  5. {secure-2.0.0 → secure-2.0.0rc1/secure.egg-info}/PKG-INFO +38 -28
  6. {secure-2.0.0 → secure-2.0.0rc1}/LICENSE +0 -0
  7. {secure-2.0.0 → secure-2.0.0rc1}/secure/__init__.py +0 -0
  8. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/__init__.py +0 -0
  9. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/_validation.py +0 -0
  10. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/base_header.py +0 -0
  11. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/cache_control.py +0 -0
  12. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/content_security_policy.py +0 -0
  13. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/cross_origin_embedder_policy.py +0 -0
  14. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/cross_origin_opener_policy.py +0 -0
  15. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/cross_origin_resource_policy.py +0 -0
  16. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/custom_header.py +0 -0
  17. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/permissions_policy.py +0 -0
  18. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/referrer_policy.py +0 -0
  19. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/server.py +0 -0
  20. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/strict_transport_security.py +0 -0
  21. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/x_content_type_options.py +0 -0
  22. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/x_dns_prefetch_control.py +0 -0
  23. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/x_frame_options.py +0 -0
  24. {secure-2.0.0 → secure-2.0.0rc1}/secure/headers/x_permitted_cross_domain_policies.py +0 -0
  25. {secure-2.0.0 → secure-2.0.0rc1}/secure/middleware/__init__.py +0 -0
  26. {secure-2.0.0 → secure-2.0.0rc1}/secure/middleware/asgi.py +0 -0
  27. {secure-2.0.0 → secure-2.0.0rc1}/secure/middleware/wsgi.py +0 -0
  28. {secure-2.0.0 → secure-2.0.0rc1}/secure/py.typed +0 -0
  29. {secure-2.0.0 → secure-2.0.0rc1}/secure.egg-info/SOURCES.txt +0 -0
  30. {secure-2.0.0 → secure-2.0.0rc1}/secure.egg-info/dependency_links.txt +0 -0
  31. {secure-2.0.0 → secure-2.0.0rc1}/secure.egg-info/top_level.txt +0 -0
  32. {secure-2.0.0 → secure-2.0.0rc1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: secure
3
- Version: 2.0.0
3
+ Version: 2.0.0rc1
4
4
  Summary: A lightweight package that adds security headers for Python web frameworks.
5
5
  Author-email: Caleb Kinney <caleb@typeerror.com>
6
6
  License-Expression: MIT
@@ -9,7 +9,7 @@ Project-URL: Documentation, https://github.com/TypeError/secure/tree/main/docs
9
9
  Project-URL: Repository, https://github.com/TypeError/secure
10
10
  Project-URL: Issue Tracker, https://github.com/TypeError/secure/issues
11
11
  Keywords: security,headers,web,framework,HTTP
12
- Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Development Status :: 4 - Beta
13
13
  Classifier: Intended Audience :: Developers
14
14
  Classifier: Operating System :: OS Independent
15
15
  Classifier: Programming Language :: Python :: 3
@@ -18,6 +18,7 @@ Classifier: Programming Language :: Python :: 3.10
18
18
  Classifier: Programming Language :: Python :: 3.11
19
19
  Classifier: Programming Language :: Python :: 3.12
20
20
  Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
21
22
  Classifier: Typing :: Typed
22
23
  Classifier: Topic :: Software Development :: Libraries
23
24
  Requires-Python: >=3.10
@@ -77,6 +78,7 @@ If you want your app to ship with a strong security baseline without pulling in
77
78
  | [aiohttp](https://docs.aiohttp.org) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#aiohttp) |
78
79
  | [Bottle](https://bottlepy.org) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#bottle) |
79
80
  | [CherryPy](https://cherrypy.dev/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#cherrypy) |
81
+ | [Dash](https://dash.plotly.com/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#dash) |
80
82
  | [Django](https://www.djangoproject.com) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#django) |
81
83
  | [Falcon](https://falconframework.org) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#falcon) |
82
84
  | [FastAPI](https://fastapi.tiangolo.com) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#fastapi) |
@@ -87,6 +89,7 @@ If you want your app to ship with a strong security baseline without pulling in
87
89
  | [Quart](https://quart.palletsprojects.com/en/latest/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#quart) |
88
90
  | [Responder](https://responder.kennethreitz.org/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#responder) |
89
91
  | [Sanic](https://sanicframework.org) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#sanic) |
92
+ | [Shiny](https://shiny.posit.co/py/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#shiny) |
90
93
  | [Starlette](https://www.starlette.io/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#starlette) |
91
94
  | [Tornado](https://www.tornadoweb.org/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#tornado) |
92
95
  | [TurboGears](https://turbogears.org/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#turbogears) |
@@ -181,7 +184,7 @@ If your framework uses a different contract, see the framework specific guides o
181
184
 
182
185
  ## Middleware
183
186
 
184
- `secure.middleware` re-exports `SecureWSGIMiddleware` and `SecureASGIMiddleware`. Each middleware accepts a `Secure` instance (defaulting to `Secure.with_default_headers()`), overwrites headers by default, and only appends duplicates when a normalized name is included in `multi_ok` (the default `secure.secure.MULTI_OK` includes `Content-Security-Policy`).
187
+ `secure.middleware` re-exports `SecureWSGIMiddleware` and `SecureASGIMiddleware`. Each middleware accepts a `Secure` instance (defaulting to `Secure.with_default_headers()`), overwrites headers by default, and only appends duplicates when a normalized name is included in `multi_ok` (the default `secure.MULTI_OK` includes `Content-Security-Policy`).
185
188
 
186
189
  ### WSGI (Flask + Django)
187
190
 
@@ -322,10 +325,10 @@ Cross-Origin-Resource-Policy: same-origin
322
325
  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
323
326
  Strict-Transport-Security: max-age=31536000; includeSubDomains
324
327
  Referrer-Policy: no-referrer
325
- X-Content-Type-Options: nosniff
326
- X-Frame-Options: SAMEORIGIN
327
328
  X-Permitted-Cross-Domain-Policies: none
328
329
  X-DNS-Prefetch-Control: off
330
+ X-Content-Type-Options: nosniff
331
+ X-Frame-Options: SAMEORIGIN
329
332
  Origin-Agent-Cluster: ?1
330
333
  X-Download-Options: noopen
331
334
  X-XSS-Protection: 0
@@ -361,10 +364,11 @@ Start with `BALANCED` and move to `STRICT` once you have validated that your app
361
364
  ### Content Security Policy
362
365
 
363
366
  ```python
364
- import secure
367
+ from secure import Secure
368
+ from secure.headers import ContentSecurityPolicy
365
369
 
366
370
  csp = (
367
- secure.ContentSecurityPolicy()
371
+ ContentSecurityPolicy()
368
372
  .default_src("'self'")
369
373
  .script_src("'self'", "cdn.typeerror.com")
370
374
  .style_src("'unsafe-inline'")
@@ -372,7 +376,7 @@ csp = (
372
376
  .connect_src("'self'", "api.typeerror.com")
373
377
  )
374
378
 
375
- secure_headers = secure.Secure(csp=csp)
379
+ secure_headers = Secure(csp=csp)
376
380
  ```
377
381
 
378
382
  Resulting header:
@@ -386,22 +390,20 @@ You can treat the CSP builder as a safe string builder for CSP directives and ke
386
390
  ### Permissions Policy
387
391
 
388
392
  ```python
389
- import secure
393
+ from secure import Secure
394
+ from secure.headers import PermissionsPolicy
390
395
 
391
396
  permissions = (
392
- secure.PermissionsPolicy()
393
- .geolocation("'self'")
394
- .camera("'none'")
395
- .microphone("'none'")
397
+ PermissionsPolicy().geolocation("'self'").camera("'none'").microphone("'none'")
396
398
  )
397
399
 
398
- secure_headers = secure.Secure(permissions=permissions)
400
+ secure_headers = Secure(permissions=permissions)
399
401
  ```
400
402
 
401
403
  Resulting header:
402
404
 
403
405
  ```http
404
- Permissions-Policy: geolocation=('self'), camera=('none'), microphone=('none')
406
+ Permissions-Policy: geolocation=(self), camera=(), microphone=()
405
407
  ```
406
408
 
407
409
  Other headers, such as `StrictTransportSecurity`, `CrossOriginOpenerPolicy`, `CrossOriginEmbedderPolicy`, `ReferrerPolicy`, `Server`, and `XFrameOptions`, also have small builder classes that mirror their directive structure.
@@ -414,27 +416,28 @@ For most applications, it is enough to construct a `Secure` instance and call `s
414
416
 
415
417
  ```python
416
418
  import logging
417
- import secure
419
+
420
+ from secure import COMMA_JOIN_OK, DEFAULT_ALLOWED_HEADERS, MULTI_OK, Secure
418
421
 
419
422
  logger = logging.getLogger("secure")
420
423
 
421
424
  secure_headers = (
422
- secure.Secure.with_default_headers()
425
+ Secure.with_default_headers()
423
426
  .allowlist_headers(
424
- allowed=secure.DEFAULT_ALLOWED_HEADERS,
427
+ allowed=DEFAULT_ALLOWED_HEADERS,
425
428
  allow_extra=["X-My-App-Header"],
426
- on_unexpected="warn", # "raise" (default), "drop", or "warn"
429
+ on_unexpected="warn", # "raise" (default), "drop", or "warn"
427
430
  allow_x_prefixed=False,
428
431
  logger=logger,
429
432
  )
430
433
  .deduplicate_headers(
431
- action="raise", # "raise" (default), "first", "last", or "concat"
432
- comma_join_ok=secure.COMMA_JOIN_OK,
433
- multi_ok=secure.MULTI_OK,
434
+ action="raise", # "raise" (default), "first", "last", or "concat"
435
+ comma_join_ok=COMMA_JOIN_OK,
436
+ multi_ok=MULTI_OK,
434
437
  logger=logger,
435
438
  )
436
439
  .validate_and_normalize_headers(
437
- on_invalid="drop", # "drop" (default), "warn", or "raise"
440
+ on_invalid="drop", # "drop" (default), "warn", or "raise"
438
441
  strict=False,
439
442
  allow_obs_text=False,
440
443
  logger=logger,
@@ -541,17 +544,20 @@ def read_root():
541
544
  #### Recommended: `add_middleware` (ASGI)
542
545
 
543
546
  ```python
544
- from starlette.applications import Starlette
545
547
  from secure import Secure
546
548
  from secure.middleware import SecureASGIMiddleware
549
+ from starlette.applications import Starlette
550
+ from starlette.responses import JSONResponse
547
551
 
548
552
  secure_headers = Secure.with_default_headers()
553
+
549
554
  app = Starlette()
550
555
  app.add_middleware(SecureASGIMiddleware, secure=secure_headers)
551
556
 
557
+
552
558
  @app.route("/")
553
559
  async def read_root(request):
554
- return {"hello": "world"}
560
+ return JSONResponse({"hello": "world"})
555
561
  ```
556
562
 
557
563
  ### Flask
@@ -676,15 +682,19 @@ This project is licensed under the terms of the [MIT License](https://opensource
676
682
 
677
683
  ## Contributing
678
684
 
679
- Issues and pull requests are welcome. If you would like to discuss an idea, open an issue on GitHub so we can talk about the design before implementation.
685
+ Issues and pull requests are welcome. If you’d like to discuss an idea, please open a GitHub issue so we can align on the design before implementation. See [CONTRIBUTING](https://github.com/TypeError/secure/blob/main/CONTRIBUTING.md) for details.
686
+
687
+ ---
688
+
689
+ ## Code of Conduct
680
690
 
681
- Repository: <https://github.com/TypeError/secure>
691
+ See [CODE_OF_CONDUCT](https://github.com/TypeError/secure/blob/main/CODE_OF_CONDUCT.md) for our Code of Conduct.
682
692
 
683
693
  ---
684
694
 
685
695
  ## Changelog
686
696
 
687
- See the [CHANGELOG](https://github.com/TypeError/secure/blob/main/CHANGELOG.md) for a detailed list of changes by release.
697
+ See [CHANGELOG](https://github.com/TypeError/secure/blob/main/CHANGELOG.md) for a detailed list of changes by release.
688
698
 
689
699
  ---
690
700
 
@@ -50,6 +50,7 @@ If you want your app to ship with a strong security baseline without pulling in
50
50
  | [aiohttp](https://docs.aiohttp.org) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#aiohttp) |
51
51
  | [Bottle](https://bottlepy.org) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#bottle) |
52
52
  | [CherryPy](https://cherrypy.dev/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#cherrypy) |
53
+ | [Dash](https://dash.plotly.com/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#dash) |
53
54
  | [Django](https://www.djangoproject.com) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#django) |
54
55
  | [Falcon](https://falconframework.org) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#falcon) |
55
56
  | [FastAPI](https://fastapi.tiangolo.com) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#fastapi) |
@@ -60,6 +61,7 @@ If you want your app to ship with a strong security baseline without pulling in
60
61
  | [Quart](https://quart.palletsprojects.com/en/latest/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#quart) |
61
62
  | [Responder](https://responder.kennethreitz.org/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#responder) |
62
63
  | [Sanic](https://sanicframework.org) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#sanic) |
64
+ | [Shiny](https://shiny.posit.co/py/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#shiny) |
63
65
  | [Starlette](https://www.starlette.io/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#starlette) |
64
66
  | [Tornado](https://www.tornadoweb.org/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#tornado) |
65
67
  | [TurboGears](https://turbogears.org/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#turbogears) |
@@ -154,7 +156,7 @@ If your framework uses a different contract, see the framework specific guides o
154
156
 
155
157
  ## Middleware
156
158
 
157
- `secure.middleware` re-exports `SecureWSGIMiddleware` and `SecureASGIMiddleware`. Each middleware accepts a `Secure` instance (defaulting to `Secure.with_default_headers()`), overwrites headers by default, and only appends duplicates when a normalized name is included in `multi_ok` (the default `secure.secure.MULTI_OK` includes `Content-Security-Policy`).
159
+ `secure.middleware` re-exports `SecureWSGIMiddleware` and `SecureASGIMiddleware`. Each middleware accepts a `Secure` instance (defaulting to `Secure.with_default_headers()`), overwrites headers by default, and only appends duplicates when a normalized name is included in `multi_ok` (the default `secure.MULTI_OK` includes `Content-Security-Policy`).
158
160
 
159
161
  ### WSGI (Flask + Django)
160
162
 
@@ -295,10 +297,10 @@ Cross-Origin-Resource-Policy: same-origin
295
297
  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
296
298
  Strict-Transport-Security: max-age=31536000; includeSubDomains
297
299
  Referrer-Policy: no-referrer
298
- X-Content-Type-Options: nosniff
299
- X-Frame-Options: SAMEORIGIN
300
300
  X-Permitted-Cross-Domain-Policies: none
301
301
  X-DNS-Prefetch-Control: off
302
+ X-Content-Type-Options: nosniff
303
+ X-Frame-Options: SAMEORIGIN
302
304
  Origin-Agent-Cluster: ?1
303
305
  X-Download-Options: noopen
304
306
  X-XSS-Protection: 0
@@ -334,10 +336,11 @@ Start with `BALANCED` and move to `STRICT` once you have validated that your app
334
336
  ### Content Security Policy
335
337
 
336
338
  ```python
337
- import secure
339
+ from secure import Secure
340
+ from secure.headers import ContentSecurityPolicy
338
341
 
339
342
  csp = (
340
- secure.ContentSecurityPolicy()
343
+ ContentSecurityPolicy()
341
344
  .default_src("'self'")
342
345
  .script_src("'self'", "cdn.typeerror.com")
343
346
  .style_src("'unsafe-inline'")
@@ -345,7 +348,7 @@ csp = (
345
348
  .connect_src("'self'", "api.typeerror.com")
346
349
  )
347
350
 
348
- secure_headers = secure.Secure(csp=csp)
351
+ secure_headers = Secure(csp=csp)
349
352
  ```
350
353
 
351
354
  Resulting header:
@@ -359,22 +362,20 @@ You can treat the CSP builder as a safe string builder for CSP directives and ke
359
362
  ### Permissions Policy
360
363
 
361
364
  ```python
362
- import secure
365
+ from secure import Secure
366
+ from secure.headers import PermissionsPolicy
363
367
 
364
368
  permissions = (
365
- secure.PermissionsPolicy()
366
- .geolocation("'self'")
367
- .camera("'none'")
368
- .microphone("'none'")
369
+ PermissionsPolicy().geolocation("'self'").camera("'none'").microphone("'none'")
369
370
  )
370
371
 
371
- secure_headers = secure.Secure(permissions=permissions)
372
+ secure_headers = Secure(permissions=permissions)
372
373
  ```
373
374
 
374
375
  Resulting header:
375
376
 
376
377
  ```http
377
- Permissions-Policy: geolocation=('self'), camera=('none'), microphone=('none')
378
+ Permissions-Policy: geolocation=(self), camera=(), microphone=()
378
379
  ```
379
380
 
380
381
  Other headers, such as `StrictTransportSecurity`, `CrossOriginOpenerPolicy`, `CrossOriginEmbedderPolicy`, `ReferrerPolicy`, `Server`, and `XFrameOptions`, also have small builder classes that mirror their directive structure.
@@ -387,27 +388,28 @@ For most applications, it is enough to construct a `Secure` instance and call `s
387
388
 
388
389
  ```python
389
390
  import logging
390
- import secure
391
+
392
+ from secure import COMMA_JOIN_OK, DEFAULT_ALLOWED_HEADERS, MULTI_OK, Secure
391
393
 
392
394
  logger = logging.getLogger("secure")
393
395
 
394
396
  secure_headers = (
395
- secure.Secure.with_default_headers()
397
+ Secure.with_default_headers()
396
398
  .allowlist_headers(
397
- allowed=secure.DEFAULT_ALLOWED_HEADERS,
399
+ allowed=DEFAULT_ALLOWED_HEADERS,
398
400
  allow_extra=["X-My-App-Header"],
399
- on_unexpected="warn", # "raise" (default), "drop", or "warn"
401
+ on_unexpected="warn", # "raise" (default), "drop", or "warn"
400
402
  allow_x_prefixed=False,
401
403
  logger=logger,
402
404
  )
403
405
  .deduplicate_headers(
404
- action="raise", # "raise" (default), "first", "last", or "concat"
405
- comma_join_ok=secure.COMMA_JOIN_OK,
406
- multi_ok=secure.MULTI_OK,
406
+ action="raise", # "raise" (default), "first", "last", or "concat"
407
+ comma_join_ok=COMMA_JOIN_OK,
408
+ multi_ok=MULTI_OK,
407
409
  logger=logger,
408
410
  )
409
411
  .validate_and_normalize_headers(
410
- on_invalid="drop", # "drop" (default), "warn", or "raise"
412
+ on_invalid="drop", # "drop" (default), "warn", or "raise"
411
413
  strict=False,
412
414
  allow_obs_text=False,
413
415
  logger=logger,
@@ -514,17 +516,20 @@ def read_root():
514
516
  #### Recommended: `add_middleware` (ASGI)
515
517
 
516
518
  ```python
517
- from starlette.applications import Starlette
518
519
  from secure import Secure
519
520
  from secure.middleware import SecureASGIMiddleware
521
+ from starlette.applications import Starlette
522
+ from starlette.responses import JSONResponse
520
523
 
521
524
  secure_headers = Secure.with_default_headers()
525
+
522
526
  app = Starlette()
523
527
  app.add_middleware(SecureASGIMiddleware, secure=secure_headers)
524
528
 
529
+
525
530
  @app.route("/")
526
531
  async def read_root(request):
527
- return {"hello": "world"}
532
+ return JSONResponse({"hello": "world"})
528
533
  ```
529
534
 
530
535
  ### Flask
@@ -649,15 +654,19 @@ This project is licensed under the terms of the [MIT License](https://opensource
649
654
 
650
655
  ## Contributing
651
656
 
652
- Issues and pull requests are welcome. If you would like to discuss an idea, open an issue on GitHub so we can talk about the design before implementation.
657
+ Issues and pull requests are welcome. If you’d like to discuss an idea, please open a GitHub issue so we can align on the design before implementation. See [CONTRIBUTING](https://github.com/TypeError/secure/blob/main/CONTRIBUTING.md) for details.
658
+
659
+ ---
660
+
661
+ ## Code of Conduct
653
662
 
654
- Repository: <https://github.com/TypeError/secure>
663
+ See [CODE_OF_CONDUCT](https://github.com/TypeError/secure/blob/main/CODE_OF_CONDUCT.md) for our Code of Conduct.
655
664
 
656
665
  ---
657
666
 
658
667
  ## Changelog
659
668
 
660
- See the [CHANGELOG](https://github.com/TypeError/secure/blob/main/CHANGELOG.md) for a detailed list of changes by release.
669
+ See [CHANGELOG](https://github.com/TypeError/secure/blob/main/CHANGELOG.md) for a detailed list of changes by release.
661
670
 
662
671
  ---
663
672
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "secure"
7
- version = "2.0.0"
7
+ version = "2.0.0rc1"
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 :: 5 - Production/Stable",
16
+ "Development Status :: 4 - Beta",
17
17
  "Intended Audience :: Developers",
18
18
  "Operating System :: OS Independent",
19
19
  "Programming Language :: Python :: 3",
@@ -22,7 +22,7 @@ classifiers = [
22
22
  "Programming Language :: Python :: 3.11",
23
23
  "Programming Language :: Python :: 3.12",
24
24
  "Programming Language :: Python :: 3.13",
25
- # "Programming Language :: Python :: 3.14",
25
+ "Programming Language :: Python :: 3.14",
26
26
  "Typing :: Typed",
27
27
  "Topic :: Software Development :: Libraries",
28
28
  ]
@@ -7,10 +7,10 @@ import inspect
7
7
  import logging
8
8
  import re
9
9
  from types import MappingProxyType
10
- from typing import TYPE_CHECKING, Literal, Protocol, TypeAlias
10
+ from typing import TYPE_CHECKING, Literal, Protocol, TypeAlias, cast
11
11
 
12
12
  if TYPE_CHECKING:
13
- from collections.abc import Iterable, Mapping
13
+ from collections.abc import Callable, Iterable, Mapping, MutableMapping
14
14
 
15
15
  from .headers import (
16
16
  BaseHeader,
@@ -84,8 +84,8 @@ class HeaderSetError(RuntimeError):
84
84
 
85
85
 
86
86
  class HeadersProtocol(Protocol):
87
- # Intentionally broad: frameworks type headers differently.
88
- headers: object
87
+ @property
88
+ def headers(self) -> object: ...
89
89
 
90
90
 
91
91
  class SetHeaderProtocol(Protocol):
@@ -853,19 +853,20 @@ class Secure:
853
853
 
854
854
  # Path 2: response.headers...
855
855
  if hasattr(response, "headers"):
856
- hdrs = response.headers
856
+ hdrs = cast("object", response.headers)
857
857
 
858
- # Prefer Werkzeug-style: response.headers.set(name, value)
859
858
  set_fn = getattr(hdrs, "set", None)
860
859
  if callable(set_fn):
861
- if inspect.iscoroutinefunction(set_fn):
860
+ set_fn_typed = cast("Callable[[str, str], object]", set_fn)
861
+
862
+ if inspect.iscoroutinefunction(set_fn_typed):
862
863
  raise RuntimeError(
863
864
  "Async headers setter detected in sync context. Use 'await set_headers_async(response)'."
864
865
  )
865
866
 
866
867
  try:
867
868
  for name, value in items:
868
- result = set_fn(name, value)
869
+ result = set_fn_typed(name, value)
869
870
  if inspect.isawaitable(result):
870
871
  raise RuntimeError(
871
872
  "Async headers setter returned awaitable in sync context. "
@@ -876,17 +877,20 @@ class Secure:
876
877
 
877
878
  return
878
879
 
879
- # Fallback: response.headers[name] = value # noqa: ERA001
880
880
  setitem = getattr(hdrs, "__setitem__", None)
881
881
  if callable(setitem):
882
- if inspect.iscoroutinefunction(setitem):
882
+ setitem_typed = cast("Callable[[str, str], object]", setitem)
883
+
884
+ if inspect.iscoroutinefunction(setitem_typed):
883
885
  raise RuntimeError(
884
886
  "Async headers mapping detected in sync context. Use 'await set_headers_async(response)'."
885
887
  )
886
888
 
887
889
  try:
890
+ # Use mapping assignment for the common case.
891
+ hdrs_map = cast("MutableMapping[str, str]", hdrs)
888
892
  for name, value in items:
889
- hdrs[name] = value
893
+ hdrs_map[name] = value
890
894
  except (TypeError, ValueError, AttributeError) as e:
891
895
  raise HeaderSetError(f"Failed to set headers: {e}") from e
892
896
 
@@ -944,14 +948,15 @@ class Secure:
944
948
 
945
949
  # Path 2: response.headers...
946
950
  if hasattr(response, "headers"):
947
- hdrs = response.headers
951
+ hdrs = cast("object", response.headers)
948
952
 
949
- # Prefer Werkzeug-style: response.headers.set(name, value)
950
953
  set_fn = getattr(hdrs, "set", None)
951
954
  if callable(set_fn):
955
+ set_fn_typed = cast("Callable[[str, str], object]", set_fn)
956
+
952
957
  try:
953
958
  for name, value in items:
954
- result = set_fn(name, value)
959
+ result = set_fn_typed(name, value)
955
960
  if inspect.isawaitable(result):
956
961
  await result
957
962
  except (TypeError, ValueError, AttributeError) as e:
@@ -959,12 +964,13 @@ class Secure:
959
964
 
960
965
  return
961
966
 
962
- # Fallback: response.headers.__setitem__(name, value) # noqa: ERA001
963
967
  setitem = getattr(hdrs, "__setitem__", None)
964
968
  if callable(setitem):
969
+ setitem_typed = cast("Callable[[str, str], object]", setitem)
970
+
965
971
  try:
966
972
  for name, value in items:
967
- result = setitem(name, value)
973
+ result = setitem_typed(name, value)
968
974
  if inspect.isawaitable(result):
969
975
  await result
970
976
  except (TypeError, ValueError, AttributeError) as e:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: secure
3
- Version: 2.0.0
3
+ Version: 2.0.0rc1
4
4
  Summary: A lightweight package that adds security headers for Python web frameworks.
5
5
  Author-email: Caleb Kinney <caleb@typeerror.com>
6
6
  License-Expression: MIT
@@ -9,7 +9,7 @@ Project-URL: Documentation, https://github.com/TypeError/secure/tree/main/docs
9
9
  Project-URL: Repository, https://github.com/TypeError/secure
10
10
  Project-URL: Issue Tracker, https://github.com/TypeError/secure/issues
11
11
  Keywords: security,headers,web,framework,HTTP
12
- Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Development Status :: 4 - Beta
13
13
  Classifier: Intended Audience :: Developers
14
14
  Classifier: Operating System :: OS Independent
15
15
  Classifier: Programming Language :: Python :: 3
@@ -18,6 +18,7 @@ Classifier: Programming Language :: Python :: 3.10
18
18
  Classifier: Programming Language :: Python :: 3.11
19
19
  Classifier: Programming Language :: Python :: 3.12
20
20
  Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
21
22
  Classifier: Typing :: Typed
22
23
  Classifier: Topic :: Software Development :: Libraries
23
24
  Requires-Python: >=3.10
@@ -77,6 +78,7 @@ If you want your app to ship with a strong security baseline without pulling in
77
78
  | [aiohttp](https://docs.aiohttp.org) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#aiohttp) |
78
79
  | [Bottle](https://bottlepy.org) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#bottle) |
79
80
  | [CherryPy](https://cherrypy.dev/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#cherrypy) |
81
+ | [Dash](https://dash.plotly.com/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#dash) |
80
82
  | [Django](https://www.djangoproject.com) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#django) |
81
83
  | [Falcon](https://falconframework.org) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#falcon) |
82
84
  | [FastAPI](https://fastapi.tiangolo.com) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#fastapi) |
@@ -87,6 +89,7 @@ If you want your app to ship with a strong security baseline without pulling in
87
89
  | [Quart](https://quart.palletsprojects.com/en/latest/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#quart) |
88
90
  | [Responder](https://responder.kennethreitz.org/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#responder) |
89
91
  | [Sanic](https://sanicframework.org) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#sanic) |
92
+ | [Shiny](https://shiny.posit.co/py/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#shiny) |
90
93
  | [Starlette](https://www.starlette.io/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#starlette) |
91
94
  | [Tornado](https://www.tornadoweb.org/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#tornado) |
92
95
  | [TurboGears](https://turbogears.org/) | [Integration Guide](https://github.com/TypeError/secure/blob/main/docs/frameworks.md#turbogears) |
@@ -181,7 +184,7 @@ If your framework uses a different contract, see the framework specific guides o
181
184
 
182
185
  ## Middleware
183
186
 
184
- `secure.middleware` re-exports `SecureWSGIMiddleware` and `SecureASGIMiddleware`. Each middleware accepts a `Secure` instance (defaulting to `Secure.with_default_headers()`), overwrites headers by default, and only appends duplicates when a normalized name is included in `multi_ok` (the default `secure.secure.MULTI_OK` includes `Content-Security-Policy`).
187
+ `secure.middleware` re-exports `SecureWSGIMiddleware` and `SecureASGIMiddleware`. Each middleware accepts a `Secure` instance (defaulting to `Secure.with_default_headers()`), overwrites headers by default, and only appends duplicates when a normalized name is included in `multi_ok` (the default `secure.MULTI_OK` includes `Content-Security-Policy`).
185
188
 
186
189
  ### WSGI (Flask + Django)
187
190
 
@@ -322,10 +325,10 @@ Cross-Origin-Resource-Policy: same-origin
322
325
  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
323
326
  Strict-Transport-Security: max-age=31536000; includeSubDomains
324
327
  Referrer-Policy: no-referrer
325
- X-Content-Type-Options: nosniff
326
- X-Frame-Options: SAMEORIGIN
327
328
  X-Permitted-Cross-Domain-Policies: none
328
329
  X-DNS-Prefetch-Control: off
330
+ X-Content-Type-Options: nosniff
331
+ X-Frame-Options: SAMEORIGIN
329
332
  Origin-Agent-Cluster: ?1
330
333
  X-Download-Options: noopen
331
334
  X-XSS-Protection: 0
@@ -361,10 +364,11 @@ Start with `BALANCED` and move to `STRICT` once you have validated that your app
361
364
  ### Content Security Policy
362
365
 
363
366
  ```python
364
- import secure
367
+ from secure import Secure
368
+ from secure.headers import ContentSecurityPolicy
365
369
 
366
370
  csp = (
367
- secure.ContentSecurityPolicy()
371
+ ContentSecurityPolicy()
368
372
  .default_src("'self'")
369
373
  .script_src("'self'", "cdn.typeerror.com")
370
374
  .style_src("'unsafe-inline'")
@@ -372,7 +376,7 @@ csp = (
372
376
  .connect_src("'self'", "api.typeerror.com")
373
377
  )
374
378
 
375
- secure_headers = secure.Secure(csp=csp)
379
+ secure_headers = Secure(csp=csp)
376
380
  ```
377
381
 
378
382
  Resulting header:
@@ -386,22 +390,20 @@ You can treat the CSP builder as a safe string builder for CSP directives and ke
386
390
  ### Permissions Policy
387
391
 
388
392
  ```python
389
- import secure
393
+ from secure import Secure
394
+ from secure.headers import PermissionsPolicy
390
395
 
391
396
  permissions = (
392
- secure.PermissionsPolicy()
393
- .geolocation("'self'")
394
- .camera("'none'")
395
- .microphone("'none'")
397
+ PermissionsPolicy().geolocation("'self'").camera("'none'").microphone("'none'")
396
398
  )
397
399
 
398
- secure_headers = secure.Secure(permissions=permissions)
400
+ secure_headers = Secure(permissions=permissions)
399
401
  ```
400
402
 
401
403
  Resulting header:
402
404
 
403
405
  ```http
404
- Permissions-Policy: geolocation=('self'), camera=('none'), microphone=('none')
406
+ Permissions-Policy: geolocation=(self), camera=(), microphone=()
405
407
  ```
406
408
 
407
409
  Other headers, such as `StrictTransportSecurity`, `CrossOriginOpenerPolicy`, `CrossOriginEmbedderPolicy`, `ReferrerPolicy`, `Server`, and `XFrameOptions`, also have small builder classes that mirror their directive structure.
@@ -414,27 +416,28 @@ For most applications, it is enough to construct a `Secure` instance and call `s
414
416
 
415
417
  ```python
416
418
  import logging
417
- import secure
419
+
420
+ from secure import COMMA_JOIN_OK, DEFAULT_ALLOWED_HEADERS, MULTI_OK, Secure
418
421
 
419
422
  logger = logging.getLogger("secure")
420
423
 
421
424
  secure_headers = (
422
- secure.Secure.with_default_headers()
425
+ Secure.with_default_headers()
423
426
  .allowlist_headers(
424
- allowed=secure.DEFAULT_ALLOWED_HEADERS,
427
+ allowed=DEFAULT_ALLOWED_HEADERS,
425
428
  allow_extra=["X-My-App-Header"],
426
- on_unexpected="warn", # "raise" (default), "drop", or "warn"
429
+ on_unexpected="warn", # "raise" (default), "drop", or "warn"
427
430
  allow_x_prefixed=False,
428
431
  logger=logger,
429
432
  )
430
433
  .deduplicate_headers(
431
- action="raise", # "raise" (default), "first", "last", or "concat"
432
- comma_join_ok=secure.COMMA_JOIN_OK,
433
- multi_ok=secure.MULTI_OK,
434
+ action="raise", # "raise" (default), "first", "last", or "concat"
435
+ comma_join_ok=COMMA_JOIN_OK,
436
+ multi_ok=MULTI_OK,
434
437
  logger=logger,
435
438
  )
436
439
  .validate_and_normalize_headers(
437
- on_invalid="drop", # "drop" (default), "warn", or "raise"
440
+ on_invalid="drop", # "drop" (default), "warn", or "raise"
438
441
  strict=False,
439
442
  allow_obs_text=False,
440
443
  logger=logger,
@@ -541,17 +544,20 @@ def read_root():
541
544
  #### Recommended: `add_middleware` (ASGI)
542
545
 
543
546
  ```python
544
- from starlette.applications import Starlette
545
547
  from secure import Secure
546
548
  from secure.middleware import SecureASGIMiddleware
549
+ from starlette.applications import Starlette
550
+ from starlette.responses import JSONResponse
547
551
 
548
552
  secure_headers = Secure.with_default_headers()
553
+
549
554
  app = Starlette()
550
555
  app.add_middleware(SecureASGIMiddleware, secure=secure_headers)
551
556
 
557
+
552
558
  @app.route("/")
553
559
  async def read_root(request):
554
- return {"hello": "world"}
560
+ return JSONResponse({"hello": "world"})
555
561
  ```
556
562
 
557
563
  ### Flask
@@ -676,15 +682,19 @@ This project is licensed under the terms of the [MIT License](https://opensource
676
682
 
677
683
  ## Contributing
678
684
 
679
- Issues and pull requests are welcome. If you would like to discuss an idea, open an issue on GitHub so we can talk about the design before implementation.
685
+ Issues and pull requests are welcome. If you’d like to discuss an idea, please open a GitHub issue so we can align on the design before implementation. See [CONTRIBUTING](https://github.com/TypeError/secure/blob/main/CONTRIBUTING.md) for details.
686
+
687
+ ---
688
+
689
+ ## Code of Conduct
680
690
 
681
- Repository: <https://github.com/TypeError/secure>
691
+ See [CODE_OF_CONDUCT](https://github.com/TypeError/secure/blob/main/CODE_OF_CONDUCT.md) for our Code of Conduct.
682
692
 
683
693
  ---
684
694
 
685
695
  ## Changelog
686
696
 
687
- See the [CHANGELOG](https://github.com/TypeError/secure/blob/main/CHANGELOG.md) for a detailed list of changes by release.
697
+ See [CHANGELOG](https://github.com/TypeError/secure/blob/main/CHANGELOG.md) for a detailed list of changes by release.
688
698
 
689
699
  ---
690
700
 
File without changes
File without changes
File without changes
File without changes