Skip to content

fix(agent-server): tie uvicorn proxy_headers to trust_forwarded_headers - #5929

Merged
enyst merged 2 commits into
mainfrom
claude/umbrella-bug-fixes-tgz9ai-forwarded-proto
Oct 14, 2026
Merged

enyst merged 2 commits into
mainfrom
claude/umbrella-bug-fixes-tgz9ai-forwarded-proto

Conversation

@enyst

@enyst enyst commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

HUMAN:
This PR proposes a fix for a bug uncovered during the first full agentic run of verify-agent-server.


AGENT:

I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.

Requested by Engel · project thread

Decision needed

This changes deployment behaviour behind a real proxy, so a maintainer should confirm it.

  • Implemented: python -m openhands.agent_server starts uvicorn with proxy_headers=trust_forwarded_headers. With the setting off (the default), uvicorn no longer rewrites the request scheme and client address from X-Forwarded-Proto/X-Forwarded-For, even for loopback peers (FORWARDED_ALLOW_IPS). With it on, uvicorn behaves as before. This matches what config.py documents for trust_forwarded_headers, and it is the only place the fix can live: uvicorn mutates the ASGI scope in place before the app runs, so the bridge cannot read the raw scheme back.
  • Who notices: a deployment with a TLS-terminating proxy on loopback (or listed in FORWARDED_ALLOW_IPS) that relied on uvicorn's rewrite while leaving trust_forwarded_headers off. There, request.url.scheme becomes http and request.client becomes the proxy's address (only logged, in settings_router.py), so the Canvas bridge answers 421 against an https ingress until the operator sets OH_TRUST_FORWARDED_HEADERS=true. Secure workspace cookies are unaffected (auth_router._request_is_secure_context reads X-Forwarded-Proto itself).
  • Alternative: keep uvicorn's default and instead document that FORWARDED_ALLOW_IPS must be set to an unused address when trust_forwarded_headers is off. That keeps today's deployments unchanged but leaves the documented contract false by default.

Why

With trust_forwarded_headers off, a client on a loopback peer that sends X-Forwarded-Proto: https changed the Canvas App bridge's request origin: the /app-backends/{name}/session bootstrap and proxied requests on a plain-HTTP ingress answered 421 Canvas App request must use the configured ingress origin instead of 200, and the same header let a loopback client claim https against an HTTPS ingress. uvicorn's default proxy_headers rewrote the scheme before the bridge's _request_origin read it.

Summary

  • __main__.py: resolve the agent-server config once through a small _load_server_config() helper (shared with _auth_enabled) and pass proxy_headers=_trust_forwarded_headers() to uvicorn's Config. No change to the bridge proxy code.
  • Tests: test_preload_modules.py checks the proxy_headers value main() passes for each flag setting; canvas_extensions/test_canvas_extension_bridge.py runs the bridge on a live uvicorn server with the launcher's proxy_headers value and sends X-Forwarded-Proto: https against a plain-HTTP ingress (bootstrap and proxied GET answer 200) and against an HTTPS ingress reached over plain HTTP (421), as the issue's triage asks.
  • F29 map: drop the known bug marker from F29.session-forwarded-proto, rewrite its prose and the uvicorn Gotchas line as the expected behaviour.

Issue Number

Fixes #5928

How to Test

  • uv run pytest -q tests/agent_server/canvas_extensions/test_canvas_extension_bridge.py tests/agent_server/test_preload_modules.py: 50 passed. On main's behaviour the new cases fail: KeyError: 'proxy_headers' for the launcher test, and with uvicorn's default proxy_headers=True the live test gets 421 == 200 (plain-HTTP ingress) and 200 == 421 (HTTPS ingress).
  • Map proof, AGENT_SERVER_VERIFY_HOME=/var/tmp/avs-forwarded-proto:
    • main (--checkout at 6e7a853): control-agent-server map run --file F29 --only F29.session-forwarded-proto --fresh --repeat 2 --checkout <main> -> xfail in both attempts.
    • this branch (before removing the marker): same command without --checkout -> xpass in both attempts.
    • after removing the marker: control-agent-server map check ok (34 files, 935 ids); map run --file F29 --only F29.session-forwarded-proto --fresh -> pass; --only F29.trusted-forwarded-headers -> pass (forwarded headers still decide the origin when trusted); full map run --file F29 --fresh -> 25 pass, 0 fail, 5 xfail (other known bugs).
  • uv run pre-commit run --files <changed files>: all passed.

