This is an automated email from the ASF dual-hosted git repository. LinkinStars pushed a commit to branch fix/2.0.4/user in repository https://gitbox.apache.org/repos/asf/answer.git
commit 640a9ab2ccfbe659180619f817fd317e1b87712c Author: LinkinStars <[email protected]> AuthorDate: Tue Oct 20 17:45:43 2026 +0800 fix(security): enforce follow visibility controls --- cmd/wire_gen.go | 4 +- internal/controller/follow_controller.go | 1 + internal/schema/follow_schema.go | 2 + internal/service/follow/follow_service.go | 44 +++++++- .../follow/follow_service_visibility_test.go | 123 +++++++++++++++++++++ .../service/notification_common/notification.go | 46 +++++++- .../notification_visibility_test.go | 58 ++++++++++ 7 files changed, 267 insertions(+), 11 deletions(-) diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go index 8c8a689f..3445eaad 100644 --- a/cmd/wire_gen.go +++ b/cmd/wire_gen.go @@ -225,7 +225,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, voteController := controller.NewVoteController(voteService, rankService, captchaService) tagController := controller.NewTagController(tagService, tagCommonService, rankService) followFollowRepo := activity.NewFollowRepo(dataData, uniqueIDRepo, activityRepo) - followService := follow.NewFollowService(followFollowRepo, followRepo, tagCommonRepo) + followService := follow.NewFollowService(followFollowRepo, followRepo, tagCommonRepo, objService) followController := controller.NewFollowController(followService) collectionGroupRepo := collection.NewCollectionGroupRepo(dataData) collectionService := collection2.NewCollectionService(collectionRepo, collectionGroupRepo, questionCommon, objService) @@ -253,7 +253,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, siteInfoService := siteinfo.NewSiteInfoService(siteInfoRepo, siteInfoCommonService, emailService, tagCommonService, configService, questionCommon, fileRecordService) siteInfoController := controller_admin.NewSiteInfoController(siteInfoService) controllerSiteInfoController := controller.NewSiteInfoController(siteInfoCommonService) - notificationCommon := notificationcommon.NewNotificationCommon(dataData, notificationRepo, userCommon, activityRepo, followRepo, objService, noticequeueService, userExternalLoginRepo, siteInfoCommonService) + notificationCommon := notificationcommon.NewNotificationCommon(dataData, notificationRepo, userCommon, activityRepo, followRepo, objService, userRoleRelService, noticequeueService, userExternalLoginRepo, siteInfoCommonService) badgeRepo := badge.NewBadgeRepo(dataData, uniqueIDRepo) notificationService := notification.NewNotificationService(dataData, notificationRepo, notificationCommon, revisionService, userRepo, reportRepo, reviewService, badgeRepo) notificationController := controller.NewNotificationController(notificationService, rankService) diff --git a/internal/controller/follow_controller.go b/internal/controller/follow_controller.go index aabf2d26..922b7c16 100644 --- a/internal/controller/follow_controller.go +++ b/internal/controller/follow_controller.go @@ -58,6 +58,7 @@ func (fc *FollowController) Follow(ctx *gin.Context) { dto := &schema.FollowDTO{} _ = copier.Copy(dto, req) dto.UserID = middleware.GetLoginUserIDFromContext(ctx) + dto.IsAdminModerator = middleware.GetUserIsAdminModerator(ctx) resp, err := fc.followService.Follow(ctx, dto) if err != nil { diff --git a/internal/schema/follow_schema.go b/internal/schema/follow_schema.go index 17d70422..66ac5b30 100644 --- a/internal/schema/follow_schema.go +++ b/internal/schema/follow_schema.go @@ -42,6 +42,8 @@ type FollowDTO struct { IsCancel bool // user TagID UserID string + // whether the requester is an administrator or moderator + IsAdminModerator bool } // UpdateFollowTagsReq update user follow tags diff --git a/internal/service/follow/follow_service.go b/internal/service/follow/follow_service.go index 743b0da6..c09efb1c 100644 --- a/internal/service/follow/follow_service.go +++ b/internal/service/follow/follow_service.go @@ -22,10 +22,14 @@ package follow import ( "context" + "github.com/apache/answer/internal/base/constant" + "github.com/apache/answer/internal/base/reason" "github.com/apache/answer/internal/entity" "github.com/apache/answer/internal/schema" "github.com/apache/answer/internal/service/activity_common" tagcommon "github.com/apache/answer/internal/service/tag_common" + "github.com/apache/answer/pkg/obj" + "github.com/segmentfault/pacman/errors" ) type FollowRepo interface { @@ -33,21 +37,28 @@ type FollowRepo interface { FollowCancel(ctx context.Context, objectId, userId string) error } +type ObjectInfoService interface { + GetInfo(ctx context.Context, objectID string) (*schema.SimpleObjectInfo, error) +} + type FollowService struct { - tagRepo tagcommon.TagCommonRepo - followRepo FollowRepo - followCommonRepo activity_common.FollowRepo + tagRepo tagcommon.TagCommonRepo + followRepo FollowRepo + followCommonRepo activity_common.FollowRepo + objectInfoService ObjectInfoService } func NewFollowService( followRepo FollowRepo, followCommonRepo activity_common.FollowRepo, tagRepo tagcommon.TagCommonRepo, + objectInfoService ObjectInfoService, ) *FollowService { return &FollowService{ - followRepo: followRepo, - followCommonRepo: followCommonRepo, - tagRepo: tagRepo, + followRepo: followRepo, + followCommonRepo: followCommonRepo, + tagRepo: tagRepo, + objectInfoService: objectInfoService, } } @@ -56,6 +67,9 @@ func (fs *FollowService) Follow(ctx context.Context, dto *schema.FollowDTO) (res if dto.IsCancel { err = fs.followRepo.FollowCancel(ctx, dto.ObjectID, dto.UserID) } else { + if err := fs.checkVisibility(ctx, dto); err != nil { + return resp, err + } err = fs.followRepo.Follow(ctx, dto.ObjectID, dto.UserID) } if err != nil { @@ -71,6 +85,24 @@ func (fs *FollowService) Follow(ctx context.Context, dto *schema.FollowDTO) (res return resp, nil } +func (fs *FollowService) checkVisibility(ctx context.Context, dto *schema.FollowDTO) error { + objectType, err := obj.GetObjectTypeStrByObjectID(dto.ObjectID) + if err != nil { + return err + } + if objectType != constant.QuestionObjectType && objectType != constant.AnswerObjectType && objectType != constant.CommentObjectType { + return nil + } + objectInfo, err := fs.objectInfoService.GetInfo(ctx, dto.ObjectID) + if err != nil { + if serviceErr, ok := err.(*errors.Error); ok && serviceErr.Reason == reason.ObjectNotFound { + return errors.NotFound(reason.QuestionNotFound) + } + return err + } + return objectInfo.CheckVisibility(dto.UserID, dto.IsAdminModerator) +} + // UpdateFollowTags update user follow tags func (fs *FollowService) UpdateFollowTags(ctx context.Context, req *schema.UpdateFollowTagsReq) (err error) { objIDs, err := fs.followCommonRepo.GetFollowIDs(ctx, req.UserID, entity.Tag{}.TableName()) diff --git a/internal/service/follow/follow_service_visibility_test.go b/internal/service/follow/follow_service_visibility_test.go new file mode 100644 index 00000000..42920e0e --- /dev/null +++ b/internal/service/follow/follow_service_visibility_test.go @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package follow + +import ( + "context" + "testing" + + "github.com/apache/answer/internal/entity" + "github.com/apache/answer/internal/schema" +) + +type followVisibilityObjectInfoService struct{ info *schema.SimpleObjectInfo } + +func (s followVisibilityObjectInfoService) GetInfo(context.Context, string) (*schema.SimpleObjectInfo, error) { + return s.info, nil +} + +type followVisibilityRepo struct{ followCalled, cancelCalled bool } + +func (r *followVisibilityRepo) Follow(context.Context, string, string) error { + r.followCalled = true + return nil +} +func (r *followVisibilityRepo) FollowCancel(context.Context, string, string) error { + r.cancelCalled = true + return nil +} + +type followVisibilityCommonRepo struct{} + +func (followVisibilityCommonRepo) GetFollowIDs(context.Context, string, string) ([]string, error) { + return nil, nil +} +func (followVisibilityCommonRepo) GetFollowAmount(context.Context, string) (int, error) { + return 0, nil +} +func (followVisibilityCommonRepo) GetFollowUserIDs(context.Context, string) ([]string, error) { + return nil, nil +} +func (followVisibilityCommonRepo) IsFollowed(context.Context, string, string) (bool, error) { + return false, nil +} +func (followVisibilityCommonRepo) MigrateFollowers(context.Context, string, string, string) error { + return nil +} + +func TestFollowRejectsRestrictedQuestionForOtherUser(t *testing.T) { + repo := &followVisibilityRepo{} + service := &FollowService{ + followRepo: repo, + objectInfoService: followVisibilityObjectInfoService{info: &schema.SimpleObjectInfo{ + ObjectType: "question", + QuestionCreatorUserID: "author", + QuestionStatus: entity.QuestionStatusAvailable, + QuestionShow: entity.QuestionHide, + }}, + } + + _, err := service.Follow(context.Background(), &schema.FollowDTO{ + ObjectID: "10010000000000001", UserID: "other-user", + }) + if err == nil { + t.Fatal("Follow() allowed another user to follow a hidden question") + } + if repo.followCalled { + t.Fatal("Follow() called the repository after visibility was denied") + } +} + +func TestFollowAllowsQuestionAuthorToFollowRestrictedQuestion(t *testing.T) { + repo := &followVisibilityRepo{} + service := &FollowService{ + followRepo: repo, + objectInfoService: followVisibilityObjectInfoService{info: &schema.SimpleObjectInfo{ + ObjectType: "question", + QuestionCreatorUserID: "author", + QuestionStatus: entity.QuestionStatusPending, + QuestionShow: entity.QuestionShow, + }}, + } + + if err := service.checkVisibility(context.Background(), &schema.FollowDTO{ + ObjectID: "10010000000000001", UserID: "author", + }); err != nil { + t.Fatalf("question author was denied: %v", err) + } +} + +func TestFollowCancelAllowsRemovalOfHistoricalRestrictedFollow(t *testing.T) { + repo := &followVisibilityRepo{} + service := &FollowService{ + followRepo: repo, + followCommonRepo: followVisibilityCommonRepo{}, + } + + _, err := service.Follow(context.Background(), &schema.FollowDTO{ + ObjectID: "10010000000000001", UserID: "ordinary-user", IsCancel: true, + }) + if err != nil { + t.Fatalf("Follow() cancel error: %v", err) + } + if !repo.cancelCalled { + t.Fatal("Follow() did not cancel the historical follow relationship") + } +} diff --git a/internal/service/notification_common/notification.go b/internal/service/notification_common/notification.go index aa3f4106..e1607340 100644 --- a/internal/service/notification_common/notification.go +++ b/internal/service/notification_common/notification.go @@ -37,6 +37,7 @@ import ( "github.com/apache/answer/internal/service/activity_common" "github.com/apache/answer/internal/service/noticequeue" "github.com/apache/answer/internal/service/object_info" + "github.com/apache/answer/internal/service/role" usercommon "github.com/apache/answer/internal/service/user_common" "github.com/apache/answer/pkg/uid" "github.com/apache/answer/plugin" @@ -59,13 +60,22 @@ type NotificationRepo interface { DeleteUserNotificationConfig(ctx context.Context, userID string) (err error) } +type ObjectInfoService interface { + GetInfo(ctx context.Context, objectID string) (*schema.SimpleObjectInfo, error) +} + +type UserRoleService interface { + GetUserRoleRelMapping(ctx context.Context, userIDs []string) (map[string]int, error) +} + type NotificationCommon struct { data *data.Data notificationRepo NotificationRepo activityRepo activity_common.ActivityRepo followRepo activity_common.FollowRepo userCommon *usercommon.UserCommon - objectInfoService *object_info.ObjService + objectInfoService ObjectInfoService + userRoleService UserRoleService notificationQueueService noticequeue.Service userExternalLoginRepo user_external_login.UserExternalLoginRepo siteInfoService siteinfo_common.SiteInfoCommonService @@ -78,6 +88,7 @@ func NewNotificationCommon( activityRepo activity_common.ActivityRepo, followRepo activity_common.FollowRepo, objectInfoService *object_info.ObjService, + userRoleService *role.UserRoleRelService, notificationQueueService noticequeue.Service, userExternalLoginRepo user_external_login.UserExternalLoginRepo, siteInfoService siteinfo_common.SiteInfoCommonService, @@ -89,6 +100,7 @@ func NewNotificationCommon( followRepo: followRepo, userCommon: userCommon, objectInfoService: objectInfoService, + userRoleService: userRoleService, notificationQueueService: notificationQueueService, userExternalLoginRepo: userExternalLoginRepo, siteInfoService: siteInfoService, @@ -345,8 +357,18 @@ func (ns *NotificationCommon) SendNotificationToAllFollower(ctx context.Context, log.Error(err) return } - log.Infof("send notification to all followers: %s %d", condObjectID, len(userIDs)) - for _, userID := range userIDs { + questionInfo, err := ns.objectInfoService.GetInfo(ctx, condObjectID) + if err != nil { + log.Errorf("get question %s for follower notification failed: %v", condObjectID, err) + return + } + visibleUserIDs, err := ns.visibleFollowers(ctx, questionInfo, userIDs) + if err != nil { + log.Errorf("filter followers for question %s failed: %v", condObjectID, err) + return + } + log.Infof("send notification to visible followers: %s %d", condObjectID, len(visibleUserIDs)) + for _, userID := range visibleUserIDs { t := &schema.NotificationMsg{} _ = copier.Copy(t, msg) t.ReceiverUserID = userID @@ -356,6 +378,24 @@ func (ns *NotificationCommon) SendNotificationToAllFollower(ctx context.Context, } } +func (ns *NotificationCommon) visibleFollowers(ctx context.Context, questionInfo *schema.SimpleObjectInfo, + userIDs []string) ([]string, error) { + userRoleMapping, err := ns.userRoleService.GetUserRoleRelMapping(ctx, userIDs) + if err != nil { + return nil, err + } + visibleUserIDs := make([]string, 0, len(userIDs)) + for _, userID := range userIDs { + roleID := userRoleMapping[userID] + isAdminModerator := roleID == role.RoleAdminID || roleID == role.RoleModeratorID + if err := questionInfo.CheckVisibility(userID, isAdminModerator); err != nil { + continue + } + visibleUserIDs = append(visibleUserIDs, userID) + } + return visibleUserIDs, nil +} + func (ns *NotificationCommon) syncNotificationToPlugin(ctx context.Context, objInfo *schema.SimpleObjectInfo, msg *schema.NotificationMsg) { if objInfo == nil { diff --git a/internal/service/notification_common/notification_visibility_test.go b/internal/service/notification_common/notification_visibility_test.go new file mode 100644 index 00000000..5653ce78 --- /dev/null +++ b/internal/service/notification_common/notification_visibility_test.go @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package notificationcommon + +import ( + "context" + "reflect" + "testing" + + "github.com/apache/answer/internal/base/constant" + "github.com/apache/answer/internal/entity" + "github.com/apache/answer/internal/schema" + "github.com/apache/answer/internal/service/role" +) + +type notificationVisibilityRoleService struct{ roles map[string]int } + +func (s notificationVisibilityRoleService) GetUserRoleRelMapping(context.Context, []string) (map[string]int, error) { + return s.roles, nil +} + +func TestVisibleFollowersFiltersRestrictedQuestion(t *testing.T) { + service := &NotificationCommon{userRoleService: notificationVisibilityRoleService{roles: map[string]int{ + "moderator": role.RoleModeratorID, + }}} + question := &schema.SimpleObjectInfo{ + ObjectType: constant.QuestionObjectType, + QuestionCreatorUserID: "author", + QuestionStatus: entity.QuestionStatusAvailable, + QuestionShow: entity.QuestionHide, + } + + visible, err := service.visibleFollowers(context.Background(), question, + []string{"ordinary-user", "author", "moderator"}) + if err != nil { + t.Fatalf("visibleFollowers() error: %v", err) + } + if want := []string{"author", "moderator"}; !reflect.DeepEqual(visible, want) { + t.Fatalf("visible followers = %v, want %v", visible, want) + } +}
