Skip to content

Conversation

@shanemort1982
Copy link

Description

Fixes CORS errors when accessing OpenHands from a remote browser.

Problem

When accessing OpenHands from a remote machine, the browser shows CORS errors:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://192.168.1.206:42015/api/...
(Reason: CORS header 'Access-Control-Allow-Origin' missing)

The main OpenHands app runs on http://192.168.1.206:3000, but when it makes requests to agent-server containers (on different ports), the LocalhostCORSMiddleware blocks them because it only allows localhost and 127.0.0.1.

Root Cause

The LocalhostCORSMiddleware in middleware.py only checks for localhost/127.0.0.1 origins. When OpenHands is accessed via a server IP (e.g., 192.168.1.206), cross-origin requests from the main app to agent-server containers are blocked.

Solution

This PR adds support for the DOCKER_HOST_ADDR environment variable to the CORS middleware. When set, the middleware will also allow requests from that IP address.

Changes

  • File: openhands-agent-server/openhands/agent_server/middleware.py
  • Lines: Added import for os and check for DOCKER_HOST_ADDR (lines 1, 31-34)
  • Impact: Minimal, safe change that extends CORS allowlist

Testing

Tested with:

  • Server: 192.168.1.206
  • Main app: http://192.168.1.206:3000
  • Agent-server containers: Various ports (42015, 52051, etc.)
  • Environment: DOCKER_HOST_ADDR=192.168.1.206

Before this fix: CORS errors block API requests from main app to agent-server
After this fix: Requests from 192.168.1.206:3000 to agent-server allowed

Related Issues

Backward Compatibility

  • If DOCKER_HOST_ADDR is not set, behavior is unchanged
  • No breaking changes
  • Works with local (localhost) and remote deployments

Security Note

This change only allows cross-origin requests from the IP specified in DOCKER_HOST_ADDR. The middleware still:

  • Requires explicit configuration (env variable must be set)
  • Only allows the specific IP, not wildcard origins
  • Maintains all other CORS security checks

When accessing OpenHands from a remote browser, CORS blocks requests
from the main app (e.g., http://192.168.1.206:3000) to agent-server
containers because the middleware only allowed localhost/127.0.0.1.

This adds support for the DOCKER_HOST_ADDR environment variable,
allowing the agent-server to accept requests from the server's actual
IP address.

Co-authored-by: openhands <openhands@all-hands.dev>
@blacksmith-sh blacksmith-sh bot requested a review from xingyaoww December 23, 2025 12:26
@blacksmith-sh
Copy link
Contributor

blacksmith-sh bot commented Dec 23, 2025

[Automatic Post]: I have assigned @xingyaoww as a reviewer based on git blame information. Thanks in advance for the help!

@blacksmith-sh
Copy link
Contributor

blacksmith-sh bot commented Dec 28, 2025

[Automatic Post]: This PR seems to be currently waiting for review. @xingyaoww, could you please take a look when you have a chance?

Copy link
Collaborator

@xingyaoww xingyaoww left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@xingyaoww xingyaoww enabled auto-merge (squash) December 28, 2025 18:14
@all-hands-bot
Copy link
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @shanemort1982, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants