2024-12-27 11:38:33 +08:00
|
|
|
#
|
|
|
|
|
# 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
|
fix(agent): defend against `@` in var names at all template-split sites (#16469)
## 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>
2026-07-03 16:56:27 +05:30
|
|
|
|
2024-12-27 11:38:33 +08:00
|
|
|
from agent.component.base import ComponentBase, ComponentParamBase
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IterationItemParam(ComponentParamBase):
|
|
|
|
|
"""
|
|
|
|
|
Define the IterationItem component parameters.
|
|
|
|
|
"""
|
2026-07-03 12:53:39 +08:00
|
|
|
|
2024-12-27 11:38:33 +08:00
|
|
|
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
|
|
|
|
|
|
2025-07-30 19:41:09 +08:00
|
|
|
def _invoke(self, **kwargs):
|
2025-11-11 17:36:48 +08:00
|
|
|
if self.check_if_canceled("IterationItem processing"):
|
|
|
|
|
return
|
|
|
|
|
|
2024-12-27 11:38:33 +08:00
|
|
|
parent = self.get_parent()
|
2025-07-30 19:41:09 +08:00
|
|
|
arr = self._canvas.get_variable_value(parent._param.items_ref)
|
|
|
|
|
if not isinstance(arr, list):
|
2025-03-11 19:56:21 +08:00
|
|
|
self._idx = -1
|
2026-07-03 12:53:39 +08:00
|
|
|
raise Exception(parent._param.items_ref + " must be an array, but its type is " + str(type(arr)))
|
2025-03-11 19:56:21 +08:00
|
|
|
|
2025-07-30 19:41:09 +08:00
|
|
|
if self._idx > 0:
|
2025-11-11 17:36:48 +08:00
|
|
|
if self.check_if_canceled("IterationItem processing"):
|
|
|
|
|
return
|
2025-07-30 19:41:09 +08:00
|
|
|
self.output_collation()
|
|
|
|
|
|
|
|
|
|
if self._idx >= len(arr):
|
2024-12-27 11:38:33 +08:00
|
|
|
self._idx = -1
|
2025-07-30 19:41:09 +08:00
|
|
|
return
|
|
|
|
|
|
2025-11-11 17:36:48 +08:00
|
|
|
if self.check_if_canceled("IterationItem processing"):
|
|
|
|
|
return
|
|
|
|
|
|
2026-05-12 13:05:21 +08:00
|
|
|
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)
|
2025-07-30 19:41:09 +08:00
|
|
|
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
|
|
|
|
|
|
2026-04-02 10:35:28 +08:00
|
|
|
if p.component_name.lower() in ["categorize", "message", "switch", "userfillup", "iterationitem"]:
|
2025-07-30 19:41:09 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
for k, o in p._param.outputs.items():
|
|
|
|
|
if "ref" not in o:
|
|
|
|
|
continue
|
fix(agent): defend against `@` in var names at all template-split sites (#16469)
## 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>
2026-07-03 16:56:27 +05:30
|
|
|
# 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)
|
2025-07-30 19:41:09 +08:00
|
|
|
if _cid != cid:
|
|
|
|
|
continue
|
|
|
|
|
res = p.output(k)
|
|
|
|
|
if not res:
|
|
|
|
|
res = []
|
|
|
|
|
res.append(obj.output(var))
|
|
|
|
|
p.set_output(k, res)
|
2024-12-27 11:38:33 +08:00
|
|
|
|
|
|
|
|
def end(self):
|
|
|
|
|
return self._idx == -1
|
|
|
|
|
|
2025-07-31 15:13:45 +08:00
|
|
|
def thoughts(self) -> str:
|
2025-11-11 17:36:48 +08:00
|
|
|
return "Next turn..."
|