Repository navigation
fix(agent-server): tie uvicorn proxy_headers to trust_forwarded_headers - #5929
Conversation
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
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
…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
left a comment
There was a problem hiding this comment.
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_headersboth route through one_load_server_config()helper, and the fallback now logs and returnsNone(auth defaults to "not configured", trusted headers default to off) — both fail-safe. - Changed-branch parity: the diff was checked against base
6e7a8532. Onlyrequest.url.schemeconsumers arebridge.py:290(the buggy path) andauth_router._request_is_secure_context, which readsX-Forwarded-Protoitself and falls back torequest.url.scheme; the secure-cookie path is unaffected.request.clientis only used for logging insettings_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 assertsproxy_headersisFalsefor{}/falseandTruefortrue; 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, includingagent-server-tests,cross-tests,agent-server-stress-tests,pre-commit,REST API (OpenAPI),Check OpenAPI Schema, andbuild-binary-and-teston ubuntu/macos/windows. DockerBuild & Pushjobs 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# bugmarker 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
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.
python -m openhands.agent_serverstarts uvicorn withproxy_headers=trust_forwarded_headers. With the setting off (the default), uvicorn no longer rewrites the request scheme and client address fromX-Forwarded-Proto/X-Forwarded-For, even for loopback peers (FORWARDED_ALLOW_IPS). With it on, uvicorn behaves as before. This matches whatconfig.pydocuments fortrust_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.FORWARDED_ALLOW_IPS) that relied on uvicorn's rewrite while leavingtrust_forwarded_headersoff. There,request.url.schemebecomeshttpandrequest.clientbecomes the proxy's address (only logged, insettings_router.py), so the Canvas bridge answers 421 against anhttpsingress until the operator setsOH_TRUST_FORWARDED_HEADERS=true. Secure workspace cookies are unaffected (auth_router._request_is_secure_contextreadsX-Forwarded-Protoitself).FORWARDED_ALLOW_IPSmust be set to an unused address whentrust_forwarded_headersis off. That keeps today's deployments unchanged but leaves the documented contract false by default.Why
With
trust_forwarded_headersoff, a client on a loopback peer that sendsX-Forwarded-Proto: httpschanged the Canvas App bridge's request origin: the/app-backends/{name}/sessionbootstrap and proxied requests on a plain-HTTP ingress answered421 Canvas App request must use the configured ingress origininstead of200, and the same header let a loopback client claimhttpsagainst an HTTPS ingress. uvicorn's defaultproxy_headersrewrote the scheme before the bridge's_request_originread it.Summary
__main__.py: resolve the agent-server config once through a small_load_server_config()helper (shared with_auth_enabled) and passproxy_headers=_trust_forwarded_headers()to uvicorn'sConfig. No change to the bridge proxy code.test_preload_modules.pychecks theproxy_headersvaluemain()passes for each flag setting;canvas_extensions/test_canvas_extension_bridge.pyruns the bridge on a live uvicorn server with the launcher'sproxy_headersvalue and sendsX-Forwarded-Proto: httpsagainst 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.known bugmarker fromF29.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 defaultproxy_headers=Truethe live test gets421 == 200(plain-HTTP ingress) and200 == 421(HTTPS ingress).AGENT_SERVER_VERIFY_HOME=/var/tmp/avs-forwarded-proto:--checkoutat 6e7a853):control-agent-server map run --file F29 --only F29.session-forwarded-proto --fresh --repeat 2 --checkout <main>->xfailin both attempts.--checkout->xpassin both attempts.control-agent-server map checkok (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); fullmap 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
Notes
Found by the first full agentic run of verify-agent-server; tracked in #5653. Only the
python -m openhands.agent_serverentry point is changed; a deployment that launchesuvicorn openhands.agent_server.api:apidirectly 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
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimepython-node-runtimegolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:26192d9-pythonRun
All tags pushed for this build
About Multi-Architecture Support
26192d9-python) is a multi-arch manifest supporting both amd64 and arm6426192d9-python-amd64) are also available if needed