mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-29 04:08:12 +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:
85
common/tag_feature_utils.py
Normal file
85
common/tag_feature_utils.py
Normal file
@@ -0,0 +1,85 @@
|
||||
#
|
||||
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
import ast
|
||||
import json
|
||||
import math
|
||||
|
||||
|
||||
def parse_tag_features(raw, *, allow_json_string=True, allow_python_literal=False):
|
||||
if raw is None:
|
||||
return {}
|
||||
|
||||
parsed = raw
|
||||
if isinstance(raw, str):
|
||||
raw = raw.strip()
|
||||
if not raw:
|
||||
return {}
|
||||
parsed = None
|
||||
if allow_json_string:
|
||||
try:
|
||||
parsed = json.loads(raw)
|
||||
except Exception:
|
||||
parsed = None
|
||||
if parsed is None and allow_python_literal:
|
||||
try:
|
||||
parsed = ast.literal_eval(raw)
|
||||
except Exception:
|
||||
parsed = None
|
||||
if parsed is None:
|
||||
return {}
|
||||
elif not isinstance(raw, dict):
|
||||
return {}
|
||||
|
||||
if not isinstance(parsed, dict):
|
||||
return {}
|
||||
|
||||
cleaned = {}
|
||||
for key, value in parsed.items():
|
||||
if not isinstance(key, str):
|
||||
continue
|
||||
key = key.strip()
|
||||
if not key:
|
||||
continue
|
||||
if isinstance(value, bool):
|
||||
continue
|
||||
if isinstance(value, (int, float)) and math.isfinite(float(value)):
|
||||
cleaned[key] = float(value)
|
||||
return cleaned
|
||||
|
||||
|
||||
def validate_tag_features(raw):
|
||||
if raw is None:
|
||||
return None
|
||||
|
||||
if not isinstance(raw, dict):
|
||||
raise ValueError("must be an object mapping string tags to finite numeric scores")
|
||||
|
||||
cleaned = {}
|
||||
for key, value in raw.items():
|
||||
if not isinstance(key, str):
|
||||
raise ValueError("keys must be strings")
|
||||
key = key.strip()
|
||||
if not key:
|
||||
raise ValueError("keys must be non-empty strings")
|
||||
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
||||
raise ValueError("values must be finite numbers")
|
||||
numeric = float(value)
|
||||
if not math.isfinite(numeric):
|
||||
raise ValueError("values must be finite numbers")
|
||||
cleaned[key] = numeric
|
||||
|
||||
return cleaned
|
||||
Reference in New Issue
Block a user