mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-06 19:38:36 +08:00
## Summary While fixing #16467 (IterationItem crash on `@` in user-defined output keys), an audit of `agent/**/*.py` revealed **three additional sites** with the same vulnerability. This PR hardens all of them with `maxsplit=1` and adds regression tests. This is **defense-in-depth hardening**, not a behavior change. The current `variable_ref_patt` regex constrains `var_nm` to `[A-Za-z0-9_.-]+`, so single-`@` templates resolve exactly as before. The `maxsplit=1` only kicks in if the trailing side itself contains `@` — currently unreachable from the public DSL surface, but trivially exploitable the moment a user-defined output key happens to contain `@` (e.g. `user@email`) or the regex is ever relaxed. > **Note on issue scope**: The primary fix for #16467 (the `list_tenant_added_models` `ValueError` crash on `@` in model names) is in PR #16468. This PR is a **follow-up hardening sweep** of the same vulnerability class found in `agent/` during that audit; it does not duplicate or replace #16468. ## Sites hardened | File | Line | Method | |------|------|--------| | `agent/canvas.py` | 206 | `Graph.get_variable_value` | | `agent/canvas.py` | 256 | `Graph.set_variable_value` | | `agent/component/base.py` | 533 | `ComponentBase.get_input_elements_from_text` | | `agent/component/iterationitem.py` | 88 | `IterationItem.output_collation` | All now use `split("@", 1)` with an inline comment explaining the rationale. The trailing side keeps any embedded `@`. ## Sites already safe (audited but left alone) | File | Reason safe | |------|------------| | `agent/canvas.py:708` (`is_reff`) | Pre-checks `len(arr) != 2` | | `agent/component/categorize.py` | Uses `rsplit` | | `agent/component/iteration.py` | Pre-validates via regex | | Other call sites | `rsplit` or regex pre-validation | ## Regression tests 9 new tests across 2 files, all `pytest.mark.p2`: | File | Tests | |------|-------| | `test/unit_test/agent/test_canvas_at_split.py` | 6 — `get_variable_value`, `set_variable_value`, round-trip, single-`@`, missing-component | | `test/unit_test/agent/component/test_iterationitem_at_split.py` | 3 — `output_collation` with `@` in var, single-`@`, non-matching cid | Each test was **verified to fail with `ValueError: too many values to unpack (expected 2)`** when the corresponding fix is temporarily reverted, confirming the tests actually catch the bug rather than just exercising the happy path. ## Test results ``` 9 passed in 0.04s ``` Full agent unit suite also clean (38 passed, 3 skipped; 6 unrelated pre-existing collection errors from missing `peewee`/`requests` in local venv — not caused by this PR). ## Related - Issue: #16467 - Primary fix PR: #16468 (closes the issue) - This PR: defense-in-depth follow-up, intentionally non-blocking on #16467 --------- Co-authored-by: skbs-eng <skbs-eng@users.noreply.github.com>
102 lines
3.3 KiB
Python
102 lines
3.3 KiB
Python
#
|
|
# Copyright 2024 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.
|
|
#
|
|
from abc import ABC
|
|
|
|
from agent.component.base import ComponentBase, ComponentParamBase
|
|
|
|
|
|
class IterationItemParam(ComponentParamBase):
|
|
"""
|
|
Define the IterationItem component parameters.
|
|
"""
|
|
|
|
def check(self):
|
|
return True
|
|
|
|
|
|
class IterationItem(ComponentBase, ABC):
|
|
component_name = "IterationItem"
|
|
|
|
def __init__(self, canvas, id, param: ComponentParamBase):
|
|
super().__init__(canvas, id, param)
|
|
self._idx = 0
|
|
|
|
def _invoke(self, **kwargs):
|
|
if self.check_if_canceled("IterationItem processing"):
|
|
return
|
|
|
|
parent = self.get_parent()
|
|
arr = self._canvas.get_variable_value(parent._param.items_ref)
|
|
if not isinstance(arr, list):
|
|
self._idx = -1
|
|
raise Exception(parent._param.items_ref + " must be an array, but its type is " + str(type(arr)))
|
|
|
|
if self._idx > 0:
|
|
if self.check_if_canceled("IterationItem processing"):
|
|
return
|
|
self.output_collation()
|
|
|
|
if self._idx >= len(arr):
|
|
self._idx = -1
|
|
return
|
|
|
|
if self.check_if_canceled("IterationItem processing"):
|
|
return
|
|
|
|
current_item = arr[self._idx]
|
|
self.set_output("item", current_item)
|
|
# Keep `result` as a compatibility alias because existing DSL examples
|
|
# and downstream references may still consume IterationItem via `@result`.
|
|
self.set_output("result", current_item)
|
|
self.set_output("index", self._idx)
|
|
|
|
self._idx += 1
|
|
|
|
def output_collation(self):
|
|
pid = self.get_parent()._id
|
|
for cid in self._canvas.components.keys():
|
|
obj = self._canvas.get_component_obj(cid)
|
|
p = obj.get_parent()
|
|
if not p:
|
|
continue
|
|
if p._id != pid:
|
|
continue
|
|
|
|
if p.component_name.lower() in ["categorize", "message", "switch", "userfillup", "iterationitem"]:
|
|
continue
|
|
|
|
for k, o in p._param.outputs.items():
|
|
if "ref" not in o:
|
|
continue
|
|
# Use maxsplit=1 so an `@` legitimately embedded in `var`
|
|
# (e.g. a user-defined output key that happens to contain
|
|
# '@') does not raise `ValueError: too many values to unpack`.
|
|
# `_cid` is system-generated and never contains '@'.
|
|
_cid, var = o["ref"].split("@", 1)
|
|
if _cid != cid:
|
|
continue
|
|
res = p.output(k)
|
|
if not res:
|
|
res = []
|
|
res.append(obj.output(var))
|
|
p.set_output(k, res)
|
|
|
|
def end(self):
|
|
return self._idx == -1
|
|
|
|
def thoughts(self) -> str:
|
|
return "Next turn..."
|