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 82c8233021cc09a44db40bc51c9fec29f29aafb5 Author: LinkinStars <[email protected]> AuthorDate: Wed Aug 26 11:04:20 2026 +0800 fix(security): enforce mutation visibility checks --- internal/controller/answer_controller.go | 3 ++- internal/controller/comment_controller.go | 3 ++- internal/controller/report_controller.go | 3 ++- internal/controller/vote_controller.go | 6 ++++-- internal/schema/answer_schema.go | 23 ++++++++++++----------- internal/schema/comment_schema.go | 3 ++- internal/schema/report_schema.go | 7 ++++--- internal/schema/vote_schema.go | 11 ++++++----- internal/service/comment/comment_service.go | 3 +++ internal/service/content/answer_service.go | 8 ++++++++ internal/service/content/vote_service.go | 6 ++++++ internal/service/report/report_service.go | 3 +++ internal/service/user_admin/user_backyard.go | 1 + 13 files changed, 55 insertions(+), 25 deletions(-) diff --git a/internal/controller/answer_controller.go b/internal/controller/answer_controller.go index 6e16c6a8..58e55c11 100644 --- a/internal/controller/answer_controller.go +++ b/internal/controller/answer_controller.go @@ -208,6 +208,7 @@ func (ac *AnswerController) AddAnswer(ctx *gin.Context) { }() req.QuestionID = uid.DeShortID(req.QuestionID) req.UserID = middleware.GetLoginUserIDFromContext(ctx) + req.IsAdminModerator = middleware.GetUserIsAdminModerator(ctx) canList, err := ac.rankService.CheckOperationPermissions(ctx, req.UserID, []string{ permission.AnswerEdit, @@ -220,7 +221,7 @@ func (ac *AnswerController) AddAnswer(ctx *gin.Context) { } linkUrlLimitUser := canList[2] - isAdmin := middleware.GetUserIsAdminModerator(ctx) + isAdmin := req.IsAdminModerator if !isAdmin || !linkUrlLimitUser { captchaPass := ac.actionService.ActionRecordVerifyCaptcha(ctx, entity.CaptchaActionAnswer, req.UserID, req.CaptchaID, req.CaptchaCode) if !captchaPass { diff --git a/internal/controller/comment_controller.go b/internal/controller/comment_controller.go index e0f18a8a..4bdd8ac2 100644 --- a/internal/controller/comment_controller.go +++ b/internal/controller/comment_controller.go @@ -88,6 +88,7 @@ func (cc *CommentController) AddComment(ctx *gin.Context) { }() req.ObjectID = uid.DeShortID(req.ObjectID) req.UserID = middleware.GetLoginUserIDFromContext(ctx) + req.IsAdminModerator = middleware.GetUserIsAdminModerator(ctx) canList, err := cc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{ permission.CommentAdd, @@ -100,7 +101,7 @@ func (cc *CommentController) AddComment(ctx *gin.Context) { return } linkUrlLimitUser := canList[3] - isAdmin := middleware.GetUserIsAdminModerator(ctx) + isAdmin := req.IsAdminModerator if !isAdmin || !linkUrlLimitUser { captchaPass := cc.actionService.ActionRecordVerifyCaptcha(ctx, entity.CaptchaActionComment, req.UserID, req.CaptchaID, req.CaptchaCode) if !captchaPass { diff --git a/internal/controller/report_controller.go b/internal/controller/report_controller.go index 13b4c095..53b298ed 100644 --- a/internal/controller/report_controller.go +++ b/internal/controller/report_controller.go @@ -73,7 +73,8 @@ func (rc *ReportController) AddReport(ctx *gin.Context) { } req.ObjectID = uid.DeShortID(req.ObjectID) req.UserID = middleware.GetLoginUserIDFromContext(ctx) - isAdmin := middleware.GetUserIsAdminModerator(ctx) + req.IsAdminModerator = middleware.GetUserIsAdminModerator(ctx) + isAdmin := req.IsAdminModerator if !isAdmin { captchaPass := rc.actionService.ActionRecordVerifyCaptcha(ctx, entity.CaptchaActionReport, req.UserID, req.CaptchaID, req.CaptchaCode) if !captchaPass { diff --git a/internal/controller/vote_controller.go b/internal/controller/vote_controller.go index 30279667..10ce42a7 100644 --- a/internal/controller/vote_controller.go +++ b/internal/controller/vote_controller.go @@ -72,6 +72,7 @@ func (vc *VoteController) VoteUp(ctx *gin.Context) { } req.ObjectID = uid.DeShortID(req.ObjectID) req.UserID = middleware.GetLoginUserIDFromContext(ctx) + req.IsAdminModerator = middleware.GetUserIsAdminModerator(ctx) can, needRank, err := vc.rankService.CheckVotePermission(ctx, req.UserID, req.ObjectID, true) if err != nil { @@ -85,7 +86,7 @@ func (vc *VoteController) VoteUp(ctx *gin.Context) { return } - isAdmin := middleware.GetUserIsAdminModerator(ctx) + isAdmin := req.IsAdminModerator if !isAdmin { captchaPass := vc.actionService.ActionRecordVerifyCaptcha(ctx, entity.CaptchaActionVote, req.UserID, req.CaptchaID, req.CaptchaCode) if !captchaPass { @@ -126,7 +127,8 @@ func (vc *VoteController) VoteDown(ctx *gin.Context) { } req.ObjectID = uid.DeShortID(req.ObjectID) req.UserID = middleware.GetLoginUserIDFromContext(ctx) - isAdmin := middleware.GetUserIsAdminModerator(ctx) + req.IsAdminModerator = middleware.GetUserIsAdminModerator(ctx) + isAdmin := req.IsAdminModerator can, needRank, err := vc.rankService.CheckVotePermission(ctx, req.UserID, req.ObjectID, false) if err != nil { diff --git a/internal/schema/answer_schema.go b/internal/schema/answer_schema.go index bf80c56e..e81b8630 100644 --- a/internal/schema/answer_schema.go +++ b/internal/schema/answer_schema.go @@ -47,17 +47,18 @@ const ( ) type AnswerAddReq struct { - QuestionID string `json:"question_id"` - Content string `validate:"required,notblank,gte=6,lte=65535" json:"content"` - HTML string `json:"-"` - UserID string `json:"-"` - CanEdit bool `json:"-"` - CanDelete bool `json:"-"` - CanRecover bool `json:"-"` - CaptchaID string `json:"captcha_id"` - CaptchaCode string `json:"captcha_code"` - IP string `json:"-"` - UserAgent string `json:"-"` + QuestionID string `json:"question_id"` + Content string `validate:"required,notblank,gte=6,lte=65535" json:"content"` + HTML string `json:"-"` + UserID string `json:"-"` + IsAdminModerator bool `json:"-"` + CanEdit bool `json:"-"` + CanDelete bool `json:"-"` + CanRecover bool `json:"-"` + CaptchaID string `json:"captcha_id"` + CaptchaCode string `json:"captcha_code"` + IP string `json:"-"` + UserAgent string `json:"-"` } func (req *AnswerAddReq) Check() (errFields []*validator.FormErrorField, err error) { diff --git a/internal/schema/comment_schema.go b/internal/schema/comment_schema.go index 8297c880..0fe5ee00 100644 --- a/internal/schema/comment_schema.go +++ b/internal/schema/comment_schema.go @@ -44,7 +44,8 @@ type AddCommentReq struct { CaptchaCode string `json:"captcha_code"` // user id - UserID string `json:"-"` + UserID string `json:"-"` + IsAdminModerator bool `json:"-"` // whether user can add it CanAdd bool `json:"-"` // whether user can edit it diff --git a/internal/schema/report_schema.go b/internal/schema/report_schema.go index 1f702df4..3c1e3325 100644 --- a/internal/schema/report_schema.go +++ b/internal/schema/report_schema.go @@ -28,9 +28,10 @@ type AddReportReq struct { // report content Content string `validate:"omitempty,gt=0,lte=500" json:"content"` // user id - UserID string `json:"-"` - CaptchaID string `json:"captcha_id"` // captcha_id - CaptchaCode string `json:"captcha_code"` + UserID string `json:"-"` + IsAdminModerator bool `json:"-"` + CaptchaID string `json:"captcha_id"` // captcha_id + CaptchaCode string `json:"captcha_code"` } // GetReportListReq get report list all request diff --git a/internal/schema/vote_schema.go b/internal/schema/vote_schema.go index e82adcc2..15a54794 100644 --- a/internal/schema/vote_schema.go +++ b/internal/schema/vote_schema.go @@ -20,11 +20,12 @@ package schema type VoteReq struct { - ObjectID string `validate:"required" json:"object_id"` - IsCancel bool `validate:"omitempty" json:"is_cancel"` - CaptchaID string `json:"captcha_id"` - CaptchaCode string `json:"captcha_code"` - UserID string `json:"-"` + ObjectID string `validate:"required" json:"object_id"` + IsCancel bool `validate:"omitempty" json:"is_cancel"` + CaptchaID string `json:"captcha_id"` + CaptchaCode string `json:"captcha_code"` + UserID string `json:"-"` + IsAdminModerator bool `json:"-"` } type VoteResp struct { diff --git a/internal/service/comment/comment_service.go b/internal/service/comment/comment_service.go index 455ab864..0cbf2028 100644 --- a/internal/service/comment/comment_service.go +++ b/internal/service/comment/comment_service.go @@ -144,6 +144,9 @@ func (cs *CommentService) AddComment(ctx context.Context, req *schema.AddComment if objInfo.IsDeleted() { return nil, errors.BadRequest(reason.NewObjectAlreadyDeleted) } + if err := objInfo.CheckVisibility(req.UserID, req.IsAdminModerator); err != nil { + return nil, err + } objInfo.ObjectID = uid.DeShortID(objInfo.ObjectID) objInfo.QuestionID = uid.DeShortID(objInfo.QuestionID) objInfo.AnswerID = uid.DeShortID(objInfo.AnswerID) diff --git a/internal/service/content/answer_service.go b/internal/service/content/answer_service.go index bda7b582..e43467a5 100644 --- a/internal/service/content/answer_service.go +++ b/internal/service/content/answer_service.go @@ -258,6 +258,14 @@ func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq) ( if !exist { return "", errors.BadRequest(reason.QuestionNotFound) } + if err := (&schema.SimpleObjectInfo{ + ObjectType: constant.QuestionObjectType, + QuestionCreatorUserID: questionInfo.UserID, + QuestionStatus: questionInfo.Status, + QuestionShow: questionInfo.Show, + }).CheckVisibility(req.UserID, req.IsAdminModerator); err != nil { + return "", err + } if questionInfo.Status == entity.QuestionStatusClosed || questionInfo.Status == entity.QuestionStatusDeleted { err = errors.BadRequest(reason.AnswerCannotAddByClosedQuestion) return "", err diff --git a/internal/service/content/vote_service.go b/internal/service/content/vote_service.go index 1f74769f..04565438 100644 --- a/internal/service/content/vote_service.go +++ b/internal/service/content/vote_service.go @@ -94,6 +94,9 @@ func (vs *VoteService) VoteUp(ctx context.Context, req *schema.VoteReq) (resp *s if objectInfo.IsDeleted() { return nil, errors.BadRequest(reason.NewObjectAlreadyDeleted) } + if err := objectInfo.CheckVisibility(req.UserID, req.IsAdminModerator); err != nil { + return nil, err + } // make object id must be decoded objectInfo.ObjectID = req.ObjectID @@ -145,6 +148,9 @@ func (vs *VoteService) VoteDown(ctx context.Context, req *schema.VoteReq) (resp if objectInfo.IsDeleted() { return nil, errors.BadRequest(reason.NewObjectAlreadyDeleted) } + if err := objectInfo.CheckVisibility(req.UserID, req.IsAdminModerator); err != nil { + return nil, err + } // make object id must be decoded objectInfo.ObjectID = req.ObjectID diff --git a/internal/service/report/report_service.go b/internal/service/report/report_service.go index 3edbc1b4..c57d83e9 100644 --- a/internal/service/report/report_service.go +++ b/internal/service/report/report_service.go @@ -99,6 +99,9 @@ func (rs *ReportService) AddReport(ctx context.Context, req *schema.AddReportReq if objInfo.IsDeleted() { return errors.BadRequest(reason.NewObjectAlreadyDeleted) } + if err := objInfo.CheckVisibility(req.UserID, req.IsAdminModerator); err != nil { + return err + } cf, err := rs.configService.GetConfigByID(ctx, req.ReportType) if err != nil || cf == nil { diff --git a/internal/service/user_admin/user_backyard.go b/internal/service/user_admin/user_backyard.go index 072feec9..acbaf996 100644 --- a/internal/service/user_admin/user_backyard.go +++ b/internal/service/user_admin/user_backyard.go @@ -170,6 +170,7 @@ func (us *UserAdminService) UpdateUserStatus(ctx context.Context, req *schema.Up if err := us.revokeUserAPIKeys(ctx, userInfo.ID); err != nil { return err } + us.authService.RemoveUserAllTokens(ctx, userInfo.ID) } // remove all content that user created, such as question, answer, comment, etc.
