Security Headers
noun
say it: “sih-KYOOR-ih-tee HED-erz” /sɪˈkjʊrɪti ˈhɛdərz/
Response headers that tell a browser to turn off a risky habit, such as guessing a file's type or showing the page inside another site's frame.
What are security headers?
Security headers are response headers that tell a browser to turn off a risky habit:
X-Content-Type-Options: nosniff (don't guess a file's type), X-Frame-Options: DENY (don't show this page inside
another site's frame) and Referrer-Policy: no-referrer (don't tell other sites which address a person came from).
A middleware can add them to every answer.
from fastapi import FastAPI, Request
from fastapi.testclient import TestClient
app = FastAPI()
@app.middleware("http")
async def security_headers(request: Request, call_next):
response = await call_next(request)
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["X-Frame-Options"] = "DENY"
response.headers["Referrer-Policy"] = "no-referrer"
return response
@app.get("/menu")
def menu():
return ["latte", "mocha"]
response = TestClient(app).get("/menu")
print(response.headers["x-frame-options"]) # DENY
In a sentence
- “A middleware adds the security headers to every answer, so no route can forget them.”
- “X-Frame-Options: DENY is one of the security headers the coffee club API sends.”
Level: Journeyman · More Web Development terms at this level
Updated