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

feat: disable mail dispatch for invalid recovery email #2585

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions driver/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (
ViperKeyCourierSMTPClientKeyPath = "courier.smtp.client_key_path"
ViperKeyCourierTemplatesPath = "courier.template_override_path"
ViperKeyCourierTemplatesRecoveryInvalidEmail = "courier.templates.recovery.invalid.email"
ViperKeyCourierTemplatesRecoveryInvalidSend = "courier.templates.recovery.invalid.send"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this key should go in another object because here we are configuring the templates. Given that this affects recovery and the link and code strategy, it should probably be configured there?

For completeness it would also be good to be able to disable this for both recovery and verification.

ViperKeyCourierTemplatesRecoveryValidEmail = "courier.templates.recovery.valid.email"
ViperKeyCourierTemplatesRecoveryCodeInvalidEmail = "courier.templates.recovery_code.invalid.email"
ViperKeyCourierTemplatesRecoveryCodeValidEmail = "courier.templates.recovery_code.valid.email"
Expand Down Expand Up @@ -995,6 +996,10 @@ func (p *Config) CourierTemplatesRecoveryValid(ctx context.Context) *CourierEmai
return p.CourierTemplatesHelper(ctx, ViperKeyCourierTemplatesRecoveryValidEmail)
}

func (p *Config) CourierTemplatesRecoveryInvalidSend(ctx context.Context) bool {
return p.p.BoolF(ViperKeyCourierTemplatesRecoveryInvalidSend, true)
}

func (p *Config) CourierTemplatesRecoveryCodeInvalid(ctx context.Context) *CourierEmailTemplate {
return p.CourierTemplatesHelper(ctx, ViperKeyCourierTemplatesRecoveryCodeInvalidEmail)
}
Expand Down
36 changes: 32 additions & 4 deletions embedx/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@
}
}
},
"database": {
"database": {
"type": "object",
"title": "Database related configuration",
"description": "Miscellaneous settings used in database related tasks (cleanup, etc.)",
Expand All @@ -1555,7 +1555,7 @@
"title": "Database cleanup settings",
"description": "Settings that controls how the database cleanup process is configured (delays, batch size, etc.)",
"properties": {
"batch_size" : {
"batch_size": {
"type": "integer",
"title": "Number of records to clean in one iteration",
"description": "Controls how many records should be purged from one table during database cleanup task",
Expand Down Expand Up @@ -1609,7 +1609,35 @@
"type": "object",
"properties": {
"recovery": {
"$ref": "#/definitions/courierTemplates"
"invalid": {
"additionalProperties": false,
"type": "object",
"properties": {
"send": {
"type": "boolean",
"description": "Enable sending invalid recovery email",
"default": true
},
"email": {
"$ref": "#/definitions/emailCourierTemplate"
}
},
"required": [
"email"
]
},
"valid": {
"additionalProperties": false,
"type": "object",
"properties": {
"email": {
"$ref": "#/definitions/emailCourierTemplate"
}
},
"required": [
"email"
]
}
},
"recovery_code": {
"$ref": "#/definitions/courierTemplates"
Expand Down Expand Up @@ -2533,4 +2561,4 @@
"selfservice"
],
"additionalProperties": false
}
}
6 changes: 6 additions & 0 deletions selfservice/strategy/link/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func (s *Sender) SendRecoveryLink(ctx context.Context, r *http.Request, f *recov

address, err := s.r.IdentityPool().FindRecoveryAddressByValue(ctx, identity.RecoveryAddressTypeEmail, to)
if err != nil {
if !s.r.Config().CourierTemplatesRecoveryInvalidSend(ctx) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this PR was opened, we also added a code strategy which is now the default and also needs this feature.

s.r.Logger().Info("Suppressing invalid recovery email.")

return nil
}

if err := s.send(ctx, string(via), email.NewRecoveryInvalid(s.r, &email.RecoveryInvalidModel{To: to})); err != nil {
return err
}
Expand Down
25 changes: 25 additions & 0 deletions selfservice/strategy/link/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"testing"
"time"

"github.com/pkg/errors"

"github.com/ory/kratos/internal/testhelpers"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ory/kratos/courier"
"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/internal"
Expand Down Expand Up @@ -92,4 +95,26 @@ func TestManager(t *testing.T) {
require.NoError(t, err)
assert.EqualValues(t, identity.VerifiableAddressStatusSent, address.Status)
})

t.Run("case=should not send recovery link", func(t *testing.T) {
conf.Set(ctx, "courier.templates.recovery.invalid.send", false)

t.Cleanup(func() {
conf.Set(ctx, "courier.templates.recovery.invalid.send", true)
})

s, err := reg.RecoveryStrategies(ctx).Strategy("link")
require.NoError(t, err)
f, err := recovery.NewFlow(conf, time.Hour, "", u, s, flow.TypeBrowser)
require.NoError(t, err)

require.NoError(t, reg.RecoveryFlowPersister().CreateRecoveryFlow(context.Background(), f))

require.Equal(t, reg.LinkSender().SendRecoveryLink(context.Background(), hr, f, "email", "[email protected]"), nil)

messages, err := reg.CourierPersister().NextMessages(context.Background(), 0)

require.True(t, errors.Is(err, courier.ErrQueueEmpty))
require.Len(t, messages, 0)
})
}