Skip to content
Open
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
8 changes: 7 additions & 1 deletion openhands-agent-server/openhands/agent_server/middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from urllib.parse import urlparse

from fastapi.middleware.cors import CORSMiddleware
Expand All @@ -6,7 +7,7 @@

class LocalhostCORSMiddleware(CORSMiddleware):
"""Custom CORS middleware that allows any request from localhost/127.0.0.1 domains,
while using standard CORS rules for other origins.
as well as the DOCKER_HOST_ADDR IP, while using standard CORS rules for other origins.
"""

def __init__(self, app: ASGIApp, allow_origins: list[str]) -> None:
Expand All @@ -27,6 +28,11 @@ def is_allowed_origin(self, origin: str) -> bool:
if hostname in ["localhost", "127.0.0.1"]:
return True

# Also allow DOCKER_HOST_ADDR if set (for remote browser access)
docker_host_addr = os.environ.get("DOCKER_HOST_ADDR")
if docker_host_addr and hostname == docker_host_addr:
return True

# For missing origin or other origins, use the parent class's logic
result: bool = super().is_allowed_origin(origin)
return result