Skip to content

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

fix(router): accept beta tokens on document image routes for shared chats (#20185)

jay777212be666b44aaf
diff --git a/internal/router/router.go b/internal/router/router.go
index 963cc1ff5c..60ad3bdce9 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -217,8 +217,15 @@ func (r *Router) Setup(engine *gin.Engine) {
agentBotGroup.GET("/:agent_id/inputs", r.botHandler.AgentbotInputs)
agentBotGroup.GET("/:agent_id/logs/:message_id", r.botHandler.GetAgentbotLogs)
- // Public bot endpoints (authenticated with an SDK beta token, not a session)
+ // Public bot endpoints (authenticated with an SDK beta token, not a session).
+ // The image/thumbnail routes accept the beta token like Python's
+ // login_required(auth_types=[AUTH_JWT, AUTH_API, AUTH_BETA]); shared
+ // chats render their images through them. Ownership is enforced in the
+ // service layer, not by the middleware group.
apiBetaAuth.GET("/documents/:id/preview", r.documentHandler.GetDocumentPreview)
+ apiBetaAuth.GET("/documents/:id/thumbnail", r.documentHandler.GetDocumentThumbnail)
+ apiBetaAuth.GET("/documents/:id/images/:image_id", r.documentHandler.GetDocumentImageForDocument)
+ apiBetaAuth.GET("/documents/images/:image_id", r.documentHandler.GetDocumentImage)
apiBetaAuth.GET("/thumbnails", r.documentHandler.GetThumbnail)
apiBetaAuth.POST("/agents/:canvas_id/upload", r.agentHandler.UploadAgentFile)
@@ -303,9 +310,6 @@ func (r *Router) Setup(engine *gin.Engine) {
documents.POST("/upload", r.documentHandler.UploadInfo)
documents.GET("", r.documentHandler.ListDocuments)
documents.GET("/artifact/:filename", r.documentHandler.GetDocumentArtifact)
- documents.GET("/:id/thumbnail", r.documentHandler.GetDocumentThumbnail)
- documents.GET("/:id/images/:image_id", r.documentHandler.GetDocumentImageForDocument)
- documents.GET("/images/:image_id", r.documentHandler.GetDocumentImage)
documents.GET("/:id", r.documentHandler.GetDocumentByID)
documents.PUT("/:id", r.documentHandler.UpdateDocument)
documents.DELETE("/:id", r.documentHandler.DeleteDocument)
diff --git a/internal/router/router_test.go b/internal/router/router_test.go
index 891294393f..891ccfec2c 100644
--- a/internal/router/router_test.go
+++ b/internal/router/router_test.go
@@ -191,3 +191,47 @@ func TestRouterSetupRegistersChatMindMapRoute(t *testing.T) {
t.Fatalf("status=%d body=%s; want auth middleware to handle registered Chat MindMap route", resp.Code, resp.Body.String())
}
}
+
+func TestRouterSetupRegistersDocumentImageRoutesWithBetaAuth(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+
+ engine := gin.New()
+ r := &Router{
+ authHandler: handler.NewAuthHandler(),
+ }
+ r.Setup(engine)
+
+ // These routes must sit behind BetaAuthMiddleware so the beta token a
+ // shared chat carries (?auth=...) can fetch the images its answer cites,
+ // matching Python's login_required(auth_types=[AUTH_JWT, AUTH_API,
+ // AUTH_BETA]). Behind the strict AuthMiddleware they answer HTTP 401 for
+ // beta tokens and shared chats render broken images.
+ paths := []string{
+ "/api/v1/documents/doc-1/thumbnail",
+ "/api/v1/documents/doc-1/images/kb-1-img-1",
+ "/api/v1/documents/images/kb-1-img-1",
+ }
+ for _, path := range paths {
+ t.Run(path, func(t *testing.T) {
+ resp := httptest.NewRecorder()
+ req := httptest.NewRequest(http.MethodGet, path, nil)
+ engine.ServeHTTP(resp, req)
+
+ if resp.Code == http.StatusNotFound {
+ t.Fatalf("GET %s returned 404; route is not registered", path)
+ }
+ if resp.Code == http.StatusUnauthorized {
+ t.Fatalf("GET %s returned 401; route is behind the strict auth middleware instead of the beta one", path)
+ }
+ var body struct {
+ Code common.ErrorCode `json:"code"`
+ }
+ if err := json.Unmarshal(resp.Body.Bytes(), &body); err != nil {
+ t.Fatalf("failed to decode response body: %v", err)
+ }
+ if body.Code != common.CodeDataError {
+ t.Fatalf("status=%d body=%s; want beta auth middleware to handle registered route", resp.Code, resp.Body.String())
+ }
+ })
+ }
+}
diff --git a/test/testcases/restful_api/test_document_raw_routes.py b/test/testcases/restful_api/test_document_raw_routes.py
index 1fc24cc56c..41d53599f4 100644
--- a/test/testcases/restful_api/test_document_raw_routes.py
+++ b/test/testcases/restful_api/test_document_raw_routes.py
@@ -15,7 +15,7 @@
#
import pytest
-from test.testcases.configs import INVALID_API_TOKEN
+from test.testcases.configs import INVALID_API_TOKEN, IS_GO_PROXY
from test.testcases.restful_api.helpers.assertions import assert_auth_error
from test.testcases.restful_api.helpers.client import RestClient
@@ -40,6 +40,12 @@ def test_document_image_invalid_id_contract(rest_client):
)
def test_private_document_images_require_auth(rest_client_noauth, path):
res = rest_client_noauth.get(path)
+ if IS_GO_PROXY:
+ assert res.status_code == 200, res.text
+ payload = res.json()
+ assert payload["code"] == 102, payload
+ assert payload["message"] == "Authorization is not valid!", payload
+ return
assert res.status_code == 401, res.text
assert_auth_error(res.json(), "missing token")
diff --git a/test/unit_test/deepdoc/vision/test_ocr_rotate_crop_limit.py b/test/unit_test/deepdoc/vision/test_ocr_rotate_crop_limit.py
index 4584ac9933..c94178320c 100644
--- a/test/unit_test/deepdoc/vision/test_ocr_rotate_crop_limit.py
+++ b/test/unit_test/deepdoc/vision/test_ocr_rotate_crop_limit.py
@@ -18,8 +18,8 @@ from deepdoc.vision.ocr import _OPENCV_REMAP_MAX_DIM, OCR
def _cv2_runtime_available() -> bool:
try:
- cv2.resize(np.zeros((2, 2, 3), dtype=np.uint8), (1, 1))
- return True
+ out = cv2.resize(np.zeros((2, 2, 3), dtype=np.uint8), (1, 1))
+ return isinstance(out, np.ndarray) and out.shape == (1, 1, 3)
except Exception:
return False
@@ -37,7 +37,7 @@ requires_cv2 = pytest.mark.skipif(
def _ocr_stub() -> OCR:
"""Bypass model loading; only exercise get_rotate_crop_image geometry."""
ocr = object.__new__(OCR)
- ocr.text_recognizer = [MagicMock()]
+ ocr.text_recognizer = [MagicMock(return_value=[[("text", 0.99)]])]
return ocr