This is an automated email from the ASF dual-hosted git repository.
LinkinStars pushed a commit to branch fix/2.0.3/user
in repository https://gitbox.apache.org/repos/asf/answer.git
The following commit(s) were added to refs/heads/fix/2.0.3/user by this push:
new 22867b41 fix(security): enforce reaction content visibility
22867b41 is described below
commit 22867b411c1a802f0a8f9dae79a07ab0289c37fd
Author: LinkinStars <[email protected]>
AuthorDate: Tue Sep 15 10:11:37 2026 +0800
fix(security): enforce reaction content visibility
---
cmd/wire_gen.go | 2 +-
internal/controller/meta_controller.go | 2 +
internal/schema/meta_schema.go | 14 +--
internal/service/meta/meta_service.go | 22 +++++
.../service/meta/meta_service_visibility_test.go | 103 +++++++++++++++++++++
5 files changed, 136 insertions(+), 7 deletions(-)
diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go
index 446f6cc0..66e16278 100644
--- a/cmd/wire_gen.go
+++ b/cmd/wire_gen.go
@@ -274,7 +274,7 @@ func initApplication(debug bool, serverConf *conf.Server,
dbConf *data.Database,
permissionController := controller.NewPermissionController(rankService)
userPluginController :=
controller.NewUserPluginController(pluginCommonService)
reviewController := controller.NewReviewController(reviewService,
rankService, captchaService)
- metaService := meta2.NewMetaService(metaCommonService, userCommon,
answerRepo, questionRepo, eventqueueService)
+ metaService := meta2.NewMetaService(metaCommonService, userCommon,
answerRepo, questionRepo, objService, eventqueueService)
metaController := controller.NewMetaController(metaService)
badgeGroupRepo := badge_group.NewBadgeGroupRepo(dataData, uniqueIDRepo)
eventRuleRepo := badge.NewEventRuleRepo(dataData)
diff --git a/internal/controller/meta_controller.go
b/internal/controller/meta_controller.go
index 624daf09..2bce5eb1 100644
--- a/internal/controller/meta_controller.go
+++ b/internal/controller/meta_controller.go
@@ -57,6 +57,7 @@ func (mc *MetaController) AddOrUpdateReaction(ctx
*gin.Context) {
}
req.ObjectID = uid.DeShortID(req.ObjectID)
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
+ req.IsAdminModerator = middleware.GetUserIsAdminModerator(ctx)
resp, err := mc.metaService.AddOrUpdateReaction(ctx, req)
handler.HandleResponse(ctx, err, resp)
@@ -78,6 +79,7 @@ func (mc *MetaController) GetReaction(ctx *gin.Context) {
}
req.ObjectID = uid.DeShortID(req.ObjectID)
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
+ req.IsAdminModerator = middleware.GetUserIsAdminModerator(ctx)
resp, err := mc.metaService.GetReactionByObjectId(ctx, req)
handler.HandleResponse(ctx, err, resp)
diff --git a/internal/schema/meta_schema.go b/internal/schema/meta_schema.go
index e5a07252..64362879 100644
--- a/internal/schema/meta_schema.go
+++ b/internal/schema/meta_schema.go
@@ -22,15 +22,17 @@ package schema
import "slices"
type UpdateReactionReq struct {
- ObjectID string `validate:"required" json:"object_id"`
- Emoji string `validate:"required,oneof=heart smile frown"
json:"emoji"`
- Reaction string `validate:"required,oneof=activate deactivate"
json:"reaction"`
- UserID string `json:"-"`
+ ObjectID string `validate:"required" json:"object_id"`
+ Emoji string `validate:"required,oneof=heart smile frown"
json:"emoji"`
+ Reaction string `validate:"required,oneof=activate deactivate"
json:"reaction"`
+ UserID string `json:"-"`
+ IsAdminModerator bool `json:"-"`
}
type GetReactionReq struct {
- ObjectID string `validate:"required" form:"object_id"`
- UserID string `json:"-"`
+ ObjectID string `validate:"required" form:"object_id"`
+ UserID string `json:"-"`
+ IsAdminModerator bool `json:"-"`
}
// ReactionsSummaryMeta reactions summary meta
diff --git a/internal/service/meta/meta_service.go
b/internal/service/meta/meta_service.go
index e48e8f46..070c04a8 100644
--- a/internal/service/meta/meta_service.go
+++ b/internal/service/meta/meta_service.go
@@ -36,18 +36,24 @@ import (
"github.com/apache/answer/internal/schema"
answercommon "github.com/apache/answer/internal/service/answer_common"
metacommon "github.com/apache/answer/internal/service/meta_common"
+ "github.com/apache/answer/internal/service/object_info"
questioncommon
"github.com/apache/answer/internal/service/question_common"
usercommon "github.com/apache/answer/internal/service/user_common"
"github.com/apache/answer/pkg/obj"
myErrors "github.com/segmentfault/pacman/errors"
)
+type objectInfoService interface {
+ GetInfo(ctx context.Context, objectID string) (objInfo
*schema.SimpleObjectInfo, err error)
+}
+
// MetaService user service
type MetaService struct {
metaCommonService *metacommon.MetaCommonService
userCommon *usercommon.UserCommon
questionRepo questioncommon.QuestionRepo
answerRepo answercommon.AnswerRepo
+ objectInfoService objectInfoService
eventQueueService eventqueue.Service
}
@@ -56,6 +62,7 @@ func NewMetaService(
userCommon *usercommon.UserCommon,
answerRepo answercommon.AnswerRepo,
questionRepo questioncommon.QuestionRepo,
+ objectInfoService *object_info.ObjService,
eventQueueService eventqueue.Service,
) *MetaService {
return &MetaService{
@@ -63,12 +70,16 @@ func NewMetaService(
questionRepo: questionRepo,
userCommon: userCommon,
answerRepo: answerRepo,
+ objectInfoService: objectInfoService,
eventQueueService: eventQueueService,
}
}
// GetReactionByObjectId get reaction
func (ms *MetaService) GetReactionByObjectId(ctx context.Context, req
*schema.GetReactionReq) (resp *schema.GetReactionByObjectIdResp, err error) {
+ if err := ms.checkReactionVisibility(ctx, req.ObjectID, req.UserID,
req.IsAdminModerator); err != nil {
+ return nil, err
+ }
reactionMeta, err := ms.metaCommonService.GetMetaByObjectIdAndKey(ctx,
req.ObjectID, entity.ObjectReactSummaryKey)
// if not exist, return nil
@@ -91,6 +102,9 @@ func (ms *MetaService) GetReactionByObjectId(ctx
context.Context, req *schema.Ge
// AddOrUpdateReaction add or update reaction
func (ms *MetaService) AddOrUpdateReaction(ctx context.Context, req
*schema.UpdateReactionReq) (resp *schema.GetReactionByObjectIdResp, err error) {
+ if err := ms.checkReactionVisibility(ctx, req.ObjectID, req.UserID,
req.IsAdminModerator); err != nil {
+ return nil, err
+ }
// check if object exist and it's answer or question
objectType, err := obj.GetObjectTypeStrByObjectID(req.ObjectID)
if err != nil {
@@ -158,6 +172,14 @@ func (ms *MetaService) AddOrUpdateReaction(ctx
context.Context, req *schema.Upda
return resp, nil
}
+func (ms *MetaService) checkReactionVisibility(ctx context.Context, objectID,
userID string, isAdminModerator bool) error {
+ objectInfo, err := ms.objectInfoService.GetInfo(ctx, objectID)
+ if err != nil {
+ return err
+ }
+ return objectInfo.CheckVisibility(userID, isAdminModerator)
+}
+
// updateReaction update reaction
func (ms *MetaService) updateReaction(req *schema.UpdateReactionReq, reactions
*schema.ReactionsSummaryMeta) {
switch req.Reaction {
diff --git a/internal/service/meta/meta_service_visibility_test.go
b/internal/service/meta/meta_service_visibility_test.go
new file mode 100644
index 00000000..a3cbbdbb
--- /dev/null
+++ b/internal/service/meta/meta_service_visibility_test.go
@@ -0,0 +1,103 @@
+/*
+ * 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 meta
+
+import (
+ "context"
+ "testing"
+
+ "github.com/apache/answer/internal/base/constant"
+ "github.com/apache/answer/internal/entity"
+ "github.com/apache/answer/internal/schema"
+)
+
+func TestReactionVisibility(t *testing.T) {
+ tests := []struct {
+ name string
+ objectInfo *schema.SimpleObjectInfo
+ userID string
+ isAdminModerator bool
+ allowed bool
+ }{
+ {
+ name: "anonymous cannot access hidden question",
+ objectInfo: &schema.SimpleObjectInfo{ObjectType:
constant.QuestionObjectType, QuestionCreatorUserID: "question-owner",
QuestionShow: entity.QuestionHide},
+ },
+ {
+ name: "non-owner cannot access pending question",
+ objectInfo: &schema.SimpleObjectInfo{ObjectType:
constant.QuestionObjectType, QuestionCreatorUserID: "question-owner",
QuestionStatus: entity.QuestionStatusPending, QuestionShow:
entity.QuestionShow},
+ userID: "other-user",
+ },
+ {
+ name: "non-owner cannot access deleted answer",
+ objectInfo: &schema.SimpleObjectInfo{ObjectType:
constant.AnswerObjectType, ObjectCreatorUserID: "answer-owner",
QuestionCreatorUserID: "question-owner", AnswerStatus:
entity.AnswerStatusDeleted, QuestionShow: entity.QuestionShow},
+ userID: "other-user",
+ },
+ {
+ name: "non-owner cannot access answer on hidden
question",
+ objectInfo: &schema.SimpleObjectInfo{ObjectType:
constant.AnswerObjectType, ObjectCreatorUserID: "answer-owner", QuestionID:
"question-id", QuestionCreatorUserID: "question-owner", AnswerStatus:
entity.AnswerStatusAvailable, QuestionShow: entity.QuestionHide},
+ userID: "other-user",
+ },
+ {
+ name: "question owner can access hidden question",
+ objectInfo: &schema.SimpleObjectInfo{ObjectType:
constant.QuestionObjectType, QuestionCreatorUserID: "question-owner",
QuestionShow: entity.QuestionHide},
+ userID: "question-owner",
+ allowed: true,
+ },
+ {
+ name: "moderator can access deleted answer",
+ objectInfo: &schema.SimpleObjectInfo{ObjectType:
constant.AnswerObjectType, ObjectCreatorUserID: "answer-owner",
QuestionCreatorUserID: "question-owner", AnswerStatus:
entity.AnswerStatusDeleted, QuestionShow: entity.QuestionShow},
+ isAdminModerator: true,
+ allowed: true,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ service := &MetaService{objectInfoService:
metaTestObjectInfoService{objectInfo: test.objectInfo}}
+ err :=
service.checkReactionVisibility(context.Background(), "object-id", test.userID,
test.isAdminModerator)
+ if (err == nil) != test.allowed {
+ t.Fatalf("visibility error = %v, allowed = %v",
err, test.allowed)
+ }
+ })
+ }
+}
+
+func TestReactionOperationsRejectRestrictedObject(t *testing.T) {
+ service := &MetaService{objectInfoService: metaTestObjectInfoService{
+ objectInfo: &schema.SimpleObjectInfo{ObjectType:
constant.QuestionObjectType, QuestionCreatorUserID: "question-owner",
QuestionShow: entity.QuestionHide},
+ }}
+
+ if _, err := service.GetReactionByObjectId(context.Background(),
&schema.GetReactionReq{ObjectID: "question-id"}); err == nil {
+ t.Fatal("anonymous read of a hidden question reaction was
allowed")
+ }
+ if _, err := service.AddOrUpdateReaction(context.Background(),
&schema.UpdateReactionReq{ObjectID: "question-id", UserID: "other-user"}); err
== nil {
+ t.Fatal("non-owner write to a hidden question reaction was
allowed")
+ }
+}
+
+type metaTestObjectInfoService struct {
+ objectInfo *schema.SimpleObjectInfo
+ err error
+}
+
+func (s metaTestObjectInfoService) GetInfo(context.Context, string)
(*schema.SimpleObjectInfo, error) {
+ return s.objectInfo, s.err
+}