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 38aa122bd1f8442d7a81f44ee2c98f13da602db4
Author: LinkinStars <[email protected]>
AuthorDate: Tue Nov 3 12:22:30 2026 +0800

    fix(security): enforce search result visibility
---
 internal/repo/answer/answer_repo.go                |  19 +++
 internal/repo/question/question_repo.go            | 134 +++++++++++++++++++-
 internal/repo/search_common/search_repo.go         |   8 +-
 internal/repo/search_common/search_repo_test.go    |  61 +++++++++
 internal/repo/search_sync/search_sync.go           |  81 +++++++++++-
 internal/repo/search_sync/search_sync_test.go      | 141 +++++++++++++++++++++
 .../service/plugin_common/plugin_common_service.go |  21 ++-
 7 files changed, 456 insertions(+), 9 deletions(-)

diff --git a/internal/repo/answer/answer_repo.go 
b/internal/repo/answer/answer_repo.go
index 40f78b3a..19720f25 100644
--- a/internal/repo/answer/answer_repo.go
+++ b/internal/repo/answer/answer_repo.go
@@ -505,6 +505,10 @@ func (ar *answerRepo) updateSearch(ctx context.Context, 
answerID string) (err er
        if !exist {
                return
        }
+       if answer.Status != entity.AnswerStatusAvailable || question.Show != 
entity.QuestionShow ||
+               (question.Status != entity.QuestionStatusAvailable && 
question.Status != entity.QuestionStatusClosed) {
+               return s.DeleteContent(ctx, answerID)
+       }
 
        // get tags
        var (
@@ -553,6 +557,9 @@ func (ar *answerRepo) DeletePermanentlyAnswers(ctx 
context.Context) error {
        if len(ids) == 0 {
                return nil
        }
+       for _, id := range ids {
+               _ = ar.deleteSearchAnswerDocument(ctx, id)
+       }
 
        // delete all revisions permanently
        _, err = ar.data.DB.Context(ctx).In("object_id", 
ids).Delete(&entity.Revision{})
@@ -566,3 +573,15 @@ func (ar *answerRepo) DeletePermanentlyAnswers(ctx 
context.Context) error {
        }
        return nil
 }
+
+func (ar *answerRepo) deleteSearchAnswerDocument(ctx context.Context, answerID 
string) error {
+       var search plugin.Search
+       _ = plugin.CallSearch(func(s plugin.Search) error {
+               search = s
+               return nil
+       })
+       if search == nil {
+               return nil
+       }
+       return search.DeleteContent(ctx, uid.DeShortID(answerID))
+}
diff --git a/internal/repo/question/question_repo.go 
b/internal/repo/question/question_repo.go
index 379b4380..6ab141ab 100644
--- a/internal/repo/question/question_repo.go
+++ b/internal/repo/question/question_repo.go
@@ -81,6 +81,7 @@ func (qr *questionRepo) AddQuestion(ctx context.Context, 
question *entity.Questi
 // RemoveQuestion delete question
 func (qr *questionRepo) RemoveQuestion(ctx context.Context, id string) (err 
error) {
        id = uid.DeShortID(id)
+       _ = qr.deleteSearchQuestionDocuments(ctx, id)
        _, err = qr.data.DB.Context(ctx).Where("id =?", 
id).Delete(&entity.Question{})
        if err != nil {
                err = 
errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
@@ -153,7 +154,7 @@ func (qr *questionRepo) UpdateQuestionStatus(ctx 
context.Context, questionID str
        if err != nil {
                return 
errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
        }
-       _ = qr.UpdateSearch(ctx, questionID)
+       _ = qr.syncSearchQuestionDocuments(ctx, questionID)
        return nil
 }
 
@@ -163,7 +164,7 @@ func (qr *questionRepo) 
UpdateQuestionStatusWithOutUpdateTime(ctx context.Contex
        if err != nil {
                return 
errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
        }
-       _ = qr.UpdateSearch(ctx, question.ID)
+       _ = qr.syncSearchQuestionDocuments(ctx, question.ID)
        return nil
 }
 
@@ -178,6 +179,9 @@ func (qr *questionRepo) DeletePermanentlyQuestions(ctx 
context.Context) (err err
        if len(ids) == 0 {
                return nil
        }
+       for _, id := range ids {
+               _ = qr.deleteSearchQuestionDocuments(ctx, id)
+       }
 
        // delete all revisions permanently
        _, err = qr.data.DB.Context(ctx).In("object_id", 
ids).Delete(&entity.Revision{})
@@ -198,7 +202,7 @@ func (qr *questionRepo) RecoverQuestion(ctx 
context.Context, questionID string)
        if err != nil {
                return 
errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
        }
-       _ = qr.UpdateSearch(ctx, questionID)
+       _ = qr.syncSearchQuestionDocuments(ctx, questionID)
        return nil
 }
 
@@ -208,6 +212,7 @@ func (qr *questionRepo) UpdateQuestionOperation(ctx 
context.Context, question *e
        if err != nil {
                return 
errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
        }
+       _ = qr.syncSearchQuestionDocuments(ctx, question.ID)
        return nil
 }
 
@@ -599,6 +604,9 @@ func (qr *questionRepo) UpdateSearch(ctx context.Context, 
questionID string) (er
        if err != nil {
                return err
        }
+       if !isSearchPublicQuestion(question) {
+               return s.DeleteContent(ctx, questionID)
+       }
 
        // get tags
        var (
@@ -634,6 +642,124 @@ func (qr *questionRepo) UpdateSearch(ctx context.Context, 
questionID string) (er
        return
 }
 
+// syncSearchQuestionDocuments keeps question and answer search documents in
+// step with a question visibility or status transition. Search indexes are not
+// an authorization source, so restricted content must be removed eagerly.
+func (qr *questionRepo) syncSearchQuestionDocuments(ctx context.Context, 
questionID string) error {
+       var search plugin.Search
+       _ = plugin.CallSearch(func(s plugin.Search) error {
+               search = s
+               return nil
+       })
+       if search == nil {
+               return nil
+       }
+
+       questionID = uid.DeShortID(questionID)
+       question, exist, err := qr.GetQuestion(ctx, questionID)
+       if err != nil || !exist {
+               return err
+       }
+       questionID = uid.DeShortID(question.ID)
+
+       answers := make([]*entity.Answer, 0)
+       if err := qr.data.DB.Context(ctx).Where("question_id = ?", 
questionID).Find(&answers); err != nil {
+               return 
errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+       }
+
+       if !isSearchPublicQuestion(question) {
+               if err := search.DeleteContent(ctx, questionID); err != nil {
+                       return err
+               }
+               for _, answer := range answers {
+                       if err := search.DeleteContent(ctx, 
uid.DeShortID(answer.ID)); err != nil {
+                               return err
+                       }
+               }
+               return nil
+       }
+
+       if err := qr.UpdateSearch(ctx, questionID); err != nil {
+               return err
+       }
+
+       tags, err := qr.getSearchTags(ctx, questionID)
+       if err != nil {
+               return err
+       }
+       for _, answer := range answers {
+               answerID := uid.DeShortID(answer.ID)
+               if answer.Status != entity.AnswerStatusAvailable {
+                       if err := search.DeleteContent(ctx, answerID); err != 
nil {
+                               return err
+                       }
+                       continue
+               }
+               if err := search.UpdateContent(ctx, &plugin.SearchContent{
+                       ObjectID:    answerID,
+                       Title:       question.Title,
+                       Type:        constant.AnswerObjectType,
+                       Content:     answer.OriginalText,
+                       Status:      plugin.SearchContentStatus(answer.Status),
+                       Tags:        tags,
+                       QuestionID:  questionID,
+                       UserID:      answer.UserID,
+                       Views:       int64(question.ViewCount),
+                       Created:     answer.CreatedAt.Unix(),
+                       Active:      answer.UpdatedAt.Unix(),
+                       Score:       int64(answer.VoteCount),
+                       HasAccepted: answer.Accepted == 
schema.AnswerAcceptedEnable,
+               }); err != nil {
+                       return err
+               }
+       }
+       return nil
+}
+
+func (qr *questionRepo) deleteSearchQuestionDocuments(ctx context.Context, 
questionID string) error {
+       var search plugin.Search
+       _ = plugin.CallSearch(func(s plugin.Search) error {
+               search = s
+               return nil
+       })
+       if search == nil {
+               return nil
+       }
+
+       questionID = uid.DeShortID(questionID)
+       if err := search.DeleteContent(ctx, questionID); err != nil {
+               return err
+       }
+       answers := make([]*entity.Answer, 0)
+       if err := qr.data.DB.Context(ctx).Where("question_id = ?", 
questionID).Find(&answers); err != nil {
+               return 
errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+       }
+       for _, answer := range answers {
+               if err := search.DeleteContent(ctx, uid.DeShortID(answer.ID)); 
err != nil {
+                       return err
+               }
+       }
+       return nil
+}
+
+func (qr *questionRepo) getSearchTags(ctx context.Context, questionID string) 
([]string, error) {
+       tagList := make([]*entity.TagRel, 0)
+       if err := qr.data.DB.Context(ctx).Where("object_id = ? AND status = ?", 
questionID,
+               entity.TagRelStatusAvailable).Find(&tagList); err != nil {
+               return nil, 
errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+       }
+       tags := make([]string, 0, len(tagList))
+       for _, tag := range tagList {
+               tags = append(tags, tag.TagID)
+       }
+       return tags, nil
+}
+
+func isSearchPublicQuestion(question *entity.Question) bool {
+       return question != nil && question.Show == entity.QuestionShow &&
+               (question.Status == entity.QuestionStatusAvailable || 
question.Status == entity.QuestionStatusClosed)
+}
+
 func (qr *questionRepo) RemoveAllUserQuestion(ctx context.Context, userID 
string) (err error) {
        // get all question id that need to be deleted
        questionIDs := make([]string, 0)
@@ -662,7 +788,7 @@ func (qr *questionRepo) RemoveAllUserQuestion(ctx 
context.Context, userID string
 
        // update search content
        for _, id := range questionIDs {
-               _ = qr.UpdateSearch(ctx, id)
+               _ = qr.syncSearchQuestionDocuments(ctx, id)
        }
        return nil
 }
diff --git a/internal/repo/search_common/search_repo.go 
b/internal/repo/search_common/search_repo.go
index a56b8edb..b4f78808 100644
--- a/internal/repo/search_common/search_repo.go
+++ b/internal/repo/search_common/search_repo.go
@@ -474,12 +474,15 @@ func (sr *searchRepo) ParseSearchPluginResult(ctx 
context.Context, sres []plugin
                switch r.Type {
                case "question":
                        b = 
builder.MySQL().Select(qFields...).From("question").Where(builder.Eq{"id": 
r.ID}).
-                               And(builder.Lt{"`status`": 
entity.QuestionStatusDeleted})
+                               And(builder.Lt{"`status`": 
entity.QuestionStatusDeleted}).
+                               And(builder.Eq{"`show`": entity.QuestionShow})
                case "answer":
                        b = 
builder.MySQL().Select(aFields...).From("answer").LeftJoin("`question`", 
"`question`.`id` = `answer`.`question_id`").
                                Where(builder.Eq{"`answer`.`id`": r.ID}).
                                And(builder.Lt{"`question`.`status`": 
entity.QuestionStatusDeleted}).
                                And(builder.Lt{"`answer`.`status`": 
entity.AnswerStatusDeleted}).And(builder.Eq{"`question`.`show`": 
entity.QuestionShow})
+               default:
+                       continue
                }
                qres, err = sr.data.DB.Context(ctx).Query(b)
                if err != nil || len(qres) == 0 {
@@ -495,6 +498,9 @@ func (sr *searchRepo) parseResult(ctx context.Context, res 
[]map[string][]byte,
        questionIDs := make([]string, 0)
        userIDs := make([]string, 0)
        resultList := make([]*schema.SearchResult, 0)
+       if len(res) == 0 {
+               return resultList, nil
+       }
        for _, r := range res {
                questionIDs = append(questionIDs, string(r["question_id"]))
                userIDs = append(userIDs, string(r["user_id"]))
diff --git a/internal/repo/search_common/search_repo_test.go 
b/internal/repo/search_common/search_repo_test.go
new file mode 100644
index 00000000..c43ab738
--- /dev/null
+++ b/internal/repo/search_common/search_repo_test.go
@@ -0,0 +1,61 @@
+/*
+ * 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 search_common
+
+import (
+       "context"
+       "testing"
+
+       "github.com/apache/answer/internal/base/data"
+       "github.com/apache/answer/internal/entity"
+       "github.com/apache/answer/plugin"
+)
+
+func TestParseSearchPluginResultExcludesHiddenQuestions(t *testing.T) {
+       ctx := context.Background()
+       db, err := data.NewDB(false, &data.Database{Driver: "sqlite", 
Connection: ":memory:"})
+       if err != nil {
+               t.Fatalf("create SQLite database: %v", err)
+       }
+       t.Cleanup(func() { _ = db.Close() })
+       if err := db.Sync2(new(entity.Question)); err != nil {
+               t.Fatalf("create question table: %v", err)
+       }
+
+       hidden := &entity.Question{
+               ID:     "10010000000000001",
+               Status: entity.QuestionStatusAvailable,
+               Show:   entity.QuestionHide,
+       }
+       if _, err := db.Context(ctx).Insert(hidden); err != nil {
+               t.Fatalf("insert hidden question: %v", err)
+       }
+
+       repo := &searchRepo{data: &data.Data{DB: db}}
+       result, err := repo.ParseSearchPluginResult(ctx, []plugin.SearchResult{{
+               ID: hidden.ID, Type: "question",
+       }}, []string{"confidential"})
+       if err != nil {
+               t.Fatalf("parse search result: %v", err)
+       }
+       if len(result) != 0 {
+               t.Fatalf("hidden question appeared in search results: %#v", 
result)
+       }
+}
diff --git a/internal/repo/search_sync/search_sync.go 
b/internal/repo/search_sync/search_sync.go
index 889ffe5b..7d81363e 100644
--- a/internal/repo/search_sync/search_sync.go
+++ b/internal/repo/search_sync/search_sync.go
@@ -21,6 +21,7 @@ package search_sync
 
 import (
        "context"
+       "fmt"
 
        "github.com/apache/answer/internal/base/constant"
        "github.com/apache/answer/internal/base/data"
@@ -31,6 +32,8 @@ import (
        "github.com/segmentfault/pacman/log"
 )
 
+const cleanupPageSize = 500
+
 func NewPluginSyncer(data *data.Data) plugin.SearchSyncer {
        return &PluginSyncer{data: data}
 }
@@ -43,7 +46,12 @@ func (p *PluginSyncer) GetAnswersPage(ctx context.Context, 
page, pageSize int) (
        answerList []*plugin.SearchContent, err error) {
        answers := make([]*entity.Answer, 0)
        startNum := (page - 1) * pageSize
-       err = p.data.DB.Context(ctx).Limit(pageSize, startNum).Find(&answers)
+       err = p.data.DB.Context(ctx).
+               Join("INNER", "question", "answer.question_id = question.id").
+               Where("answer.status = ? AND question.show = ? AND 
question.status IN (?, ?)",
+                       entity.AnswerStatusAvailable, entity.QuestionShow,
+                       entity.QuestionStatusAvailable, 
entity.QuestionStatusClosed).
+               OrderBy("answer.id ASC").Limit(pageSize, 
startNum).Find(&answers)
        if err != nil {
                return nil, err
        }
@@ -54,7 +62,10 @@ func (p *PluginSyncer) GetQuestionsPage(ctx context.Context, 
page, pageSize int)
        questionList []*plugin.SearchContent, err error) {
        questions := make([]*entity.Question, 0)
        startNum := (page - 1) * pageSize
-       err = p.data.DB.Context(ctx).Limit(pageSize, startNum).Find(&questions)
+       err = p.data.DB.Context(ctx).
+               Where("show = ? AND status IN (?, ?)", entity.QuestionShow,
+                       entity.QuestionStatusAvailable, 
entity.QuestionStatusClosed).
+               OrderBy("id ASC").Limit(pageSize, startNum).Find(&questions)
        if err != nil {
                return nil, err
        }
@@ -64,13 +75,16 @@ func (p *PluginSyncer) GetQuestionsPage(ctx 
context.Context, page, pageSize int)
 func (p *PluginSyncer) convertAnswers(ctx context.Context, answers 
[]*entity.Answer) (
        answerList []*plugin.SearchContent, err error) {
        for _, answer := range answers {
+               if answer.Status != entity.AnswerStatusAvailable {
+                       continue
+               }
                question := &entity.Question{}
                exist, err := p.data.DB.Context(ctx).Where("id = ?", 
answer.QuestionID).Get(question)
                if err != nil {
                        log.Errorf("get question failed %s", err)
                        continue
                }
-               if !exist {
+               if !exist || !isPublicQuestion(question) {
                        continue
                }
 
@@ -109,6 +123,9 @@ func (p *PluginSyncer) convertAnswers(ctx context.Context, 
answers []*entity.Ans
 func (p *PluginSyncer) convertQuestions(ctx context.Context, questions 
[]*entity.Question) (
        questionList []*plugin.SearchContent, err error) {
        for _, question := range questions {
+               if !isPublicQuestion(question) {
+                       continue
+               }
                tagListList := make([]*entity.TagRel, 0)
                tags := make([]string, 0)
                err := p.data.DB.Context(ctx).Where("object_id = ?", 
question.ID).
@@ -139,3 +156,61 @@ func (p *PluginSyncer) convertQuestions(ctx 
context.Context, questions []*entity
        }
        return questionList, nil
 }
+
+func isPublicQuestion(question *entity.Question) bool {
+       return question != nil && question.Show == entity.QuestionShow &&
+               (question.Status == entity.QuestionStatusAvailable || 
question.Status == entity.QuestionStatusClosed)
+}
+
+// RemoveRestrictedContents removes documents written before visibility 
filtering
+// was enforced. It uses the existing Search interface so search plugins remain
+// source and binary compatible.
+func RemoveRestrictedContents(ctx context.Context, data *data.Data, search 
plugin.Search) error {
+       if data == nil || data.DB == nil || search == nil {
+               return nil
+       }
+
+       questions := make([]*entity.Question, 0, cleanupPageSize)
+       // Object IDs are positive numeric IDs. Start from zero so the predicate
+       // also works with databases that store the ID column as BIGINT.
+       lastID := "0"
+       for {
+               questions = questions[:0]
+               if err := data.DB.Context(ctx).Where("id > ?", 
lastID).OrderBy("id ASC").Limit(cleanupPageSize).Find(&questions); err != nil {
+                       return fmt.Errorf("list questions for search cleanup: 
%w", err)
+               }
+               for _, question := range questions {
+                       if isPublicQuestion(question) {
+                               answers := make([]*entity.Answer, 0)
+                               if err := 
data.DB.Context(ctx).Where("question_id = ? AND status <> ?", question.ID,
+                                       
entity.AnswerStatusAvailable).Find(&answers); err != nil {
+                                       return fmt.Errorf("list restricted 
answers for question %s: %w", question.ID, err)
+                               }
+                               for _, answer := range answers {
+                                       if err := search.DeleteContent(ctx, 
uid.DeShortID(answer.ID)); err != nil {
+                                               return fmt.Errorf("delete 
restricted answer search document %s: %w", answer.ID, err)
+                                       }
+                               }
+                               continue
+                       }
+
+                       if err := search.DeleteContent(ctx, 
uid.DeShortID(question.ID)); err != nil {
+                               return fmt.Errorf("delete restricted question 
search document %s: %w", question.ID, err)
+                       }
+                       answers := make([]*entity.Answer, 0)
+                       if err := data.DB.Context(ctx).Where("question_id = ?", 
question.ID).Find(&answers); err != nil {
+                               return fmt.Errorf("list answers for restricted 
question %s: %w", question.ID, err)
+                       }
+                       for _, answer := range answers {
+                               if err := search.DeleteContent(ctx, 
uid.DeShortID(answer.ID)); err != nil {
+                                       return fmt.Errorf("delete answer search 
document %s: %w", answer.ID, err)
+                               }
+                       }
+               }
+               if len(questions) < cleanupPageSize {
+                       break
+               }
+               lastID = questions[len(questions)-1].ID
+       }
+       return nil
+}
diff --git a/internal/repo/search_sync/search_sync_test.go 
b/internal/repo/search_sync/search_sync_test.go
new file mode 100644
index 00000000..8b1bfacd
--- /dev/null
+++ b/internal/repo/search_sync/search_sync_test.go
@@ -0,0 +1,141 @@
+/*
+ * 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 search_sync
+
+import (
+       "context"
+       "testing"
+
+       "github.com/apache/answer/internal/base/data"
+       "github.com/apache/answer/internal/entity"
+       "github.com/apache/answer/plugin"
+)
+
+type cleanupTestSearch struct {
+       deleted map[string]bool
+}
+
+func (*cleanupTestSearch) Info() plugin.Info { return plugin.Info{} }
+func (*cleanupTestSearch) Description() plugin.SearchDesc {
+       return plugin.SearchDesc{}
+}
+func (*cleanupTestSearch) RegisterSyncer(context.Context, plugin.SearchSyncer) 
{}
+func (*cleanupTestSearch) SearchContents(context.Context, 
*plugin.SearchBasicCond) ([]plugin.SearchResult, int64, error) {
+       return nil, 0, nil
+}
+func (*cleanupTestSearch) SearchQuestions(context.Context, 
*plugin.SearchBasicCond) ([]plugin.SearchResult, int64, error) {
+       return nil, 0, nil
+}
+func (*cleanupTestSearch) SearchAnswers(context.Context, 
*plugin.SearchBasicCond) ([]plugin.SearchResult, int64, error) {
+       return nil, 0, nil
+}
+func (*cleanupTestSearch) UpdateContent(context.Context, 
*plugin.SearchContent) error { return nil }
+func (s *cleanupTestSearch) DeleteContent(_ context.Context, objectID string) 
error {
+       s.deleted[objectID] = true
+       return nil
+}
+
+func TestPluginSyncerIndexesOnlyPublicContent(t *testing.T) {
+       ctx := context.Background()
+       db, err := data.NewDB(false, &data.Database{Driver: "sqlite", 
Connection: ":memory:"})
+       if err != nil {
+               t.Fatalf("create SQLite database: %v", err)
+       }
+       t.Cleanup(func() { _ = db.Close() })
+       if err := db.Sync2(new(entity.Question), new(entity.Answer), 
new(entity.TagRel)); err != nil {
+               t.Fatalf("create tables: %v", err)
+       }
+
+       publicQuestion := &entity.Question{ID: "10010000000000001", Status: 
entity.QuestionStatusAvailable, Show: entity.QuestionShow}
+       hiddenQuestion := &entity.Question{ID: "10010000000000002", Status: 
entity.QuestionStatusAvailable, Show: entity.QuestionHide}
+       pendingQuestion := &entity.Question{ID: "10010000000000003", Status: 
entity.QuestionStatusPending, Show: entity.QuestionShow}
+       for _, question := range []*entity.Question{publicQuestion, 
hiddenQuestion, pendingQuestion} {
+               if _, err := db.Context(ctx).Insert(question); err != nil {
+                       t.Fatalf("insert question: %v", err)
+               }
+       }
+       publicAnswer := &entity.Answer{ID: "10020000000000001", QuestionID: 
publicQuestion.ID, Status: entity.AnswerStatusAvailable}
+       hiddenAnswer := &entity.Answer{ID: "10020000000000002", QuestionID: 
hiddenQuestion.ID, Status: entity.AnswerStatusAvailable}
+       deletedAnswer := &entity.Answer{ID: "10020000000000003", QuestionID: 
publicQuestion.ID, Status: entity.AnswerStatusDeleted}
+       for _, answer := range []*entity.Answer{publicAnswer, hiddenAnswer, 
deletedAnswer} {
+               if _, err := db.Context(ctx).Insert(answer); err != nil {
+                       t.Fatalf("insert answer: %v", err)
+               }
+       }
+
+       syncer := NewPluginSyncer(&data.Data{DB: db})
+       questions, err := syncer.GetQuestionsPage(ctx, 1, 10)
+       if err != nil {
+               t.Fatalf("get questions: %v", err)
+       }
+       if len(questions) != 1 || questions[0].ObjectID != publicQuestion.ID {
+               t.Fatalf("question documents = %#v, want only the public 
question", questions)
+       }
+       answers, err := syncer.GetAnswersPage(ctx, 1, 10)
+       if err != nil {
+               t.Fatalf("get answers: %v", err)
+       }
+       if len(answers) != 1 || answers[0].ObjectID != publicAnswer.ID {
+               t.Fatalf("answer documents = %#v, want only the public answer", 
answers)
+       }
+}
+
+func TestRemoveRestrictedContentsRemovesHistoricalSearchDocuments(t 
*testing.T) {
+       ctx := context.Background()
+       db, err := data.NewDB(false, &data.Database{Driver: "sqlite", 
Connection: ":memory:"})
+       if err != nil {
+               t.Fatalf("create SQLite database: %v", err)
+       }
+       t.Cleanup(func() { _ = db.Close() })
+       if err := db.Sync2(new(entity.Question), new(entity.Answer)); err != 
nil {
+               t.Fatalf("create tables: %v", err)
+       }
+
+       hiddenQuestion := &entity.Question{ID: "10010000000000011", Status: 
entity.QuestionStatusAvailable, Show: entity.QuestionHide}
+       publicQuestion := &entity.Question{ID: "10010000000000012", Status: 
entity.QuestionStatusAvailable, Show: entity.QuestionShow}
+       for _, question := range []*entity.Question{hiddenQuestion, 
publicQuestion} {
+               if _, err := db.Context(ctx).Insert(question); err != nil {
+                       t.Fatalf("insert question: %v", err)
+               }
+       }
+       hiddenAnswer := &entity.Answer{ID: "10020000000000011", QuestionID: 
hiddenQuestion.ID, Status: entity.AnswerStatusAvailable}
+       deletedAnswer := &entity.Answer{ID: "10020000000000012", QuestionID: 
publicQuestion.ID, Status: entity.AnswerStatusDeleted}
+       publicAnswer := &entity.Answer{ID: "10020000000000013", QuestionID: 
publicQuestion.ID, Status: entity.AnswerStatusAvailable}
+       for _, answer := range []*entity.Answer{hiddenAnswer, deletedAnswer, 
publicAnswer} {
+               if _, err := db.Context(ctx).Insert(answer); err != nil {
+                       t.Fatalf("insert answer: %v", err)
+               }
+       }
+
+       search := &cleanupTestSearch{deleted: map[string]bool{}}
+       if err := RemoveRestrictedContents(ctx, &data.Data{DB: db}, search); 
err != nil {
+               t.Fatalf("remove restricted contents: %v", err)
+       }
+       for _, objectID := range []string{hiddenQuestion.ID, hiddenAnswer.ID, 
deletedAnswer.ID} {
+               if !search.deleted[objectID] {
+                       t.Fatalf("restricted search document %s was not 
deleted: %v", objectID, search.deleted)
+               }
+       }
+       for _, objectID := range []string{publicQuestion.ID, publicAnswer.ID} {
+               if search.deleted[objectID] {
+                       t.Fatalf("public search document %s was deleted: %v", 
objectID, search.deleted)
+               }
+       }
+}
diff --git a/internal/service/plugin_common/plugin_common_service.go 
b/internal/service/plugin_common/plugin_common_service.go
index 88cc0214..334e0fee 100644
--- a/internal/service/plugin_common/plugin_common_service.go
+++ b/internal/service/plugin_common/plugin_common_service.go
@@ -101,7 +101,7 @@ func (ps *PluginCommonService) UpdatePluginConfig(ctx 
context.Context, req *sche
 
        _ = plugin.CallSearch(func(search plugin.Search) error {
                if search.Info().SlugName == req.PluginSlugName {
-                       search.RegisterSyncer(ctx, 
search_sync.NewPluginSyncer(ps.data))
+                       ps.registerSearchSyncer(search)
                }
                return nil
        })
@@ -184,6 +184,12 @@ func (ps *PluginCommonService) initPluginData() {
                })
        }
 
+       // Register syncers after plugin configuration is loaded.
+       _ = plugin.CallSearch(func(search plugin.Search) error {
+               ps.registerSearchSyncer(search)
+               return nil
+       })
+
        // register syncer for vector search plugins on startup
        _ = plugin.CallVectorSearch(func(vs plugin.VectorSearch) error {
                ps.registerVectorSyncer(vs)
@@ -231,6 +237,19 @@ func (ps *PluginCommonService) initPluginData() {
        }()
 }
 
+// registerSearchSyncer removes historical restricted documents before asking a
+// plugin to rebuild its index. The result path independently rechecks database
+// visibility, so this background cleanup cannot reopen the disclosure.
+func (ps *PluginCommonService) registerSearchSyncer(search plugin.Search) {
+       go func() {
+               ctx := context.Background()
+               if err := search_sync.RemoveRestrictedContents(ctx, ps.data, 
search); err != nil {
+                       log.Errorf("remove restricted search content for %s 
failed: %v", search.Info().SlugName, err)
+               }
+               search.RegisterSyncer(ctx, search_sync.NewPluginSyncer(ps.data))
+       }()
+}
+
 // registerVectorSyncer removes historical restricted documents before asking a
 // plugin to rebuild its index. This can involve remote vector-store calls, so 
it
 // must not block startup or a plugin configuration request.

Reply via email to