~adnano/go-gemini-devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH go-gemini] server: abort request handling on bad requests

~hugo_wetterberg <hugo_wetterberg@git.sr.ht>
Details
Message ID
<160987501305.19208.2405626995133153301-0@git.sr.ht>
DKIM signature
missing
Download raw message
Patch: +16 -14
From: Hugo Wetterberg <hugo@wetterberg.nu>

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).
---
 server.go | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/server.go b/server.go
index 5643dcd..1f9078a 100644
--- a/server.go
+++ b/server.go
@@ -188,27 +188,29 @@ func (s *Server) respond(conn net.Conn) {
	req, err := ReadRequest(conn)
	if err != nil {
		w.WriteStatus(StatusBadRequest)
	} else {
		// Store information about the TLS connection
		if tlsConn, ok := conn.(*tls.Conn); ok {
			req.TLS = tlsConn.ConnectionState()
			if len(req.TLS.PeerCertificates) > 0 {
				peerCert := req.TLS.PeerCertificates[0]
				// Store the TLS certificate
				req.Certificate = &tls.Certificate{
					Certificate: [][]byte{peerCert.Raw},
					Leaf:        peerCert,
				}
		return
	}

	// Store information about the TLS connection
	if tlsConn, ok := conn.(*tls.Conn); ok {
		req.TLS = tlsConn.ConnectionState()
		if len(req.TLS.PeerCertificates) > 0 {
			peerCert := req.TLS.PeerCertificates[0]
			// Store the TLS certificate
			req.Certificate = &tls.Certificate{
				Certificate: [][]byte{peerCert.Raw},
				Leaf:        peerCert,
			}
		}
	}

	resp := s.responder(req)
	if resp != nil {
		resp.Respond(w, req)
	} else {
	if resp == nil {
		w.WriteStatus(StatusNotFound)
		return
	}

	resp.Respond(w, req)
}

func (s *Server) responder(r *Request) Responder {
-- 
2.26.2
Details
Message ID
<C8BMCECRQLM2.9AP62CX29M1V@nitro>
In-Reply-To
<160987501305.19208.2405626995133153301-0@git.sr.ht> (view parent)
DKIM signature
pass
Download raw message
Thanks!

To git.sr.ht:~adnano/go-gemini
   c8626ba..efef44c  master -> master
Reply to thread Export thread (mbox)