Skip to content

Add CORS origins support to Docker sandbox service for remote browser access#12489

Merged
tofarr merged 4 commits intomainfrom
fix/docker-sandbox-cors-origins
Jan 18, 2026
Merged

Add CORS origins support to Docker sandbox service for remote browser access#12489
tofarr merged 4 commits intomainfrom
fix/docker-sandbox-cors-origins

Conversation

@tofarr
Copy link
Copy Markdown
Collaborator

@tofarr tofarr commented Jan 18, 2026

Summary of PR

This PR adds CORS (Cross-Origin Resource Sharing) support to the Docker sandbox service, matching the existing implementation in the remote sandbox service. This fixes an issue where users running OpenHands via Docker on a remote server (e.g., Ubuntu Server VM) would encounter WebSocket connection failures when accessing the UI from an external machine.

Changes:

  • Added ALLOW_CORS_ORIGINS_VARIABLE constant (OH_ALLOW_CORS_ORIGINS_0) to docker_sandbox_service.py
  • Added web_url field to DockerSandboxService dataclass
  • Updated start_sandbox() to set CORS origins environment variable when web_url is configured
  • Updated DockerSandboxServiceInjector to retrieve web_url from global config and pass it to the service
  • Added unit tests for CORS functionality

When users configure WEB_HOST or web_url, the Docker sandbox service will now pass the OH_ALLOW_CORS_ORIGINS_0 environment variable to agent-server containers, allowing them to accept requests from remote browsers.

This complements the existing SANDBOX_CONTAINER_URL_PATTERN environment variable for remote access scenarios.

Demo Screenshots/Videos

N/A - Backend infrastructure change

Change Type

  • Bug fix
  • New feature
  • Breaking change
  • Refactor
  • Other (dependency update, docs, typo fixes, etc.)

Checklist

  • I have read and reviewed the code and I understand what the code is doing.
  • I have tested the code to the best of my ability and ensured it works as expected.

Fixes

Resolves the issue discussed in Discord where users running OpenHands on a remote server couldn't access from external browsers due to CORS errors with WebSocket connections.

Release Notes

  • Include this change in the Release Notes.

Added CORS support to Docker sandbox service for remote browser access. Users running OpenHands on remote servers should set WEB_HOST environment variable (e.g., WEB_HOST=192.168.1.100:3000) along with SANDBOX_CONTAINER_URL_PATTERN for proper remote access configuration.

@tofarr can click here to continue refining the PR


To run this PR locally, use the following command:

GUI with Docker:

docker run -it --rm   -p 3000:3000   -v /var/run/docker.sock:/var/run/docker.sock   --add-host host.docker.internal:host-gateway   -e SANDBOX_RUNTIME_CONTAINER_IMAGE=docker.openhands.dev/openhands/runtime:b0a69e7-nikolaik   --name openhands-app-b0a69e7   docker.openhands.dev/openhands/openhands:b0a69e7

… access

This change adds CORS (Cross-Origin Resource Sharing) support to the Docker
sandbox service, matching the existing implementation in the remote sandbox
service. This fixes an issue where users running OpenHands via Docker on a
remote server (e.g., Ubuntu Server VM) would encounter WebSocket connection
failures when accessing the UI from an external machine.

Changes:
- Added ALLOW_CORS_ORIGINS_VARIABLE constant ('OH_ALLOW_CORS_ORIGINS_0')
- Added web_url field to DockerSandboxService dataclass
- Updated start_sandbox() to set CORS origins when web_url is configured
- Updated DockerSandboxServiceInjector to retrieve web_url from global config
- Added unit tests for CORS functionality

When users configure WEB_HOST or web_url, the Docker sandbox service will now
pass the OH_ALLOW_CORS_ORIGINS_0 environment variable to agent-server containers,
allowing them to accept requests from remote browsers.

This complements the existing SANDBOX_CONTAINER_URL_PATTERN environment variable
for remote access scenarios.

Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 18, 2026

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  openhands/app_server/sandbox
  docker_sandbox_service.py 334-339, 534-544
  remote_sandbox_service.py
  sandbox_service.py
Project Total  

This report was generated by python-coverage-comment-action

@tofarr tofarr marked this pull request as ready for review January 18, 2026 14:43
Copy link
Copy Markdown
Collaborator

@hieptl hieptl left a comment

Choose a reason for hiding this comment

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

Thank you! 🙏

@kripper
Copy link
Copy Markdown
Contributor

kripper commented Apr 9, 2026

Hi @tofarr, is your self-hosted OpenHands currently working?
Are you using the latest upstream version?

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