Skip to content

chore: disallow inbox as default method #17093

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

Merged
merged 4 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
chore: disallow inbox as default method
  • Loading branch information
DanielleMaywood committed Mar 25, 2025
commit 0cd3be31b047f62c181e57c491d09418e18607c0
14 changes: 12 additions & 2 deletions coderd/notifications/enqueuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package notifications
import (
"context"
"encoding/json"
"fmt"
"slices"
"strings"
"text/template"
Expand All @@ -25,6 +26,14 @@ var (
ErrDuplicate = xerrors.New("duplicate notification")
)

type InvalidNotificationMethodError struct {
Method string
}

func (e InvalidNotificationMethodError) Error() string {
return fmt.Sprintf("given notification method %q is invalid", e.Method)
}

type StoreEnqueuer struct {
store Store
log slog.Logger
Expand All @@ -43,8 +52,9 @@ type StoreEnqueuer struct {
// NewStoreEnqueuer creates an Enqueuer implementation which can persist notification messages in the store.
func NewStoreEnqueuer(cfg codersdk.NotificationsConfig, store Store, helpers template.FuncMap, log slog.Logger, clock quartz.Clock) (*StoreEnqueuer, error) {
var method database.NotificationMethod
if err := method.Scan(cfg.Method.String()); err != nil {
return nil, xerrors.Errorf("given notification method %q is invalid", cfg.Method)
// We do not allow setting Coder Inbox as the default method.
if err := method.Scan(cfg.Method.String()); err != nil || method == database.NotificationMethodInbox {
return nil, InvalidNotificationMethodError{Method: cfg.Method.String()}
}

return &StoreEnqueuer{
Expand Down
16 changes: 16 additions & 0 deletions coderd/notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,22 @@ func TestNotificationDuplicates(t *testing.T) {
require.NoError(t, err)
}

func TestNotificationMethodCannotDefaultToInbox(t *testing.T) {
t.Parallel()

store, _ := dbtestutil.NewDB(t)
logger := testutil.Logger(t)

cfg := defaultNotificationsConfig(database.NotificationMethodInbox)

// Set the time to a known value.
mClock := quartz.NewMock(t)
mClock.Set(time.Date(2024, 1, 15, 9, 0, 0, 0, time.UTC))

_, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), mClock)
require.ErrorIs(t, err, notifications.InvalidNotificationMethodError{Method: string(database.NotificationMethodInbox)})
}

func TestNotificationTargetMatrix(t *testing.T) {
t.Parallel()

Expand Down
Loading