mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-15 04:19:08 +00:00
Merge a4f021cfa0 into a81f507bca
This commit is contained in:
commit
95c7e3deeb
2 changed files with 95 additions and 2 deletions
|
|
@ -31,6 +31,7 @@ from RestrictedPython.Eval import default_guarded_getitem
|
|||
from RestrictedPython.Guards import (
|
||||
full_write_guard,
|
||||
guarded_iter_unpack_sequence,
|
||||
guarded_unpack_sequence,
|
||||
safer_getattr,
|
||||
)
|
||||
from RestrictedPython.transformer import copy_locations
|
||||
|
|
@ -46,7 +47,10 @@ class AsyncAwareTransformer(RestrictingNodeTransformer):
|
|||
has the same ``_fields`` as ``FunctionDef`` and the same security
|
||||
semantics, so we delegate to ``visit_FunctionDef`` — name check, argument
|
||||
check, print-scope wrapping, and any future additions to that method are
|
||||
inherited automatically. ``AsyncFor``/``AsyncWith``/``Await`` delegate to
|
||||
inherited automatically. ``AsyncWith`` gets the same treatment for the same
|
||||
reason: ``node_contents_visit`` only recurses into children, so routing it
|
||||
there left ``async with x as (a, b)`` without the unpack guard that
|
||||
``with x as (a, b)`` gets. ``AsyncFor``/``Await`` delegate to
|
||||
``node_contents_visit`` so their children still get transformed.
|
||||
|
||||
``visit_While`` rewrites ``while test:`` to ``while _budget_ok_() and test:``
|
||||
|
|
@ -77,7 +81,7 @@ class AsyncAwareTransformer(RestrictingNodeTransformer):
|
|||
return self.node_contents_visit(node)
|
||||
|
||||
def visit_AsyncWith(self, node: ast.AsyncWith) -> ast.AST:
|
||||
return self.node_contents_visit(node)
|
||||
return self.visit_With(node)
|
||||
|
||||
def visit_Await(self, node: ast.Await) -> ast.AST:
|
||||
return self.node_contents_visit(node)
|
||||
|
|
@ -137,6 +141,12 @@ def build_sandbox_globals() -> dict[str, object]:
|
|||
"_getitem_": default_guarded_getitem,
|
||||
"_getiter_": budgeted_iter,
|
||||
"_iter_unpack_sequence_": guarded_iter_unpack_sequence,
|
||||
# RestrictedPython emits _unpack_sequence_ for every tuple-unpacking
|
||||
# target that is not a for-loop target -- ``a, b = pair``,
|
||||
# ``a, *rest = seq``, ``with x as (a, b)`` -- and, like _inplacevar_,
|
||||
# ships no default. Without it those statements compile and then raise
|
||||
# NameError the first time the guardrail runs.
|
||||
"_unpack_sequence_": guarded_unpack_sequence,
|
||||
"_write_": full_write_guard,
|
||||
"_inplacevar_": _inplacevar_,
|
||||
"_budget_ok_": budget_ok,
|
||||
|
|
|
|||
|
|
@ -846,3 +846,86 @@ def test_config_without_timeout_uses_the_default():
|
|||
_initialize_from_config("custom-code-default-timeout", {}).execution_timeout
|
||||
== DEFAULT_EXECUTION_TIMEOUT_SECONDS
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("body", "expected"),
|
||||
[
|
||||
# Every one of these compiles fine and then raises
|
||||
# "NameError: name '_unpack_sequence_' is not defined" at call time when
|
||||
# the guard is missing from the sandbox globals.
|
||||
(" a, b = inputs['pair']\n return a + b\n", 3),
|
||||
(" a, (b, c) = inputs['nested']\n return a + b + c\n", 6),
|
||||
(" a, *rest = inputs['seq']\n return rest\n", [2, 3]),
|
||||
(" with inputs['ctx'] as (a, b):\n return a + b\n", 15),
|
||||
],
|
||||
)
|
||||
def test_tuple_unpacking_runs(body: str, expected):
|
||||
"""Ordinary tuple unpacking must work inside a guardrail, not NameError."""
|
||||
|
||||
class _Ctx:
|
||||
def __enter__(self):
|
||||
return (7, 8)
|
||||
|
||||
def __exit__(self, *args):
|
||||
return False
|
||||
|
||||
guardrail = _compile("def apply_guardrail(inputs, request_data, input_type):\n" + body)
|
||||
fn = guardrail._compiled_function
|
||||
assert fn is not None
|
||||
inputs = {"pair": (1, 2), "nested": (1, (2, 3)), "seq": [1, 2, 3], "ctx": _Ctx()}
|
||||
assert fn(inputs, {}, "request") == expected
|
||||
|
||||
|
||||
def _guard_names(source: str) -> set[str]:
|
||||
"""Names of the RestrictedPython guards the compiled bytecode calls."""
|
||||
import types
|
||||
|
||||
from litellm.proxy.guardrails.guardrail_hooks.custom_code.sandbox import (
|
||||
compile_sandboxed,
|
||||
)
|
||||
|
||||
found: set[str] = set()
|
||||
stack = [compile_sandboxed(source)]
|
||||
while stack:
|
||||
code = stack.pop()
|
||||
found |= {n for n in code.co_names + code.co_varnames if n.startswith("_") and n.endswith("_")}
|
||||
stack += [c for c in code.co_consts if isinstance(c, types.CodeType)]
|
||||
return found
|
||||
|
||||
|
||||
def test_async_with_gets_the_same_guards_as_with():
|
||||
"""`async with` is the async spelling of `with`; it must not enforce less."""
|
||||
sync_guards = _guard_names("def f(x):\n with x as (a, b):\n pass\n")
|
||||
async_guards = _guard_names("async def f(x):\n async with x as (a, b):\n pass\n")
|
||||
|
||||
assert "_unpack_sequence_" in sync_guards, "precondition: `with` unpacking is guarded"
|
||||
assert sync_guards <= async_guards
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_with_still_executes():
|
||||
"""Guarding `async with` must not break it."""
|
||||
from litellm.proxy.guardrails.guardrail_hooks.custom_code.sandbox import (
|
||||
build_sandbox_globals,
|
||||
compile_sandboxed,
|
||||
)
|
||||
|
||||
class _ACtx:
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
async def __aenter__(self):
|
||||
return self.value
|
||||
|
||||
async def __aexit__(self, *args):
|
||||
return False
|
||||
|
||||
sandbox_globals = build_sandbox_globals()
|
||||
exec( # noqa: S102
|
||||
compile_sandboxed(
|
||||
"async def f(ctx):\n async with ctx as (a, b):\n return a + b\n"
|
||||
),
|
||||
sandbox_globals,
|
||||
)
|
||||
assert await sandbox_globals["f"](_ACtx((5, 6))) == 11
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue