Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add improved security headers to FastAPI responses #1355

Merged
merged 6 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ kedro viz --save-file=filename.json

We also recommend wrapping the `Kedro-Viz` component with a parent HTML/JSX element that has a specified height (as seen in the above example) in order for Kedro-Viz to be styled properly.

**_Our documentation contains [additional examples on how to visualise with kedro-viz](https://docs.kedro.org/en/stable/visualisation/)_**
**_Our documentation contains [additional examples on how to visualise with Kedro-Viz.](https://docs.kedro.org/en/stable/visualisation/)_**

## Feature Flags

Expand Down
9 changes: 9 additions & 0 deletions package/kedro_viz/api/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time
from pathlib import Path

import secure
from fastapi import FastAPI, HTTPException
from fastapi.requests import Request
from fastapi.responses import HTMLResponse, JSONResponse, Response
Expand All @@ -20,6 +21,8 @@

_HTML_DIR = Path(__file__).parent.parent.absolute() / "html"

secure_headers = secure.Secure()


def _create_etag() -> str:
"""Generate the current timestamp to use as etag."""
Expand Down Expand Up @@ -56,6 +59,12 @@ def create_api_app_from_project(
# this is used as an etag embedded in the frontend for client to use when making requests.
app_etag = _create_etag()

@app.middleware("http")
async def set_secure_headers(request, call_next):
response = await call_next(request)
secure_headers.framework.fastapi(response)
return response

@app.get("/")
@app.get("/experiment-tracking")
async def index():
Expand Down
1 change: 1 addition & 0 deletions package/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ sqlalchemy>=1.4, <3
strawberry-graphql>=0.99.0, <1.0
networkx>=1.0
orjson~=3.8
secure>=0.3.0