mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-13 16:38:26 +08:00
Fix tag_feas code injection in retrieval ranking (#13923)
## Summary - remove eval-based parsing from retrieval rank feature scoring - validate `tag_feas` at write time in chunk APIs and SDK routes - add regression tests for safe parsing and malicious payload rejection ## Details `tag_feas` is intended to be structured rank-feature data, but the retrieval ranking path was evaluating stored values as Python expressions. This change treats `tag_feas` strictly as data. ### What changed - replace `eval()` in `rag/nlp/search.py` with safe parsing via `json.loads()` and optional `ast.literal_eval()` compatibility for legacy Python-dict strings - strictly filter parsed values down to `dict[str, finite number]` - reject invalid `tag_feas` payloads at write time in web chunk routes and SDK document chunk routes - add focused regression tests to prove executable strings are ignored and invalid payloads are rejected ## Validation - `python -m pytest test/unit_test/common/test_tag_feature_utils.py test/unit_test/rag/test_rank_feature_scores.py -q` --------- Co-authored-by: unknown <zhenglinkai@CCN.Local> Co-authored-by: Yingfeng Zhang <yingfeng.zhang@gmail.com>
This commit is contained in:
@@ -584,6 +584,14 @@ def test_set_chunk_bytes_qa_image_and_guard_matrix_unit(monkeypatch):
|
||||
"get_by_id",
|
||||
lambda _doc_id: (True, _DummyDoc(doc_id="doc-1", parser_id=module.ParserType.NAIVE)),
|
||||
)
|
||||
_set_request_json(
|
||||
monkeypatch,
|
||||
module,
|
||||
{"doc_id": "doc-1", "chunk_id": "chunk-1", "content_with_weight": "abc", "tag_feas": [0.1]},
|
||||
)
|
||||
res = _run(module.set())
|
||||
assert "`tag_feas` must be an object mapping string tags to finite numeric scores" in res["message"], res
|
||||
|
||||
_set_request_json(
|
||||
monkeypatch,
|
||||
module,
|
||||
@@ -594,7 +602,7 @@ def test_set_chunk_bytes_qa_image_and_guard_matrix_unit(monkeypatch):
|
||||
"important_kwd": ["important"],
|
||||
"question_kwd": ["question"],
|
||||
"tag_kwd": ["tag"],
|
||||
"tag_feas": [0.1],
|
||||
"tag_feas": {"tag": 0.1},
|
||||
"available_int": 0,
|
||||
},
|
||||
)
|
||||
@@ -762,6 +770,14 @@ def test_create_chunk_guards_pagerank_and_success_unit(monkeypatch):
|
||||
assert res["message"] == "Knowledgebase not found!", res
|
||||
|
||||
monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, SimpleNamespace(pagerank=0.8)))
|
||||
_set_request_json(
|
||||
monkeypatch,
|
||||
module,
|
||||
{"doc_id": "doc-1", "content_with_weight": "chunk", "tag_feas": [0.2]},
|
||||
)
|
||||
res = _run(module.create())
|
||||
assert "`tag_feas` must be an object mapping string tags to finite numeric scores" in res["message"], res
|
||||
|
||||
_set_request_json(
|
||||
monkeypatch,
|
||||
module,
|
||||
@@ -770,7 +786,7 @@ def test_create_chunk_guards_pagerank_and_success_unit(monkeypatch):
|
||||
"content_with_weight": "chunk",
|
||||
"important_kwd": ["i1"],
|
||||
"question_kwd": ["q1"],
|
||||
"tag_feas": [0.2],
|
||||
"tag_feas": {"tag": 0.2},
|
||||
},
|
||||
)
|
||||
res = _run(module.create())
|
||||
|
||||
@@ -166,7 +166,7 @@ class TestAddChunk:
|
||||
payload = {
|
||||
"doc_id": doc_id,
|
||||
"content_with_weight": "chunk with tags",
|
||||
"tag_feas": [0.1, 0.2],
|
||||
"tag_feas": {"tag1": 0.1, "tag2": 0.2},
|
||||
"important_kwd": ["tag"],
|
||||
"question_kwd": ["question"],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user