2015-05-05 04:21:06 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2020-02-28 05:50:58 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-05-05 04:21:06 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-05-05 04:21:06 -05:00
|
|
|
)
|
|
|
|
|
2021-10-11 07:30:59 -05:00
|
|
|
func ValidateHostHeader(cfg *setting.Cfg) web.Handler {
|
2020-02-28 05:50:58 -06:00
|
|
|
return func(c *models.ReqContext) {
|
2016-12-05 04:20:01 -06:00
|
|
|
// ignore local render calls
|
|
|
|
if c.IsRenderCall {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-05 04:21:06 -05:00
|
|
|
h := c.Req.Host
|
|
|
|
if i := strings.Index(h, ":"); i >= 0 {
|
|
|
|
h = h[:i]
|
|
|
|
}
|
|
|
|
|
2020-12-15 12:09:04 -06:00
|
|
|
if !strings.EqualFold(h, cfg.Domain) {
|
|
|
|
c.Redirect(strings.TrimSuffix(cfg.AppURL, "/")+c.Req.RequestURI, 301)
|
2015-05-05 04:21:06 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|