Spaces:
Sleeping
Sleeping
Deploy backend fbefedd
Browse files- app/main.py +2 -0
- app/routes/origin.py +31 -0
app/main.py
CHANGED
|
@@ -11,6 +11,7 @@ from app.logs import configure_logging
|
|
| 11 |
from app.registry import REGISTRY
|
| 12 |
from app.routes import errors, health, jobs, tools
|
| 13 |
from app.routes.limits import BodyLimit
|
|
|
|
| 14 |
from app.services.jobs import InMemoryJobStore, Sweeper
|
| 15 |
from app.services.runner import JobRunner
|
| 16 |
from app.settings import Settings
|
|
@@ -59,6 +60,7 @@ def create_app(
|
|
| 59 |
# The last middleware added runs first, so CORS headers reach every error response.
|
| 60 |
app.add_middleware(BodyLimit, upload_bytes=settings.max_request_bytes)
|
| 61 |
app.add_middleware(errors.CatchAll)
|
|
|
|
| 62 |
app.add_middleware(
|
| 63 |
CORSMiddleware,
|
| 64 |
allow_origins=[settings.frontend_origin],
|
|
|
|
| 11 |
from app.registry import REGISTRY
|
| 12 |
from app.routes import errors, health, jobs, tools
|
| 13 |
from app.routes.limits import BodyLimit
|
| 14 |
+
from app.routes.origin import OriginGuard
|
| 15 |
from app.services.jobs import InMemoryJobStore, Sweeper
|
| 16 |
from app.services.runner import JobRunner
|
| 17 |
from app.settings import Settings
|
|
|
|
| 60 |
# The last middleware added runs first, so CORS headers reach every error response.
|
| 61 |
app.add_middleware(BodyLimit, upload_bytes=settings.max_request_bytes)
|
| 62 |
app.add_middleware(errors.CatchAll)
|
| 63 |
+
app.add_middleware(OriginGuard, allowed=settings.frontend_origin)
|
| 64 |
app.add_middleware(
|
| 65 |
CORSMiddleware,
|
| 66 |
allow_origins=[settings.frontend_origin],
|
app/routes/origin.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from http import HTTPStatus
|
| 4 |
+
|
| 5 |
+
from starlette.datastructures import Headers
|
| 6 |
+
from starlette.types import ASGIApp, Receive, Scope, Send
|
| 7 |
+
|
| 8 |
+
from app.domain.errors import ErrorCode
|
| 9 |
+
from app.routes.errors import error_response
|
| 10 |
+
from app.schemas import ApiError
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class OriginGuard:
|
| 14 |
+
"""Refuses a request that a page on any origin but `allowed` sent.
|
| 15 |
+
|
| 16 |
+
CORS headers alone do not keep other sites out on Hugging Face: its proxy answers preflights
|
| 17 |
+
itself and sends its own Access-Control-Allow-Origin, which echoes any origin. A request with
|
| 18 |
+
no Origin header, such as curl or the Space's health probe, comes from no page and passes.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
def __init__(self, app: ASGIApp, allowed: str) -> None:
|
| 22 |
+
self.app = app
|
| 23 |
+
self.allowed = allowed
|
| 24 |
+
|
| 25 |
+
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
| 26 |
+
origin = Headers(scope=scope).get("origin") if scope["type"] == "http" else None
|
| 27 |
+
if origin is None or origin == self.allowed:
|
| 28 |
+
await self.app(scope, receive, send)
|
| 29 |
+
return
|
| 30 |
+
error = ApiError.of(ErrorCode.INVALID_REQUEST, "This API answers only its own web app.")
|
| 31 |
+
await error_response(error, HTTPStatus.FORBIDDEN)(scope, receive, send)
|