Skip to content

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

fix: reject non-string metadata selector document IDs (#20540)

helloxjade2afc01f21054
diff --git a/internal/handler/document.go b/internal/handler/document.go
index eafc322eb9..dd54e90f3a 100644
--- a/internal/handler/document.go
+++ b/internal/handler/document.go
@@ -2015,7 +2015,11 @@ func parseMetadataSelector(raw interface{}) (*document.MetadataSelector, string)
return nil, "document_ids must be a list."
}
for _, id := range ids {
- selector.DocumentIDs = append(selector.DocumentIDs, id.(string))
+ docID, ok := id.(string)
+ if !ok {
+ return nil, "document_ids must contain only strings."
+ }
+ selector.DocumentIDs = append(selector.DocumentIDs, docID)
}
}
if v, ok := m["metadata_condition"]; ok && v != nil {
diff --git a/internal/handler/document_test.go b/internal/handler/document_test.go
index 08c754ded3..da9379ee09 100644
--- a/internal/handler/document_test.go
+++ b/internal/handler/document_test.go
@@ -25,6 +25,7 @@ import (
"mime/multipart"
"net/http"
"net/http/httptest"
+ "reflect"
"strings"
"testing"
@@ -40,6 +41,84 @@ import (
"ragflow/internal/service/document"
)
+func TestParseMetadataSelectorDocumentIDs(t *testing.T) {
+ tests := []struct {
+ name string
+ body string
+ wantIDs []string
+ wantErr string
+ }{
+ {name: "missing selector", body: `{}`},
+ {name: "missing IDs", body: `{"selector":{}}`},
+ {name: "null IDs", body: `{"selector":{"document_ids":null}}`},
+ {name: "empty IDs", body: `{"selector":{"document_ids":[]}}`},
+ {name: "string IDs", body: `{"selector":{"document_ids":["doc-1","doc-2"]}}`, wantIDs: []string{"doc-1", "doc-2"}},
+ {name: "non-list IDs", body: `{"selector":{"document_ids":"doc-1"}}`, wantErr: "document_ids must be a list."},
+ {name: "numeric ID", body: `{"selector":{"document_ids":[123]}}`, wantErr: "document_ids must contain only strings."},
+ {name: "boolean ID", body: `{"selector":{"document_ids":[true]}}`, wantErr: "document_ids must contain only strings."},
+ {name: "null ID", body: `{"selector":{"document_ids":[null]}}`, wantErr: "document_ids must contain only strings."},
+ {name: "object ID", body: `{"selector":{"document_ids":[{}]}}`, wantErr: "document_ids must contain only strings."},
+ {name: "array ID", body: `{"selector":{"document_ids":[[]]}}`, wantErr: "document_ids must contain only strings."},
+ {name: "mixed IDs", body: `{"selector":{"document_ids":["doc-1",123]}}`, wantErr: "document_ids must contain only strings."},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ var body map[string]interface{}
+ if err := json.Unmarshal([]byte(tt.body), &body); err != nil {
+ t.Fatal(err)
+ }
+ selector, errMsg := parseMetadataSelector(body["selector"])
+ if errMsg != tt.wantErr {
+ t.Fatalf("error = %q, want %q", errMsg, tt.wantErr)
+ }
+ if tt.wantErr != "" {
+ if selector != nil {
+ t.Fatalf("selector = %#v, want nil", selector)
+ }
+ return
+ }
+ if selector == nil || !reflect.DeepEqual(selector.DocumentIDs, tt.wantIDs) {
+ t.Fatalf("selector = %#v, want IDs %v", selector, tt.wantIDs)
+ }
+ })
+ }
+}
+
+func TestDocumentMetadataBatchRejectsNonStringIDs(t *testing.T) {
+ setupDocumentPermissionDB(t, true)
+ h := &DocumentHandler{
+ datasetService: dataset.NewDatasetService(),
+ }
+ tests := []struct {
+ method string
+ path string
+ handler gin.HandlerFunc
+ }{
+ {http.MethodPost, "/api/v1/datasets/kb-owner/metadata/update", h.MetadataBatchUpdate},
+ {http.MethodPatch, "/api/v1/datasets/kb-owner/documents/metadatas", h.UpdateDocumentMetadatas},
+ }
+ for _, tt := range tests {
+ t.Run(tt.method, func(t *testing.T) {
+ c, w := setupGinContextWithUser(tt.method, tt.path, `{"selector":{"document_ids":[123]}}`)
+ c.Params = gin.Params{{Key: "dataset_id", Value: "kb-owner"}}
+ tt.handler(c)
+ if w.Code != http.StatusOK {
+ t.Fatalf("status = %d, want 200: %s", w.Code, w.Body.String())
+ }
+ var resp struct {
+ Code common.ErrorCode `json:"code"`
+ Message string `json:"message"`
+ }
+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
+ t.Fatal(err)
+ }
+ if resp.Code != common.CodeDataError || resp.Message != "document_ids must contain only strings." {
+ t.Fatalf("unexpected response: %s", w.Body.String())
+ }
+ })
+ }
+}
+
// fakeDocumentService implements documentServiceIface for handler tests.
type fakeDocumentService struct {
deleted int