From fe5018de51f63af3b5b02c025a55f71dba49bc4d Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Fri, 2 Feb 2018 10:11:25 +0100 Subject: [PATCH] Issue #433: Ensure proxy.noroutestatus has three digits go1.10 requires that http.WriteHeader() is called with a valid status code. Fixes #433 --- config/load.go | 7 ++++++- config/load_test.go | 12 ++++++++++++ proxy/http_proxy.go | 6 +++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/config/load.go b/config/load.go index 1b42436a9..4529693f5 100644 --- a/config/load.go +++ b/config/load.go @@ -120,7 +120,7 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c f.IntVar(&cfg.Proxy.MaxConn, "proxy.maxconn", defaultConfig.Proxy.MaxConn, "maximum number of cached connections") f.StringVar(&cfg.Proxy.Strategy, "proxy.strategy", defaultConfig.Proxy.Strategy, "load balancing strategy") f.StringVar(&cfg.Proxy.Matcher, "proxy.matcher", defaultConfig.Proxy.Matcher, "path matching algorithm") - f.IntVar(&cfg.Proxy.NoRouteStatus, "proxy.noroutestatus", defaultConfig.Proxy.NoRouteStatus, "status code for invalid route") + f.IntVar(&cfg.Proxy.NoRouteStatus, "proxy.noroutestatus", defaultConfig.Proxy.NoRouteStatus, "status code for invalid route. Must be three digits") f.DurationVar(&cfg.Proxy.ShutdownWait, "proxy.shutdownwait", defaultConfig.Proxy.ShutdownWait, "time for graceful shutdown") f.DurationVar(&cfg.Proxy.DialTimeout, "proxy.dialtimeout", defaultConfig.Proxy.DialTimeout, "connection timeout for backend connections") f.DurationVar(&cfg.Proxy.ResponseHeaderTimeout, "proxy.responseheadertimeout", defaultConfig.Proxy.ResponseHeaderTimeout, "response header timeout") @@ -252,6 +252,11 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c return nil, fmt.Errorf("invalid ui.access: %s", cfg.UI.Access) } + // go1.10 will not accept a non-three digit status code + if cfg.Proxy.NoRouteStatus < 100 || cfg.Proxy.NoRouteStatus > 999 { + return nil, fmt.Errorf("proxy.noroutestatus must be between 100 and 999") + } + // handle deprecations deprecate := func(name, msg string) { if f.IsSet(name) { diff --git a/config/load_test.go b/config/load_test.go index b0fc6f41f..a58745f81 100644 --- a/config/load_test.go +++ b/config/load_test.go @@ -917,6 +917,18 @@ func TestLoad(t *testing.T) { cfg: func(cfg *Config) *Config { return nil }, err: errors.New("cert source requires proto 'https' or 'tcp'"), }, + { + desc: "-proxy.noroutestatus too small", + args: []string{"-proxy.noroutestatus", "10"}, + cfg: func(cfg *Config) *Config { return nil }, + err: errors.New("proxy.noroutestatus must be between 100 and 999"), + }, + { + desc: "-proxy.noroutestatus too big", + args: []string{"-proxy.noroutestatus", "1000"}, + cfg: func(cfg *Config) *Config { return nil }, + err: errors.New("proxy.noroutestatus must be between 100 and 999"), + }, { args: []string{"-cfg"}, cfg: func(cfg *Config) *Config { return nil }, diff --git a/proxy/http_proxy.go b/proxy/http_proxy.go index 95511026f..ba505fc97 100644 --- a/proxy/http_proxy.go +++ b/proxy/http_proxy.go @@ -72,7 +72,11 @@ func (p *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { t := p.Lookup(r) if t == nil { - w.WriteHeader(p.Config.NoRouteStatus) + status := p.Config.NoRouteStatus + if status < 100 || status > 999 { + status = http.StatusNotFound + } + w.WriteHeader(status) html := noroute.GetHTML() if html != "" { io.WriteString(w, html)