Skip to content

fix(caddy): stop the redirect loop on the balena Public Device URL - #3535

Open
vpetersson-bot wants to merge 1 commit into
masterfrom
fix/balena-public-url-redirect-loop
Open

vpetersson-bot wants to merge 1 commit into
masterfrom
fix/balena-public-url-redirect-loop

Conversation

@vpetersson-bot

Copy link
Copy Markdown
Contributor

What this fixes

Fixes #3534. On balenaCloud, the web interface can't be opened through the Public Device URL since 2026.11.2: the browser stops with ERR_TOO_MANY_REDIRECTS.

balena's public URL terminates HTTPS on balena's side and forwards to the device's port 80 over plain HTTP, keeping the public host name. :80 redirected every non-API request to https://{host}{uri}, so the browser was sent back to the URL it was already on, forever.

The change

src/anthias_server/lib/caddy.py:

  • On balena (BALENA set), the :80 server gets a first route for *.balena-devices.com. It serves the UI through the same routes as :443 (CSP headers, small-body caps on /login and the certificate upload, X-Anthias-Listener: ui, client-cert header stripped), and sets X-Forwarded-Proto: https. Django then treats the request as secure, so its CSRF origin check (https://<uuid>.balena-devices.com) and the Secure session/CSRF cookies match what the browser sees.
  • HSTS is not sent for that host, because the domain belongs to balena.
  • Other hosts on :80 keep today's behavior: /api/* is proxied and everything else gets a 308 to HTTPS. Non-balena installs get exactly the same config as before.
  • proxy_handler() and _ui_routes() take an optional forwarded_https flag. Their default output is unchanged.

Security notes

  • The route keys on the Host header, which a LAN client controls. A client that sends Host: x.balena-devices.com to :80 on purpose gets the UI over plain HTTP, the same as any pre-HTTPS release. It can't use this to downgrade another user's browser: an attacker in a position to do that could already strip the redirect.
  • Caddy's automatic HTTPS does not manage certificates for servers on its http_port, so the host matcher does not make the device request a certificate for *.balena-devices.com. The local check below confirmed no ACME attempt.

How I know it works

  • Unit tests in tests/test_web_tls.py: the balena route, its headers and its order on balena, and no balena hosts off balena. tests/test_web_tls.py passes (84 tests), as do 407 caddy/TLS/management/controller tests. ruff check and ruff format --check are clean.
  • End to end with Caddy 2.11.9: I loaded the generated :80 config and put a TLS-terminating Caddy site in front of it for uuid.balena-devices.com, standing in for balena's proxy. A header-echoing upstream showed what Django receives.
    • Before: HTTP 308, location: https://uuid.balena-devices.com/, a loop.
    • After: HTTP 200. Upstream receives X-Forwarded-Proto: https and X-Anthias-Listener: ui, even when the client sends X-Anthias-Listener: m2m. CSP is set.
    • Unchanged: http://<lan-ip>/ still returns a 308 to HTTPS, and http://<lan-ip>/api/v2/info still returns 200.
  • I have not tested this against a real balenaCloud public URL.

🤖 Generated with Claude Code

balena's Public Device URL terminates HTTPS on balena's side and
forwards to the device's port 80 over plain HTTP, keeping the public
host name. :80 redirected every non-API request to https:// on the
same host, so the browser was sent back to the URL it was already on
until it gave up with ERR_TOO_MANY_REDIRECTS.

On balena, :80 now serves the UI for *.balena-devices.com through the
same routes as :443 (security headers, small-body caps, listener tag)
and tells Django the browser is on HTTPS, so CSRF origin checks and
secure cookies match. Other hosts and non-balena installs keep the
redirect.

Fixes #3534

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
@vpetersson-bot
vpetersson-bot requested a review from a team as a code owner November 30, 2026 08:06
Copilot AI lite review requested due to automatic review settings November 30, 2026 08:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot encountered an error: Your billing is not configured or you have Copilot licenses from multiple standalone organizations or enterprises. To use premium requests, select a billing entity via the GitHub site, under Settings > Copilot > Features.

@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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.

Redirect loop on the balenaCloud Public Device URL since 2026.11.2

2 participants