Video/Screenshots

n/a

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

Found by the first full agentic run of verify-agent-server; tracked in #5653. Only the python -m openhands.agent_server entry point is changed; a deployment that launches uvicorn openhands.agent_server.api:api directly keeps uvicorn's own defaults (no such launcher exists in this repository).

🤖 Generated with Claude Code

https://claude.ai/code/session_014MKndVj7XWvRQmvW9w2bDg


🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:26192d9-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-26192d9-python \
  ghcr.io/openhands/agent-server:26192d9-python

All tags pushed for this build

ghcr.io/openhands/agent-server:26192d9-golang-amd64
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-golang-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-golang-amd64
ghcr.io/openhands/agent-server:26192d9-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:26192d9-golang-arm64
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-golang-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-golang-arm64
ghcr.io/openhands/agent-server:26192d9-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:26192d9-java-amd64
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-java-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-java-amd64
ghcr.io/openhands/agent-server:26192d9-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:26192d9-java-arm64
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-java-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-java-arm64
ghcr.io/openhands/agent-server:26192d9-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:26192d9-python-amd64
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-python-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-python-amd64
ghcr.io/openhands/agent-server:26192d9-python-node-runtime-amd64
ghcr.io/openhands/agent-server:26192d9-python-arm64
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-python-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-python-arm64
ghcr.io/openhands/agent-server:26192d9-python-node-runtime-arm64
ghcr.io/openhands/agent-server:26192d9-python-minimal-amd64
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-python-minimal-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-python-minimal-amd64
ghcr.io/openhands/agent-server:26192d9-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:26192d9-python-minimal-arm64
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-python-minimal-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-python-minimal-arm64
ghcr.io/openhands/agent-server:26192d9-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:26192d9-python-slim-amd64
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-python-slim-amd64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-python-slim-amd64
ghcr.io/openhands/agent-server:26192d9-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:26192d9-python-slim-arm64
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-python-slim-arm64
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-python-slim-arm64
ghcr.io/openhands/agent-server:26192d9-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:26192d9-golang
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-golang
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-golang
ghcr.io/openhands/agent-server:26192d9-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:26192d9-java
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-java
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-java
ghcr.io/openhands/agent-server:26192d9-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:26192d9-python-minimal
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-python-minimal
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-python-minimal
ghcr.io/openhands/agent-server:26192d9-python-node-runtime-minimal
ghcr.io/openhands/agent-server:26192d9-python-slim
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-python-slim
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-python-slim
ghcr.io/openhands/agent-server:26192d9-python-node-runtime-slim
ghcr.io/openhands/agent-server:26192d9-python
ghcr.io/openhands/agent-server:26192d9242edf8f7bc09db6e7ce9c9d71413b1b2-python
ghcr.io/openhands/agent-server:claude-umbrella-bug-fixes-tgz9ai-forwarded-proto-python
ghcr.io/openhands/agent-server:26192d9-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 26192d9-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 26192d9-python-amd64) are also available if needed

uvicorn's default proxy_headers rewrote the request scheme from
X-Forwarded-Proto for loopback peers before the Canvas bridge derived the
request origin, so a client could change that origin (421 instead of 200)
while trust_forwarded_headers was off. Start uvicorn with proxy_headers
only when trust_forwarded_headers is on, and drop the F29 known-bug marker.

Fixes #5928

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014MKndVj7XWvRQmvW9w2bDg
@enyst enyst self-assigned this Oct 14, 2026
@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   __main__.py1431391%41–42, 47, 116, 207–208, 213, 229, 288, 326, 357–358, 366
TOTAL497431981760% 

