From 3563388d4d0579054e6a1dd8a43fab9e833dd687 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Thu, 13 Nov 2025 03:32:14 +0000 Subject: [PATCH] Frontend Service: Client side redirect to custom domain (#113717) * handle redirect in frontend service * add comments --- pkg/middleware/validate_host.go | 10 ++++++++++ pkg/services/frontend/index.html | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/pkg/middleware/validate_host.go b/pkg/middleware/validate_host.go index eb077b8e06c..ebdf96b4274 100644 --- a/pkg/middleware/validate_host.go +++ b/pkg/middleware/validate_host.go @@ -32,6 +32,16 @@ func ValidateHostHeader(cfg *setting.Cfg) web.Handler { } if !strings.EqualFold(h, cfg.Domain) { + // the normal redirecting logic doesn't work when running as frontend service, since it has no knowledge of the custom domain. + // instead, we modify the single tenant `/bootdata` call with a 204 response and a header indicating the domain to redirect to + // this is safe because only the frontend service calls `/bootdata` + // the redirect is then handled client side. + // see pkg/services/frontend/index.html + if c.Req.URL.Path == "/bootdata" { + c.Resp.Header().Set("Redirect-Domain", cfg.Domain) + c.Resp.WriteHeader(204) + return + } hostRedirectCounter.Inc() c.Logger.Info("Enforcing Host header", "hosted", c.Req.Host, "expected", cfg.Domain) c.Redirect(strings.TrimSuffix(cfg.AppURL, "/")+c.Req.RequestURI, 301) diff --git a/pkg/services/frontend/index.html b/pkg/services/frontend/index.html index f937a987ca4..c8ba00870b9 100644 --- a/pkg/services/frontend/index.html +++ b/pkg/services/frontend/index.html @@ -220,6 +220,16 @@ } const resp = await fetch(bootDataUrl); + + // manual redirect for custom domains + // see pkg/middleware/validate_host.go + if (resp.status === 204) { + const redirectDomain = resp.headers.get('Redirect-Domain'); + if (redirectDomain) { + window.location.hostname = redirectDomain; + return; + } + } const textResponse = await resp.text(); let rawBootData;