From 46897d6fa44296bdb32f6825453ee73eb2c13b02 Mon Sep 17 00:00:00 2001 From: jony376 Date: Sun, 10 May 2026 22:26:05 -0700 Subject: [PATCH] Fix: bind memory message `user_id` to authenticated user for JWT auth (#14745) ### Related issues Closes #14744 ### What problem does this PR solve? The Memory REST endpoint `POST /api/v1/messages` previously persisted whatever `user_id` the client sent in the JSON body. Memory rows were therefore attributed to an arbitrary string, even when the caller authenticated as a normal workspace user via JWT (browser/session-style bearer token decoded into an access token). That broke attribution and audit semantics for shared memories (team visibility): any authorized writer could spoof another subject id. The Python SDK already sends an optional `user_id` for integrations using **API keys** (`APIToken`) to tag an external subject distinct from the tenant owner user. ### Solution - Record **`g.auth_via_api_token`** in `_load_user` (`api/apps/__init__.py`): set `True` only when authentication resolves via `APIToken`, otherwise `False` after JWT-based login succeeds. - In **`POST /messages`** (`memory_api.add_message`): if the request was authenticated with an API key, keep accepting optional `user_id` from the body (default empty string). For JWT-authenticated users, **always** set stored `user_id` to **`current_user.id`** and ignore the client field. - Guard reads of `g` with **`RuntimeError`** handling so isolated imports or tests without a Quart application context do not fail when resolving `user_id`. - Document on **`RAGFlow.add_message`** that `user_id` is only meaningful for API-key authentication. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) - [ ] New Feature (non-breaking change which adds functionality) - [ ] Documentation Update - [ ] Refactoring - [ ] Performance Improvement - [ ] Other (please describe): ### Testing - `python -m py_compile` on modified modules (`api/apps/__init__.py`, `api/apps/restful_apis/memory_api.py`). - Recommended: run web/SDK memory message tests (`test_add_message`, `test_message_routes_unit`) against a full environment with `quart` and configured services. ### Notes for reviewers - Behavior change **only** for callers using JWT-style authorization on `POST /messages`; API-key callers keep prior optional `user_id` semantics. Co-authored-by: jony376 Co-authored-by: Cursor --- api/apps/__init__.py | 2 ++ api/apps/restful_apis/memory_api.py | 14 ++++++++++++-- sdk/python/ragflow_sdk/ragflow.py | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/api/apps/__init__.py b/api/apps/__init__.py index e26b2c39af..6df12f47a8 100644 --- a/api/apps/__init__.py +++ b/api/apps/__init__.py @@ -130,6 +130,7 @@ def _load_user(): jwt = Serializer(secret_key=settings.get_secret_key()) authorization = request.headers.get("Authorization") g.user = None + g.auth_via_api_token = False if not authorization: return _load_user_from_session() @@ -175,6 +176,7 @@ def _load_user(): if not user[0].access_token or not user[0].access_token.strip(): logging.warning(f"User {user[0].email} has empty access_token in database") return _load_user_from_session() + g.auth_via_api_token = True g.user = user[0] return user[0] logging.warning(f"load_user: No user found for tenant_id={objs[0].tenant_id} from APIToken") diff --git a/api/apps/restful_apis/memory_api.py b/api/apps/restful_apis/memory_api.py index c361d816b6..1be67b8a70 100644 --- a/api/apps/restful_apis/memory_api.py +++ b/api/apps/restful_apis/memory_api.py @@ -17,7 +17,7 @@ import logging import os import time -from quart import request +from quart import request, g from common.constants import LLMType, RetCode from common.exceptions import ArgumentException, NotFoundException from api.apps import login_required, current_user @@ -188,8 +188,18 @@ async def add_message(): req = await get_request_json() memory_ids = req["memory_id"] + # JWT / session users cannot spoof attribution; API-key callers may supply an external subject id. + try: + trust_client_subject = bool(getattr(g, "auth_via_api_token", False)) + except RuntimeError: + trust_client_subject = False + if trust_client_subject: + effective_user_id = req.get("user_id", "") + else: + effective_user_id = current_user.id + message_dict = { - "user_id": req.get("user_id"), + "user_id": effective_user_id, "agent_id": req["agent_id"], "session_id": req["session_id"], "user_input": req["user_input"], diff --git a/sdk/python/ragflow_sdk/ragflow.py b/sdk/python/ragflow_sdk/ragflow.py index fe0a683719..679f5ba5f3 100644 --- a/sdk/python/ragflow_sdk/ragflow.py +++ b/sdk/python/ragflow_sdk/ragflow.py @@ -334,6 +334,7 @@ class RAGFlow: raise Exception(res["message"]) def add_message(self, memory_id: list[str], agent_id: str, session_id: str, user_input: str, agent_response: str, user_id: str = "") -> str: + """Append messages to memories; ``user_id`` is forwarded only for API-key auth (external subject).""" payload = { "memory_id": memory_id, "agent_id": agent_id,