Frontend Service: Client side redirect to custom domain (#113717)
* handle redirect in frontend service * add comments
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user