Skip to content

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

Fix: restrict tenant updates to the current user's own tenant (#19155)

Lynnc534144a4c47
diff --git a/api/apps/restful_apis/user_api.py b/api/apps/restful_apis/user_api.py
index 673da23f6b..6f86d7f1a5 100644
--- a/api/apps/restful_apis/user_api.py
+++ b/api/apps/restful_apis/user_api.py
@@ -29,6 +29,7 @@ from api.apps.auth import get_auth_client
from api.db import FileType, UserTenantRole
from api.db.services.file_service import FileService
from api.db.services.user_service import TenantService, UserService, UserTenantService
+from api.db.joint_services.tenant_model_service import ensure_tenant_model_ids_for_params
from common.time_utils import current_timestamp, datetime_format, get_format_time
from common.misc_utils import download_img, get_uuid
from common.constants import RetCode
@@ -172,7 +173,7 @@ async def oauth_login(channel):
state = get_uuid()
session["oauth_state"] = state
auth_url = auth_cli.get_authorization_url(state)
- logging.info("OAuth login initiated: channel='%s', state='%s'", channel, state)
+ logging.info("OAuth login initiated: channel='%s'", channel)
return redirect(auth_url)
@@ -645,7 +646,11 @@ async def set_tenant_info():
req = await get_request_json()
try:
tid = req.pop("tenant_id")
- TenantService.update_by_id(tid, req)
+ if tid != current_user.id:
+ logging.warning("IDOR attempt blocked: user %s requested tenant_id %s on %s", current_user.id, tid, request.path)
+ return get_json_result(data=False, message="No authorization.", code=RetCode.AUTHENTICATION_ERROR)
+ update_dict = ensure_tenant_model_ids_for_params(tid, req)
+ TenantService.update_by_id(tid, update_dict)
return get_json_result(data=True)
except Exception as e:
return server_error_response(e)
diff --git a/test/testcases/restful_api/test_user_tenant_routes_unit.py b/test/testcases/restful_api/test_user_tenant_routes_unit.py
index 1832bf45f5..137533c2d9 100644
--- a/test/testcases/restful_api/test_user_tenant_routes_unit.py
+++ b/test/testcases/restful_api/test_user_tenant_routes_unit.py
@@ -419,7 +419,7 @@ def _load_user_app(monkeypatch):
quart_mod = ModuleType("quart")
quart_mod.session = {}
- quart_mod.request = SimpleNamespace(args=_Args({}))
+ quart_mod.request = SimpleNamespace(args=_Args({}), path="/v1/user/setting")
async def _make_response(data):
return _DummyResponse(data)
@@ -451,6 +451,10 @@ def _load_user_app(monkeypatch):
monkeypatch.setitem(sys.modules, "api.db", db_mod)
api_pkg.db = db_mod
+ tenant_model_service_mod = ModuleType("api.db.joint_services.tenant_model_service")
+ tenant_model_service_mod.ensure_tenant_model_ids_for_params = lambda _tenant_id, params: params
+ monkeypatch.setitem(sys.modules, "api.db.joint_services.tenant_model_service", tenant_model_service_mod)
+
db_models_mod = ModuleType("api.db.db_models")
class _DummyTenantLLMModel:
@@ -1115,10 +1119,21 @@ def test_tenant_info_and_set_tenant_info_exception_matrix_unit(monkeypatch):
assert res["code"] == module.RetCode.EXCEPTION_ERROR, res
assert "tenant info boom" in res["message"], res
+ # IDOR: tenant_id from request body must match the authenticated user
+ _set_request_json(
+ monkeypatch,
+ module,
+ {"tenant_id": "other-tenant", "llm_id": "l", "embd_id": "e", "asr_id": "a", "img2txt_id": "i"},
+ )
+ res = _run(module.set_tenant_info())
+ assert res["code"] == module.RetCode.AUTHENTICATION_ERROR, res
+ assert res["message"] == "No authorization.", res
+
+ # Authorized request: tenant_id matches current_user.id ("current-user")
_set_request_json(
monkeypatch,
module,
- {"tenant_id": "tenant-1", "llm_id": "l", "embd_id": "e", "asr_id": "a", "img2txt_id": "i"},
+ {"tenant_id": "current-user", "llm_id": "l", "embd_id": "e", "asr_id": "a", "img2txt_id": "i"},
)
def _raise_update(_tenant_id, _payload):
diff --git a/test/testcases/test_web_api/test_user_app/test_user_app_unit.py b/test/testcases/test_web_api/test_user_app/test_user_app_unit.py
index 6843a6478d..71df172d44 100644
--- a/test/testcases/test_web_api/test_user_app/test_user_app_unit.py
+++ b/test/testcases/test_web_api/test_user_app/test_user_app_unit.py
@@ -143,7 +143,7 @@ def _load_user_app(monkeypatch):
quart_mod = ModuleType("quart")
quart_mod.session = {}
- quart_mod.request = SimpleNamespace(args=_Args({}))
+ quart_mod.request = SimpleNamespace(args=_Args({}), path="/v1/user/setting")
async def _make_response(data):
return _DummyResponse(data)
@@ -175,6 +175,10 @@ def _load_user_app(monkeypatch):
monkeypatch.setitem(sys.modules, "api.db", db_mod)
api_pkg.db = db_mod
+ tenant_model_service_mod = ModuleType("api.db.joint_services.tenant_model_service")
+ tenant_model_service_mod.ensure_tenant_model_ids_for_params = lambda _tenant_id, params: params
+ monkeypatch.setitem(sys.modules, "api.db.joint_services.tenant_model_service", tenant_model_service_mod)
+
db_models_mod = ModuleType("api.db.db_models")
class _DummyTenantLLMModel:
@@ -854,10 +858,21 @@ def test_tenant_info_and_set_tenant_info_exception_matrix_unit(monkeypatch):
assert res["code"] == module.RetCode.EXCEPTION_ERROR, res
assert "tenant info boom" in res["message"], res
+ # IDOR: tenant_id from request body must match the authenticated user
+ _set_request_json(
+ monkeypatch,
+ module,
+ {"tenant_id": "other-tenant", "llm_id": "l", "embd_id": "e", "asr_id": "a", "img2txt_id": "i"},
+ )
+ res = _run(module.set_tenant_info())
+ assert res["code"] == module.RetCode.AUTHENTICATION_ERROR, res
+ assert res["message"] == "No authorization.", res
+
+ # Authorized request: tenant_id matches current_user.id ("current-user")
_set_request_json(
monkeypatch,
module,
- {"tenant_id": "tenant-1", "llm_id": "l", "embd_id": "e", "asr_id": "a", "img2txt_id": "i"},
+ {"tenant_id": "current-user", "llm_id": "l", "embd_id": "e", "asr_id": "a", "img2txt_id": "i"},
)
def _raise_update(_tenant_id, _payload):