Skip to content

Commit

Permalink
Reset HTTP request body after reading the stream during webhook valid…
Browse files Browse the repository at this point in the history
…ation (#64)
  • Loading branch information
shonun1 committed May 24, 2024
1 parent 1dd11e2 commit 836ab00
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,11 @@ func TestValidateWebhook(t *testing.T) {
isValid, err := replicate.ValidateWebhookRequest(req, testSecret)
require.NoError(t, err)
assert.True(t, isValid)

// Ensure that the request body is available after validation
bodyBytes, err := io.ReadAll(req.Body)
require.NoError(t, err)
assert.Equal(t, body, string(bodyBytes))
}

func TestGetDeployment(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions webhook.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package replicate

import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
Expand Down Expand Up @@ -80,6 +81,9 @@ func ValidateWebhookRequest(req *http.Request, secret WebhookSigningSecret) (boo
if err != nil {
return false, fmt.Errorf("failed to read request body: %w", err)
}
defer req.Body.Close()

req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
body := string(bodyBytes)

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

0 comments on commit 836ab00

Please sign in to comment.