@enyst
enyst marked this pull request as ready for review October 14, 2026 02:20
@enyst
enyst requested a review from all-hands-bot October 14, 2026 02:22
…onfig

Run the Canvas bridge on a live uvicorn server with the proxy_headers
value the launcher derives from trust_forwarded_headers, for a plain-HTTP
ingress (200 with X-Forwarded-Proto: https) and an HTTPS ingress reached
over plain HTTP (421), as the issue's triage acceptance criteria ask.

Refs #5928

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014MKndVj7XWvRQmvW9w2bDg

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This review was posted by an AI agent (OpenHands).

Summary

Ties uvicorn's proxy_headers to Config.trust_forwarded_headers in the python -m openhands.agent_server launcher (openhands-agent-server/openhands/agent_server/__main__.py). With the setting off (the default), uvicorn's ProxyHeadersMiddleware no longer rewrites scope["scheme"]/scope["client"] from X-Forwarded-Proto/X-Forwarded-For for FORWARDED_ALLOW_IPS peers (loopback by default), so the Canvas bridge's _request_origin reads the request's own scheme. With the setting on, behaviour is unchanged. This matches what config.py (line 374) documents and what bridge.py _request_origin already implements in its untrusted branch.

Verification

  • Scope: the change lives in the Agent Server entry point this repository owns; the canvas bridge proxy code is untouched. The refactor _auth_enabled/_trust_forwarded_headers both route through one _load_server_config() helper, and the fallback now logs and returns None (auth defaults to "not configured", trusted headers default to off) — both fail-safe.
  • Changed-branch parity: the diff was checked against base 6e7a8532. Only request.url.scheme consumers are bridge.py:290 (the buggy path) and auth_router._request_is_secure_context, which reads X-Forwarded-Proto itself and falls back to request.url.scheme; the secure-cookie path is unaffected. request.client is only used for logging in settings_router.py.
  • Focused tests: uv run pytest -q tests/agent_server/test_preload_modules.py tests/agent_server/canvas_extensions/test_canvas_extension_bridge.py -> 50 passed. The new parametrized launcher test asserts proxy_headers is False for {}/false and True for true; the live-bridge test boots real uvicorn with the launcher's value and confirms the header does not change the origin (200 on a plain-HTTP ingress, 421 against an HTTPS ingress).
  • CI on the exact head 26192d92: all completed checks pass, including agent-server-tests, cross-tests, agent-server-stress-tests, pre-commit, REST API (OpenAPI), Check OpenAPI Schema, and build-binary-and-test on ubuntu/macos/windows. Docker Build & Push jobs were still in progress at review time; they do not affect this unit-level change.
  • Linked issue #5928 is open with labels bug, priority:low, ready-for-dev; its four acceptance criteria (untrusted proto does not change origin; trusted forwarding still decides; a unit test covers the fix; the F29 map bullet passes with the # bug marker removed) are all addressed by this head, and the F29 map prose/Gotchas are updated accordingly.

Note for the maintainer

This deliberately changes deployment behaviour: a TLS-terminating proxy on loopback (or in FORWARDED_ALLOW_IPS) that relied on uvicorn's rewrite while leaving OH_TRUST_FORWARDED_HEADERS unset will now see request.url.scheme == http and the proxy address as the client. That is the documented contract for the flag being off, and the PR body flags it explicitly; the alternative (leaving uvicorn's default and documenting FORWARDED_ALLOW_IPS as a workaround) would leave the documented contract false by default. No code defect found.

✅ APPROVED

@enyst
enyst merged commit 4dc42d3 into main Oct 14, 2026
74 of 78 checks passed
@enyst
enyst deleted the claude/umbrella-bug-fixes-tgz9ai-forwarded-proto branch October 14, 2026 18:28
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.

[Bug]: X-Forwarded-Proto changes the Canvas bridge's request origin while trust_forwarded_headers is off (421 instead of 200)

3 participants