Skip to content

Commit

Permalink
Fix reading webhook response when content length is unknown (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
mraerino authored Apr 28, 2023
1 parent fef344e commit 381cbd3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
9 changes: 5 additions & 4 deletions api/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,15 @@ func (w *Webhook) trigger() (io.ReadCloser, error) {
}
dur := time.Since(start)
rspLog := hooklog.WithFields(logrus.Fields{
"status_code": rsp.StatusCode,
"dur": dur.Nanoseconds(),
"status_code": rsp.StatusCode,
"dur": dur.Nanoseconds(),
"content_length": rsp.ContentLength,
})
switch rsp.StatusCode {
case http.StatusOK, http.StatusNoContent, http.StatusAccepted:
rspLog.Infof("Finished processing webhook in %s", dur)
rspLog.Info("Finished processing webhook")
var body io.ReadCloser
if rsp.ContentLength > 0 {
if rsp.Body != http.NoBody && rsp.ContentLength != 0 {
body = rsp.Body
}
return body, nil
Expand Down
17 changes: 17 additions & 0 deletions api/signup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -92,6 +93,7 @@ func (ts *SignupTestSuite) TestWebhookTriggered() {
token, err := p.ParseWithClaims(signature, claims, func(token *jwt.Token) (interface{}, error) {
return []byte(ts.Config.Webhook.Secret), nil
})
assert.NoError(err)
assert.True(token.Valid)
assert.Equal(ts.instanceID.String(), claims.Subject) // not configured for multitenancy
assert.Equal("gotrue", claims.Issuer)
Expand Down Expand Up @@ -126,6 +128,16 @@ func (ts *SignupTestSuite) TestWebhookTriggered() {
require.True(ok)
assert.Len(usermeta, 1)
assert.EqualValues(1, usermeta["a"])

w.WriteHeader(http.StatusOK)
w.(http.Flusher).Flush() // needed so we don't set a content-length

pl := `{
"app_metadata": {
"roles": ["dev"]
}
}`
fmt.Fprint(w, pl)
}))
defer svr.Close()

Expand Down Expand Up @@ -155,6 +167,11 @@ func (ts *SignupTestSuite) TestWebhookTriggered() {
ts.API.handler.ServeHTTP(w, req)
assert.Equal(http.StatusOK, w.Code)
assert.Equal(1, callCount)

var user models.User
require.NoError(json.NewDecoder(w.Body).Decode(&user))

assert.EqualValues(models.JSONMap{"roles": []interface{}{"dev"}}, user.AppMetaData)
}

func (ts *SignupTestSuite) TestFailingWebhook() {
Expand Down

0 comments on commit 381cbd3

Please sign in to comment.