Skip to content

Commit

Permalink
Fix transport connection broken issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Roshick committed Oct 14, 2024
1 parent 927b4bb commit 53c88cc
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 53c88cc

Please sign in to comment.