Skip to content

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

fix(go): stop masking document preview failures as "document not found" and add tenant access check (#19348)

euvre7090ca834bda
diff --git a/internal/handler/document.go b/internal/handler/document.go
index bd2570a6f6..cb4ad6f9f4 100644
--- a/internal/handler/document.go
+++ b/internal/handler/document.go
@@ -70,7 +70,7 @@ type documentServiceIface interface {
DeleteDocumentAllMetadata(ctx context.Context, docID string) error
GetDocumentMetadataByID(ctx context.Context, docID string) (map[string]interface{}, error)
GetDocumentArtifact(ctx context.Context, filename, userID string) (*document.ArtifactResponse, error)
- GetDocumentPreview(ctx context.Context, docID string) (*document.DocumentPreview, error)
+ GetDocumentPreview(ctx context.Context, userID, docID string) (*document.DocumentPreview, error)
UploadLocalDocuments(ctx context.Context, kb *entity.Knowledgebase, tenantID string, files []*multipart.FileHeader, parentPath string, parserConfigOverride map[string]interface{}) ([]map[string]interface{}, []string)
UploadWebDocument(ctx context.Context, kb *entity.Knowledgebase, tenantID, name, url string) (map[string]interface{}, common.ErrorCode, error)
UploadEmptyDocument(ctx context.Context, kb *entity.Knowledgebase, tenantID, name string) (map[string]interface{}, common.ErrorCode, error)
@@ -255,6 +255,12 @@ func (h *DocumentHandler) GetDocumentArtifact(c *gin.Context) {
}
func (h *DocumentHandler) GetDocumentPreview(c *gin.Context) {
+ user, errorCode, errorMessage := GetUser(c)
+ if errorCode != common.CodeSuccess {
+ common.ErrorWithCode(c, errorCode, errorMessage)
+ return
+ }
+
docID := c.Param("id")
if docID == "" {
@@ -263,9 +269,23 @@ func (h *DocumentHandler) GetDocumentPreview(c *gin.Context) {
}
ctx := c.Request.Context()
- preview, err := h.documentService.GetDocumentPreview(ctx, docID)
+ preview, err := h.documentService.GetDocumentPreview(ctx, user.ID, docID)
if err != nil {
- common.ErrorWithCode(c, common.CodeDataError, "document not found")
+ switch {
+ case errors.Is(err, document.ErrPreviewDocumentNotFound):
+ common.ErrorWithCode(c, common.CodeDataError, "document not found")
+ case errors.Is(err, document.ErrPreviewFileEmpty):
+ common.ErrorWithCode(c, common.CodeDataError, "This file is empty.")
+ default:
+ // Surface the failure as a distinct server error (storage
+ // unreachable, missing object, bad address) instead of masking
+ // it as a missing document, while keeping the raw detail --
+ // which names the object-store bucket/key -- in the server
+ // log only.
+ common.Error("GetDocumentPreview failed", err,
+ zap.String("doc_id", docID), zap.String("user_id", user.ID))
+ common.ResponseWithCodeData(c, common.CodeServerError, nil, "Failed to load document preview")
+ }
return
}
diff --git a/internal/handler/document_test.go b/internal/handler/document_test.go
index b795ea2e13..8fa2646066 100644
--- a/internal/handler/document_test.go
+++ b/internal/handler/document_test.go
@@ -120,9 +120,15 @@ func (f *fakeDocumentService) GetDocumentArtifact(ctx context.Context, filename,
ForceAttachment: false,
}, nil
}
-func (f *fakeDocumentService) GetDocumentPreview(ctx context.Context, docID string) (*document.DocumentPreview, error) {
+func (f *fakeDocumentService) GetDocumentPreview(ctx context.Context, userID, docID string) (*document.DocumentPreview, error) {
if docID == "not-found" {
- return nil, fmt.Errorf("not found")
+ return nil, document.ErrPreviewDocumentNotFound
+ }
+ if docID == "empty-file" {
+ return nil, document.ErrPreviewFileEmpty
+ }
+ if docID == "storage-error" {
+ return nil, fmt.Errorf("read document object b/k: connection refused")
}
return &document.DocumentPreview{
Data: []byte("preview content"),
@@ -1758,6 +1764,61 @@ func TestGetDocumentPreview_NotFound(t *testing.T) {
if resp["code"] != float64(common.CodeDataError) {
t.Fatalf("expected code %d, got %v", common.CodeDataError, resp["code"])
}
+ if resp["message"] != "document not found" {
+ t.Fatalf("expected message %q, got %v", "document not found", resp["message"])
+ }
+}
+
+func TestGetDocumentPreview_EmptyFile(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ h := &DocumentHandler{
+ documentService: &fakeDocumentService{},
+ }
+ c, w := setupGinContextWithUser("GET", "/api/v1/documents/empty-file/preview", "")
+ c.Params = gin.Params{{Key: "id", Value: "empty-file"}}
+
+ h.GetDocumentPreview(c)
+
+ if w.Code != http.StatusOK {
+ t.Fatalf("expected 200, got %d", w.Code)
+ }
+ var resp map[string]interface{}
+ json.Unmarshal(w.Body.Bytes(), &resp)
+ if resp["code"] != float64(common.CodeDataError) {
+ t.Fatalf("expected code %d, got %v", common.CodeDataError, resp["code"])
+ }
+ if resp["message"] != "This file is empty." {
+ t.Fatalf("expected message %q, got %v", "This file is empty.", resp["message"])
+ }
+}
+
+func TestGetDocumentPreview_StorageErrorGenericMessage(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ h := &DocumentHandler{
+ documentService: &fakeDocumentService{},
+ }
+ c, w := setupGinContextWithUser("GET", "/api/v1/documents/storage-error/preview", "")
+ c.Params = gin.Params{{Key: "id", Value: "storage-error"}}
+
+ h.GetDocumentPreview(c)
+
+ if w.Code != http.StatusOK {
+ t.Fatalf("expected 200, got %d", w.Code)
+ }
+ var resp map[string]interface{}
+ json.Unmarshal(w.Body.Bytes(), &resp)
+ if resp["code"] != float64(common.CodeServerError) {
+ t.Fatalf("expected code %d, got %v", common.CodeServerError, resp["code"])
+ }
+ msg, _ := resp["message"].(string)
+ if msg != "Failed to load document preview" {
+ t.Fatalf("expected generic message, got %v", resp["message"])
+ }
+ // The storage detail (bucket/key, transport error) must stay in the
+ // server log, not in the client-visible message.
+ if strings.Contains(msg, "connection refused") || strings.Contains(msg, "b/k") {
+ t.Fatalf("storage detail leaked to client: %v", resp["message"])
+ }
}
func TestDownloadDocument_Success(t *testing.T) {
diff --git a/internal/service/document/document.go b/internal/service/document/document.go
index c3208e0f56..cac341d018 100644
--- a/internal/service/document/document.go
+++ b/internal/service/document/document.go
@@ -173,6 +173,16 @@ var (
ErrArtifactInvalidFilename = errors.New("invalid filename")
ErrArtifactInvalidFileType = errors.New("invalid file type")
ErrArtifactNotFound = errors.New("artifact not found")
+
+ // ErrPreviewDocumentNotFound covers both "document row missing" and
+ // "caller may not read this document" so the preview endpoint cannot be
+ // used to probe foreign document IDs. Mirrors the Python preview route.
+ ErrPreviewDocumentNotFound = errors.New("document not found")
+ // ErrPreviewFileEmpty marks a document whose backing object has zero
+ // bytes; the handler maps it to Python's "This file is empty."
+ // preview response, so the sentinel text itself is never sent to
+ // clients.
+ ErrPreviewFileEmpty = errors.New("preview file empty")
)
var artifactContentTypes = map[string]string{
diff --git a/internal/service/document/document_artifact.go b/internal/service/document/document_artifact.go
index 935033f247..d2fa9dd79d 100644
--- a/internal/service/document/document_artifact.go
+++ b/internal/service/document/document_artifact.go
@@ -194,15 +194,25 @@ func shouldForceArtifactAttachment(ext, contentType string) bool {
return ok
}
-func (s *DocumentService) GetDocumentPreview(ctx context.Context, docID string) (*DocumentPreview, error) {
+func (s *DocumentService) GetDocumentPreview(ctx context.Context, userID, docID string) (*DocumentPreview, error) {
doc, err := s.documentDAO.GetByID(ctx, dao.DB, docID)
- if err != nil {
- return nil, err
+ if err != nil || doc == nil {
+ return nil, ErrPreviewDocumentNotFound
+ }
+
+ // Reuse KnowledgebaseDAO.Accessible — the exact rule the chunk list on
+ // the same page uses — so the two panels can never disagree: the owning
+ // tenant always, and tenant members only when the dataset's permission
+ // is TEAM. A denial stays indistinguishable from a missing document so
+ // an unauthorized caller cannot probe document IDs (mirrors Python
+ // DocumentService.accessible in the preview path).
+ if !s.kbDAO.Accessible(ctx, dao.DB, doc.KbID, userID) {
+ return nil, ErrPreviewDocumentNotFound
}
bucket, name, err := s.GetDocumentStorageAddress(ctx, doc)
if err != nil {
- return nil, err
+ return nil, fmt.Errorf("resolve storage address for document %s: %w", docID, err)
}
storageImpl := storage.GetStorageFactory().GetStorage()
@@ -212,10 +222,10 @@ func (s *DocumentService) GetDocumentPreview(ctx context.Context, docID string)
data, err := storageImpl.Get(ctx, bucket, name)
if err != nil {
- return nil, err
+ return nil, fmt.Errorf("read document object %s/%s: %w", bucket, name, err)
}
if len(data) == 0 {
- return nil, ErrArtifactNotFound
+ return nil, ErrPreviewFileEmpty
}
fileName := ""
diff --git a/internal/service/document/document_test.go b/internal/service/document/document_test.go
index ca7c5785b9..0cb58c004b 100644
--- a/internal/service/document/document_test.go
+++ b/internal/service/document/document_test.go
@@ -1823,9 +1823,144 @@ func TestGetDocumentPreview_DocumentNotFound(t *testing.T) {
svc := testDocumentService(t)
ctx := t.Context()
- _, err := svc.GetDocumentPreview(ctx, "nonexistent")
+ _, err := svc.GetDocumentPreview(ctx, "tenant-1", "nonexistent")
+ if !errors.Is(err, ErrPreviewDocumentNotFound) {
+ t.Errorf("expected ErrPreviewDocumentNotFound, got %v", err)
+ }
+}
+
+// insertTestPreviewDoc creates a doc with a name/location and seeds its blob.
+func insertTestPreviewDoc(t *testing.T, db *gorm.DB, mockStorage *fakeUploadStorage, id, kbID, content string) {
+ t.Helper()
+ name := id + ".txt"
+ loc := id + ".txt"
+ doc := &entity.Document{
+ ID: id,
+ KbID: kbID,
+ ParserID: "naive",
+ ParserConfig: entity.JSONMap{},
+ Name: &name,
+ Location: &loc,
+ Suffix: "txt",
+ Status: sptr("1"),
+ }
+ if err := db.Create(doc).Error; err != nil {
+ t.Fatalf("insert test doc: %v", err)
+ }
+ if err := mockStorage.Put(context.Background(), kbID, loc, []byte(content)); err != nil {
+ t.Fatalf("seed preview object: %v", err)
+ }
+}
+
+func useFakeStorage(t *testing.T) *fakeUploadStorage {
+ t.Helper()
+ mockStorage := newFakeUploadStorage()
+ factory := storage.GetStorageFactory()
+ origStorage := factory.GetStorage()
+ factory.SetStorage(mockStorage)
+ t.Cleanup(func() { factory.SetStorage(origStorage) })
+ return mockStorage
+}
+
+func TestGetDocumentPreview_AccessControl(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ mockStorage := useFakeStorage(t)
+ insertTestKB(t, "kb-prev", "tenant-1", 1, 0, 0)
+ insertTestPreviewDoc(t, db, mockStorage, "doc-prev", "kb-prev", "preview body")
+
+ svc := testDocumentService(t)
+ ctx := t.Context()
+
+ // Dataset owner reads the original file.
+ p, err := svc.GetDocumentPreview(ctx, "tenant-1", "doc-prev")
+ if err != nil {
+ t.Fatalf("owner preview: %v", err)
+ }
+ if string(p.Data) != "preview body" {
+ t.Fatalf("unexpected body: %q", string(p.Data))
+ }
+
+ // A member of the dataset's tenant reads it too.
+ if err := db.Create(&entity.UserTenant{
+ ID: "ut-prev", UserID: "user-2", TenantID: "tenant-1",
+ Role: "normal", InvitedBy: "tenant-1", Status: sptr("1"),
+ }).Error; err != nil {
+ t.Fatalf("insert user_tenant: %v", err)
+ }
+ if _, err = svc.GetDocumentPreview(ctx, "user-2", "doc-prev"); err != nil {
+ t.Fatalf("team member preview: %v", err)
+ }
+
+ // An unrelated user gets the same answer as for a missing document.
+ _, err = svc.GetDocumentPreview(ctx, "tenant-2", "doc-prev")
+ if !errors.Is(err, ErrPreviewDocumentNotFound) {
+ t.Fatalf("stranger preview: expected ErrPreviewDocumentNotFound, got %v", err)
+ }
+
+ // A private (ME) dataset stays owner-only even for tenant members: the
+ // same rule the chunk list applies via KnowledgebaseDAO.Accessible.
+ if err := db.Create(&entity.Knowledgebase{
+ ID: "kb-prev-me", TenantID: "tenant-1", Name: "private-kb", EmbdID: "embd-1",
+ CreatedBy: "user-1", Permission: string(entity.TenantPermissionMe),
+ DocNum: 1, Status: sptr(string(entity.StatusValid)),
+ }).Error; err != nil {
+ t.Fatalf("insert private kb: %v", err)
+ }
+ insertTestPreviewDoc(t, db, mockStorage, "doc-prev-me", "kb-prev-me", "private body")
+
+ if _, err = svc.GetDocumentPreview(ctx, "user-2", "doc-prev-me"); !errors.Is(err, ErrPreviewDocumentNotFound) {
+ t.Fatalf("team member on private dataset: expected ErrPreviewDocumentNotFound, got %v", err)
+ }
+ p, err = svc.GetDocumentPreview(ctx, "tenant-1", "doc-prev-me")
+ if err != nil {
+ t.Fatalf("owner preview private dataset: %v", err)
+ }
+ if string(p.Data) != "private body" {
+ t.Fatalf("unexpected private body: %q", string(p.Data))
+ }
+}
+
+func TestGetDocumentPreview_EmptyObject(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ mockStorage := useFakeStorage(t)
+ insertTestKB(t, "kb-prev-empty", "tenant-1", 1, 0, 0)
+ insertTestPreviewDoc(t, db, mockStorage, "doc-prev-empty", "kb-prev-empty", "")
+
+ svc := testDocumentService(t)
+ _, err := svc.GetDocumentPreview(t.Context(), "tenant-1", "doc-prev-empty")
+ if !errors.Is(err, ErrPreviewFileEmpty) {
+ t.Fatalf("expected ErrPreviewFileEmpty, got %v", err)
+ }
+}
+
+func TestGetDocumentPreview_MissingObjectSurfacesStorageError(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ useFakeStorage(t)
+ insertTestKB(t, "kb-prev-miss", "tenant-1", 1, 0, 0)
+ // Document exists but its blob was never seeded.
+ name := "doc.txt"
+ loc := "doc.txt"
+ if err := db.Create(&entity.Document{
+ ID: "doc-prev-miss", KbID: "kb-prev-miss", ParserID: "naive",
+ ParserConfig: entity.JSONMap{}, Name: &name, Location: &loc,
+ Suffix: "txt", Status: sptr("1"),
+ }).Error; err != nil {
+ t.Fatalf("insert test doc: %v", err)
+ }
+
+ svc := testDocumentService(t)
+ _, err := svc.GetDocumentPreview(t.Context(), "tenant-1", "doc-prev-miss")
if err == nil {
- t.Error("expected error for nonexistent document")
+ t.Fatal("expected storage read error")
+ }
+ if errors.Is(err, ErrPreviewDocumentNotFound) {
+ t.Fatalf("storage failure must not be masked as document not found, got %v", err)
+ }
+ if !strings.Contains(err.Error(), "kb-prev-miss") {
+ t.Fatalf("error should carry the storage address for diagnosis: %v", err)
}
}
diff --git a/web/src/components/document-preview/txt-preview.tsx b/web/src/components/document-preview/txt-preview.tsx
index ae2bad1669..56349ce2c0 100644
--- a/web/src/components/document-preview/txt-preview.tsx
+++ b/web/src/components/document-preview/txt-preview.tsx
@@ -26,6 +26,31 @@ export const TxtPreviewer = ({ className, url }: TxtPreviewerProps) => {
// const url = useGetDocumentUrl();
const [loading, setLoading] = useState(false);
const [data, setData] = useState<string>('');
+
+ // Business errors come back as HTTP 200 JSON bodies (e.g.
+ // {"code":102,"message":"document not found"}). Detect that shape and
+ // surface the message instead of painting raw JSON as file content.
+ const extractBusinessError = (text: string): string | null => {
+ try {
+ const parsed = JSON.parse(text) as {
+ code?: unknown;
+ message?: unknown;
+ };
+ if (
+ parsed &&
+ typeof parsed === 'object' &&
+ typeof parsed.code === 'number' &&
+ parsed.code !== 0 &&
+ typeof parsed.message === 'string'
+ ) {
+ return parsed.message;
+ }
+ } catch {
+ // Plain text document content.
+ }
+ return null;
+ };
+
const fetchTxt = async () => {
setLoading(true);
const res = await request(url, {
@@ -38,6 +63,13 @@ export const TxtPreviewer = ({ className, url }: TxtPreviewerProps) => {
});
// Handles UTF-8/UTF-16 (BOM) as well as GB2312/GBK files
const text = await decodeBlobText(res.data);
+ const businessError = extractBusinessError(text);
+ if (businessError) {
+ message.error(businessError);
+ setData('');
+ setLoading(false);
+ return;
+ }
setData(text);
setLoading(false);
};