1package middleware
2
3import (
4	"strings"
5
6	"github.com/grafana/grafana/pkg/models"
7	"github.com/grafana/grafana/pkg/setting"
8	"github.com/grafana/grafana/pkg/web"
9)
10
11func ValidateHostHeader(cfg *setting.Cfg) web.Handler {
12	return func(c *models.ReqContext) {
13		// ignore local render calls
14		if c.IsRenderCall {
15			return
16		}
17
18		h := c.Req.Host
19		if i := strings.Index(h, ":"); i >= 0 {
20			h = h[:i]
21		}
22
23		if !strings.EqualFold(h, cfg.Domain) {
24			c.Redirect(strings.TrimSuffix(cfg.AppURL, "/")+c.Req.RequestURI, 301)
25			return
26		}
27	}
28}
29