server: abort request handling on bad requests

A request to a hostname that hasn't been registered with the server
currently results in a nil pointer deref panic in server.go:215 as
request handling continues even if ReadRequest() returns an error.

This change changes all if-else error handling in Server.respond() to
a WriteStatus-call and early return. This makes it clear when request
handling is aborted (and actually aborts when ReadRequest() fails).
This commit is contained in:
Hugo Wetterberg 2021-01-05 20:16:33 +01:00 committed by Adnan Maolood
parent c8626bae17
commit efef44c2f9

View File

@ -188,27 +188,29 @@ func (s *Server) respond(conn net.Conn) {
req, err := ReadRequest(conn) req, err := ReadRequest(conn)
if err != nil { if err != nil {
w.WriteStatus(StatusBadRequest) w.WriteStatus(StatusBadRequest)
} else { return
// Store information about the TLS connection }
if tlsConn, ok := conn.(*tls.Conn); ok {
req.TLS = tlsConn.ConnectionState() // Store information about the TLS connection
if len(req.TLS.PeerCertificates) > 0 { if tlsConn, ok := conn.(*tls.Conn); ok {
peerCert := req.TLS.PeerCertificates[0] req.TLS = tlsConn.ConnectionState()
// Store the TLS certificate if len(req.TLS.PeerCertificates) > 0 {
req.Certificate = &tls.Certificate{ peerCert := req.TLS.PeerCertificates[0]
Certificate: [][]byte{peerCert.Raw}, // Store the TLS certificate
Leaf: peerCert, req.Certificate = &tls.Certificate{
} Certificate: [][]byte{peerCert.Raw},
Leaf: peerCert,
} }
} }
} }
resp := s.responder(req) resp := s.responder(req)
if resp != nil { if resp == nil {
resp.Respond(w, req)
} else {
w.WriteStatus(StatusNotFound) w.WriteStatus(StatusNotFound)
return
} }
resp.Respond(w, req)
} }
func (s *Server) responder(r *Request) Responder { func (s *Server) responder(r *Request) Responder {