A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
fix: constrain MCP content access
LinkinStars63a67542f4c2
diff --git a/internal/controller/mcp_controller.go b/internal/controller/mcp_controller.goindex e24c1a54..a5709664 100644--- a/internal/controller/mcp_controller.go+++ b/internal/controller/mcp_controller.go@@ -139,7 +139,7 @@ func (c *MCPController) MCPQuestionDetailHandler() func(ctx context.Context, req}question, err := c.questioncommon.Info(ctx, cond.QuestionID, "")- if err != nil {+ if err != nil || !mcpQuestionIsPublic(question) {log.Errorf("get question failed: %v", err)return mcp.NewToolResultText("No question found."), nil}@@ -161,6 +161,9 @@ func (c *MCPController) MCPAnswersHandler() func(ctx context.Context, request mcreturn nil, err}cond := schema.NewMCPSearchAnswerCond(request)+ if len(cond.QuestionID) == 0 {+ return mcp.NewToolResultText("[]"), nil+ }siteGeneral, err := c.siteInfoService.GetSiteGeneral(ctx)if err != nil {@@ -169,6 +172,10 @@ func (c *MCPController) MCPAnswersHandler() func(ctx context.Context, request mc}if len(cond.QuestionID) > 0 {+ question, err := c.questioncommon.Info(ctx, cond.QuestionID, "")+ if err != nil || !mcpQuestionIsPublic(question) {+ return mcp.NewToolResultText("[]"), nil+ }answerList, err := c.answerRepo.GetAnswerList(ctx, &entity.Answer{QuestionID: cond.QuestionID})if err != nil {log.Errorf("get answers failed: %v", err)@@ -214,12 +221,33 @@ func (c *MCPController) MCPAnswersHandler() func(ctx context.Context, request mc}}+func mcpQuestionIsPublic(question *schema.QuestionInfoResp) bool {+ return question != nil && question.Show == entity.QuestionShow &&+ (question.Status == entity.QuestionStatusAvailable || question.Status == entity.QuestionStatusClosed)+}++func (c *MCPController) mcpObjectQuestionIsPublic(ctx context.Context, objectID string) bool {+ question, err := c.questioncommon.Info(ctx, objectID, "")+ if err == nil {+ return mcpQuestionIsPublic(question)+ }+ answer, exist, err := c.answerRepo.GetAnswer(ctx, objectID)+ if err != nil || !exist || answer.Status != entity.AnswerStatusAvailable {+ return false+ }+ question, err = c.questioncommon.Info(ctx, answer.QuestionID, "")+ return err == nil && mcpQuestionIsPublic(question)+}+func (c *MCPController) MCPCommentsHandler() func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {if err := c.ensureMCPEnabled(ctx); err != nil {return nil, err}cond := schema.NewMCPSearchCommentCond(request)+ if len(cond.ObjectID) == 0 || !c.mcpObjectQuestionIsPublic(ctx, cond.ObjectID) {+ return mcp.NewToolResultText("No comments found."), nil+ }siteGeneral, err := c.siteInfoService.GetSiteGeneral(ctx)if err != nil {diff --git a/internal/controller/mcp_controller_test.go b/internal/controller/mcp_controller_test.gonew file mode 100644index 00000000..09f75a6a--- /dev/null+++ b/internal/controller/mcp_controller_test.go@@ -0,0 +1,50 @@+/*+ * 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 controller++import (+ "testing"++ "github.com/apache/answer/internal/entity"+ "github.com/apache/answer/internal/schema"+)++func TestMCPQuestionIsPublic(t *testing.T) {+ testCases := []struct {+ name string+ question *schema.QuestionInfoResp+ want bool+ }{+ {"nil", nil, false},+ {"available", &schema.QuestionInfoResp{Status: entity.QuestionStatusAvailable, Show: entity.QuestionShow}, true},+ {"closed", &schema.QuestionInfoResp{Status: entity.QuestionStatusClosed, Show: entity.QuestionShow}, true},+ {"hidden", &schema.QuestionInfoResp{Status: entity.QuestionStatusAvailable, Show: entity.QuestionHide}, false},+ {"deleted", &schema.QuestionInfoResp{Status: entity.QuestionStatusDeleted, Show: entity.QuestionShow}, false},+ {"pending", &schema.QuestionInfoResp{Status: entity.QuestionStatusPending, Show: entity.QuestionShow}, false},+ }++ for _, testCase := range testCases {+ t.Run(testCase.name, func(t *testing.T) {+ if got := mcpQuestionIsPublic(testCase.question); got != testCase.want {+ t.Fatalf("mcpQuestionIsPublic() = %v, want %v", got, testCase.want)+ }+ })+ }+}