Skip to content

Commit 836ab00

Browse files
authored
Reset HTTP request body after reading the stream during webhook validation (#64)
1 parent 1dd11e2 commit 836ab00

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

client_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,11 @@ func TestValidateWebhook(t *testing.T) {
17441744
isValid, err := replicate.ValidateWebhookRequest(req, testSecret)
17451745
require.NoError(t, err)
17461746
assert.True(t, isValid)
1747+
1748+
// Ensure that the request body is available after validation
1749+
bodyBytes, err := io.ReadAll(req.Body)
1750+
require.NoError(t, err)
1751+
assert.Equal(t, body, string(bodyBytes))
17471752
}
17481753

17491754
func TestGetDeployment(t *testing.T) {

webhook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package replicate
22

33
import (
4+
"bytes"
45
"context"
56
"crypto/hmac"
67
"crypto/sha256"
@@ -80,6 +81,9 @@ func ValidateWebhookRequest(req *http.Request, secret WebhookSigningSecret) (boo
8081
if err != nil {
8182
return false, fmt.Errorf("failed to read request body: %w", err)
8283
}
84+
defer req.Body.Close()
85+
86+
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
8387
body := string(bodyBytes)
8488

8589
signedContent := fmt.Sprintf("%s.%s.%s", id, timestamp, body)

0 commit comments

Comments
 (0)