RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
chore(router): gate /pipelines endpoints behind auth (#21776)
xugangqiang727c57451ccb
diff --git a/internal/handler/pipeline.go b/internal/handler/pipeline.goindex 61fed60bb9..9200713231 100644--- a/internal/handler/pipeline.go+++ b/internal/handler/pipeline.go@@ -57,7 +57,9 @@ func NewPipelineHandler() *PipelineHandler {// Returns available pipeline templates. When type=builtin (or when no// type is specified), returns the built-in pipeline catalog shipped with// the binary. Support for user-defined pipelines may be added later.-// This is public static data, so no auth is required.+// The endpoint is registered under the authenticated group so the+// /pipelines namespace is ready for per-tenant user pipelines; built-in+// templates themselves are static data with no tenant scoping.func (h *PipelineHandler) ListPipelines(c *gin.Context) {if h == nil || h.registry == nil {common.SuccessWithData(c, &pipelinepkg.BuiltinPipelineListResponse{@@ -77,7 +79,8 @@ func (h *PipelineHandler) ListPipelines(c *gin.Context) {// GetPipeline GET /api/v1/pipelines/:id// Returns a single built-in pipeline template with its DSL.-// This is public static data, so no auth is required.+// The endpoint is registered under the authenticated group; built-in+// templates are static data with no tenant scoping.func (h *PipelineHandler) GetPipeline(c *gin.Context) {if h == nil || h.registry == nil {common.ErrorWithCode(c, common.CodeDataError, "pipeline not found")diff --git a/internal/handler/pipeline_test.go b/internal/handler/pipeline_test.goindex fbb7bb6e6a..77e922c000 100644--- a/internal/handler/pipeline_test.go+++ b/internal/handler/pipeline_test.go@@ -17,14 +17,18 @@package handlerimport (+ "context""encoding/json"+ "net/http""net/http/httptest""testing""github.com/gin-gonic/gin""ragflow/internal/common"+ "ragflow/internal/entity"pipelinepkg "ragflow/internal/ingestion/pipeline"+ "ragflow/internal/service")// fakePipelineRegistry is a test double for builtinPipelineRegistry.@@ -246,3 +250,50 @@ func TestPipelineHandler_GetPipeline_NotFound(t *testing.T) {t.Fatal("expected error code for nonexistent pipeline")}}++// TestPipelineHandler_AuthenticatedServesBuiltin locks the contract that an+// authenticated request reaches the handler and returns the built-in catalog.+// Built-in templates have no tenant scoping, so gating the /pipelines+// namespace behind auth must not block authenticated reads of the static+// catalog. It mirrors the authorized-group wiring (AuthMiddleware + route)+// with a stub token resolver and guards against a future change that would+// wrongly require ownership on built-in templates.+func TestPipelineHandler_AuthenticatedServesBuiltin(t *testing.T) {+ items := []*pipelinepkg.BuiltinPipelineMeta{+ {ParserID: "general", Title: "General"},+ }+ h := &PipelineHandler{registry: fakePipelineRegistry{items: items}}++ stub := &stubUserTokenResolver{+ resolveAccessTokenFn: func(ctx context.Context, auth string) (*service.AuthIdentity, common.ErrorCode, error) {+ return &service.AuthIdentity{User: &entity.User{ID: "u-1"}}, common.CodeSuccess, nil+ },+ }+ ah := &AuthHandler{userService: stub}++ gin.SetMode(gin.TestMode)+ r := gin.New()+ g := r.Group("/api/v1")+ g.Use(ah.AuthMiddleware())+ g.GET("/pipelines", h.ListPipelines)++ req := httptest.NewRequest(http.MethodGet, "/api/v1/pipelines?type=builtin", nil)+ req.Header.Set("Authorization", "token")+ w := httptest.NewRecorder()+ r.ServeHTTP(w, req)++ if w.Code != http.StatusOK {+ t.Fatalf("status=%d body=%s; want 200", w.Code, w.Body.String())+ }+ var resp struct {+ Data struct {+ Total int64 `json:"total"`+ } `json:"data"`+ }+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {+ t.Fatalf("unmarshal: %v", err)+ }+ if resp.Data.Total != 1 {+ t.Fatalf("total=%d; want 1", resp.Data.Total)+ }+}diff --git a/internal/router/router.go b/internal/router/router.goindex 4a8c7b6938..d458d3584d 100644--- a/internal/router/router.go+++ b/internal/router/router.go@@ -161,13 +161,6 @@ func (r *Router) Setup(engine *gin.Engine) {// to choose between Go and Python code paths.apiNoAuth.GET("/language", r.systemHandler.Language)- // Pipeline catalog. Public static data (shipped with the binary),- // no auth required. The front end uses it to populate the parser- // picker without hard-coding the parser_id list.- // Query: ?type=builtin returns built-in templates (default).- apiNoAuth.GET("/pipelines", r.pipelineHandler.ListPipelines)- apiNoAuth.GET("/pipelines/:id", r.pipelineHandler.GetPipeline)-// searchbotsapiNoAuth.GET("/searchbots/detail", r.searchBotHandler.SearchBotDetail)@@ -595,6 +588,15 @@ func (r *Router) Setup(engine *gin.Engine) {plugin.GET("/tools", r.pluginHandler.ListLLMTools)}+ // Pipeline catalog. Built-in templates are static data shipped+ // with the binary, but the endpoint is gated behind auth so the+ // /pipelines namespace is already auth-protected when per-tenant+ // user pipelines (dataflow canvases) are added later. Those will+ // reuse user_canvas + CheckCanvasPermission for ownership checks.+ // Query: ?type=builtin returns built-in templates (default).+ v1.GET("/pipelines", r.pipelineHandler.ListPipelines)+ v1.GET("/pipelines/:id", r.pipelineHandler.GetPipeline)+// Component catalog — Phase 4 of// port-rag-flow-pipeline-to-go.md. Optional// ?category=ingestion,agent,shared filter; defaults todiff --git a/internal/router/router_test.go b/internal/router/router_test.goindex 1e7baf0c3e..a8593f0dd4 100644--- a/internal/router/router_test.go+++ b/internal/router/router_test.go@@ -280,6 +280,42 @@ func TestRouterSetupRegistersChatMindMapRoute(t *testing.T) {}}+// TestRouterSetupPipelinesRoutesRequireAuth asserts that the pipeline+// catalog endpoints are mounted behind the strict AuthMiddleware (the+// authorized group), not the public apiNoAuth group. Built-in templates+// are static data, but gating the /pipelines namespace behind auth keeps+// it ready for per-tenant user pipelines (dataflow canvases) that will+// later need ownership checks. An unauthenticated request must be+// rejected with 401; if the route is moved back to apiNoAuth it would+// return 200 and this test fails.+func TestRouterSetupPipelinesRoutesRequireAuth(t *testing.T) {+ gin.SetMode(gin.TestMode)+ originalLogger := common.Logger+ common.Logger = zap.NewNop()+ t.Cleanup(func() { common.Logger = originalLogger })++ engine := gin.New()+ r := &Router{+ authHandler: handler.NewAuthHandler(),+ pipelineHandler: handler.NewPipelineHandler(),+ }+ r.Setup(engine)++ paths := []string{+ "/api/v1/pipelines?type=builtin",+ "/api/v1/pipelines/general",+ }+ for _, path := range paths {+ t.Run(path, func(t *testing.T) {+ resp := httptest.NewRecorder()+ engine.ServeHTTP(resp, httptest.NewRequest(http.MethodGet, path, nil))+ if resp.Code != http.StatusUnauthorized {+ t.Fatalf("GET %s: status=%d body=%s; want 401 (route must be behind auth middleware)", path, resp.Code, resp.Body.String())+ }+ })+ }+}+func TestRouterSetupRegistersDocumentImageRoutesWithBetaAuth(t *testing.T) {gin.SetMode(gin.TestMode)