Skip to content

Commit

Permalink
fix: panic on webhook with nil body (#1890)
Browse files Browse the repository at this point in the history
Closes #1885
  • Loading branch information
NickUfer authored Oct 29, 2021
1 parent dfb9007 commit 4bf1825
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion selfservice/hook/web_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ func (e *WebHook) execute(data *templateContext) error {
}
}

if body == nil {
body = bytes.NewReader(make([]byte, 0))
}
if err = doHttpCall(conf.method, conf.url, conf.auth, body); err != nil {
return fmt.Errorf("failed to call web hook %w", err)
}
Expand All @@ -271,7 +274,7 @@ func (e *WebHook) execute(data *templateContext) error {

func createBody(l *logrusx.Logger, templateURI string, data *templateContext) (*bytes.Reader, error) {
if len(templateURI) == 0 {
return nil, nil
return bytes.NewReader(make([]byte, 0)), nil
}

f := fetcher.NewFetcher()
Expand Down
6 changes: 6 additions & 0 deletions selfservice/hook/web_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ func TestJsonNetSupport(t *testing.T) {
require.Len(t, hook.Entries, 1)
assert.Contains(t, hook.LastEntry().Message, "support for filepaths without a 'file://' scheme will be dropped")
})

t.Run("case=return non nil body reader on empty templateURI", func(t *testing.T) {
body, err := createBody(l, "", nil)
assert.NotNil(t, body)
assert.Nil(t, err)
})
}

func TestWebHookConfig(t *testing.T) {
Expand Down

0 comments on commit 4bf1825

Please sign in to comment.