diff --git a/cmd/memos/main.go b/cmd/memos/main.go index 2b928fa66..40035c294 100644 --- a/cmd/memos/main.go +++ b/cmd/memos/main.go @@ -61,7 +61,7 @@ func init() { rootCmd.Flags().Int("port", 8081, "port of server") rootCmd.Flags().String("unix-sock", "", "path to the unix socket, overrides --addr and --port") rootCmd.Flags().String("data", "", "data directory") - rootCmd.Flags().String("driver", "sqlite", "database driver (sqlite, mysql, postgres, d1)") + rootCmd.Flags().String("driver", "sqlite", "database driver (sqlite, mysql, postgres)") rootCmd.Flags().String("dsn", "", "database source name (DSN)") rootCmd.Flags().String("instance-url", "", "canonical external URL of the Memos instance") rootCmd.Flags().Bool("allow-private-webhooks", false, "allow webhooks to access any private/reserved IP address") diff --git a/filter/render.go b/filter/render.go index 4aeddb1f9..ba4422c92 100644 --- a/filter/render.go +++ b/filter/render.go @@ -185,7 +185,7 @@ func (r *renderer) renderComparison(cond *ComparisonCondition) (renderResult, er // accessorSpec maps a CEL timestamp accessor to per-dialect SQL date-part tokens // and the offset to subtract so the result matches CEL's base (e.g. CEL months // are 0-based but every dialect reports 1-based, so off=1). off is indexed -// [sqlite, postgres, mysql]; D1 shares the SQLite entry. +// [sqlite, postgres, mysql]. type accessorSpec struct { sqlite string // strftime format specifier pg string // EXTRACT field diff --git a/internal/email/message.go b/internal/email/message.go index 16d9faffa..b06780885 100644 --- a/internal/email/message.go +++ b/internal/email/message.go @@ -3,6 +3,8 @@ package email import ( "errors" "fmt" + "mime" + "net/mail" "strings" "time" ) @@ -40,30 +42,30 @@ func (m *Message) Format(fromEmail, fromName string) string { to := sanitizeEmailHeaderValues(m.To) cc := sanitizeEmailHeaderValues(m.Cc) replyTo := sanitizeEmailHeaderValue(m.ReplyTo) - subject := sanitizeEmailHeaderValue(m.Subject) + // Header values must be ASCII; RFC 2047 encodes UTF-8 text such as + // nicknames and Space titles and leaves ASCII text unchanged. + subject := mime.QEncoding.Encode("utf-8", sanitizeEmailHeaderValue(m.Subject)) - // From header - if fromName != "" { - fmt.Fprintf(&sb, "From: %s <%s>\r\n", fromName, fromEmail) - } else { - fmt.Fprintf(&sb, "From: %s\r\n", fromEmail) - } + // From header. net/mail quotes an ASCII display name that contains RFC + // 5322 specials and RFC 2047 encodes a non-ASCII one, choosing an + // encoding whose words stay valid inside a phrase. + writeHeader(&sb, "From", (&mail.Address{Name: fromName, Address: fromEmail}).String()) // To header - fmt.Fprintf(&sb, "To: %s\r\n", strings.Join(to, ", ")) + writeHeader(&sb, "To", strings.Join(to, ", ")) // Cc header (optional) if len(cc) > 0 { - fmt.Fprintf(&sb, "Cc: %s\r\n", strings.Join(cc, ", ")) + writeHeader(&sb, "Cc", strings.Join(cc, ", ")) } // Reply-To header (optional) if replyTo != "" { - fmt.Fprintf(&sb, "Reply-To: %s\r\n", replyTo) + writeHeader(&sb, "Reply-To", replyTo) } // Subject header - fmt.Fprintf(&sb, "Subject: %s\r\n", subject) + writeHeader(&sb, "Subject", subject) // Date header (RFC 5322 format) fmt.Fprintf(&sb, "Date: %s\r\n", time.Now().Format(time.RFC1123Z)) @@ -87,6 +89,35 @@ func (m *Message) Format(fromEmail, fromName string) string { return sb.String() } +// maxHeaderLineLength is the line length RFC 5322 recommends; the hard limit +// is 998 characters, which a long RFC 2047 encoded subject would otherwise +// exceed. +const maxHeaderLineLength = 78 + +// writeHeader writes one header, folding the value at spaces so no line is +// longer than maxHeaderLineLength unless a single word already is. Receivers +// unfold CRLF followed by white space back to the single space, so the value +// is unchanged. +func writeHeader(sb *strings.Builder, name, value string) { + sb.WriteString(name) + sb.WriteString(": ") + lineLength := len(name) + 2 + for i, word := range strings.Split(value, " ") { + if i > 0 { + if lineLength+1+len(word) > maxHeaderLineLength { + sb.WriteString("\r\n ") + lineLength = 1 + } else { + sb.WriteByte(' ') + lineLength++ + } + } + sb.WriteString(word) + lineLength += len(word) + } + sb.WriteString("\r\n") +} + func sanitizeEmailHeaderValue(value string) string { value = strings.NewReplacer("\r", " ", "\n", " ").Replace(value) return strings.Join(strings.Fields(value), " ") diff --git a/internal/email/message_test.go b/internal/email/message_test.go index c47f2594a..7f9ff9be4 100644 --- a/internal/email/message_test.go +++ b/internal/email/message_test.go @@ -1,6 +1,8 @@ package email import ( + "mime" + "net/mail" "strings" "testing" ) @@ -80,7 +82,7 @@ func TestMessageFormatPlainText(t *testing.T) { formatted := msg.Format("sender@example.com", "Sender Name") // Check required headers - if !strings.Contains(formatted, "From: Sender Name ") { + if !strings.Contains(formatted, `From: "Sender Name" `) { t.Error("Missing or incorrect From header") } if !strings.Contains(formatted, "To: user@example.com") { @@ -164,11 +166,102 @@ func TestMessageFormatSanitizesHeaderValues(t *testing.T) { if !strings.Contains(headers, "Subject: Test X-Injected-Subject: bad") { t.Error("subject header was not normalized") } - if !strings.Contains(headers, "From: Sender X-Injected-Name: bad ") { + if !strings.Contains(headers, `From: "Sender X-Injected-Name: bad" `) { t.Error("from header was not normalized") } } +func TestMessageFormatEncodesNonASCIIHeaders(t *testing.T) { + msg := Message{ + To: []string{"user@example.com"}, + Subject: "Zoë commented in 李雷的空间", + Body: "Test Body", + } + + formatted := msg.Format("sender@example.com", "Zoë") + headers, _, _ := strings.Cut(formatted, "\r\n\r\n") + for _, r := range headers { + if r > 127 { + t.Fatalf("headers contain raw non-ASCII text:\n%s", headers) + } + } + + parsed, err := mail.ReadMessage(strings.NewReader(formatted)) + if err != nil { + t.Fatalf("formatted message does not parse: %v", err) + } + decoded, err := new(mime.WordDecoder).DecodeHeader(parsed.Header.Get("Subject")) + if err != nil || decoded != msg.Subject { + t.Errorf("subject decodes to %q, err %v", decoded, err) + } + if !strings.Contains(headers, "From: =?utf-8?q?Zo=C3=AB?= ") { + t.Errorf("from name was not encoded:\n%s", headers) + } +} + +func TestMessageFormatFoldsLongHeaders(t *testing.T) { + msg := Message{ + To: []string{"user@example.com"}, + Subject: strings.Repeat("李雷的空间 ", 30) + "has a very long title", + Body: "Test Body", + } + + formatted := msg.Format("sender@example.com", strings.Repeat("Zoë ", 20)+"Memos") + headers, _, _ := strings.Cut(formatted, "\r\n\r\n") + for line := range strings.SplitSeq(headers, "\r\n") { + if len(line) <= maxHeaderLineLength { + continue + } + // A lone encoded word after the header name may exceed the + // recommended length; a line that still has a fold point may not. + _, value, _ := strings.Cut(line, ": ") + if strings.Contains(strings.TrimSpace(value), " ") || len(line) > 998 { + t.Errorf("header line of %d characters was not folded:\n%s", len(line), line) + } + } + + parsed, err := mail.ReadMessage(strings.NewReader(formatted)) + if err != nil { + t.Fatalf("folded message does not parse: %v", err) + } + decoded, err := new(mime.WordDecoder).DecodeHeader(parsed.Header.Get("Subject")) + if err != nil || decoded != msg.Subject { + t.Errorf("folded subject decodes to %q, err %v", decoded, err) + } + from, err := mail.ParseAddress(parsed.Header.Get("From")) + if err != nil || from.Name != strings.Repeat("Zoë ", 20)+"Memos" { + t.Errorf("folded From decodes to %v, err %v", from, err) + } +} + +func TestMessageFormatEncodesNonASCIIFromNameWithSpecials(t *testing.T) { + msg := Message{To: []string{"user@example.com"}, Subject: "Test", Body: "Test"} + for _, name := range []string{"Zoë ", "Zoë, Team", "Zoë (Memos)"} { + formatted := msg.Format("sender@example.com", name) + parsed, err := mail.ReadMessage(strings.NewReader(formatted)) + if err != nil { + t.Fatalf("formatted message for name %q does not parse: %v", name, err) + } + from := parsed.Header.Get("From") + address, err := mail.ParseAddress(from) + if err != nil { + t.Errorf("From header %q for name %q does not parse: %v", from, name, err) + continue + } + if address.Name != name || address.Address != "sender@example.com" { + t.Errorf("From header %q decodes to %q <%s>, want %q", from, address.Name, address.Address, name) + } + } +} + +func TestMessageFormatQuotesSpecialsInFromName(t *testing.T) { + msg := Message{To: []string{"user@example.com"}, Subject: "Test", Body: "Test"} + formatted := msg.Format("sender@example.com", `Memos, "Team"`) + if !strings.Contains(formatted, `From: "Memos, \"Team\"" `) { + t.Errorf("from name with specials was not quoted:\n%s", formatted) + } +} + func TestGetAllRecipients(t *testing.T) { msg := Message{ To: []string{"user1@example.com", "user2@example.com"}, diff --git a/internal/linkmeta/html_meta.go b/internal/linkmeta/html_meta.go index 643e4e19e..d75724853 100644 --- a/internal/linkmeta/html_meta.go +++ b/internal/linkmeta/html_meta.go @@ -118,10 +118,15 @@ func isInternalIP(ip net.IP) bool { if ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsUnspecified() { return true } - // IsPrivate excludes RFC 6598 shared address space (100.64.0.0/10). - // To4 also covers IPv4-mapped IPv6 addresses. + // IsUnspecified covers only 0.0.0.0, but the kernel dials the local host + // for all of "this network" (0.0.0.0/8). IsPrivate excludes RFC 6598 + // shared address space (100.64.0.0/10). To4 also covers IPv4-mapped IPv6 + // addresses. ipv4 := ip.To4() - return ipv4 != nil && ipv4[0] == 100 && ipv4[1] >= 64 && ipv4[1] <= 127 + if ipv4 == nil { + return false + } + return ipv4[0] == 0 || (ipv4[0] == 100 && ipv4[1] >= 64 && ipv4[1] <= 127) } func validateURL(urlStr string) error { diff --git a/internal/linkmeta/html_meta_test.go b/internal/linkmeta/html_meta_test.go index ef8a20528..4aa8b20b8 100644 --- a/internal/linkmeta/html_meta_test.go +++ b/internal/linkmeta/html_meta_test.go @@ -500,11 +500,16 @@ func TestHTMLMetaFetcherRejectsUnsafeURLsAndRedirects(t *testing.T) { require.ErrorIs(t, err, ErrInternalIP) } -func TestValidateURLCGNAT(t *testing.T) { +func TestValidateURLReservedIPv4Ranges(t *testing.T) { tests := []struct { ip string blocked bool }{ + {"0.0.0.0", true}, + {"0.1.2.3", true}, + {"0.255.255.255", true}, + {"::ffff:0.1.2.3", true}, + {"1.0.0.1", false}, {"100.63.255.255", false}, {"100.64.0.0", true}, {"100.100.100.200", true}, diff --git a/internal/profile/profile.go b/internal/profile/profile.go index 3ff0d9e35..b0f5ffcc6 100644 --- a/internal/profile/profile.go +++ b/internal/profile/profile.go @@ -26,7 +26,7 @@ type Profile struct { Data string // DSN points to where memos stores its own data DSN string - // Driver is the database driver: sqlite, mysql, postgres, or d1. + // Driver is the database driver: sqlite, mysql, or postgres. Driver string // Version is the current version of server Version string @@ -106,7 +106,7 @@ func (p *Profile) Validate() error { dataDir, err := checkDataDir(p.Data) if err != nil { - slog.Error("failed to check dsn", slog.String("data", dataDir), slog.String("error", err.Error())) + slog.Error("failed to check data directory", slog.String("data", p.Data), slog.String("error", err.Error())) return err } diff --git a/server/api/acl_config.go b/server/api/acl_config.go index d9f1ba92d..d113fb4c9 100644 --- a/server/api/acl_config.go +++ b/server/api/acl_config.go @@ -22,7 +22,6 @@ var PublicMethods = map[string]struct{}{ "/memos.api.UserService/CreateUser": {}, // Registration policy is enforced in UserService "/memos.api.UserService/GetUser": {}, "/memos.api.UserService/BatchGetUsers": {}, - "/memos.api.UserService/GetUserAvatar": {}, "/memos.api.UserService/GetUserStats": {}, "/memos.api.UserService/ListUserStats": {}, diff --git a/server/api/acl_config_test.go b/server/api/acl_config_test.go index 801709e13..10a2ee6de 100644 --- a/server/api/acl_config_test.go +++ b/server/api/acl_config_test.go @@ -1,9 +1,15 @@ package api import ( + "strings" "testing" "github.com/stretchr/testify/assert" + "google.golang.org/protobuf/reflect/protoreflect" + "google.golang.org/protobuf/reflect/protoregistry" + + // Registers the API service descriptors looked up below. + _ "github.com/usememos/memos/proto/gen/api" ) // TestPublicMethodsArePublic verifies that methods in PublicMethods are recognized as public. @@ -20,7 +26,6 @@ func TestPublicMethodsArePublic(t *testing.T) { "/memos.api.UserService/CreateUser", "/memos.api.UserService/GetUser", "/memos.api.UserService/BatchGetUsers", - "/memos.api.UserService/GetUserAvatar", "/memos.api.UserService/GetUserStats", "/memos.api.UserService/ListUserStats", // Identity Provider Service @@ -163,3 +168,20 @@ func TestAuthBootstrapClassification(t *testing.T) { }) } } + +// TestACLMethodsExist verifies every ACL entry names a real RPC, so a renamed +// or removed method cannot leave a stale public entry behind. +func TestACLMethodsExist(t *testing.T) { + for _, methods := range []map[string]struct{}{PublicMethods, AuthBootstrapMethods} { + for method := range methods { + t.Run(method, func(t *testing.T) { + name := protoreflect.FullName(strings.ReplaceAll(strings.TrimPrefix(method, "/"), "/", ".")) + descriptor, err := protoregistry.GlobalFiles.FindDescriptorByName(name) + if assert.NoError(t, err, "%s does not name a registered RPC", method) { + _, ok := descriptor.(protoreflect.MethodDescriptor) + assert.True(t, ok, "%s is not an RPC method", method) + } + }) + } + } +} diff --git a/server/api/health_service.go b/server/api/health_service.go deleted file mode 100644 index 66802b3fd..000000000 --- a/server/api/health_service.go +++ /dev/null @@ -1,25 +0,0 @@ -package api - -import ( - "context" - - "google.golang.org/grpc/codes" - "google.golang.org/grpc/health/grpc_health_v1" - "google.golang.org/grpc/status" -) - -func (s *APIService) Check(ctx context.Context, - _ *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) { - // Check if database is initialized by verifying instance basic setting exists - instanceBasicSetting, err := s.Store.GetInstanceBasicSetting(ctx) - if err != nil { - return nil, status.Errorf(codes.Unavailable, "database not initialized: %v", err) - } - - // Verify schema version is set (empty means database not properly initialized) - if instanceBasicSetting.SchemaVersion == "" { - return nil, status.Errorf(codes.Unavailable, "schema version not set") - } - - return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_SERVING}, nil -} diff --git a/server/api/instance_service.go b/server/api/instance_service.go index 3a2ce5484..21e948db5 100644 --- a/server/api/instance_service.go +++ b/server/api/instance_service.go @@ -225,6 +225,9 @@ func (s *APIService) UpdateInstanceSetting(ctx context.Context, request *apipb.U // An empty string means "no change", not "clear the credential". switch updateSetting.Key { case storepb.InstanceSettingKey_NOTIFICATION: + if err := store.ValidateInstanceNotificationSetting(updateSetting.GetNotificationSetting()); err != nil { + return nil, status.Errorf(codes.InvalidArgument, "invalid instance setting: %v", err) + } if notif := updateSetting.GetNotificationSetting(); notif != nil && notif.Email != nil && notif.Email.SmtpPassword == "" { existing, err := s.Store.GetInstanceNotificationSetting(ctx) if err == nil && existing != nil && existing.Email != nil { diff --git a/server/api/memo_service_comments.go b/server/api/memo_service_comments.go index ba1381c95..a5025832b 100644 --- a/server/api/memo_service_comments.go +++ b/server/api/memo_service_comments.go @@ -78,30 +78,7 @@ func (s *APIService) CreateMemoComment(ctx context.Context, request *apipb.Creat return nil, status.Errorf(codes.Internal, "failed to convert memo comment") } - creatorID := user.ID - relatedCreator, err := s.Store.GetUser(ctx, &store.FindUser{ID: &relatedMemo.CreatorID}) - if err != nil { - return nil, status.Errorf(codes.Internal, "failed to get related memo creator") - } - if creatorID != relatedMemo.CreatorID && - s.canUserAccessMentionMemos(ctx, relatedCreator, memo, relatedMemo) { - if _, err := s.createInboxWithEmailNotification(ctx, &store.Inbox{ - SenderID: creatorID, - ReceiverID: relatedMemo.CreatorID, - Status: store.UNREAD, - Message: &storepb.InboxMessage{ - Type: storepb.InboxMessage_MEMO_COMMENT, - Payload: &storepb.InboxMessage_MemoComment{ - MemoComment: &storepb.InboxMessage_MemoCommentPayload{ - MemoId: memo.ID, - RelatedMemoId: relatedMemo.ID, - }, - }, - }, - }); err != nil { - return nil, status.Errorf(codes.Internal, "failed to create inbox") - } - } + s.createMemoCommentNotification(ctx, user.ID, memo, relatedMemo) if err := s.DispatchMemoCommentCreatedWebhook(ctx, memo, relatedMemo, relatedMemo.CreatorID); err != nil { slog.Warn("Failed to dispatch memo comment created webhook", slog.Any("err", err)) @@ -114,6 +91,45 @@ func (s *APIService) CreateMemoComment(ctx context.Context, request *apipb.Creat return memoComment, nil } +// createMemoCommentNotification records the comment in the related memo +// creator's inbox. The comment itself is already persisted, so a failure here +// only loses the notification and is logged instead of failing the request; +// failing it would make clients retry and create a duplicate comment. +func (s *APIService) createMemoCommentNotification(ctx context.Context, creatorID int32, memo *store.Memo, relatedMemo *store.Memo) { + if creatorID == relatedMemo.CreatorID { + return + } + relatedCreator, err := s.Store.GetUser(ctx, &store.FindUser{ID: &relatedMemo.CreatorID}) + if err != nil { + slog.Warn("Failed to get related memo creator for comment notification", + slog.Any("err", err), + slog.Int64("memo_id", int64(memo.ID))) + return + } + if !s.canUserAccessMentionMemos(ctx, relatedCreator, memo, relatedMemo) { + return + } + if _, err := s.createInboxWithEmailNotification(ctx, &store.Inbox{ + SenderID: creatorID, + ReceiverID: relatedMemo.CreatorID, + Status: store.UNREAD, + Message: &storepb.InboxMessage{ + Type: storepb.InboxMessage_MEMO_COMMENT, + Payload: &storepb.InboxMessage_MemoComment{ + MemoComment: &storepb.InboxMessage_MemoCommentPayload{ + MemoId: memo.ID, + RelatedMemoId: relatedMemo.ID, + }, + }, + }, + }); err != nil { + slog.Warn("Failed to create memo comment notification", + slog.Any("err", err), + slog.Int64("memo_id", int64(memo.ID)), + slog.Int64("receiver_id", int64(relatedMemo.CreatorID))) + } +} + func (s *APIService) ListMemoComments(ctx context.Context, request *apipb.ListMemoCommentsRequest) (*apipb.ListMemoCommentsResponse, error) { memoUID, err := ExtractMemoUIDFromName(request.Parent) if err != nil { diff --git a/server/api/ratelimit.go b/server/api/ratelimit.go index b27ee20cf..bc8c732e5 100644 --- a/server/api/ratelimit.go +++ b/server/api/ratelimit.go @@ -216,7 +216,7 @@ func (s *APIService) reserveSignIn(clientIP, username string) (*signInAttempt, e } attempt.reserved = append(attempt.reserved, ratelimit.ScopeSignInIP) if username != "" { - if err := s.throttleAndCharge(ratelimit.ScopeSignInAccount, username, 1); err != nil { + if err := s.throttleAndCharge(ratelimit.ScopeSignInAccount, attempt.username, 1); err != nil { attempt.succeeded() return nil, err } diff --git a/server/api/ratelimit_test.go b/server/api/ratelimit_test.go index a3ee94e00..2a9388a0b 100644 --- a/server/api/ratelimit_test.go +++ b/server/api/ratelimit_test.go @@ -98,3 +98,38 @@ func TestAccountKeyBoundsLength(t *testing.T) { require.NotEqual(t, key, accountKey(long+"b")) require.Equal(t, key, accountKey(long)) } + +// recordingLimiter allows every request and records the keys it is given. +type recordingLimiter struct { + consumed map[ratelimit.Scope][]string + refunded map[ratelimit.Scope][]string +} + +func (l *recordingLimiter) Consume(scope ratelimit.Scope, key string, _ int) ratelimit.Decision { + l.consumed[scope] = append(l.consumed[scope], key) + return ratelimit.Decision{Allowed: true} +} + +func (l *recordingLimiter) Refund(scope ratelimit.Scope, key string, _ int) { + l.refunded[scope] = append(l.refunded[scope], key) +} + +func (*recordingLimiter) Allowed(ratelimit.Scope, string, int) ratelimit.Decision { + return ratelimit.Decision{Allowed: true} +} + +func (*recordingLimiter) Hit(ratelimit.Scope, string, int) {} + +func TestReserveSignInChargesAndRefundsBoundedAccountKey(t *testing.T) { + limiter := &recordingLimiter{consumed: map[ratelimit.Scope][]string{}, refunded: map[ratelimit.Scope][]string{}} + s := &APIService{RateLimiter: limiter} + long := strings.Repeat("a", identifier.MaxUsernameLength+1) + + attempt, err := s.reserveSignIn("192.0.2.1", long) + require.NoError(t, err) + attempt.succeeded() + + want := []string{accountKey(long)} + require.Equal(t, want, limiter.consumed[ratelimit.ScopeSignInAccount], "the raw submission must not become a limiter key") + require.Equal(t, want, limiter.refunded[ratelimit.ScopeSignInAccount], "the refund must return the unit that was charged") +} diff --git a/server/api/test/instance_service_test.go b/server/api/test/instance_service_test.go index c92bdf126..c3294e495 100644 --- a/server/api/test/instance_service_test.go +++ b/server/api/test/instance_service_test.go @@ -507,6 +507,58 @@ func TestUpdateInstanceSetting(t *testing.T) { } }) + t.Run("UpdateInstanceSetting - notification email validation", func(t *testing.T) { + ts := NewTestService(t) + defer ts.Cleanup() + + admin, err := ts.CreateHostUser(ctx, "notification-admin") + require.NoError(t, err) + adminCtx := ts.CreateUserContext(ctx, admin.ID) + update := func(email *apipb.InstanceSetting_NotificationSetting_EmailSetting) error { + _, err := ts.Service.UpdateInstanceSetting(adminCtx, &apipb.UpdateInstanceSettingRequest{ + InstanceSetting: &apipb.InstanceSetting{ + Name: "instance/settings/NOTIFICATION", + Value: &apipb.InstanceSetting_NotificationSetting_{ + NotificationSetting: &apipb.InstanceSetting_NotificationSetting{Email: email}, + }, + }, + }) + return err + } + valid := func() *apipb.InstanceSetting_NotificationSetting_EmailSetting { + return &apipb.InstanceSetting_NotificationSetting_EmailSetting{ + Enabled: true, + SmtpHost: "smtp.example.com", + SmtpPort: 587, + SmtpPassword: "password", + FromEmail: "bot@example.com", + UseTls: true, + } + } + + bothEncryptions := valid() + bothEncryptions.UseSsl = true + err = update(bothEncryptions) + require.Equal(t, codes.InvalidArgument, status.Code(err)) + require.Contains(t, err.Error(), "cannot enable both useTls and useSsl") + + for name, mutate := range map[string]func(*apipb.InstanceSetting_NotificationSetting_EmailSetting){ + "host": func(e *apipb.InstanceSetting_NotificationSetting_EmailSetting) { e.SmtpHost = " " }, + "port": func(e *apipb.InstanceSetting_NotificationSetting_EmailSetting) { e.SmtpPort = 0 }, + "from": func(e *apipb.InstanceSetting_NotificationSetting_EmailSetting) { e.FromEmail = "" }, + } { + email := valid() + mutate(email) + err := update(email) + require.Equal(t, codes.InvalidArgument, status.Code(err), "missing %s", name) + require.Contains(t, err.Error(), "requires smtpHost", "missing %s", name) + } + + require.NoError(t, update(valid())) + // Disabled email may be saved incomplete. + require.NoError(t, update(&apipb.InstanceSetting_NotificationSetting_EmailSetting{Enabled: false, SmtpPassword: "password"})) + }) + t.Run("UpdateInstanceSetting - access setting", func(t *testing.T) { ts := NewTestService(t) defer ts.Cleanup() diff --git a/server/api/test/memo_service_test.go b/server/api/test/memo_service_test.go index 315329628..21bfea682 100644 --- a/server/api/test/memo_service_test.go +++ b/server/api/test/memo_service_test.go @@ -141,7 +141,10 @@ func TestInstanceAdminIsSuperuserForNamedMemoOperations(t *testing.T) { _, err = ts.Service.GetMemo(ownerCtx, &apipb.GetMemoRequest{Name: private.Name}) require.Equal(t, codes.NotFound, status.Code(err)) - // An archived administrator holds no privilege. + // An archived administrator holds no privilege. Another administrator + // must remain for the instance to allow the archive. + _, err = ts.CreateHostUser(ctx, "superuser-admin-successor") + require.NoError(t, err) archived := store.Archived _, err = ts.Store.UpdateUser(ctx, &store.UpdateUser{ID: admin.ID, RowStatus: &archived}) require.NoError(t, err) diff --git a/server/api/test/user_service_last_admin_test.go b/server/api/test/user_service_last_admin_test.go new file mode 100644 index 000000000..844e830ef --- /dev/null +++ b/server/api/test/user_service_last_admin_test.go @@ -0,0 +1,42 @@ +package test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/types/known/fieldmaskpb" + + apipb "github.com/usememos/memos/proto/gen/api" + "github.com/usememos/memos/server/api" +) + +func TestLastInstanceAdminCannotRemoveThemselves(t *testing.T) { + t.Parallel() + + ts := NewTestService(t) + defer ts.Cleanup() + + ctx := context.Background() + admin, err := ts.CreateHostUser(ctx, "last-admin") + require.NoError(t, err) + adminCtx := ts.CreateUserContext(api.WithHeaderCarrier(ctx), admin.ID) + name := api.BuildUserName(admin.Username) + + for field, user := range map[string]*apipb.User{ + "state": {Name: name, State: apipb.State_ARCHIVED}, + "role": {Name: name, Role: apipb.User_USER}, + } { + _, err = ts.Service.UpdateUser(adminCtx, &apipb.UpdateUserRequest{User: user, UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{field}}}) + require.Equal(t, codes.FailedPrecondition, status.Code(err), field) + } + _, err = ts.Service.DeleteUser(adminCtx, &apipb.DeleteUserRequest{Name: name}) + require.Equal(t, codes.FailedPrecondition, status.Code(err)) + + _, err = ts.CreateHostUser(ctx, "next-admin") + require.NoError(t, err) + _, err = ts.Service.DeleteUser(adminCtx, &apipb.DeleteUserRequest{Name: name}) + require.NoError(t, err) +} diff --git a/server/api/user_service.go b/server/api/user_service.go index 30edf1ae9..66c0fb11a 100644 --- a/server/api/user_service.go +++ b/server/api/user_service.go @@ -463,6 +463,9 @@ func (s *APIService) UpdateUser(ctx context.Context, request *apipb.UpdateUserRe if stderrors.Is(err, store.ErrLastSpaceAdmin) { return nil, status.Error(codes.FailedPrecondition, "an active space must retain an active administrator") } + if stderrors.Is(err, store.ErrLastInstanceAdmin) { + return nil, status.Error(codes.FailedPrecondition, lastInstanceAdminMessage) + } return nil, convertUserWriteError(err, "failed to update user") } if passwordChanged { @@ -509,6 +512,9 @@ func (s *APIService) DeleteUser(ctx context.Context, request *apipb.DeleteUserRe if stderrors.Is(err, store.ErrUserHasSpaceMembership) { return nil, status.Error(codes.FailedPrecondition, "leave all spaces before deleting this account") } + if stderrors.Is(err, store.ErrLastInstanceAdmin) { + return nil, status.Error(codes.FailedPrecondition, lastInstanceAdminMessage) + } return nil, status.Errorf(codes.Internal, "failed to delete user: %v", err) } if isSelfDelete { @@ -525,6 +531,8 @@ func (s *APIService) DeleteUser(ctx context.Context, request *apipb.DeleteUserRe const emailTakenMessage = "email is already in use" +const lastInstanceAdminMessage = "the instance must retain an active administrator" + // convertUserWriteError maps the store's uniqueness sentinels onto // AlreadyExists and everything else onto Internal with the given context. func convertUserWriteError(err error, context string) error { diff --git a/server/fileserver/fileserver.go b/server/fileserver/fileserver.go index e7a945bf1..849250a67 100644 --- a/server/fileserver/fileserver.go +++ b/server/fileserver/fileserver.go @@ -657,7 +657,8 @@ func (s *FileServerService) getMotionPath(attachment *store.Attachment) (string, // checkAttachmentPermission verifies the user has permission to access the attachment. func (s *FileServerService) checkAttachmentPermission(ctx context.Context, c *echo.Context, attachment *store.Attachment) (access.MemoReadClass, error) { - // For unlinked attachments, only the creator can access. + // For unlinked attachments, only the creator or an admin can access, as in + // the API's GetAttachment. if attachment.MemoID == nil { user, err := s.getCurrentUser(ctx, c) if err != nil { @@ -666,7 +667,7 @@ func (s *FileServerService) checkAttachmentPermission(ctx context.Context, c *ec if user == nil { return access.MemoReadClassPrivate, echo.NewHTTPError(http.StatusUnauthorized, "unauthorized access") } - if user.ID != attachment.CreatorID { + if !access.CanManageAttachment(user, attachment) { return access.MemoReadClassPrivate, echo.NewHTTPError(http.StatusForbidden, "forbidden access") } return access.MemoReadClassPrivate, nil diff --git a/server/fileserver/fileserver_test.go b/server/fileserver/fileserver_test.go index 50981abc3..03bb06ede 100644 --- a/server/fileserver/fileserver_test.go +++ b/server/fileserver/fileserver_test.go @@ -852,6 +852,61 @@ func TestServeAttachmentFile_RefreshCookieAuthenticatesOwner(t *testing.T) { require.Equal(t, "secret content", rec.Body.String()) } +// TestServeAttachmentFile_UnlinkedAttachmentOwnerOrAdmin verifies an +// attachment not linked to a memo follows the API's GetAttachment rule: its +// creator and an instance admin may read it, other users may not. +func TestServeAttachmentFile_UnlinkedAttachmentOwnerOrAdmin(t *testing.T) { + ctx := context.Background() + svc, fs, stores, cleanup := newShareAttachmentTestServices(ctx, t) + defer cleanup() + + newUser := func(username string, role store.Role) *store.User { + user, err := svc.Store.CreateUser(ctx, &store.User{Username: username, Role: role, Email: username + "@example.com"}) + require.NoError(t, err) + return user + } + owner := newUser("unlinked-owner", store.RoleUser) + admin := newUser("unlinked-admin", store.RoleAdmin) + other := newUser("unlinked-other", store.RoleUser) + + attachment, err := svc.CreateAttachment(context.WithValue(ctx, auth.UserIDContextKey, owner.ID), &apipb.CreateAttachmentRequest{ + Attachment: &apipb.Attachment{ + Filename: "draft.txt", + Type: "text/plain", + Content: []byte("draft content"), + }, + }) + require.NoError(t, err) + + e := echo.New() + fs.RegisterRoutes(e) + url := fmt.Sprintf("/file/%s/%s", attachment.Name, attachment.Filename) + getAs := func(user *store.User) int { + req := httptest.NewRequest(http.MethodGet, url, nil) + req.AddCookie(newRefreshTokenCookie(ctx, t, stores, user.ID, svc.Secret)) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + return rec.Code + } + + require.Equal(t, http.StatusOK, getAs(owner)) + require.Equal(t, http.StatusOK, getAs(admin)) + require.Equal(t, http.StatusForbidden, getAs(other)) +} + +func newRefreshTokenCookie(ctx context.Context, t *testing.T, stores *store.Store, userID int32, secret string) *http.Cookie { + t.Helper() + tokenID := random.UUID() + require.NoError(t, stores.AddUserRefreshToken(ctx, userID, &storepb.RefreshTokensUserSetting_RefreshToken{ + TokenId: tokenID, + ExpiresAt: timestamppb.New(time.Now().Add(auth.RefreshTokenDuration)), + CreatedAt: timestamppb.Now(), + })) + refreshToken, _, err := auth.GenerateRefreshToken(userID, tokenID, []byte(secret)) + require.NoError(t, err) + return &http.Cookie{Name: auth.RefreshTokenCookieName, Value: refreshToken} +} + func newExpiredRefreshTokenCookie(ctx context.Context, t *testing.T, stores *store.Store, userID int32, secret string) *http.Cookie { t.Helper() tokenID := random.UUID() diff --git a/server/server.go b/server/server.go index 1af60709c..de06f9fb8 100644 --- a/server/server.go +++ b/server/server.go @@ -36,6 +36,9 @@ const ( idleTimeout = 2 * time.Minute ) +// healthzDatabaseTimeout bounds the database ping behind /healthz. +const healthzDatabaseTimeout = 2 * time.Second + type Server struct { Secret string Profile *profile.Profile @@ -75,8 +78,15 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store } s.Secret = secret - // Register healthz endpoint. + // Register healthz endpoint. It reports ready only while the database + // answers, so a load balancer stops routing to an instance that lost it. echoServer.GET("/healthz", func(c *echo.Context) error { + pingCtx, cancel := context.WithTimeout(c.Request().Context(), healthzDatabaseTimeout) + defer cancel() + if err := s.Store.GetDriver().GetDB().PingContext(pingCtx); err != nil { + slog.Warn("health check failed to reach the database", slog.String("error", err.Error())) + return c.String(http.StatusServiceUnavailable, "Database unavailable.") + } return c.String(http.StatusOK, "Service ready.") }) diff --git a/server/test/startup_test.go b/server/test/startup_test.go index 18ba4d223..a23fab331 100644 --- a/server/test/startup_test.go +++ b/server/test/startup_test.go @@ -254,6 +254,21 @@ func unusedPort(t *testing.T) int { return listener.Addr().(*net.TCPAddr).Port } +// TestHealthzReportsDatabaseOutage verifies /healthz checks the database +// instead of always answering ready. +func TestHealthzReportsDatabaseOutage(t *testing.T) { + ctx := context.Background() + inst := bootInstance(ctx, t, instanceOptions{instanceURL: "http://localhost"}) + + status, _ := inst.do(t, http.MethodGet, "/healthz", "", nil) + require.Equal(t, http.StatusOK, status) + + require.NoError(t, inst.server.Store.GetDriver().GetDB().Close()) + status, body := inst.do(t, http.MethodGet, "/healthz", "", nil) + require.Equal(t, http.StatusServiceUnavailable, status) + require.Equal(t, "Database unavailable.", string(body)) +} + // TestStartupServesEveryRegisteredRouter boots a fresh instance and checks that // each router mounted by server.NewServer actually answers. A registration // order regression or a gateway conflict shows up here as a 404. diff --git a/store/db/mysql/instance_setting.go b/store/db/mysql/instance_setting.go index 924524d9f..1fd8ab8d5 100644 --- a/store/db/mysql/instance_setting.go +++ b/store/db/mysql/instance_setting.go @@ -2,6 +2,7 @@ package mysql import ( "context" + "database/sql" "strings" "github.com/pkg/errors" @@ -24,8 +25,24 @@ func (d *DB) CreateInstanceSettingIfNotExists(ctx context.Context, create *store } func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSetting) (*store.InstanceSetting, error) { + if err := upsertInstanceSetting(ctx, d.db, upsert); err != nil { + return nil, err + } + + return upsert, nil +} + +func (*DB) UpsertInstanceSettingTx(ctx context.Context, tx *sql.Tx, upsert *store.InstanceSetting) error { + return upsertInstanceSetting(ctx, tx, upsert) +} + +type instanceSettingExecer interface { + ExecContext(context.Context, string, ...any) (sql.Result, error) +} + +func upsertInstanceSetting(ctx context.Context, executor instanceSettingExecer, upsert *store.InstanceSetting) error { stmt := "INSERT INTO `system_setting` (`name`, `value`, `description`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `value` = ?, `description` = ?" - _, err := d.db.ExecContext( + _, err := executor.ExecContext( ctx, stmt, upsert.Name, @@ -34,11 +51,7 @@ func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSe upsert.Value, upsert.Description, ) - if err != nil { - return nil, err - } - - return upsert, nil + return err } func (d *DB) ListInstanceSettings(ctx context.Context, find *store.FindInstanceSetting) ([]*store.InstanceSetting, error) { diff --git a/store/db/mysql/user.go b/store/db/mysql/user.go index 24353be83..a365e5ceb 100644 --- a/store/db/mysql/user.go +++ b/store/db/mysql/user.go @@ -40,6 +40,11 @@ func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.U return nil, err } defer func() { _ = tx.Rollback() }() + if update.RemovesInstanceAdmin() { + if err := ensureInstanceAdminRetained(ctx, tx, update.ID); err != nil { + return nil, err + } + } if update.RowStatus != nil && *update.RowStatus == store.Archived { if err := validateMySQLUserArchive(ctx, tx, update.ID); err != nil { return nil, err @@ -121,6 +126,44 @@ func validateMySQLUserArchive(ctx context.Context, tx *sql.Tx, userID int32) err return nil } +// ensureInstanceAdminRetained refuses a mutation that would remove the last +// active instance administrator. The target's role is read first without a +// lock so mutations of ordinary users skip the admin scan; for an active +// admin every caller then locks the active admin rows in id order before +// touching the target, so concurrent removals serialize instead of each +// seeing the other admin as still active. +func ensureInstanceAdminRetained(ctx context.Context, tx *sql.Tx, userID int32) error { + var role, rowStatus string + if err := tx.QueryRowContext(ctx, `SELECT role, row_status FROM user WHERE id = ?`, userID).Scan(&role, &rowStatus); errors.Is(err, sql.ErrNoRows) { + return nil + } else if err != nil { + return err + } + if role != string(store.RoleAdmin) || rowStatus != string(store.Normal) { + return nil + } + rows, err := tx.QueryContext(ctx, `SELECT id FROM user WHERE role = 'ADMIN' AND row_status = 'NORMAL' ORDER BY id FOR UPDATE`) + if err != nil { + return err + } + defer rows.Close() + adminCount := 0 + for rows.Next() { + var id int32 + if err := rows.Scan(&id); err != nil { + return err + } + adminCount++ + } + if err := rows.Err(); err != nil { + return err + } + if adminCount <= 1 { + return store.ErrLastInstanceAdmin + } + return nil +} + func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User, error) { where, args := []string{"1 = 1"}, []any{} orderBy := []string{"`created_ts` DESC", "`row_status` DESC", "`id` DESC"} diff --git a/store/db/mysql/user_delete.go b/store/db/mysql/user_delete.go index b18009154..4c03fbe5e 100644 --- a/store/db/mysql/user_delete.go +++ b/store/db/mysql/user_delete.go @@ -29,6 +29,9 @@ func (d *DB) DeleteUser(ctx context.Context, delete *store.DeleteUser) (*store.D defer func() { _ = tx.Rollback() }() + if err := ensureInstanceAdminRetained(ctx, tx, delete.ID); err != nil { + return nil, err + } var userID int32 // Relationship creation locks this same parent row before inserting or // activating a membership, preventing either transaction from leaving an orphan. diff --git a/store/db/postgres/instance_setting.go b/store/db/postgres/instance_setting.go index c403f2b57..34d03fc03 100644 --- a/store/db/postgres/instance_setting.go +++ b/store/db/postgres/instance_setting.go @@ -2,6 +2,7 @@ package postgres import ( "context" + "database/sql" "strings" "github.com/pkg/errors" @@ -30,6 +31,21 @@ func (d *DB) CreateInstanceSettingIfNotExists(ctx context.Context, create *store } func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSetting) (*store.InstanceSetting, error) { + if err := upsertInstanceSetting(ctx, d.db, upsert); err != nil { + return nil, err + } + return upsert, nil +} + +func (*DB) UpsertInstanceSettingTx(ctx context.Context, tx *sql.Tx, upsert *store.InstanceSetting) error { + return upsertInstanceSetting(ctx, tx, upsert) +} + +type instanceSettingExecer interface { + ExecContext(context.Context, string, ...any) (sql.Result, error) +} + +func upsertInstanceSetting(ctx context.Context, executor instanceSettingExecer, upsert *store.InstanceSetting) error { stmt := ` INSERT INTO system_setting ( name, value, description @@ -40,11 +56,8 @@ func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSe value = EXCLUDED.value, description = EXCLUDED.description ` - if _, err := d.db.ExecContext(ctx, stmt, upsert.Name, upsert.Value, upsert.Description); err != nil { - return nil, err - } - - return upsert, nil + _, err := executor.ExecContext(ctx, stmt, upsert.Name, upsert.Value, upsert.Description) + return err } func (d *DB) ListInstanceSettings(ctx context.Context, find *store.FindInstanceSetting) ([]*store.InstanceSetting, error) { diff --git a/store/db/postgres/user.go b/store/db/postgres/user.go index f7f80bb13..3cee31754 100644 --- a/store/db/postgres/user.go +++ b/store/db/postgres/user.go @@ -24,6 +24,11 @@ func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.U return nil, err } defer func() { _ = tx.Rollback() }() + if update.RemovesInstanceAdmin() { + if err := ensureInstanceAdminRetained(ctx, tx, update.ID); err != nil { + return nil, err + } + } if update.RowStatus != nil && *update.RowStatus == store.Archived { if err := validatePostgresUserArchive(ctx, tx, update.ID); err != nil { return nil, err @@ -118,6 +123,44 @@ func validatePostgresUserArchive(ctx context.Context, tx *sql.Tx, userID int32) return nil } +// ensureInstanceAdminRetained refuses a mutation that would remove the last +// active instance administrator. The target's role is read first without a +// lock so mutations of ordinary users skip the admin scan; for an active +// admin every caller then locks the active admin rows in id order before +// touching the target, so concurrent removals serialize instead of each +// seeing the other admin as still active. +func ensureInstanceAdminRetained(ctx context.Context, tx *sql.Tx, userID int32) error { + var role, rowStatus string + if err := tx.QueryRowContext(ctx, `SELECT role, row_status FROM "user" WHERE id = $1`, userID).Scan(&role, &rowStatus); errors.Is(err, sql.ErrNoRows) { + return nil + } else if err != nil { + return err + } + if role != string(store.RoleAdmin) || rowStatus != string(store.Normal) { + return nil + } + rows, err := tx.QueryContext(ctx, `SELECT id FROM "user" WHERE role = 'ADMIN' AND row_status = 'NORMAL' ORDER BY id FOR UPDATE`) + if err != nil { + return err + } + defer rows.Close() + adminCount := 0 + for rows.Next() { + var id int32 + if err := rows.Scan(&id); err != nil { + return err + } + adminCount++ + } + if err := rows.Err(); err != nil { + return err + } + if adminCount <= 1 { + return store.ErrLastInstanceAdmin + } + return nil +} + func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User, error) { where, args := []string{"1 = 1"}, []any{} orderBy := []string{"created_ts DESC", "row_status DESC", "id DESC"} diff --git a/store/db/postgres/user_delete.go b/store/db/postgres/user_delete.go index e62a313d4..842db0f91 100644 --- a/store/db/postgres/user_delete.go +++ b/store/db/postgres/user_delete.go @@ -29,6 +29,9 @@ func (d *DB) DeleteUser(ctx context.Context, delete *store.DeleteUser) (*store.D defer func() { _ = tx.Rollback() }() + if err := ensureInstanceAdminRetained(ctx, tx, delete.ID); err != nil { + return nil, err + } var userID int32 // Relationship creation locks this same parent row before inserting or // activating a membership, preventing either transaction from leaving an orphan. diff --git a/store/db/sqlite/instance_setting.go b/store/db/sqlite/instance_setting.go index b4a711842..4dc5c94ca 100644 --- a/store/db/sqlite/instance_setting.go +++ b/store/db/sqlite/instance_setting.go @@ -2,6 +2,7 @@ package sqlite import ( "context" + "database/sql" "strings" "github.com/pkg/errors" @@ -30,6 +31,17 @@ func (d *DB) CreateInstanceSettingIfNotExists(ctx context.Context, create *store } func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSetting) (*store.InstanceSetting, error) { + if err := upsertInstanceSetting(ctx, d.db, upsert); err != nil { + return nil, err + } + return upsert, nil +} + +func (*DB) UpsertInstanceSettingTx(ctx context.Context, tx *sql.Tx, upsert *store.InstanceSetting) error { + return upsertInstanceSetting(ctx, tx, upsert) +} + +func upsertInstanceSetting(ctx context.Context, executor dbExecutor, upsert *store.InstanceSetting) error { stmt := ` INSERT INTO system_setting ( name, value, description @@ -40,11 +52,8 @@ func (d *DB) UpsertInstanceSetting(ctx context.Context, upsert *store.InstanceSe value = EXCLUDED.value, description = EXCLUDED.description ` - if _, err := d.db.ExecContext(ctx, stmt, upsert.Name, upsert.Value, upsert.Description); err != nil { - return nil, err - } - - return upsert, nil + _, err := executor.ExecContext(ctx, stmt, upsert.Name, upsert.Value, upsert.Description) + return err } func (d *DB) ListInstanceSettings(ctx context.Context, find *store.FindInstanceSetting) ([]*store.InstanceSetting, error) { diff --git a/store/db/sqlite/user.go b/store/db/sqlite/user.go index 7c6d35ae6..fe7b06428 100644 --- a/store/db/sqlite/user.go +++ b/store/db/sqlite/user.go @@ -24,6 +24,11 @@ func (d *DB) UpdateUser(ctx context.Context, update *store.UpdateUser) (*store.U return nil, err } defer func() { _ = tx.Rollback() }() + if update.RemovesInstanceAdmin() { + if err := ensureInstanceAdminRetained(ctx, tx, update.ID); err != nil { + return nil, err + } + } if update.RowStatus != nil && *update.RowStatus == store.Archived { if err := validateSQLiteUserArchive(ctx, tx, update.ID); err != nil { return nil, err @@ -115,6 +120,30 @@ func validateSQLiteUserArchive(ctx context.Context, tx dbExecutor, userID int32) return nil } +// ensureInstanceAdminRetained refuses a mutation that would remove the last +// active instance administrator. Mutations of ordinary users skip the admin +// count. SQLite transactions begin IMMEDIATE, so concurrent removals +// serialize. +func ensureInstanceAdminRetained(ctx context.Context, tx dbExecutor, userID int32) error { + var role, rowStatus string + if err := tx.QueryRowContext(ctx, `SELECT role, row_status FROM user WHERE id = ?`, userID).Scan(&role, &rowStatus); errors.Is(err, sql.ErrNoRows) { + return nil + } else if err != nil { + return err + } + if role != string(store.RoleAdmin) || rowStatus != string(store.Normal) { + return nil + } + var adminCount int + if err := tx.QueryRowContext(ctx, `SELECT COUNT(*) FROM user WHERE role = 'ADMIN' AND row_status = 'NORMAL'`).Scan(&adminCount); err != nil { + return err + } + if adminCount <= 1 { + return store.ErrLastInstanceAdmin + } + return nil +} + func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User, error) { where, args := []string{"1 = 1"}, []any{} orderBy := []string{"created_ts DESC", "row_status DESC", "id DESC"} diff --git a/store/db/sqlite/user_delete.go b/store/db/sqlite/user_delete.go index 42c946e61..7bc2338bd 100644 --- a/store/db/sqlite/user_delete.go +++ b/store/db/sqlite/user_delete.go @@ -29,6 +29,9 @@ func (d *DB) DeleteUser(ctx context.Context, delete *store.DeleteUser) (*store.D defer func() { _ = tx.Rollback() }() + if err := ensureInstanceAdminRetained(ctx, tx, delete.ID); err != nil { + return nil, err + } var userID int32 if err := tx.QueryRowContext(ctx, "SELECT id FROM user WHERE id = ?", delete.ID).Scan(&userID); errors.Is(err, sql.ErrNoRows) { return &store.DeleteUserResult{}, nil diff --git a/store/deployment_config.go b/store/deployment_config.go index 0e25b3f86..61d32c98f 100644 --- a/store/deployment_config.go +++ b/store/deployment_config.go @@ -272,13 +272,8 @@ func validateAndNormalizeDeploymentInstanceSetting(setting *storepb.InstanceSett if notification == nil { return errors.New("notificationSetting must be populated for key NOTIFICATION") } - if email := notification.Email; email != nil && email.Enabled { - if strings.TrimSpace(email.SmtpHost) == "" || email.SmtpPort <= 0 || strings.TrimSpace(email.FromEmail) == "" { - return errors.New("enabled notification email requires smtpHost, a positive smtpPort, and fromEmail") - } - if email.UseTls && email.UseSsl { - return errors.New("notification email cannot enable both useTls and useSsl") - } + if err := ValidateInstanceNotificationSetting(notification); err != nil { + return err } case storepb.InstanceSettingKey_AI: if setting.GetAiSetting() == nil { diff --git a/store/doc.go b/store/doc.go index c377486a1..616e17898 100644 --- a/store/doc.go +++ b/store/doc.go @@ -1,5 +1,5 @@ // Package store is the persistence layer: the Store facade, the Driver -// interface implemented by store/db/{sqlite,mysql,postgres,d1}, migrations, seed +// interface implemented by store/db/{sqlite,mysql,postgres}, migrations, seed // data, and the in-memory cache. // // Layering: store may import provider, markdown, filter, proto/gen, and diff --git a/store/driver.go b/store/driver.go index 90a69fcbe..78b3f865d 100644 --- a/store/driver.go +++ b/store/driver.go @@ -58,6 +58,8 @@ type Driver interface { // CreateInstanceSettingIfNotExists atomically creates the setting when its name is absent and reports whether it inserted the row. CreateInstanceSettingIfNotExists(ctx context.Context, create *InstanceSetting) (bool, error) UpsertInstanceSetting(ctx context.Context, upsert *InstanceSetting) (*InstanceSetting, error) + // UpsertInstanceSettingTx writes the setting within tx, for callers that must commit it together with other statements. + UpsertInstanceSettingTx(ctx context.Context, tx *sql.Tx, upsert *InstanceSetting) error ListInstanceSettings(ctx context.Context, find *FindInstanceSetting) ([]*InstanceSetting, error) DeleteInstanceSetting(ctx context.Context, delete *DeleteInstanceSetting) error diff --git a/store/instance_setting.go b/store/instance_setting.go index b97dbd4f0..11a1b1e26 100644 --- a/store/instance_setting.go +++ b/store/instance_setting.go @@ -4,6 +4,7 @@ import ( "cmp" "context" "slices" + "strings" "github.com/pkg/errors" "google.golang.org/protobuf/encoding/protojson" @@ -314,6 +315,24 @@ func (s *Store) GetInstanceTagsSetting(ctx context.Context) (*storepb.InstanceTa return instanceTagsSetting, nil } +// ValidateInstanceNotificationSetting checks the rules a notification setting +// must meet before it is stored, whether it comes from the API or from +// deployment configuration: enabled email needs a host, a positive port, and +// a from address, and TLS and SSL are mutually exclusive. +func ValidateInstanceNotificationSetting(setting *storepb.InstanceNotificationSetting) error { + email := setting.GetEmail() + if email == nil || !email.Enabled { + return nil + } + if strings.TrimSpace(email.SmtpHost) == "" || email.SmtpPort <= 0 || strings.TrimSpace(email.FromEmail) == "" { + return errors.New("enabled notification email requires smtpHost, a positive smtpPort, and fromEmail") + } + if email.UseTls && email.UseSsl { + return errors.New("notification email cannot enable both useTls and useSsl") + } + return nil +} + func (s *Store) GetInstanceNotificationSetting(ctx context.Context) (*storepb.InstanceNotificationSetting, error) { instanceSetting, err := s.GetInstanceSetting(ctx, &FindInstanceSetting{ Name: storepb.InstanceSettingKey_NOTIFICATION.String(), diff --git a/store/migrator.go b/store/migrator.go index 026e6b8a1..e4b8478d9 100644 --- a/store/migrator.go +++ b/store/migrator.go @@ -13,6 +13,7 @@ import ( "github.com/pkg/errors" "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" storepb "github.com/usememos/memos/proto/gen/store" ) @@ -177,6 +178,14 @@ func (s *Store) applyMigrations(ctx context.Context, currentSchemaVersion, targe return err } + cachedBasicSetting, err := s.GetInstanceBasicSetting(ctx) + if err != nil { + return errors.Wrap(err, "failed to get instance basic setting") + } + // The cache hands out a shared value; change a copy until the commit. + basicSetting := &storepb.InstanceBasicSetting{} + proto.Merge(basicSetting, cachedBasicSetting) + // Start a transaction to apply migrations atomically tx, err := s.driver.GetDB().Begin() if err != nil { @@ -209,17 +218,19 @@ func (s *Store) applyMigrations(ctx context.Context, currentSchemaVersion, targe migrationsApplied++ } + // Record the schema version in the same transaction, so on SQLite and + // PostgreSQL a crash cannot leave migrated tables behind an old version + // that re-runs them. MySQL commits DDL implicitly, so there the version + // write is only adjacent to the migration, as it was before. + if err := s.writeSchemaVersionTx(ctx, tx, basicSetting, targetSchemaVersion); err != nil { + return err + } if err := tx.Commit(); err != nil { return errors.Wrap(err, "failed to commit migration transaction") } + s.cacheInstanceBasicSetting(ctx, basicSetting) slog.Info("migration completed", slog.Int("migrationsApplied", migrationsApplied)) - - // Update schema version after successful migration - if err := s.updateCurrentSchemaVersion(ctx, targetSchemaVersion); err != nil { - return errors.Wrap(err, "failed to update current schema version") - } - return nil } @@ -246,19 +257,23 @@ func (s *Store) preMigrate(ctx context.Context) error { if err := s.execute(ctx, tx, string(bytes)); err != nil { return errors.Errorf("failed to execute SQL file %s, err %s", filePath, err) } - if err := tx.Commit(); err != nil { - return errors.Wrap(err, "failed to commit transaction") - } - // Upsert current schema version to database. + // Record the schema version with the schema, so on SQLite and + // PostgreSQL a crash cannot leave an initialized database without + // one. MySQL commits DDL implicitly and keeps the earlier behavior. schemaVersion, err := s.GetCurrentSchemaVersion() if err != nil { return errors.Wrap(err, "failed to get current schema version") } - slog.Info("database initialized successfully", slog.String("schemaVersion", schemaVersion)) - if err := s.updateCurrentSchemaVersion(ctx, schemaVersion); err != nil { - return errors.Wrap(err, "failed to update current schema version") + basicSetting := &storepb.InstanceBasicSetting{} + if err := s.writeSchemaVersionTx(ctx, tx, basicSetting, schemaVersion); err != nil { + return err } + if err := tx.Commit(); err != nil { + return errors.Wrap(err, "failed to commit transaction") + } + s.cacheInstanceBasicSetting(ctx, basicSetting) + slog.Info("database initialized successfully", slog.String("schemaVersion", schemaVersion)) } if err := s.checkMinimumUpgradeVersion(ctx); err != nil { @@ -405,23 +420,30 @@ func (*Store) execute(ctx context.Context, tx *sql.Tx, stmt string) error { return nil } -// updateCurrentSchemaVersion updates the current schema version in the instance basic setting. -// It retrieves the instance basic setting, updates the schema version, and upserts the setting back to the database. -func (s *Store) updateCurrentSchemaVersion(ctx context.Context, schemaVersion string) error { - instanceBasicSetting, err := s.GetInstanceBasicSetting(ctx) +// writeSchemaVersionTx sets basicSetting's schema version and writes it +// within tx. The caller commits tx and then caches basicSetting. +func (s *Store) writeSchemaVersionTx(ctx context.Context, tx *sql.Tx, basicSetting *storepb.InstanceBasicSetting, schemaVersion string) error { + basicSetting.SchemaVersion = schemaVersion + value, err := protojson.Marshal(basicSetting) if err != nil { - return errors.Wrap(err, "failed to get instance basic setting") + return errors.Wrap(err, "failed to marshal instance basic setting") } - instanceBasicSetting.SchemaVersion = schemaVersion - if _, err := s.UpsertInstanceSetting(ctx, &storepb.InstanceSetting{ - Key: storepb.InstanceSettingKey_BASIC, - Value: &storepb.InstanceSetting_BasicSetting{BasicSetting: instanceBasicSetting}, + if err := s.driver.UpsertInstanceSettingTx(ctx, tx, &InstanceSetting{ + Name: storepb.InstanceSettingKey_BASIC.String(), + Value: string(value), }); err != nil { - return errors.Wrap(err, "failed to upsert instance setting") + return errors.Wrap(err, "failed to update current schema version") } return nil } +func (s *Store) cacheInstanceBasicSetting(ctx context.Context, basicSetting *storepb.InstanceBasicSetting) { + s.cacheInstanceSetting(ctx, &storepb.InstanceSetting{ + Key: storepb.InstanceSettingKey_BASIC, + Value: &storepb.InstanceSetting_BasicSetting{BasicSetting: basicSetting}, + }) +} + // checkMinimumUpgradeVersion rejects databases whose migration history is no longer shipped. // Fresh installations already have their schema version set by preMigrate. func (s *Store) checkMinimumUpgradeVersion(ctx context.Context) error { diff --git a/store/test/list_id_list_test.go b/store/test/list_id_list_test.go index 8da96b831..296a93622 100644 --- a/store/test/list_id_list_test.go +++ b/store/test/list_id_list_test.go @@ -10,10 +10,9 @@ import ( "github.com/usememos/memos/store" ) -// TestListsAcceptLongIDLists lists by id lists far longer than the hundred -// bound parameters Cloudflare D1 allows per statement. Every driver must -// accept them: the API hands page-sized lists, up to a thousand entries, to -// these lookups. +// TestListsAcceptLongIDLists lists by id lists of a few hundred entries. +// Every driver must accept them: the API hands page-sized lists, up to a +// thousand entries, to these lookups. func TestListsAcceptLongIDLists(t *testing.T) { ctx := context.Background() ts := NewTestingStore(ctx, t) diff --git a/store/test/memo_delete_policy_test.go b/store/test/memo_delete_policy_test.go index 6fdb98d99..2a116316b 100644 --- a/store/test/memo_delete_policy_test.go +++ b/store/test/memo_delete_policy_test.go @@ -153,8 +153,8 @@ func newMemoDeleteRaceFixture(t *testing.T) *memoDeleteRaceFixture { func TestDeleteMemoWithPolicyBlocksNewRepliesDuringSubtreeDeletion(t *testing.T) { driver := getDriverFromEnv() - if driver == "sqlite" || driver == "d1" { - t.Skip("SQLite serializes writes with IMMEDIATE transactions; D1 has no row locks") + if driver == "sqlite" { + t.Skip("SQLite serializes writes with IMMEDIATE transactions") } fixture := newMemoDeleteRaceFixture(t) @@ -221,8 +221,8 @@ func TestDeleteMemoWithPolicyBlocksNewRepliesDuringSubtreeDeletion(t *testing.T) func TestDeleteMemoWithPolicyIncludesReplyCommittedWhileWaitingForSubtree(t *testing.T) { driver := getDriverFromEnv() - if driver == "sqlite" || driver == "d1" { - t.Skip("SQLite serializes writes with IMMEDIATE transactions; D1 has no row locks") + if driver == "sqlite" { + t.Skip("SQLite serializes writes with IMMEDIATE transactions") } fixture := newMemoDeleteRaceFixture(t) diff --git a/store/test/memo_filter_test.go b/store/test/memo_filter_test.go index c867e029f..9bc305771 100644 --- a/store/test/memo_filter_test.go +++ b/store/test/memo_filter_test.go @@ -72,7 +72,6 @@ func TestMemoFilterContentUnicode(t *testing.T) { func TestMemoFilterContentUnicodeCaseFold(t *testing.T) { t.Parallel() - skipUnicodeFoldOnD1(t) tc := NewMemoFilterTestContext(t) defer tc.Close() @@ -180,7 +179,6 @@ func TestMemoFilterStartsWithCombinedAndNegated(t *testing.T) { func TestMemoFilterContentMatchesAdvanced(t *testing.T) { t.Parallel() - skipRegexFiltersOnD1(t) tc := NewMemoFilterTestContext(t) defer tc.Close() @@ -278,7 +276,6 @@ func TestMemoFilterContentEndsWith(t *testing.T) { func TestMemoFilterContentMatches(t *testing.T) { t.Parallel() - skipRegexFiltersOnD1(t) tc := NewMemoFilterTestContext(t) defer tc.Close() diff --git a/store/test/memo_mutation_test.go b/store/test/memo_mutation_test.go index fbc865627..e9538067c 100644 --- a/store/test/memo_mutation_test.go +++ b/store/test/memo_mutation_test.go @@ -483,7 +483,10 @@ func TestMemoMutationRechecksActorAndReferenceAudience(t *testing.T) { _, err := f.store.UpdateUser(f.ctx, &store.UpdateUser{ID: f.owner.ID, RowStatus: new(store.Archived)}) require.NoError(t, err) case "administrator demoted": - _, err := f.store.UpdateUser(f.ctx, &store.UpdateUser{ID: f.peer.ID, Role: new(store.RoleAdmin)}) + // The instance must keep another administrator for the demotion. + _, err := f.store.CreateUser(f.ctx, &store.User{Username: "mutation-admin", Role: store.RoleAdmin}) + require.NoError(t, err) + _, err = f.store.UpdateUser(f.ctx, &store.UpdateUser{ID: f.peer.ID, Role: new(store.RoleAdmin)}) require.NoError(t, err) mutation.Policy = memoWritePolicy(f.peer.ID, false) _, err = f.store.UpdateUser(f.ctx, &store.UpdateUser{ID: f.peer.ID, Role: new(store.RoleUser)}) diff --git a/store/test/memo_share_test.go b/store/test/memo_share_test.go index ca7ecc59b..24fd0d643 100644 --- a/store/test/memo_share_test.go +++ b/store/test/memo_share_test.go @@ -94,6 +94,9 @@ func TestMemoShareAuthorizedDeletion(t *testing.T) { wantErr = store.ErrMemoSpaceMembershipRequired case "demoted admin": delete.Policy.ActorUserID = admin.ID + // The instance must keep another administrator for the demotion. + _, err = createTestingUserWithRole(ctx, ts, "share-standing-admin", store.RoleAdmin) + require.NoError(t, err) _, err = ts.UpdateUser(ctx, &store.UpdateUser{ID: admin.ID, Role: new(store.RoleUser)}) require.NoError(t, err) wantErr = store.ErrMemoPermissionDenied diff --git a/store/test/migrator_upgrade_test.go b/store/test/migrator_upgrade_test.go index 9b5697bbd..b6af04fd7 100644 --- a/store/test/migrator_upgrade_test.go +++ b/store/test/migrator_upgrade_test.go @@ -181,9 +181,6 @@ func prepareUpgradeFixture(t *testing.T, driver, version string) (MemosContainer Driver: driver, DSN: containerDSN, }, hostDSN - case "d1": - t.Skip("previous stable releases cannot run on D1; there is no schema to upgrade from") - return MemosContainerConfig{}, "" default: t.Fatalf("unsupported driver: %s", driver) return MemosContainerConfig{}, "" diff --git a/store/test/reaction_test.go b/store/test/reaction_test.go index 2877d3047..adb25e8a1 100644 --- a/store/test/reaction_test.go +++ b/store/test/reaction_test.go @@ -353,9 +353,6 @@ func TestReactionInsertedAfterMemoCleanupStartsIsRemoved(t *testing.T) { } func TestReactionUpsertBlocksWhileMemoDeleteIsUncommitted(t *testing.T) { - if getDriverFromEnv() == "d1" { - t.Skip("D1 has no interactive transactions, so an uncommitted delete cannot hold a lock") - } setupContext := context.Background() ts := NewTestingStore(setupContext, t) t.Cleanup(func() { diff --git a/store/test/space_parent_lock_test.go b/store/test/space_parent_lock_test.go index 0e75f4eb3..aae204c14 100644 --- a/store/test/space_parent_lock_test.go +++ b/store/test/space_parent_lock_test.go @@ -83,8 +83,8 @@ func TestSQLiteRelationshipWriteSerializesWithParentDelete(t *testing.T) { func TestSpaceInvitationWaitsForConcurrentUserDelete(t *testing.T) { driver := getDriverFromEnv() - if driver == "sqlite" || driver == "d1" { - t.Skip("SQLite transactions begin IMMEDIATE and serialize competing writes without row locks; D1 has no row locks") + if driver == "sqlite" { + t.Skip("SQLite transactions begin IMMEDIATE and serialize competing writes without row locks") } setupCtx := context.Background() @@ -146,8 +146,8 @@ func TestSpaceInvitationWaitsForConcurrentUserDelete(t *testing.T) { func TestSpaceInvitationWaitsForConcurrentSpaceDelete(t *testing.T) { driver := getDriverFromEnv() - if driver == "sqlite" || driver == "d1" { - t.Skip("SQLite transactions begin IMMEDIATE and serialize competing writes without row locks; D1 has no row locks") + if driver == "sqlite" { + t.Skip("SQLite transactions begin IMMEDIATE and serialize competing writes without row locks") } setupCtx := context.Background() diff --git a/store/test/store.go b/store/test/store.go index 73dffac7f..54c3c202d 100644 --- a/store/test/store.go +++ b/store/test/store.go @@ -125,22 +125,3 @@ func getDriverFromEnv() string { } return driver } - -// skipRegexFiltersOnD1 skips tests whose matches() patterns go beyond -// literal text with anchors: D1 has no REGEXP operator, so the filter engine -// only expresses those and rejects everything else at compile time. -func skipRegexFiltersOnD1(t *testing.T) { - t.Helper() - if getDriverFromEnv() == "d1" { - t.Skip("only literal regular expressions are supported on Cloudflare D1") - } -} - -// skipUnicodeFoldOnD1 skips tests that need Unicode case folding: D1 cannot -// register custom functions, so it folds with the ASCII-only LOWER(). -func skipUnicodeFoldOnD1(t *testing.T) { - t.Helper() - if getDriverFromEnv() == "d1" { - t.Skip("Cloudflare D1 folds case with ASCII-only LOWER()") - } -} diff --git a/store/test/user_delete_test.go b/store/test/user_delete_test.go index 36eab2462..50d0be153 100644 --- a/store/test/user_delete_test.go +++ b/store/test/user_delete_test.go @@ -178,7 +178,7 @@ func TestDeleteUserCleansRelatedData(t *testing.T) { ts := NewTestingStore(ctx, t) defer ts.Close() - user, err := createTestingHostUser(ctx, ts) + user, err := createTestingUserWithRole(ctx, ts, "test", store.RoleUser) require.NoError(t, err) peer, err := createTestingUserWithRole(ctx, ts, "delete-peer", store.RoleUser) require.NoError(t, err) diff --git a/store/test/user_instance_admin_test.go b/store/test/user_instance_admin_test.go new file mode 100644 index 000000000..d4f9e5428 --- /dev/null +++ b/store/test/user_instance_admin_test.go @@ -0,0 +1,113 @@ +package test + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/usememos/memos/store" +) + +func TestUserMutationsRetainAnInstanceAdmin(t *testing.T) { + ctx := context.Background() + ts := NewTestingStore(ctx, t) + defer ts.Close() + + admin, err := createTestingUserWithRole(ctx, ts, "only-admin", store.RoleAdmin) + require.NoError(t, err) + member, err := createTestingUserWithRole(ctx, ts, "member", store.RoleUser) + require.NoError(t, err) + + archived := store.Archived + userRole := store.RoleUser + _, err = ts.UpdateUser(ctx, &store.UpdateUser{ID: admin.ID, RowStatus: &archived}) + require.ErrorIs(t, err, store.ErrLastInstanceAdmin, "archive the last admin") + _, err = ts.UpdateUser(ctx, &store.UpdateUser{ID: admin.ID, Role: &userRole}) + require.ErrorIs(t, err, store.ErrLastInstanceAdmin, "demote the last admin") + _, err = ts.DeleteUser(ctx, &store.DeleteUser{ID: admin.ID}) + require.ErrorIs(t, err, store.ErrLastInstanceAdmin, "delete the last admin") + + // Mutations that keep the admin, or touch other users, are unaffected. + nickname := "still admin" + _, err = ts.UpdateUser(ctx, &store.UpdateUser{ID: admin.ID, Nickname: &nickname}) + require.NoError(t, err) + _, err = ts.UpdateUser(ctx, &store.UpdateUser{ID: member.ID, RowStatus: &archived}) + require.NoError(t, err) + + // An archived admin does not count as the remaining admin. + archivedAdmin, err := createTestingUserWithRole(ctx, ts, "archived-admin", store.RoleAdmin) + require.NoError(t, err) + _, err = ts.UpdateUser(ctx, &store.UpdateUser{ID: archivedAdmin.ID, RowStatus: &archived}) + require.NoError(t, err) + _, err = ts.DeleteUser(ctx, &store.DeleteUser{ID: admin.ID}) + require.ErrorIs(t, err, store.ErrLastInstanceAdmin) + + // With a second active admin, the first may step down. + _, err = createTestingUserWithRole(ctx, ts, "successor-admin", store.RoleAdmin) + require.NoError(t, err) + _, err = ts.UpdateUser(ctx, &store.UpdateUser{ID: admin.ID, Role: &userRole}) + require.NoError(t, err) +} + +// TestLastAdminCheckWaitsForConcurrentAdminRemoval demotes one admin while +// another admin's demotion is still uncommitted. The check must wait for that +// transaction and then refuse, so the instance keeps exactly one active admin. +func TestLastAdminCheckWaitsForConcurrentAdminRemoval(t *testing.T) { + driver := getDriverFromEnv() + if driver == "sqlite" { + t.Skip("SQLite transactions begin IMMEDIATE and serialize competing writes without row locks") + } + + setupCtx := context.Background() + ts := NewTestingStore(setupCtx, t) + t.Cleanup(func() { require.NoError(t, ts.Close()) }) + ctx, cancel := context.WithTimeout(setupCtx, 10*time.Second) + defer cancel() + + first, err := createTestingUserWithRole(ctx, ts, "concurrent-admin-first", store.RoleAdmin) + require.NoError(t, err) + second, err := createTestingUserWithRole(ctx, ts, "concurrent-admin-second", store.RoleAdmin) + require.NoError(t, err) + + blocker, err := ts.GetDriver().GetDB().BeginTx(ctx, nil) + require.NoError(t, err) + blockerOpen := true + defer func() { + if blockerOpen { + _ = blocker.Rollback() + } + }() + demoteFirst := "UPDATE user SET role = 'USER' WHERE id = ?" + if driver == "postgres" { + demoteFirst = `UPDATE "user" SET role = 'USER' WHERE id = $1` + } + _, err = blocker.ExecContext(ctx, demoteFirst, first.ID) + require.NoError(t, err) + + demoteDone := make(chan error, 1) + go func() { + _, demoteErr := ts.UpdateUser(ctx, &store.UpdateUser{ID: second.ID, Role: new(store.RoleUser)}) + demoteDone <- demoteErr + }() + select { + case demoteErr := <-demoteDone: + require.FailNowf(t, "demotion did not wait", "returned %v while the other admin's demotion was uncommitted", demoteErr) + case <-time.After(300 * time.Millisecond): + } + + require.NoError(t, blocker.Commit()) + blockerOpen = false + select { + case demoteErr := <-demoteDone: + require.ErrorIs(t, demoteErr, store.ErrLastInstanceAdmin) + case <-ctx.Done(): + require.FailNow(t, "timed out waiting for the demotion") + } + + admins, err := ts.ListUsers(ctx, &store.FindUser{Role: new(store.RoleAdmin), RowStatus: new(store.Normal)}) + require.NoError(t, err) + require.Len(t, admins, 1) + require.Equal(t, second.ID, admins[0].ID) +} diff --git a/store/test/user_test.go b/store/test/user_test.go index b12af7aef..2d58dafcd 100644 --- a/store/test/user_test.go +++ b/store/test/user_test.go @@ -16,12 +16,12 @@ func TestUserStore(t *testing.T) { t.Parallel() ctx := context.Background() ts := NewTestingStore(ctx, t) - user, err := createTestingHostUser(ctx, ts) + user, err := createTestingUserWithRole(ctx, ts, "test", store.RoleUser) require.NoError(t, err) users, err := ts.ListUsers(ctx, &store.FindUser{}) require.NoError(t, err) require.Equal(t, 1, len(users)) - require.Equal(t, store.RoleAdmin, users[0].Role) + require.Equal(t, store.RoleUser, users[0].Role) require.Equal(t, user, users[0]) userPatchNickname := "test_nickname_2" userPatch := &store.UpdateUser{ @@ -186,7 +186,7 @@ func TestUserUpdateRowStatus(t *testing.T) { ctx := context.Background() ts := NewTestingStore(ctx, t) - user, err := createTestingHostUser(ctx, ts) + user, err := createTestingUserWithRole(ctx, ts, "test", store.RoleUser) require.NoError(t, err) require.Equal(t, store.Normal, user.RowStatus) diff --git a/store/user.go b/store/user.go index 5f8bc008f..007a65fdb 100644 --- a/store/user.go +++ b/store/user.go @@ -13,6 +13,16 @@ import ( // has been resolved explicitly. var ErrUserHasSpaceMembership = stderrors.New("user still has space memberships") +// ErrLastInstanceAdmin indicates that a user mutation would leave the instance +// without an active administrator. +var ErrLastInstanceAdmin = stderrors.New("cannot remove the last instance admin") + +// RemovesInstanceAdmin reports whether the update can take the user out of +// the set of active instance administrators. +func (update *UpdateUser) RemovesInstanceAdmin() bool { + return (update.RowStatus != nil && *update.RowStatus != Normal) || (update.Role != nil && *update.Role != RoleAdmin) +} + // Role is the type of a role. type Role string diff --git a/web/src/utils/memo.ts b/web/src/utils/memo.ts index b7c6d0ee4..322605cd9 100644 --- a/web/src/utils/memo.ts +++ b/web/src/utils/memo.ts @@ -70,7 +70,8 @@ export const getAssignableVisibilityOptions = (options: { hasSpacePlacement: boo /** Audiences offered as a persistent default. A Space-scoped default has no meaning outside a Space. */ export const DEFAULT_VISIBILITY_OPTIONS: readonly VisibilityOption[] = VISIBILITY_OPTIONS.filter((option) => !option.requiresSpace); +/** Unknown or empty names fall back to PRIVATE so a bad value never widens a memo's audience. */ export const convertVisibilityFromString = (visibility: string) => - VISIBILITY_OPTIONS.find((option) => option.name === visibility)?.value ?? Visibility.PUBLIC; + VISIBILITY_OPTIONS.find((option) => option.name === visibility)?.value ?? Visibility.PRIVATE; export const convertVisibilityToString = (visibility: Visibility) => getVisibilityOption(visibility)?.name ?? "PRIVATE"; diff --git a/web/tests/visibility-space.test.tsx b/web/tests/visibility-space.test.tsx index f9d2f816f..36fa3c1af 100644 --- a/web/tests/visibility-space.test.tsx +++ b/web/tests/visibility-space.test.tsx @@ -20,6 +20,11 @@ describe("Space visibility presentation", () => { expect(convertVisibilityToString(Visibility.SPACE)).toBe("SPACE"); }); + it("falls back to PRIVATE for an unknown or empty name", () => { + expect(convertVisibilityFromString("")).toBe(Visibility.PRIVATE); + expect(convertVisibilityFromString("UNKNOWN")).toBe(Visibility.PRIVATE); + }); + it("renders a distinct icon per audience", () => { const rendered = VISIBILITY_OPTIONS.map((option) => { const { container } = render();