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 93238c79567741d5f643800f69e0f56c73535dda
Author: LinkinStars <[email protected]>
AuthorDate: Fri Oct 23 17:44:42 2026 +0800

    fix(security): protect question list visibility
---
 internal/schema/question_schema.go           |  8 ++---
 internal/schema/question_schema_test.go      | 45 ++++++++++++++++++++++++++++
 internal/service/content/question_service.go | 28 +++++++++--------
 3 files changed, 64 insertions(+), 17 deletions(-)

diff --git a/internal/schema/question_schema.go 
b/internal/schema/question_schema.go
index 6b24b550..3d4da1f7 100644
--- a/internal/schema/question_schema.go
+++ b/internal/schema/question_schema.go
@@ -373,10 +373,10 @@ type QuestionPageReq struct {
        Username  string `validate:"omitempty,gt=0,lte=100" form:"username"`
        InDays    int    `validate:"omitempty,min=1" form:"in_days"`
 
-       LoginUserID      string `json:"-"`
-       UserIDBeSearched string `json:"-"`
-       TagID            string `json:"-"`
-       ShowPending      bool   `json:"-"`
+       LoginUserID      string `json:"-" form:"-"`
+       UserIDBeSearched string `json:"-" form:"-"`
+       TagID            string `json:"-" form:"-"`
+       ShowPending      bool   `json:"-" form:"-"`
 }
 
 const (
diff --git a/internal/schema/question_schema_test.go 
b/internal/schema/question_schema_test.go
new file mode 100644
index 00000000..30009b81
--- /dev/null
+++ b/internal/schema/question_schema_test.go
@@ -0,0 +1,45 @@
+/*
+ * 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 schema
+
+import (
+       "net/http"
+       "net/http/httptest"
+       "testing"
+
+       "github.com/gin-gonic/gin"
+)
+
+func TestQuestionPageReqDoesNotBindInternalFields(t *testing.T) {
+       ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
+       ctx.Request = httptest.NewRequest(http.MethodGet,
+               
"/?page=1&page_size=20&username=author&LoginUserID=attacker&UserIDBeSearched=attacker&TagID=tag&ShowPending=true",
 nil)
+
+       var req QuestionPageReq
+       if err := ctx.ShouldBind(&req); err != nil {
+               t.Fatalf("bind QuestionPageReq: %v", err)
+       }
+       if req.Page != 1 || req.PageSize != 20 || req.Username != "author" {
+               t.Fatalf("public fields were not bound correctly: %+v", req)
+       }
+       if req.LoginUserID != "" || req.UserIDBeSearched != "" || req.TagID != 
"" || req.ShowPending {
+               t.Fatalf("internal fields must not be bound from query 
parameters: %+v", req)
+       }
+}
diff --git a/internal/service/content/question_service.go 
b/internal/service/content/question_service.go
index d20ee1ea..b8cc65aa 100644
--- a/internal/service/content/question_service.go
+++ b/internal/service/content/question_service.go
@@ -1528,18 +1528,6 @@ func (qs *QuestionService) SimilarQuestion(ctx 
context.Context, questionID strin
 func (qs *QuestionService) GetQuestionPage(ctx context.Context, req 
*schema.QuestionPageReq) (
        questions []*schema.QuestionPageResp, total int64, err error) {
        questions = make([]*schema.QuestionPageResp, 0)
-       // query by user role
-       showHidden := false
-       if req.LoginUserID != "" && req.UserIDBeSearched != "" {
-               showHidden = req.LoginUserID == req.UserIDBeSearched
-               if !showHidden {
-                       userRole, err := qs.userRoleRelService.GetUserRole(ctx, 
req.LoginUserID)
-                       if err != nil {
-                               return nil, 0, err
-                       }
-                       showHidden = userRole == role.RoleAdminID || userRole 
== role.RoleModeratorID
-               }
-       }
        // query by tag condition
        var tagIDs = make([]string, 0)
        if len(req.Tag) > 0 {
@@ -1559,7 +1547,8 @@ func (qs *QuestionService) GetQuestionPage(ctx 
context.Context, req *schema.Ques
                }
        }
 
-       // query by user condition
+       // Determine the target user before evaluating whether the caller may 
view
+       // that user's hidden questions.
        if req.Username != "" {
                userinfo, exist, err := 
qs.userCommon.GetUserBasicInfoByUserName(ctx, req.Username)
                if err != nil {
@@ -1571,6 +1560,19 @@ func (qs *QuestionService) GetQuestionPage(ctx 
context.Context, req *schema.Ques
                req.UserIDBeSearched = userinfo.ID
        }
 
+       // query by user role
+       showHidden := false
+       if req.LoginUserID != "" && req.UserIDBeSearched != "" {
+               showHidden = req.LoginUserID == req.UserIDBeSearched
+               if !showHidden {
+                       userRole, err := qs.userRoleRelService.GetUserRole(ctx, 
req.LoginUserID)
+                       if err != nil {
+                               return nil, 0, err
+                       }
+                       showHidden = userRole == role.RoleAdminID || userRole 
== role.RoleModeratorID
+               }
+       }
+
        if req.OrderCond == schema.QuestionOrderCondHot {
                req.InDays = schema.HotInDays
        }

Reply via email to