Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ By default, `fastapi-security-headers` turns an **F** score on security scanners
| **`X-Content-Type-Options`** | `nosniff` | **MIME-Sniffing:** Prevents browsers from guessing content types, blocking malicious scripts disguised as images or JSON. |
| **`X-Frame-Options`** | `DENY` | **Clickjacking:** Prevents external sites from embedding your API inside hidden `<iframe>` overlays. |
| **`X-XSS-Protection`** | `0` | **Audit Vulnerabilities:** Disables legacy buggy XSS filters as recommended by OWASP. |
| **`Strict-Transport-Security`** | `max-age=31536000; includeSubDomains` | **SSL Stripping / MitM:** Enforces HTTPS for all future visits over the next 365 days. |
| **`Strict-Transport-Security`** | `max-age=31536000` | **SSL Stripping / MitM:** Enforces HTTPS for the current domain for the next 365 days. |
| **`Referrer-Policy`** | `strict-origin-when-cross-origin` | **Data Leakage:** Prevents leaking sensitive URL query parameters to third-party domains. |
| **`Permissions-Policy`** | `geolocation=(), microphone=(), camera=()` | **Feature Abuse:** Disables unused device APIs (GPS, camera, microphone). |
| **`Cross-Origin-Opener-Policy`** | `same-origin` | **Side-Channel Attacks:** Isolates browsing context against Spectre-style attacks. |
| **`Cross-Origin-Resource-Policy`**| `same-origin` | **Cross-Origin Reads:** Blocks external origins from reading your API responses. |

**HSTS subdomains:** `includeSubDomains` is opt-in and disabled by default. Set `include_subdomains=True` only when all subdomains support HTTPS. Built-in presets retain their explicitly configured HSTS settings.

---

## 🎛️ Built-in Presets
Expand Down
2 changes: 1 addition & 1 deletion src/fastapi_security_headers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class HSTSConfig:
"""

max_age: int = 31536000 # 1 year in seconds
include_subdomains: bool = True
include_subdomains: bool = False
preload: bool = False
require_https: bool = True

Expand Down
6 changes: 3 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
def test_hsts_config_defaults():
hsts = HSTSConfig()
assert hsts.max_age == 31536000
assert hsts.include_subdomains is True
assert hsts.include_subdomains is False
assert hsts.preload is False
assert hsts.require_https is True
assert hsts.to_header_value() == "max-age=31536000; includeSubDomains"
assert hsts.to_header_value() == "max-age=31536000"


def test_hsts_config_with_preload():
Expand All @@ -29,7 +29,7 @@ def test_security_headers_config_defaults():
assert compiled[b"x-content-type-options"] == b"nosniff"
assert compiled[b"x-frame-options"] == b"DENY"
assert compiled[b"x-xss-protection"] == b"0"
assert compiled[b"strict-transport-security"] == b"max-age=31536000; includeSubDomains"
assert compiled[b"strict-transport-security"] == b"max-age=31536000"
assert compiled[b"referrer-policy"] == b"strict-origin-when-cross-origin"
assert compiled[b"permissions-policy"] == b"geolocation=(), microphone=(), camera=()"
assert compiled[b"cross-origin-opener-policy"] == b"same-origin"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_default_headers_injected(client):
assert headers["x-content-type-options"] == "nosniff"
assert headers["x-frame-options"] == "DENY"
assert headers["x-xss-protection"] == "0"
assert headers["strict-transport-security"] == "max-age=31536000; includeSubDomains"
assert headers["strict-transport-security"] == "max-age=31536000"
assert headers["referrer-policy"] == "strict-origin-when-cross-origin"
assert headers["permissions-policy"] == "geolocation=(), microphone=(), camera=()"
assert headers["cross-origin-opener-policy"] == "same-origin"
Expand Down
Loading