From 53c88ccdabdae21e8a811c42b14362a402e58d90 Mon Sep 17 00:00:00 2001 From: "Elias R." <54182601+Roshick@users.noreply.github.com> Date: Mon, 14 Oct 2024 14:51:43 +0200 Subject: [PATCH] Fix transport connection broken issue --- main.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index b53e838..1b2af4e 100644 --- a/main.go +++ b/main.go @@ -46,17 +46,25 @@ func (m *Middleware) Validate() error { func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { var buffer bytes.Buffer r.Body = io.NopCloser(io.TeeReader(r.Body, &buffer)) + payloadBytes, err := io.ReadAll(r.Body) + if err != nil { + // bad request in case of payload error + w.WriteHeader(400) + _, err = w.Write(nil) + return err + } + r.Body = io.NopCloser(&buffer) actual := []byte(strings.TrimPrefix(r.Header.Get("X-Hub-Signature-256"), "sha256=")) mac := hmac.New(sha256.New, []byte(m.Secret)) - mac.Write(buffer.Bytes()) + mac.Write(payloadBytes) expected := []byte(hex.EncodeToString(mac.Sum(nil))) if !hmac.Equal(actual, expected) { // unauthorized in case of invalid signature w.WriteHeader(401) - _, err := w.Write(nil) + _, err = w.Write(nil) return err }