mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-30 17:53:21 -04:00 
			
		
		
		
	core: block CH queries earlier (#973)
block chaos queries, unless the chaos or proxy middleware is loaded. We respond with REFUSED. This removes the need for each middleware to do this class != ClassINET if-then. Also make config.Registry non-public.
This commit is contained in:
		| @@ -42,7 +42,7 @@ type Config struct { | |||||||
| 	// Middleware interested in announcing that they exist, so other middleware can call methods | 	// Middleware interested in announcing that they exist, so other middleware can call methods | ||||||
| 	// on them should register themselves here. The name should be the name as return by the | 	// on them should register themselves here. The name should be the name as return by the | ||||||
| 	// Handler's Name method. | 	// Handler's Name method. | ||||||
| 	Registry map[string]middleware.Handler | 	registry map[string]middleware.Handler | ||||||
| } | } | ||||||
|  |  | ||||||
| // GetConfig gets the Config that corresponds to c. | // GetConfig gets the Config that corresponds to c. | ||||||
|   | |||||||
| @@ -127,12 +127,12 @@ func (c *Config) AddMiddleware(m middleware.Middleware) { | |||||||
| // registerHandler adds a handler to a site's handler registration. Handlers | // registerHandler adds a handler to a site's handler registration. Handlers | ||||||
| //  use this to announce that they exist to other middleware. | //  use this to announce that they exist to other middleware. | ||||||
| func (c *Config) registerHandler(h middleware.Handler) { | func (c *Config) registerHandler(h middleware.Handler) { | ||||||
| 	if c.Registry == nil { | 	if c.registry == nil { | ||||||
| 		c.Registry = make(map[string]middleware.Handler) | 		c.registry = make(map[string]middleware.Handler) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Just overwrite... | 	// Just overwrite... | ||||||
| 	c.Registry[h.Name()] = h | 	c.registry[h.Name()] = h | ||||||
| } | } | ||||||
|  |  | ||||||
| // Handler returns the middleware handler that has been added to the config under its name. | // Handler returns the middleware handler that has been added to the config under its name. | ||||||
| @@ -140,10 +140,10 @@ func (c *Config) registerHandler(h middleware.Handler) { | |||||||
| // Note that this is order dependent and the order is defined in directives.go, i.e. if your middleware | // Note that this is order dependent and the order is defined in directives.go, i.e. if your middleware | ||||||
| // comes before the middleware you are checking; it will not be there (yet). | // comes before the middleware you are checking; it will not be there (yet). | ||||||
| func (c *Config) Handler(name string) middleware.Handler { | func (c *Config) Handler(name string) middleware.Handler { | ||||||
| 	if c.Registry == nil { | 	if c.registry == nil { | ||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
| 	if h, ok := c.Registry[name]; ok { | 	if h, ok := c.registry[name]; ok { | ||||||
| 		return h | 		return h | ||||||
| 	} | 	} | ||||||
| 	return nil | 	return nil | ||||||
|   | |||||||
| @@ -37,9 +37,11 @@ type Server struct { | |||||||
| 	connTimeout time.Duration      // the maximum duration of a graceful shutdown | 	connTimeout time.Duration      // the maximum duration of a graceful shutdown | ||||||
| 	trace       trace.Trace        // the trace middleware for the server | 	trace       trace.Trace        // the trace middleware for the server | ||||||
| 	debug       bool               // disable recover() | 	debug       bool               // disable recover() | ||||||
|  | 	classChaos  bool               // allow non-INET class queries | ||||||
| } | } | ||||||
|  |  | ||||||
| // NewServer returns a new CoreDNS server and compiles all middleware in to it. | // NewServer returns a new CoreDNS server and compiles all middleware in to it. By default CH class | ||||||
|  | // queries are blocked unless the chaos or proxy is loaded. | ||||||
| func NewServer(addr string, group []*Config) (*Server, error) { | func NewServer(addr string, group []*Config) (*Server, error) { | ||||||
|  |  | ||||||
| 	s := &Server{ | 	s := &Server{ | ||||||
| @@ -77,6 +79,9 @@ func NewServer(addr string, group []*Config) (*Server, error) { | |||||||
| 					s.trace = t | 					s.trace = t | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
|  | 			if stack.Name() == "chaos" || stack.Name() == "proxy" { | ||||||
|  | 				s.classChaos = true | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
| 		site.middlewareChain = stack | 		site.middlewareChain = stack | ||||||
| 	} | 	} | ||||||
| @@ -184,6 +189,11 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) | |||||||
| 		}() | 		}() | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if !s.classChaos && r.Question[0].Qclass != dns.ClassINET { | ||||||
|  | 		DefaultErrorFunc(w, r, dns.RcodeRefused) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	if m, err := edns.Version(r); err != nil { // Wrong EDNS version, return at once. | 	if m, err := edns.Version(r); err != nil { // Wrong EDNS version, return at once. | ||||||
| 		w.WriteMsg(m) | 		w.WriteMsg(m) | ||||||
| 		return | 		return | ||||||
|   | |||||||
| @@ -2,7 +2,6 @@ | |||||||
| package auto | package auto | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"errors" |  | ||||||
| 	"regexp" | 	"regexp" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| @@ -43,9 +42,6 @@ type ( | |||||||
| // ServeDNS implements the middleware.Handle interface. | // ServeDNS implements the middleware.Handle interface. | ||||||
| func (a Auto) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | func (a Auto) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | ||||||
| 	state := request.Request{W: w, Req: r} | 	state := request.Request{W: w, Req: r} | ||||||
| 	if state.QClass() != dns.ClassINET { |  | ||||||
| 		return dns.RcodeServerFailure, middleware.Error(a.Name(), errors.New("can only deal with ClassINET")) |  | ||||||
| 	} |  | ||||||
| 	qname := state.Name() | 	qname := state.Name() | ||||||
|  |  | ||||||
| 	// TODO(miek): match the qname better in the map | 	// TODO(miek): match the qname better in the map | ||||||
|   | |||||||
| @@ -1,8 +1,6 @@ | |||||||
| package etcd | package etcd | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"errors" |  | ||||||
|  |  | ||||||
| 	"github.com/coredns/coredns/middleware" | 	"github.com/coredns/coredns/middleware" | ||||||
| 	"github.com/coredns/coredns/middleware/etcd/msg" | 	"github.com/coredns/coredns/middleware/etcd/msg" | ||||||
| 	"github.com/coredns/coredns/middleware/pkg/debug" | 	"github.com/coredns/coredns/middleware/pkg/debug" | ||||||
| @@ -17,9 +15,7 @@ import ( | |||||||
| func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | func (e *Etcd) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | ||||||
| 	opt := middleware.Options{} | 	opt := middleware.Options{} | ||||||
| 	state := request.Request{W: w, Req: r} | 	state := request.Request{W: w, Req: r} | ||||||
| 	if state.QClass() != dns.ClassINET { |  | ||||||
| 		return dns.RcodeServerFailure, middleware.Error(e.Name(), errors.New("can only deal with ClassINET")) |  | ||||||
| 	} |  | ||||||
| 	name := state.Name() | 	name := state.Name() | ||||||
| 	if e.Debugging { | 	if e.Debugging { | ||||||
| 		if bug := debug.IsDebug(name); bug != "" { | 		if bug := debug.IsDebug(name); bug != "" { | ||||||
|   | |||||||
| @@ -2,7 +2,6 @@ | |||||||
| package file | package file | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"errors" |  | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
| 	"log" | 	"log" | ||||||
| @@ -32,9 +31,6 @@ type ( | |||||||
| func (f File) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | func (f File) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | ||||||
| 	state := request.Request{W: w, Req: r} | 	state := request.Request{W: w, Req: r} | ||||||
|  |  | ||||||
| 	if state.QClass() != dns.ClassINET { |  | ||||||
| 		return dns.RcodeServerFailure, middleware.Error(f.Name(), errors.New("can only deal with ClassINET")) |  | ||||||
| 	} |  | ||||||
| 	qname := state.Name() | 	qname := state.Name() | ||||||
| 	// TODO(miek): match the qname better in the map | 	// TODO(miek): match the qname better in the map | ||||||
| 	zone := middleware.Zones(f.Zones.Names).Matches(qname) | 	zone := middleware.Zones(f.Zones.Names).Matches(qname) | ||||||
|   | |||||||
| @@ -1,7 +1,6 @@ | |||||||
| package hosts | package hosts | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"errors" |  | ||||||
| 	"net" | 	"net" | ||||||
|  |  | ||||||
| 	"golang.org/x/net/context" | 	"golang.org/x/net/context" | ||||||
| @@ -23,9 +22,6 @@ type Hosts struct { | |||||||
| // ServeDNS implements the middleware.Handle interface. | // ServeDNS implements the middleware.Handle interface. | ||||||
| func (h Hosts) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | func (h Hosts) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | ||||||
| 	state := request.Request{W: w, Req: r} | 	state := request.Request{W: w, Req: r} | ||||||
| 	if state.QClass() != dns.ClassINET { |  | ||||||
| 		return dns.RcodeServerFailure, middleware.Error(h.Name(), errors.New("can only deal with ClassINET")) |  | ||||||
| 	} |  | ||||||
| 	qname := state.Name() | 	qname := state.Name() | ||||||
|  |  | ||||||
| 	answers := []dns.RR{} | 	answers := []dns.RR{} | ||||||
|   | |||||||
| @@ -1,8 +1,6 @@ | |||||||
| package kubernetes | package kubernetes | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"errors" |  | ||||||
|  |  | ||||||
| 	"github.com/coredns/coredns/middleware" | 	"github.com/coredns/coredns/middleware" | ||||||
| 	"github.com/coredns/coredns/middleware/pkg/dnsutil" | 	"github.com/coredns/coredns/middleware/pkg/dnsutil" | ||||||
| 	"github.com/coredns/coredns/request" | 	"github.com/coredns/coredns/request" | ||||||
| @@ -14,9 +12,6 @@ import ( | |||||||
| // ServeDNS implements the middleware.Handler interface. | // ServeDNS implements the middleware.Handler interface. | ||||||
| func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { | ||||||
| 	state := request.Request{W: w, Req: r} | 	state := request.Request{W: w, Req: r} | ||||||
| 	if state.QClass() != dns.ClassINET { |  | ||||||
| 		return dns.RcodeServerFailure, middleware.Error(k.Name(), errors.New("can only deal with ClassINET")) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	m := new(dns.Msg) | 	m := new(dns.Msg) | ||||||
| 	m.SetReply(r) | 	m.SetReply(r) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user