diff --git a/proto/api/instance_service.proto b/proto/api/instance_service.proto index 82fd0fb71..66dec8589 100644 --- a/proto/api/instance_service.proto +++ b/proto/api/instance_service.proto @@ -94,8 +94,9 @@ message InstanceProfile { bool needs_setup = 6; // The first administrator of the instance, for display purposes. - // May be null on an instance that has lost all admins; use needs_setup to determine whether initial setup is - // required. + // Only set for signed-in callers; anonymous requests never receive it, so the admin's login identifier is not + // disclosed before authentication. May also be null on an instance that has lost all admins; use needs_setup to + // determine whether initial setup is required. User admin = 7; // The proof-of-humanity challenge required on signup and password sign-in. diff --git a/proto/gen/api/instance_service.pb.go b/proto/gen/api/instance_service.pb.go index dfb0edb5c..4c6c7fc9c 100644 --- a/proto/gen/api/instance_service.pb.go +++ b/proto/gen/api/instance_service.pb.go @@ -337,8 +337,9 @@ type InstanceProfile struct { // mistaken for a fresh install. NeedsSetup bool `protobuf:"varint,6,opt,name=needs_setup,json=needsSetup,proto3" json:"needs_setup,omitempty"` // The first administrator of the instance, for display purposes. - // May be null on an instance that has lost all admins; use needs_setup to determine whether initial setup is - // required. + // Only set for signed-in callers; anonymous requests never receive it, so the admin's login identifier is not + // disclosed before authentication. May also be null on an instance that has lost all admins; use needs_setup to + // determine whether initial setup is required. Admin *User `protobuf:"bytes,7,opt,name=admin,proto3" json:"admin,omitempty"` // The proof-of-humanity challenge required on signup and password sign-in. // Absent when none is configured, which is the case on every self-hosted instance. diff --git a/proto/gen/openapi.yaml b/proto/gen/openapi.yaml index 8ae5b37f9..627ea7082 100644 --- a/proto/gen/openapi.yaml +++ b/proto/gen/openapi.yaml @@ -3864,8 +3864,9 @@ components: - $ref: '#/components/schemas/User' description: |- The first administrator of the instance, for display purposes. - May be null on an instance that has lost all admins; use needs_setup to determine whether initial setup is - required. + Only set for signed-in callers; anonymous requests never receive it, so the admin's login identifier is not + disclosed before authentication. May also be null on an instance that has lost all admins; use needs_setup to + determine whether initial setup is required. challenge: allOf: - $ref: '#/components/schemas/InstanceProfile_Challenge' diff --git a/server/api/instance_service.go b/server/api/instance_service.go index c571eb6b3..b95f5500b 100644 --- a/server/api/instance_service.go +++ b/server/api/instance_service.go @@ -43,7 +43,11 @@ func (c *instanceSettingCaller) currentUser(ctx context.Context, service *APISer // GetInstanceProfile returns the instance profile. func (s *APIService) GetInstanceProfile(ctx context.Context, _ *apipb.GetInstanceProfileRequest) (*apipb.InstanceProfile, error) { - admin, err := s.GetInstanceAdmin(ctx) + currentUser, err := s.fetchCurrentUser(ctx) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err) + } + admin, err := s.getInstanceAdmin(ctx, currentUser) if err != nil { return nil, status.Errorf(codes.Internal, "failed to get instance admin: %v", err) } @@ -66,7 +70,7 @@ func (s *APIService) GetInstanceProfile(ctx context.Context, _ *apipb.GetInstanc Version: s.Profile.Version, Demo: s.Profile.Demo, InstanceUrl: s.Profile.InstanceURL, - Admin: admin, // for display only; may be nil even on a populated instance + Admin: admin, // signed-in callers only; may be nil even on a populated instance Commit: s.Profile.Commit, NeedsSetup: len(users) == 0, AccessMode: convertInstanceAccessModeFromStore(accessSetting.AccessMode), @@ -378,7 +382,17 @@ func sameSMTPConnectionIdentity(setting, existing *storepb.InstanceNotificationS setting.UseSsl == existing.UseSsl } -func (s *APIService) GetInstanceAdmin(ctx context.Context) (*apipb.User, error) { +// getInstanceAdmin returns the first admin of the instance as seen by viewer, +// or nil when the instance has no admin. +// +// A nil viewer is an anonymous caller and always gets nil: the profile endpoint +// is reachable without authentication so the sign-in page can render, and an +// anonymous visitor must not learn the login identifier of the most privileged +// account, especially on a private instance. +func (s *APIService) getInstanceAdmin(ctx context.Context, viewer *store.User) (*apipb.User, error) { + if viewer == nil { + return nil, nil + } adminUserType := store.RoleAdmin user, err := s.Store.GetUser(ctx, &store.FindUser{ Role: &adminUserType, @@ -389,7 +403,5 @@ func (s *APIService) GetInstanceAdmin(ctx context.Context) (*apipb.User, error) if user == nil { return nil, nil } - - currentUser, _ := s.fetchCurrentUser(ctx) - return convertUserFromStore(user, currentUser), nil + return convertUserFromStore(user, viewer), nil } diff --git a/server/api/test/instance_admin_cache_test.go b/server/api/test/instance_admin_cache_test.go index 170735d91..ab2151301 100644 --- a/server/api/test/instance_admin_cache_test.go +++ b/server/api/test/instance_admin_cache_test.go @@ -28,7 +28,7 @@ func TestInstanceAdminRetrieval(t *testing.T) { require.NotNil(t, user) // Verify instance is now initialized - profile2, err := ts.Service.GetInstanceProfile(ctx, &apipb.GetInstanceProfileRequest{}) + profile2, err := ts.Service.GetInstanceProfile(ts.CreateUserContext(ctx, user.ID), &apipb.GetInstanceProfileRequest{}) require.NoError(t, err) require.NotNil(t, profile2.Admin, "Instance should be initialized after first admin user is created") require.Equal(t, user.Username, profile2.Admin.Username) @@ -42,10 +42,11 @@ func TestInstanceAdminRetrieval(t *testing.T) { // Create admin user user, err := ts.CreateHostUser(ctx, "admin") require.NoError(t, err) + userCtx := ts.CreateUserContext(ctx, user.ID) // Multiple calls should return consistent admin user (from cache) for range 5 { - profile, err := ts.Service.GetInstanceProfile(ctx, &apipb.GetInstanceProfileRequest{}) + profile, err := ts.Service.GetInstanceProfile(userCtx, &apipb.GetInstanceProfileRequest{}) require.NoError(t, err) require.NotNil(t, profile.Admin) require.Equal(t, user.Username, profile.Admin.Username) diff --git a/server/api/test/instance_service_test.go b/server/api/test/instance_service_test.go index e888d2146..ad893a9e4 100644 --- a/server/api/test/instance_service_test.go +++ b/server/api/test/instance_service_test.go @@ -53,9 +53,9 @@ func TestGetInstanceProfile(t *testing.T) { require.NoError(t, err) require.NotNil(t, hostUser) - // Call GetInstanceProfile directly + // Call GetInstanceProfile as the signed-in admin req := &apipb.GetInstanceProfileRequest{} - resp, err := ts.Service.GetInstanceProfile(ctx, req) + resp, err := ts.Service.GetInstanceProfile(ts.CreateUserContext(ctx, hostUser.ID), req) // Verify response require.NoError(t, err) @@ -73,6 +73,30 @@ func TestGetInstanceProfile(t *testing.T) { require.False(t, resp.NeedsSetup) }) + t.Run("GetInstanceProfile hides the admin from anonymous callers", func(t *testing.T) { + ts := NewTestService(t) + defer ts.Cleanup() + + hostUser, err := ts.CreateHostUser(ctx, "admin") + require.NoError(t, err) + + // The endpoint is public so the sign-in page can render, but an + // unauthenticated visitor must not learn the admin's username. + resp, err := ts.Service.GetInstanceProfile(ctx, &apipb.GetInstanceProfileRequest{}) + require.NoError(t, err) + require.NotNil(t, resp) + require.Nil(t, resp.Admin) + require.False(t, resp.NeedsSetup) + + // Any signed-in user, not only the admin, may see it. + regularUser, err := ts.CreateRegularUser(ctx, "alice") + require.NoError(t, err) + resp, err = ts.Service.GetInstanceProfile(ts.CreateUserContext(ctx, regularUser.ID), &apipb.GetInstanceProfileRequest{}) + require.NoError(t, err) + require.NotNil(t, resp.Admin) + require.Equal(t, hostUser.Username, resp.Admin.Username) + }) + t.Run("GetInstanceProfile with users but no admin", func(t *testing.T) { // Create test service for this specific test ts := NewTestService(t) @@ -86,7 +110,7 @@ func TestGetInstanceProfile(t *testing.T) { require.NotNil(t, regularUser) req := &apipb.GetInstanceProfileRequest{} - resp, err := ts.Service.GetInstanceProfile(ctx, req) + resp, err := ts.Service.GetInstanceProfile(ts.CreateUserContext(ctx, regularUser.ID), req) require.NoError(t, err) require.NotNil(t, resp) @@ -105,8 +129,9 @@ func TestGetInstanceProfile_Concurrency(t *testing.T) { defer ts.Cleanup() // Create a host user - _, err := ts.CreateHostUser(ctx, "admin") + hostUser, err := ts.CreateHostUser(ctx, "admin") require.NoError(t, err) + userCtx := ts.CreateUserContext(ctx, hostUser.ID) // Make concurrent requests numGoroutines := 10 @@ -116,7 +141,7 @@ func TestGetInstanceProfile_Concurrency(t *testing.T) { for range numGoroutines { go func() { req := &apipb.GetInstanceProfileRequest{} - resp, err := ts.Service.GetInstanceProfile(ctx, req) + resp, err := ts.Service.GetInstanceProfile(userCtx, req) if err != nil { errors <- err return diff --git a/server/api/test/user_email_visibility_test.go b/server/api/test/user_email_visibility_test.go index 179f11ed3..5a65e04db 100644 --- a/server/api/test/user_email_visibility_test.go +++ b/server/api/test/user_email_visibility_test.go @@ -96,14 +96,16 @@ func TestUserEmailVisibility(t *testing.T) { require.Equal(t, user.Email, got.User.Email) }) - t.Run("GetInstanceProfile redacts admin email for anonymous callers", func(t *testing.T) { + t.Run("GetInstanceProfile redacts admin email for other users", func(t *testing.T) { ts := NewTestService(t) defer ts.Cleanup() admin, err := ts.CreateHostUser(ctx, "admin") require.NoError(t, err) + user, err := ts.CreateRegularUser(ctx, "user") + require.NoError(t, err) - got, err := ts.Service.GetInstanceProfile(ctx, &apipb.GetInstanceProfileRequest{}) + got, err := ts.Service.GetInstanceProfile(ts.CreateUserContext(ctx, user.ID), &apipb.GetInstanceProfileRequest{}) require.NoError(t, err) require.NotNil(t, got) require.NotNil(t, got.Admin) diff --git a/web/src/types/proto/api/instance_service_pb.ts b/web/src/types/proto/api/instance_service_pb.ts index 359f487b2..c552b8686 100644 --- a/web/src/types/proto/api/instance_service_pb.ts +++ b/web/src/types/proto/api/instance_service_pb.ts @@ -74,8 +74,9 @@ export type InstanceProfile = Message<"memos.api.InstanceProfile"> & { /** * The first administrator of the instance, for display purposes. - * May be null on an instance that has lost all admins; use needs_setup to determine whether initial setup is - * required. + * Only set for signed-in callers; anonymous requests never receive it, so the admin's login identifier is not + * disclosed before authentication. May also be null on an instance that has lost all admins; use needs_setup to + * determine whether initial setup is required. * * @generated from field: memos.api.User admin = 7; */