Skip to content

Commit

Permalink
http_proxy_errors: improve tls record header error msg
Browse files Browse the repository at this point in the history
  • Loading branch information
Choraden authored and mmatczuk committed Sep 20, 2024
1 parent d5aef2f commit e2427f1
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion http_proxy_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ func handleTLSRecordHeader(req *http.Request, err error) (code int, msg, label s
var headerErr tls.RecordHeaderError
if errors.As(err, &headerErr) {
code = http.StatusBadGateway
msg = fmt.Sprintf("tls handshake failed for host %q", req.Host)
msg = fmt.Sprintf("tls handshake failed for host %q: ", req.Host)
if tlsRecordHeaderLooksLikeHTTP(headerErr.RecordHeader) {
msg += "scheme mismatch (looks like an HTTP request)"
} else {
msg += fmt.Sprintf("record header: %x", headerErr.RecordHeader)
}
label = "tls_record_header"
}

Expand Down Expand Up @@ -235,6 +240,15 @@ func handleStatusText(req *http.Request, err error) (code int, msg, label string
return
}

func tlsRecordHeaderLooksLikeHTTP(hdr [5]byte) bool {
return bytes.HasPrefix(hdr[:], []byte("HTTP")) ||
bytes.HasPrefix(hdr[:], []byte("GET")) ||
bytes.HasPrefix(hdr[:], []byte("HEAD")) ||
bytes.HasPrefix(hdr[:], []byte("POST")) ||
bytes.HasPrefix(hdr[:], []byte("PUT")) ||
bytes.HasPrefix(hdr[:], []byte("OPTIO"))
}

func describeCertificates(chain []*x509.Certificate) string {
var sb strings.Builder
for _, cert := range chain {
Expand Down

0 comments on commit e2427f1

Please sign in to comment.