Skip to content

Commit 1bbdf46

Browse files
author
cak
committed
update documentation for frameworks and presets
1 parent 45c9f9c commit 1bbdf46

3 files changed

Lines changed: 265 additions & 117 deletions

File tree

README.md

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,14 @@ secure_headers.set_headers(response)
154154
By default, `secure.py` applies the following headers when using `with_default_headers()`:
155155

156156
```http
157-
Strict-Transport-Security: max-age=31536000; includeSubDomains
158-
X-Frame-Options: DENY
157+
Cache-Control: no-store
158+
Cross-Origin-Opener-Policy: same-origin
159+
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'
160+
Strict-Transport-Security: max-age=31536000
161+
Permissions-Policy: geolocation=(), microphone=(), camera=()
162+
Referrer-Policy: strict-origin-when-cross-origin
163+
Server:
159164
X-Content-Type-Options: nosniff
160-
Content-Security-Policy: default-src 'self'
161-
Referrer-Policy: no-referrer-when-downgrade
162-
Permissions-Policy: camera=(), microphone=()
163165
```
164166

165167
---
@@ -190,7 +192,7 @@ secure_headers = secure.Secure(csp=csp)
190192
**Resulting HTTP headers:**
191193

192194
```http
193-
content-security-policy: default-src 'self'; script-src 'self' cdn.example.com; style-src 'self' cdn.example.com; img-src 'self' images.example.com; connect-src 'self' api.example.com
195+
Content-Security-Policy: default-src 'self'; script-src 'self' cdn.example.com; style-src 'self' cdn.example.com; img-src 'self' images.example.com; connect-src 'self' api.example.com
194196
```
195197

196198
### **Permissions-Policy Example**
@@ -213,7 +215,7 @@ secure_headers = secure.Secure(permissions=permissions)
213215
**Resulting HTTP headers:**
214216

215217
```http
216-
permissions-policy: geolocation=('self'), camera=(), microphone=()
218+
Permissions-Policy: geolocation=('self'), camera=('none'), microphone=('none')
217219
```
218220

219221
---
@@ -223,70 +225,50 @@ permissions-policy: geolocation=('self'), camera=(), microphone=()
223225
### **FastAPI**
224226

225227
```python
226-
import uvicorn
227228
from fastapi import FastAPI
228-
import secure
229+
230+
from secure import Secure
229231

230232
app = FastAPI()
233+
secure_headers = Secure.with_default_headers()
231234

232-
# Define security headers
233-
secure_headers = secure.Secure()
234235

235-
# Apply headers middleware
236236
@app.middleware("http")
237-
async def set_secure_headers(request, call_next):
237+
async def add_security_headers(request, call_next):
238238
response = await call_next(request)
239-
await secure_headers.set_headers_async(response)
239+
secure_headers.set_headers(response)
240240
return response
241241

242-
@app.get("/")
243-
async def root():
244-
return {"message": "Hello, World!"}
245-
246-
if __name__ == "__main__":
247-
uvicorn.run(app, port=8081, host="localhost")
248-
```
249-
250-
### **Django**
251-
252-
```python
253-
# settings.py
254-
import secure
255242

256-
secure_headers = secure.Secure()
257-
258-
MIDDLEWARE = [
259-
# ... other middleware ...
260-
'your_project.middleware.SecureHeadersMiddleware',
261-
]
262-
263-
# your_project/middleware.py
264-
from django.utils.deprecation import MiddlewareMixin
265-
266-
class SecureHeadersMiddleware(MiddlewareMixin):
267-
def process_response(self, request, response):
268-
secure_headers.set_headers(response)
269-
return response
243+
@app.get("/")
244+
def read_root():
245+
return {"Hello": "World"}
270246
```
271247

272248
### Flask
273249

274250
```python
275-
from flask import Flask
276-
import secure
251+
from flask import Flask, Response
252+
253+
from secure import Secure
277254

278255
app = Flask(__name__)
279-
secure_headers = secure.Secure()
256+
secure_headers = Secure.with_default_headers()
257+
280258

281259
@app.after_request
282-
def set_secure_headers(response):
260+
def add_security_headers(response: Response):
283261
secure_headers.set_headers(response)
284262
return response
285263

286-
@app.route('/')
287-
def index():
288-
return 'Hello, World!'
289264

265+
@app.route("/")
266+
def home():
267+
return "Hello, world"
268+
269+
270+
if __name__ == "__main__":
271+
app.run()
290272
```
291273

292274
---

0 commit comments

Comments
 (0)