Skip to content

Commit 4983e52

Browse files
committed
fix: do not double-commit webhooks on registration
Closes ory-corp/cloud#3360
1 parent 5bce0b9 commit 4983e52

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

selfservice/hook/web_hook.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ import (
3232
"github.com/ory/x/otelx"
3333
)
3434

35-
var _ registration.PostHookPostPersistExecutor = new(WebHook)
36-
var _ registration.PostHookPrePersistExecutor = new(WebHook)
37-
var _ verification.PostHookExecutor = new(WebHook)
38-
var _ recovery.PostHookExecutor = new(WebHook)
39-
var _ settings.PostHookPostPersistExecutor = new(WebHook)
35+
var (
36+
_ registration.PostHookPostPersistExecutor = new(WebHook)
37+
_ registration.PostHookPrePersistExecutor = new(WebHook)
38+
39+
_ verification.PostHookExecutor = new(WebHook)
40+
41+
_ recovery.PostHookExecutor = new(WebHook)
42+
43+
_ settings.PostHookPostPersistExecutor = new(WebHook)
44+
)
4045

4146
type (
4247
webHookDependencies interface {
@@ -155,6 +160,10 @@ func (e *WebHook) ExecuteRegistrationPreHook(_ http.ResponseWriter, req *http.Re
155160

156161
func (e *WebHook) ExecutePostRegistrationPrePersistHook(_ http.ResponseWriter, req *http.Request, flow *registration.Flow, id *identity.Identity) error {
157162
ctx, _ := e.deps.Tracer(req.Context()).Tracer().Start(req.Context(), "selfservice.hook.ExecutePostRegistrationPrePersistHook")
163+
if !gjson.GetBytes(e.conf, "can_interrupt").Bool() {
164+
return nil
165+
}
166+
158167
return e.execute(ctx, &templateContext{
159168
Flow: flow,
160169
RequestHeaders: req.Header,
@@ -166,6 +175,10 @@ func (e *WebHook) ExecutePostRegistrationPrePersistHook(_ http.ResponseWriter, r
166175

167176
func (e *WebHook) ExecutePostRegistrationPostPersistHook(_ http.ResponseWriter, req *http.Request, flow *registration.Flow, session *session.Session) error {
168177
ctx, _ := e.deps.Tracer(req.Context()).Tracer().Start(req.Context(), "selfservice.hook.ExecutePostRegistrationPostPersistHook")
178+
if gjson.GetBytes(e.conf, "can_interrupt").Bool() {
179+
return nil
180+
}
181+
169182
return e.execute(ctx, &templateContext{
170183
Flow: flow,
171184
RequestHeaders: req.Header,
@@ -187,6 +200,10 @@ func (e *WebHook) ExecuteSettingsPreHook(_ http.ResponseWriter, req *http.Reques
187200

188201
func (e *WebHook) ExecuteSettingsPostPersistHook(_ http.ResponseWriter, req *http.Request, flow *settings.Flow, id *identity.Identity) error {
189202
ctx, _ := e.deps.Tracer(req.Context()).Tracer().Start(req.Context(), "selfservice.hook.ExecuteSettingsPostPersistHook")
203+
if gjson.GetBytes(e.conf, "can_interrupt").Bool() {
204+
return nil
205+
}
206+
190207
return e.execute(ctx, &templateContext{
191208
Flow: flow,
192209
RequestHeaders: req.Header,

selfservice/hook/web_hook_integration_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,18 @@ func TestWebHooks(t *testing.T) {
457457
},
458458
expectedError: webhookError,
459459
},
460+
{
461+
uc: "Post Registration Post Persist Hook - ignored because block",
462+
createFlow: func() flow.Flow { return &registration.Flow{ID: x.NewUUID()} },
463+
callWebHook: func(wh *hook.WebHook, req *http.Request, f flow.Flow, s *session.Session) error {
464+
return wh.ExecutePostRegistrationPostPersistHook(nil, req, f.(*registration.Flow), s)
465+
},
466+
// This would usually error, but post persist does not execute blocking web hooks, so we expect no error.
467+
webHookResponse: func() (int, []byte) {
468+
return http.StatusBadRequest, webHookResponse
469+
},
470+
expectedError: nil,
471+
},
460472
{
461473
uc: "Post Recovery Hook - no block",
462474
createFlow: func() flow.Flow { return &recovery.Flow{ID: x.NewUUID()} },

0 commit comments

Comments
 (0)