This is an automated email from the ASF dual-hosted git repository. LinkinStars pushed a commit to branch dev in repository https://gitbox.apache.org/repos/asf/answer.git
commit 625ead8f6bed5c8bee94189cec6ddb0d6f51065b Author: LinkinStars <[email protected]> AuthorDate: Mon Aug 17 10:28:49 2026 +0800 fix: enforce question access controls --- internal/controller/question_controller.go | 42 +++++++++----- internal/service/content/question_service.go | 28 +++++++--- .../content/question_service_visibility_test.go | 65 ++++++++++++++++++++++ 3 files changed, 113 insertions(+), 22 deletions(-) diff --git a/internal/controller/question_controller.go b/internal/controller/question_controller.go index c83fd578..66d7b221 100644 --- a/internal/controller/question_controller.go +++ b/internal/controller/question_controller.go @@ -242,6 +242,24 @@ func (qc *QuestionController) GetQuestion(ctx *gin.Context) { id := ctx.Query("id") id = uid.DeShortID(id) userID := middleware.GetLoginUserIDFromContext(ctx) + req, err := qc.questionPermission(ctx, userID, id) + if err != nil { + handler.HandleResponse(ctx, err, nil) + return + } + + info, err := qc.questionService.GetQuestionAndAddPV(ctx, id, userID, req) + if err != nil { + handler.HandleResponse(ctx, err, nil) + return + } + if handler.GetEnableShortID(ctx) { + info.ID = uid.EnShortID(info.ID) + } + handler.HandleResponse(ctx, nil, info) +} + +func (qc *QuestionController) questionPermission(ctx *gin.Context, userID, questionID string) (schema.QuestionPermission, error) { req := schema.QuestionPermission{} req.IsAdminModerator = middleware.GetUserIsAdminModerator(ctx) canList, err := qc.rankService.CheckOperationPermissions(ctx, userID, []string{ @@ -257,10 +275,9 @@ func (qc *QuestionController) GetQuestion(ctx *gin.Context) { permission.QuestionUnDelete, }) if err != nil { - handler.HandleResponse(ctx, err, nil) - return + return req, err } - objectOwner := qc.rankService.CheckOperationObjectOwner(ctx, userID, id) + objectOwner := qc.rankService.CheckOperationObjectOwner(ctx, userID, questionID) req.CanEdit = canList[0] || objectOwner req.CanDelete = canList[1] @@ -272,16 +289,7 @@ func (qc *QuestionController) GetQuestion(ctx *gin.Context) { req.CanShow = canList[7] req.CanInviteOtherToAnswer = canList[8] req.CanRecover = canList[9] - - info, err := qc.questionService.GetQuestionAndAddPV(ctx, id, userID, req) - if err != nil { - handler.HandleResponse(ctx, err, nil) - return - } - if handler.GetEnableShortID(ctx) { - info.ID = uid.EnShortID(info.ID) - } - handler.HandleResponse(ctx, nil, info) + return req, nil } // GetQuestionInviteUserInfo get question invite user info @@ -295,7 +303,13 @@ func (qc *QuestionController) GetQuestion(ctx *gin.Context) { // @Router /answer/api/v1/question/invite [get] func (qc *QuestionController) GetQuestionInviteUserInfo(ctx *gin.Context) { questionID := uid.DeShortID(ctx.Query("id")) - resp, err := qc.questionService.InviteUserInfo(ctx, questionID) + userID := middleware.GetLoginUserIDFromContext(ctx) + per, err := qc.questionPermission(ctx, userID, questionID) + if err != nil { + handler.HandleResponse(ctx, err, nil) + return + } + resp, err := qc.questionService.InviteUserInfo(ctx, questionID, userID, per) handler.HandleResponse(ctx, err, resp) } diff --git a/internal/service/content/question_service.go b/internal/service/content/question_service.go index b172018b..f7899566 100644 --- a/internal/service/content/question_service.go +++ b/internal/service/content/question_service.go @@ -1091,13 +1091,8 @@ func (qs *QuestionService) GetQuestion(ctx context.Context, questionID, userID s if err != nil { return } - // If the question is deleted or pending, only the administrator and the author can view it - if (question.Status == entity.QuestionStatusDeleted || - question.Status == entity.QuestionStatusPending) && !per.CanReopen && question.UserID != userID { - return nil, errors.NotFound(reason.QuestionNotFound) - } - if question.Show == entity.QuestionHide && !per.IsAdminModerator && question.UserID != userID { - return nil, errors.NotFound(reason.QuestionNotFound) + if err = checkQuestionVisibility(question, userID, per); err != nil { + return nil, err } if question.Status != entity.QuestionStatusClosed { per.CanReopen = false @@ -1142,6 +1137,19 @@ func (qs *QuestionService) GetQuestion(ctx context.Context, questionID, userID s return question, nil } +func checkQuestionVisibility(question *schema.QuestionInfoResp, userID string, per schema.QuestionPermission) error { + // Deleted and pending questions are visible only to their author or users who can reopen them. + if (question.Status == entity.QuestionStatusDeleted || + question.Status == entity.QuestionStatusPending) && !per.CanReopen && question.UserID != userID { + return errors.NotFound(reason.QuestionNotFound) + } + // Hidden questions are visible only to their author or an administrator/moderator. + if question.Show == entity.QuestionHide && !per.IsAdminModerator && question.UserID != userID { + return errors.NotFound(reason.QuestionNotFound) + } + return nil +} + // GetQuestionAndAddPV get question one func (qs *QuestionService) GetQuestionAndAddPV(ctx context.Context, questionID, loginUserID string, per schema.QuestionPermission) ( @@ -1153,7 +1161,11 @@ func (qs *QuestionService) GetQuestionAndAddPV(ctx context.Context, questionID, return qs.GetQuestion(ctx, questionID, loginUserID, per) } -func (qs *QuestionService) InviteUserInfo(ctx context.Context, questionID string) (inviteList []*schema.UserBasicInfo, err error) { +func (qs *QuestionService) InviteUserInfo(ctx context.Context, questionID, userID string, + per schema.QuestionPermission) (inviteList []*schema.UserBasicInfo, err error) { + if _, err = qs.GetQuestion(ctx, questionID, userID, per); err != nil { + return nil, err + } return qs.questioncommon.InviteUserInfo(ctx, questionID) } diff --git a/internal/service/content/question_service_visibility_test.go b/internal/service/content/question_service_visibility_test.go new file mode 100644 index 00000000..9e10b47b --- /dev/null +++ b/internal/service/content/question_service_visibility_test.go @@ -0,0 +1,65 @@ +/* + * 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 content + +import ( + "testing" + + "github.com/apache/answer/internal/entity" + "github.com/apache/answer/internal/schema" +) + +func TestCheckQuestionVisibility(t *testing.T) { + testCases := []struct { + name string + status int + show int + userID string + viewer string + per schema.QuestionPermission + allow bool + }{ + {"public question", entity.QuestionStatusAvailable, entity.QuestionShow, "author", "", schema.QuestionPermission{}, true}, + {"pending question anonymous", entity.QuestionStatusPending, entity.QuestionShow, "author", "", schema.QuestionPermission{}, false}, + {"pending question author", entity.QuestionStatusPending, entity.QuestionShow, "author", "author", schema.QuestionPermission{}, true}, + {"pending question reviewer", entity.QuestionStatusPending, entity.QuestionShow, "author", "reviewer", schema.QuestionPermission{CanReopen: true}, true}, + {"deleted question anonymous", entity.QuestionStatusDeleted, entity.QuestionShow, "author", "", schema.QuestionPermission{}, false}, + {"hidden question anonymous", entity.QuestionStatusAvailable, entity.QuestionHide, "author", "", schema.QuestionPermission{}, false}, + {"hidden question author", entity.QuestionStatusAvailable, entity.QuestionHide, "author", "author", schema.QuestionPermission{}, true}, + {"hidden question moderator", entity.QuestionStatusAvailable, entity.QuestionHide, "author", "moderator", schema.QuestionPermission{IsAdminModerator: true}, true}, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + question := &schema.QuestionInfoResp{ + Status: testCase.status, + Show: testCase.show, + UserID: testCase.userID, + } + err := checkQuestionVisibility(question, testCase.viewer, testCase.per) + if testCase.allow && err != nil { + t.Fatalf("visibility unexpectedly denied: %v", err) + } + if !testCase.allow && err == nil { + t.Fatal("visibility unexpectedly allowed") + } + }) + } +}
