Skip to content

Commit

Permalink
WIP: custom DoH request validation
Browse files Browse the repository at this point in the history
Signed-off-by: Johnny Bergström <johnny@klaudify.se>
  • Loading branch information
balboah committed Dec 10, 2020
1 parent 6bbb48d commit 09b5443
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions core/dnsserver/config.go
Expand Up @@ -3,6 +3,7 @@ package dnsserver
import (
"crypto/tls"
"fmt"
"net/http"

"github.com/coredns/caddy"
"github.com/coredns/coredns/plugin"
Expand Down Expand Up @@ -31,6 +32,10 @@ type Config struct {
// DNS-over-TLS or DNS-over-gRPC.
Transport string

// If this function is not nil it will be used to inspect and validate
// HTTP requests.
HTTPRequestValidateFunc func(*http.Request) bool

// If this function is not nil it will be used to further filter access
// to this handler. The primary use is to limit access to a reverse zone
// on a non-octet boundary, i.e. /17
Expand Down
22 changes: 17 additions & 5 deletions core/dnsserver/server_https.go
Expand Up @@ -20,9 +20,10 @@ import (
// ServerHTTPS represents an instance of a DNS-over-HTTPS server.
type ServerHTTPS struct {
*Server
httpsServer *http.Server
listenAddr net.Addr
tlsConfig *tls.Config
httpsServer *http.Server
listenAddr net.Addr
tlsConfig *tls.Config
validRequest func(*http.Request) bool
}

// NewServerHTTPS returns a new CoreDNS GRPC server and compiles all plugins in to it.
Expand All @@ -45,12 +46,23 @@ func NewServerHTTPS(addr string, group []*Config) (*ServerHTTPS, error) {
// or the upgrade won't happen.
tlsConfig.NextProtos = []string{"h2", "http/1.1"}

// Use a custom request validation func or use the standard DoH path check.
var validator func(*http.Request) bool
for _, conf := range s.zones {
validator = conf.HTTPRequestValidateFunc
}
if validator == nil {
validator = func(r *http.Request) bool { return r.URL.Path == doh.Path }
}

srv := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
sh := &ServerHTTPS{Server: s, tlsConfig: tlsConfig, httpsServer: srv}
sh := &ServerHTTPS{
Server: s, tlsConfig: tlsConfig, httpsServer: srv, validRequest: validator,
}
sh.httpsServer.Handler = sh

return sh, nil
Expand Down Expand Up @@ -114,7 +126,7 @@ func (s *ServerHTTPS) Stop() error {
// chain, converts it back and write it to the client.
func (s *ServerHTTPS) ServeHTTP(w http.ResponseWriter, r *http.Request) {

if r.URL.Path != doh.Path {
if !s.validRequest(r) {
http.Error(w, "", http.StatusNotFound)
return
}
Expand Down

0 comments on commit 09b5443

Please sign in to comment.