From 8bfd5adbbb551a80489d91618cb4d283249be5b3 Mon Sep 17 00:00:00 2001 From: Azareal Date: Sun, 14 Apr 2019 10:13:32 +1000 Subject: [PATCH] Added the LooseHost config setting. Malformed requests are now logged with an integer denoting the block of code which triggered it. --- common/site.go | 1 + docs/configuration.md | 2 ++ gen_router.go | 14 +++++++------- router_gen/main.go | 14 +++++++------- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/common/site.go b/common/site.go index a80091c5..bf5956a6 100644 --- a/common/site.go +++ b/common/site.go @@ -92,6 +92,7 @@ type config struct { DisableLiveTopicList bool DisableJSAntispam bool //LooseCSP bool + LooseHost bool DisableServerPush bool EnableCDNPush bool diff --git a/docs/configuration.md b/docs/configuration.md index 41fa30fb..3a509c4f 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -84,6 +84,8 @@ DisableLiveTopicList - This switch allows you to disable the live topic list. DisableJSAntispam - This switch lets you disable the JS anti-spam feature. It may be useful if you primarily get users who for one reason or another have decided to disable JavaScript. +LooseHost - Disable host header checks in the router. May be useful when using a reverse-proxy like Nginx. + DisableServerPush - This switch lets you disable the HTTP/2 server push feature. EnableCDNPush - This switch lets you enable the HTTP/2 CDN Server Push feature. This operates by sending a Link header on every request and may also work with reverse-proxies like Nginx for doing HTTP/2 server pushes. diff --git a/gen_router.go b/gen_router.go index 98a20715..43564b8f 100644 --- a/gen_router.go +++ b/gen_router.go @@ -704,10 +704,10 @@ func isLocalHost(host string) bool { // TODO: SetDefaultPath // TODO: GetDefaultPath func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { - var malformedRequest = func() { + var malformedRequest = func(typ int) { w.WriteHeader(200) // 400 w.Write([]byte("")) - r.DumpRequest(req,"Malformed Request") + r.DumpRequest(req,"Malformed Request T"+strconv.Itoa(typ)) counters.AgentViewCounter.Bump(27) } @@ -716,7 +716,7 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { if req.Host[0]=='[' { spl := strings.Split(req.Host,"]") if len(spl) > 2 { - malformedRequest() + malformedRequest(0) return } shost = strings.TrimPrefix(spl[0],"[") @@ -724,7 +724,7 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { } else { spl := strings.Split(req.Host,":") if len(spl) > 2 { - malformedRequest() + malformedRequest(1) return } shost = spl[0] @@ -734,7 +734,7 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { } // TODO: Reject requests from non-local IPs, if the site host is set to localhost or a localhost IP if common.Site.PortInt != 80 && common.Site.PortInt != 443 && sport != common.Site.Port { - malformedRequest() + malformedRequest(2) return } @@ -759,8 +759,8 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { } // Deflect malformed requests - if len(req.URL.Path) == 0 || req.URL.Path[0] != '/' || shost != common.Site.Host { - malformedRequest() + if len(req.URL.Path) == 0 || req.URL.Path[0] != '/' || (!common.Config.LooseHost && shost != common.Site.Host) { + malformedRequest(3) return } if common.Dev.FullReqLog { diff --git a/router_gen/main.go b/router_gen/main.go index 9d76c959..cd238e46 100644 --- a/router_gen/main.go +++ b/router_gen/main.go @@ -483,10 +483,10 @@ func isLocalHost(host string) bool { // TODO: SetDefaultPath // TODO: GetDefaultPath func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { - var malformedRequest = func() { + var malformedRequest = func(typ int) { w.WriteHeader(200) // 400 w.Write([]byte("")) - r.DumpRequest(req,"Malformed Request") + r.DumpRequest(req,"Malformed Request T"+strconv.Itoa(typ)) counters.AgentViewCounter.Bump({{.AllAgentMap.malformed}}) } @@ -495,7 +495,7 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { if req.Host[0]=='[' { spl := strings.Split(req.Host,"]") if len(spl) > 2 { - malformedRequest() + malformedRequest(0) return } shost = strings.TrimPrefix(spl[0],"[") @@ -503,7 +503,7 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { } else { spl := strings.Split(req.Host,":") if len(spl) > 2 { - malformedRequest() + malformedRequest(1) return } shost = spl[0] @@ -513,7 +513,7 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { } // TODO: Reject requests from non-local IPs, if the site host is set to localhost or a localhost IP if common.Site.PortInt != 80 && common.Site.PortInt != 443 && sport != common.Site.Port { - malformedRequest() + malformedRequest(2) return } @@ -538,8 +538,8 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { } // Deflect malformed requests - if len(req.URL.Path) == 0 || req.URL.Path[0] != '/' || shost != common.Site.Host { - malformedRequest() + if len(req.URL.Path) == 0 || req.URL.Path[0] != '/' || (!common.Config.LooseHost && shost != common.Site.Host) { + malformedRequest(3) return } if common.Dev.FullReqLog {