Repository navigation
fix(caddy): stop the redirect loop on the balena Public Device URL - #3535
Open
vpetersson-bot wants to merge 1 commit into
Open
vpetersson-bot wants to merge 1 commit into
vpetersson-bot wants to merge 1 commit into
Conversation
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>
There was a problem hiding this comment.
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.
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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.
:80redirected every non-API request tohttps://{host}{uri}, so the browser was sent back to the URL it was already on, forever.The change
src/anthias_server/lib/caddy.py:BALENAset), the:80server gets a first route for*.balena-devices.com. It serves the UI through the same routes as:443(CSP headers, small-body caps on/loginand the certificate upload,X-Anthias-Listener: ui, client-cert header stripped), and setsX-Forwarded-Proto: https. Django then treats the request as secure, so its CSRF origin check (https://<uuid>.balena-devices.com) and theSecuresession/CSRF cookies match what the browser sees.:80keep 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 optionalforwarded_httpsflag. Their default output is unchanged.Security notes
Hostheader, which a LAN client controls. A client that sendsHost: x.balena-devices.comto:80on 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.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
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.pypasses (84 tests), as do 407 caddy/TLS/management/controller tests.ruff checkandruff format --checkare clean.:80config and put a TLS-terminating Caddy site in front of it foruuid.balena-devices.com, standing in for balena's proxy. A header-echoing upstream showed what Django receives.HTTP 308,location: https://uuid.balena-devices.com/, a loop.HTTP 200. Upstream receivesX-Forwarded-Proto: httpsandX-Anthias-Listener: ui, even when the client sendsX-Anthias-Listener: m2m. CSP is set.http://<lan-ip>/still returns a 308 to HTTPS, andhttp://<lan-ip>/api/v2/infostill returns 200.🤖 Generated with Claude Code