Skip to content

Commit c8f3b35

Browse files
fix: prevent password reset notifications ending up in coder inbox (coder#17109)
We do not want password reset notifications to end up in Coder Inbox as this doesn't make much sense. This implements the logic to ensure they are not delivered if the method is Coder Inbox. In the future we might want to investigate a better solution but for now this works.
1 parent 1bbbae8 commit c8f3b35

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

coderd/notifications/enqueuer.go

+7
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ func (s *StoreEnqueuer) EnqueueWithData(ctx context.Context, userID, templateID
116116

117117
uuids := make([]uuid.UUID, 0, 2)
118118
for _, method := range methods {
119+
// TODO(DanielleMaywood):
120+
// We should have a more permanent solution in the future, but for now this will work.
121+
// We do not want password reset notifications to end up in Coder Inbox.
122+
if method == database.NotificationMethodInbox && templateID == TemplateUserRequestedOneTimePasscode {
123+
continue
124+
}
125+
119126
id := uuid.New()
120127
err = s.store.EnqueueNotificationMessage(ctx, database.EnqueueNotificationMessageParams{
121128
ID: id,

coderd/notifications/notifications_test.go

+77
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,83 @@ func TestNotificationTargetMatrix(t *testing.T) {
19521952
}
19531953
}
19541954

1955+
func TestNotificationOneTimePasswordDeliveryTargets(t *testing.T) {
1956+
t.Parallel()
1957+
1958+
t.Run("Inbox", func(t *testing.T) {
1959+
t.Parallel()
1960+
1961+
// nolint:gocritic // Unit test.
1962+
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
1963+
store, _ := dbtestutil.NewDB(t)
1964+
logger := testutil.Logger(t)
1965+
1966+
// Given: Coder Inbox is enabled and SMTP/Webhook are disabled.
1967+
cfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
1968+
cfg.Inbox.Enabled = true
1969+
cfg.SMTP = codersdk.NotificationsEmailConfig{}
1970+
cfg.Webhook = codersdk.NotificationsWebhookConfig{}
1971+
1972+
enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewMock(t))
1973+
require.NoError(t, err)
1974+
user := createSampleUser(t, store)
1975+
1976+
// When: A one-time-passcode notification is sent, it does not enqueue a notification.
1977+
enqueued, err := enq.Enqueue(ctx, user.ID, notifications.TemplateUserRequestedOneTimePasscode,
1978+
map[string]string{"one_time_passcode": "1234"}, "test", user.ID)
1979+
require.NoError(t, err)
1980+
require.Len(t, enqueued, 0)
1981+
})
1982+
1983+
t.Run("SMTP", func(t *testing.T) {
1984+
t.Parallel()
1985+
1986+
// nolint:gocritic // Unit test.
1987+
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
1988+
store, _ := dbtestutil.NewDB(t)
1989+
logger := testutil.Logger(t)
1990+
1991+
// Given: Coder Inbox/Webhook are disabled and SMTP is enabled.
1992+
cfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
1993+
cfg.Inbox.Enabled = false
1994+
cfg.Webhook = codersdk.NotificationsWebhookConfig{}
1995+
1996+
enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewMock(t))
1997+
require.NoError(t, err)
1998+
user := createSampleUser(t, store)
1999+
2000+
// When: A one-time-passcode notification is sent, it does enqueue a notification.
2001+
enqueued, err := enq.Enqueue(ctx, user.ID, notifications.TemplateUserRequestedOneTimePasscode,
2002+
map[string]string{"one_time_passcode": "1234"}, "test", user.ID)
2003+
require.NoError(t, err)
2004+
require.Len(t, enqueued, 1)
2005+
})
2006+
2007+
t.Run("Webhook", func(t *testing.T) {
2008+
t.Parallel()
2009+
2010+
// nolint:gocritic // Unit test.
2011+
ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
2012+
store, _ := dbtestutil.NewDB(t)
2013+
logger := testutil.Logger(t)
2014+
2015+
// Given: Coder Inbox/SMTP are disabled and Webhook is enabled.
2016+
cfg := defaultNotificationsConfig(database.NotificationMethodWebhook)
2017+
cfg.Inbox.Enabled = false
2018+
cfg.SMTP = codersdk.NotificationsEmailConfig{}
2019+
2020+
enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewMock(t))
2021+
require.NoError(t, err)
2022+
user := createSampleUser(t, store)
2023+
2024+
// When: A one-time-passcode notification is sent, it does enqueue a notification.
2025+
enqueued, err := enq.Enqueue(ctx, user.ID, notifications.TemplateUserRequestedOneTimePasscode,
2026+
map[string]string{"one_time_passcode": "1234"}, "test", user.ID)
2027+
require.NoError(t, err)
2028+
require.Len(t, enqueued, 1)
2029+
})
2030+
}
2031+
19552032
type fakeHandler struct {
19562033
mu sync.RWMutex
19572034
succeeded, failed []string

0 commit comments

Comments
 (0)