Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(py-go-connector): enforce kb authorization in rebuild (#19190)

mkaaadd29a4fc26b10
diff --git a/api/apps/restful_apis/connector_api.py b/api/apps/restful_apis/connector_api.py
index 9d0ae7ed0a..d00ead22d7 100644
--- a/api/apps/restful_apis/connector_api.py
+++ b/api/apps/restful_apis/connector_api.py
@@ -25,7 +25,7 @@ from quart import request, make_response
from google_auth_oauthlib.flow import Flow
from api.db import InputType
-from api.db.services.connector_service import ConnectorService, SyncLogsService
+from api.db.services.connector_service import ConnectorAuthorizationError, ConnectorService, SyncLogsService
from api.utils.api_utils import get_data_error_result, get_json_result, get_request_json, validate_request
from api.utils.pagination_utils import DEFAULT_PAGE, DEFAULT_PAGE_SIZE, validate_rest_api_page, validate_rest_api_page_size
from common.constants import FileSource, RetCode, TaskStatus
@@ -160,7 +160,11 @@ async def rebuild(connector_id):
if "kb_id" not in req:
return get_json_result(code=RetCode.ARGUMENT_ERROR, message="required argument is missing: kb_id")
- err = ConnectorService.rebuild(req["kb_id"], connector_id, current_user.id)
+ kb_id = req["kb_id"]
+ try:
+ err = ConnectorService.rebuild(kb_id, connector_id, current_user.id)
+ except ConnectorAuthorizationError as exc:
+ return get_json_result(data=False, message=str(exc), code=RetCode.AUTHENTICATION_ERROR)
if err:
return get_json_result(data=False, message=err, code=RetCode.SERVER_ERROR)
return get_json_result(data=True)
diff --git a/api/db/services/connector_service.py b/api/db/services/connector_service.py
index 3cc064d2d4..a46b4fb028 100644
--- a/api/db/services/connector_service.py
+++ b/api/db/services/connector_service.py
@@ -26,6 +26,7 @@ from api.db.db_models import DB, Connector, SyncLogs, Connector2Kb, Knowledgebas
from api.db.services.common_service import CommonService
from api.db.services.document_service import DocumentService
from api.db.services.document_service import DocMetadataService
+from api.db.services.knowledgebase_service import KnowledgebaseService
from api.utils.common import hash128
from common import settings
from common.misc_utils import get_uuid
@@ -36,6 +37,14 @@ from common.time_utils import current_timestamp, timestamp_to_date
LOGGER = logging.getLogger(__name__)
+class ConnectorAuthorizationError(Exception):
+ """Raised when a caller is not authorized to run a connector operation.
+
+ Carries a user-facing message so handlers can map the denial to an
+ authentication/authorization error without inspecting internals.
+ """
+
+
def _is_gaussdb_compatible_metadata_db() -> bool:
return settings.normalize_database_type(settings.DATABASE_TYPE) == "gaussdb"
@@ -194,8 +203,33 @@ class ConnectorService(CommonService):
@classmethod
def rebuild(cls, kb_id: str, connector_id: str, tenant_id: str):
+ """Delete the connector's documents in *kb_id* and schedule a re-sync.
+
+ Authorization is owned by the service (not the HTTP layer) because this
+ operation deletes documents and schedules sync tasks against the
+ caller-supplied kb. The caller must be able to access the kb and the
+ connector must actually be bound to it; the binding check mirrors
+ ``cleanup_stale_documents_for_task``.
+ """
from api.db.services.file_service import FileService
+ if not KnowledgebaseService.accessible(kb_id, tenant_id):
+ LOGGER.warning(
+ "rebuild denied: kb not accessible connector_id=%s kb_id=%s user_id=%s",
+ connector_id,
+ kb_id,
+ tenant_id,
+ )
+ raise ConnectorAuthorizationError("no authorization")
+ if not Connector2KbService.query(connector_id=connector_id, kb_id=kb_id):
+ LOGGER.warning(
+ "rebuild denied: connector not bound to kb connector_id=%s kb_id=%s user_id=%s",
+ connector_id,
+ kb_id,
+ tenant_id,
+ )
+ raise ConnectorAuthorizationError("Connector is not bound to this knowledge base.")
+
e, conn = cls.get_by_id(connector_id)
if not e:
return None
diff --git a/internal/dao/connector.go b/internal/dao/connector.go
index ab186dd38c..8ec0b626b1 100644
--- a/internal/dao/connector.go
+++ b/internal/dao/connector.go
@@ -272,6 +272,18 @@ func (dao *ConnectorDAO) ScheduleConnectorTasks(ctx context.Context, db *gorm.DB
return taskIDs, err
}
+// Connector2KBExists reports whether the connector is bound to the knowledge base.
+func (dao *ConnectorDAO) Connector2KBExists(ctx context.Context, db *gorm.DB, connectorID, kbID string) (bool, error) {
+ var count int64
+ err := db.WithContext(ctx).Model(&entity.Connector2Kb{}).
+ Where("connector_id = ? AND kb_id = ?", connectorID, kbID).
+ Count(&count).Error
+ if err != nil {
+ return false, err
+ }
+ return count > 0, nil
+}
+
// ListDocumentsByKBAndSourceType lists connector documents in a dataset.
func (dao *ConnectorDAO) ListDocumentsByKBAndSourceType(ctx context.Context, db *gorm.DB, kbID, sourceType string) ([]*entity.Document, error) {
var documents []*entity.Document
diff --git a/internal/service/connector.go b/internal/service/connector.go
index ee330cb1d2..02be172485 100644
--- a/internal/service/connector.go
+++ b/internal/service/connector.go
@@ -71,13 +71,14 @@ var (
connectorRedisGet = redis.Get
)
-// Sentinel errors so handlers can map to the proper response codes,
-// mirroring the Python connector_api responses.
+// Sentinel errors so handlers can map to the proper response codes.
var (
- // ErrConnectorNotFound mirrors Python's "Can't find this Connector!".
+ // ErrConnectorNotFound is returned when a connector is not found.
ErrConnectorNotFound = errors.New("can't find this Connector")
- // ErrConnectorNoAuth mirrors Python's "no authorization" denial.
+ // ErrConnectorNoAuth is returned when the caller cannot access the connector or knowledge base.
ErrConnectorNoAuth = errors.New("no authorization")
+ // ErrConnectorNotBoundToKB is returned when the connector is not bound to the kb being rebuilt.
+ ErrConnectorNotBoundToKB = errors.New("connector is not bound to this knowledge base")
// ErrConnectorIDRequired is returned when a connector ID is missing.
ErrConnectorIDRequired = errors.New("connector_id is required")
// ErrConnectorTestUnsupported is returned for connector sources without a settings validator.
@@ -91,6 +92,7 @@ var (
// ConnectorService connector service
type ConnectorService struct {
connectorDAO *dao.ConnectorDAO
+ knowledgebaseDAO *dao.KnowledgebaseDAO
userTenantDAO *dao.UserTenantDAO
connectorRegistry *syncerconnector.Registry
}
@@ -130,6 +132,7 @@ var getSyncCheckpointDeleter = func() (syncCheckpointDeleter, bool) {
func NewConnectorService() *ConnectorService {
return &ConnectorService{
connectorDAO: dao.NewConnectorDAO(),
+ knowledgebaseDAO: dao.NewKnowledgebaseDAO(),
userTenantDAO: dao.NewUserTenantDAO(),
connectorRegistry: newConnectorRegistry(),
}
@@ -146,7 +149,7 @@ type ListConnectorsResponse struct {
Connectors []*dao.ConnectorListItem `json:"connectors"`
}
-// CreateConnectorRequest creates a connector with Python-compatible defaults.
+// CreateConnectorRequest holds the fields used to create a connector.
type CreateConnectorRequest struct {
Name string `json:"name"`
Source string `json:"source"`
@@ -291,7 +294,6 @@ func (s *ConnectorService) cancelConnectorTasks(ctx context.Context, connectorID
}
// CreateConnector creates a connector owned by the current user.
-// Equivalent to Python's create_connector endpoint.
func (s *ConnectorService) CreateConnector(ctx context.Context, userID string, req *CreateConnectorRequest) (*entity.Connector, error) {
refreshFreq := int64(defaultConnectorFreq)
if req.RefreshFreq != nil {
@@ -1084,6 +1086,23 @@ func (s *ConnectorService) RebuildConnector(ctx context.Context, connectorID, us
return false, common.CodeAuthenticationError, fmt.Errorf("no authorization")
}
+ // The caller-supplied kb is targeted by delete + re-sync below, so it must
+ // be accessible to the caller and the connector must be bound to it.
+ if !s.knowledgebaseDAO.Accessible(ctx, dao.DB, kbID, userID) {
+ common.Warn("rebuild denied: kb not accessible",
+ zap.String("connector_id", connectorID), zap.String("kb_id", kbID), zap.String("user_id", userID))
+ return false, common.CodeAuthenticationError, ErrConnectorNoAuth
+ }
+ bound, err := s.connectorDAO.Connector2KBExists(ctx, dao.DB, connectorID, kbID)
+ if err != nil {
+ return false, common.CodeServerError, err
+ }
+ if !bound {
+ common.Warn("rebuild denied: connector not bound to kb",
+ zap.String("connector_id", connectorID), zap.String("kb_id", kbID), zap.String("user_id", userID))
+ return false, common.CodeAuthenticationError, ErrConnectorNotBoundToKB
+ }
+
sourceType := fmt.Sprintf("%s/%s", connector.Source, connector.ID)
documents, err := s.connectorDAO.ListDocumentsByKBAndSourceType(ctx, dao.DB, kbID, sourceType)
if err != nil {
diff --git a/internal/service/connector_syncer_test.go b/internal/service/connector_syncer_test.go
index ba1057d29a..b5c8fabf7c 100644
--- a/internal/service/connector_syncer_test.go
+++ b/internal/service/connector_syncer_test.go
@@ -2,6 +2,7 @@ package service
import (
"context"
+ "errors"
"testing"
"ragflow/internal/common"
@@ -271,6 +272,50 @@ func TestRebuildConnectorDeletesOldSyncCheckpointsBeforePublishing(t *testing.T)
}
}
+func TestRebuildConnectorRejectsCrossTenantAndUnboundKB(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ if err := db.AutoMigrate(&entity.Connector{}, &entity.Connector2Kb{}, &entity.Knowledgebase{}, &entity.SyncLogs{}); err != nil {
+ t.Fatalf("migrate connector tables: %v", err)
+ }
+ if err := db.Create(&entity.Connector{
+ ID: "conn-1", TenantID: "user-1", Name: "conn-1", Source: "gmail",
+ InputType: "poll", Config: entity.JSONMap{}, Status: string(entity.TaskStatusSchedule),
+ RefreshFreq: 0, PruneFreq: 0, TimeoutSecs: 60,
+ }).Error; err != nil {
+ t.Fatalf("insert connector: %v", err)
+ }
+ // kb-1 is owned by the caller but is never bound to conn-1.
+ if err := db.Create(&entity.Knowledgebase{ID: "kb-1", TenantID: "user-1", Name: "kb-1", CreatedBy: "user-1", EmbdID: "embd"}).Error; err != nil {
+ t.Fatalf("insert kb-1: %v", err)
+ }
+ // kb-foreign belongs to another tenant.
+ if err := db.Create(&entity.Knowledgebase{ID: "kb-foreign", TenantID: "user-2", Name: "kb-foreign", CreatedBy: "user-2", EmbdID: "embd"}).Error; err != nil {
+ t.Fatalf("insert kb-foreign: %v", err)
+ }
+
+ svc := NewConnectorService()
+
+ // The caller can access kb-1, but conn-1 is not bound to it: denied before
+ // any documents are listed or deleted.
+ ok, code, err := svc.RebuildConnector(t.Context(), "conn-1", "user-1", "kb-1")
+ if err == nil || !errors.Is(err, ErrConnectorNotBoundToKB) {
+ t.Fatalf("err = %v, want ErrConnectorNotBoundToKB", err)
+ }
+ if ok || code != common.CodeAuthenticationError {
+ t.Fatalf("ok/code = %v/%v, want false/authentication error", ok, code)
+ }
+
+ // The caller cannot access a cross-tenant kb at all: denied up front.
+ ok, code, err = svc.RebuildConnector(t.Context(), "conn-1", "user-1", "kb-foreign")
+ if err == nil || !errors.Is(err, ErrConnectorNoAuth) {
+ t.Fatalf("err = %v, want ErrConnectorNoAuth", err)
+ }
+ if ok || code != common.CodeAuthenticationError {
+ t.Fatalf("ok/code = %v/%v, want false/authentication error", ok, code)
+ }
+}
+
func TestUpdateConnectorScheduleDoesNotDuplicateRunningTask(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)
diff --git a/test/testcases/restful_api/test_connector_routes_unit.py b/test/testcases/restful_api/test_connector_routes_unit.py
index 7af3d0a5ad..95f3aad8f9 100644
--- a/test/testcases/restful_api/test_connector_routes_unit.py
+++ b/test/testcases/restful_api/test_connector_routes_unit.py
@@ -222,8 +222,12 @@ def _load_connector_app(monkeypatch):
def list_sync_tasks(*_args, **_kwargs):
return [], 0
+ class _StubConnectorAuthorizationError(Exception):
+ pass
+
connector_service_mod.ConnectorService = _StubConnectorService
connector_service_mod.SyncLogsService = _StubSyncLogsService
+ connector_service_mod.ConnectorAuthorizationError = _StubConnectorAuthorizationError
monkeypatch.setitem(sys.modules, "api.db.services.connector_service", connector_service_mod)
api_utils_mod = ModuleType("api.utils.api_utils")
@@ -510,6 +514,49 @@ def test_connector_by_id_routes_reject_cross_tenant_access(monkeypatch):
assert bad_config_res["code"] == module.RetCode.ARGUMENT_ERROR
+def test_connector_rebuild_maps_service_auth_denials(monkeypatch):
+ """The route lets the service own kb authorization and only maps the
+ service's ConnectorAuthorizationError to an auth response. This keeps the
+ destructive service operation protected regardless of the HTTP caller.
+ """
+ module = _load_connector_app(monkeypatch)
+ monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue({"kb_id": "kb-1"}))
+
+ def _deny(message):
+ def _rebuild(*_args, **_kwargs):
+ raise module.ConnectorAuthorizationError(message)
+
+ return _rebuild
+
+ monkeypatch.setattr(module.ConnectorService, "rebuild", _deny("no authorization"))
+ denied_kb = _run(module.rebuild("conn-rb"))
+ assert denied_kb["code"] == module.RetCode.AUTHENTICATION_ERROR
+ assert denied_kb["message"] == "no authorization"
+ assert denied_kb["data"] is False
+
+ monkeypatch.setattr(
+ module.ConnectorService,
+ "rebuild",
+ _deny("Connector is not bound to this knowledge base."),
+ )
+ denied_unbound = _run(module.rebuild("conn-rb"))
+ assert denied_unbound["code"] == module.RetCode.AUTHENTICATION_ERROR
+ assert denied_unbound["message"] == "Connector is not bound to this knowledge base."
+ assert denied_unbound["data"] is False
+
+ # A non-authorization service failure still maps to a server error.
+ monkeypatch.setattr(module.ConnectorService, "rebuild", lambda *_args: "rebuild-failed")
+ failed = _run(module.rebuild("conn-rb"))
+ assert failed["code"] == module.RetCode.SERVER_ERROR
+ assert failed["message"] == "rebuild-failed"
+
+ # Success path returns data True.
+ monkeypatch.setattr(module.ConnectorService, "rebuild", lambda *_args: None)
+ ok = _run(module.rebuild("conn-rb"))
+ assert ok["data"] is True
+
+
@pytest.mark.p2
def test_connector_oauth_helper_functions(monkeypatch):
module = _load_connector_app(monkeypatch)
diff --git a/test/unit_test/api/db/services/test_connector_rebuild_authz.py b/test/unit_test/api/db/services/test_connector_rebuild_authz.py
new file mode 100644
index 0000000000..51a578064d
--- /dev/null
+++ b/test/unit_test/api/db/services/test_connector_rebuild_authz.py
@@ -0,0 +1,84 @@
+#
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+#
+# Licensed 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.
+#
+"""ConnectorService.rebuild enforces kb authorization in the service layer.
+
+Rebuild deletes the connector's documents in the caller-supplied kb and
+schedules sync tasks, so the service itself (not just the HTTP handler) must
+refuse to touch a kb the caller cannot access or that the connector is not
+bound to. This mirrors the binding guard already used by
+ConnectorService.cleanup_stale_documents_for_task.
+"""
+
+from types import SimpleNamespace
+
+import pytest
+
+from api.db.services.connector_service import (
+ Connector2KbService,
+ ConnectorAuthorizationError,
+ ConnectorService,
+ DocumentService,
+ KnowledgebaseService,
+ SyncLogsService,
+)
+from api.db.services.file_service import FileService
+
+
+def test_rebuild_rejects_kb_the_caller_cannot_access(monkeypatch):
+ monkeypatch.setattr(KnowledgebaseService, "accessible", staticmethod(lambda kb_id, uid: False))
+
+ with pytest.raises(ConnectorAuthorizationError, match="no authorization"):
+ ConnectorService.rebuild("kb-foreign", "conn-1", "user-1")
+
+
+def test_rebuild_rejects_kb_the_connector_is_not_bound_to(monkeypatch):
+ monkeypatch.setattr(KnowledgebaseService, "accessible", staticmethod(lambda kb_id, uid: True))
+ monkeypatch.setattr(Connector2KbService, "query", staticmethod(lambda **kwargs: []))
+
+ with pytest.raises(ConnectorAuthorizationError, match="Connector is not bound to this knowledge base."):
+ ConnectorService.rebuild("kb-1", "conn-1", "user-1")
+
+
+def test_rebuild_runs_only_after_both_guards_pass(monkeypatch):
+ touched = []
+
+ monkeypatch.setattr(KnowledgebaseService, "accessible", staticmethod(lambda kb_id, uid: True))
+ monkeypatch.setattr(Connector2KbService, "query", staticmethod(lambda **kwargs: [object()]))
+ monkeypatch.setattr(
+ ConnectorService,
+ "get_by_id",
+ staticmethod(lambda cid: (True, SimpleNamespace(id=cid, source="rss", config={}))),
+ )
+ monkeypatch.setattr(DocumentService, "query", staticmethod(lambda **kwargs: []))
+ monkeypatch.setattr(FileService, "delete_docs", staticmethod(lambda *args, **kwargs: None))
+ monkeypatch.setattr(
+ SyncLogsService,
+ "filter_delete",
+ staticmethod(lambda *args, **kwargs: touched.append("filter_delete")),
+ )
+ monkeypatch.setattr(
+ SyncLogsService,
+ "schedule",
+ staticmethod(lambda *args, **kwargs: touched.append("schedule")),
+ )
+
+ err = ConnectorService.rebuild("kb-1", "conn-1", "user-1")
+
+ assert err is None
+ assert touched == ["filter_delete", "schedule"], "delete/schedule should run once the guards pass"