Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: panic on webhook with nil body #1890

Merged
merged 3 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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