|
12 | 12 | """ |
13 | 13 |
|
14 | 14 | import base64 |
| 15 | +import enum |
15 | 16 | import os |
16 | 17 | import pickle |
17 | 18 | import tempfile |
@@ -49,6 +50,39 @@ def __reduce__(self) -> tuple[Any, tuple[str]]: |
49 | 50 | return (_base64_to_unpickle, (self.nested_payload,)) |
50 | 51 |
|
51 | 52 |
|
| 53 | +class _NestedTypeContainer: |
| 54 | + @dataclass |
| 55 | + class NestedType: |
| 56 | + value: int |
| 57 | + |
| 58 | + |
| 59 | +class _NestedSetStateContainer: |
| 60 | + class Evil: |
| 61 | + setstate_called = False |
| 62 | + |
| 63 | + def __init__(self) -> None: |
| 64 | + self.value = 42 |
| 65 | + |
| 66 | + def __setstate__(self, state: dict[str, object]) -> None: |
| 67 | + type(self).setstate_called = True |
| 68 | + self.__dict__.update(state) |
| 69 | + |
| 70 | + |
| 71 | +class _NamedPickleColor(enum.Enum): |
| 72 | + RED = 1 |
| 73 | + BLUE = 2 |
| 74 | + CRIMSON = 1 |
| 75 | + |
| 76 | + def __reduce_ex__(self, proto: object) -> tuple[Any, tuple[type[enum.Enum], str]]: |
| 77 | + del proto |
| 78 | + return (getattr, (type(self), self.name)) |
| 79 | + |
| 80 | + |
| 81 | +class _EnumNonMemberAttributePayload: |
| 82 | + def __reduce__(self): |
| 83 | + return (getattr, (_NamedPickleColor, "__members__")) |
| 84 | + |
| 85 | + |
52 | 86 | def test_restricted_decode_blocks_arbitrary_callable(): |
53 | 87 | """Restricted decoding blocks arbitrary module-level callables.""" |
54 | 88 | pickled = pickle.dumps(os.getpid, protocol=pickle.HIGHEST_PROTOCOL) |
@@ -300,6 +334,136 @@ def test_restricted_unpickler_raises_pickle_error(): |
300 | 334 | unpickler.load() |
301 | 335 |
|
302 | 336 |
|
| 337 | +def test_restricted_decode_rejects_non_type_global_in_allowed_types(): |
| 338 | + """Explicit allowed_types entries must resolve to types.""" |
| 339 | + from agent_framework._workflows._checkpoint_encoding import _RestrictedUnpickler |
| 340 | + |
| 341 | + unpickler = _RestrictedUnpickler(pickle.dumps(object), frozenset({"os:getpid"})) |
| 342 | + with pytest.raises(pickle.UnpicklingError, match="non-type global"): |
| 343 | + unpickler.find_class("os", "getpid") |
| 344 | + |
| 345 | + |
| 346 | +def test_restricted_decode_rejects_non_type_global_under_prefix(): |
| 347 | + """Allowed package prefixes must not expose arbitrary module globals.""" |
| 348 | + from agent_framework._workflows._checkpoint_encoding import _RestrictedUnpickler |
| 349 | + |
| 350 | + unpickler = _RestrictedUnpickler(pickle.dumps(object), frozenset()) |
| 351 | + with pytest.raises(pickle.UnpicklingError, match="non-type global"): |
| 352 | + unpickler.find_class( |
| 353 | + "agent_framework._workflows._checkpoint_encoding", |
| 354 | + "_value_type_to_key", |
| 355 | + ) |
| 356 | + |
| 357 | + |
| 358 | +@pytest.mark.parametrize("protocol", [2, 3]) |
| 359 | +def test_restricted_decode_allows_explicitly_listed_nested_type(protocol: int): |
| 360 | + """Protocol 2/3 nested types load when both the container and nested type are allowed.""" |
| 361 | + original = _NestedTypeContainer.NestedType(42) |
| 362 | + checkpoint_value = { |
| 363 | + _PICKLE_MARKER: base64.b64encode(pickle.dumps(original, protocol=protocol)).decode("ascii"), |
| 364 | + _TYPE_MARKER: f"{type(original).__module__}:{type(original).__qualname__}", |
| 365 | + } |
| 366 | + allowed_types = frozenset({ |
| 367 | + f"{_NestedTypeContainer.__module__}:{_NestedTypeContainer.__qualname__}", |
| 368 | + f"{_NestedTypeContainer.NestedType.__module__}:{_NestedTypeContainer.NestedType.__qualname__}", |
| 369 | + }) |
| 370 | + |
| 371 | + decoded = decode_checkpoint_value(checkpoint_value, allowed_types=allowed_types) |
| 372 | + |
| 373 | + assert isinstance(decoded, _NestedTypeContainer.NestedType) |
| 374 | + assert decoded.value == 42 |
| 375 | + |
| 376 | + |
| 377 | +@pytest.mark.parametrize("protocol", [2, 3]) |
| 378 | +def test_restricted_decode_blocks_unlisted_nested_type_before_setstate(protocol: int): |
| 379 | + """Allowing only a container must not allow its nested types or run their state hooks.""" |
| 380 | + original = _NestedSetStateContainer.Evil() |
| 381 | + checkpoint_value = { |
| 382 | + _PICKLE_MARKER: base64.b64encode(pickle.dumps(original, protocol=protocol)).decode("ascii"), |
| 383 | + _TYPE_MARKER: f"{type(original).__module__}:{type(original).__qualname__}", |
| 384 | + } |
| 385 | + outer_type_key = f"{_NestedSetStateContainer.__module__}:{_NestedSetStateContainer.__qualname__}" |
| 386 | + _NestedSetStateContainer.Evil.setstate_called = False |
| 387 | + |
| 388 | + with pytest.raises(WorkflowCheckpointException, match="nested type"): |
| 389 | + decode_checkpoint_value(checkpoint_value, allowed_types=frozenset({outer_type_key})) |
| 390 | + |
| 391 | + assert not _NestedSetStateContainer.Evil.setstate_called |
| 392 | + |
| 393 | + |
| 394 | +@pytest.mark.parametrize("protocol", [2, 3, 4, 5]) |
| 395 | +def test_restricted_decode_allows_allowlisted_enum_member_by_name(protocol: int): |
| 396 | + """Named enum members load only through their allowlisted enum class.""" |
| 397 | + original = _NamedPickleColor.RED |
| 398 | + checkpoint_value = { |
| 399 | + _PICKLE_MARKER: base64.b64encode(pickle.dumps(original, protocol=protocol)).decode("ascii"), |
| 400 | + _TYPE_MARKER: f"{type(original).__module__}:{type(original).__qualname__}", |
| 401 | + } |
| 402 | + enum_type_key = f"{_NamedPickleColor.__module__}:{_NamedPickleColor.__qualname__}" |
| 403 | + |
| 404 | + decoded = decode_checkpoint_value(checkpoint_value, allowed_types=frozenset({enum_type_key})) |
| 405 | + |
| 406 | + assert decoded is _NamedPickleColor.RED |
| 407 | + |
| 408 | + |
| 409 | +def test_restricted_encoder_round_trips_allowlisted_enum_member_by_name(): |
| 410 | + """The current checkpoint encoder's protocol preserves named enum members.""" |
| 411 | + enum_type_key = f"{_NamedPickleColor.__module__}:{_NamedPickleColor.__qualname__}" |
| 412 | + |
| 413 | + encoded = encode_checkpoint_value(_NamedPickleColor.RED) |
| 414 | + decoded = decode_checkpoint_value(encoded, allowed_types=frozenset({enum_type_key})) |
| 415 | + |
| 416 | + assert decoded is _NamedPickleColor.RED |
| 417 | + |
| 418 | + |
| 419 | +def test_restricted_decode_blocks_non_member_attribute_on_allowlisted_enum(): |
| 420 | + """Allowing an enum class does not expose its other attributes through getattr.""" |
| 421 | + payload = pickle.dumps(_EnumNonMemberAttributePayload(), protocol=pickle.HIGHEST_PROTOCOL) |
| 422 | + checkpoint_value = { |
| 423 | + _PICKLE_MARKER: base64.b64encode(payload).decode("ascii"), |
| 424 | + _TYPE_MARKER: "builtins:dict", |
| 425 | + } |
| 426 | + enum_type_key = f"{_NamedPickleColor.__module__}:{_NamedPickleColor.__qualname__}" |
| 427 | + |
| 428 | + with pytest.raises(WorkflowCheckpointException, match="non-type attribute"): |
| 429 | + decode_checkpoint_value(checkpoint_value, allowed_types=frozenset({enum_type_key})) |
| 430 | + |
| 431 | + |
| 432 | +def test_restricted_decode_blocks_member_of_unlisted_enum(): |
| 433 | + """Enum member reconstruction still requires the enum class in allowed_types.""" |
| 434 | + original = _NamedPickleColor.RED |
| 435 | + checkpoint_value = { |
| 436 | + _PICKLE_MARKER: base64.b64encode(pickle.dumps(original, protocol=pickle.HIGHEST_PROTOCOL)).decode("ascii"), |
| 437 | + _TYPE_MARKER: f"{type(original).__module__}:{type(original).__qualname__}", |
| 438 | + } |
| 439 | + |
| 440 | + with pytest.raises(WorkflowCheckpointException, match="deserialization blocked"): |
| 441 | + decode_checkpoint_value(checkpoint_value, allowed_types=frozenset()) |
| 442 | + |
| 443 | + |
| 444 | +def test_restricted_decode_blocks_getattr_globals_pickle_loads_chain(): |
| 445 | + """Restricted decoding blocks attribute traversal to an inner unrestricted pickle load.""" |
| 446 | + inner_pickle = b"cbuiltins\neval\n(V40 + 2\ntR." |
| 447 | + escaped_inner_pickle = inner_pickle.decode("ascii").replace("\\", "\\u005c").replace("\n", "\\u000a") |
| 448 | + # The outer pickle memoizes getattr and walks __init__.__globals__["pickle"].loads. |
| 449 | + payload = ( |
| 450 | + b"cbuiltins\ngetattr\np0\n0" |
| 451 | + b"g0\n(cagent_framework._workflows._checkpoint_encoding\n_RestrictedUnpickler\nV__init__\ntRp1\n0" |
| 452 | + b"g0\n(g1\nV__globals__\ntRp2\n0" |
| 453 | + b"g0\n(cbuiltins\ndict\nV__getitem__\ntRp3\n0" |
| 454 | + b"g3\n(g2\nVpickle\ntRp4\n0" |
| 455 | + b"g0\n(g4\nVloads\ntRp5\n0" |
| 456 | + b"g5\n(cbuiltins\nbytearray\n(V" + escaped_inner_pickle.encode("ascii") + b"\nVascii\ntRtR." |
| 457 | + ) |
| 458 | + checkpoint_value = { |
| 459 | + _PICKLE_MARKER: base64.b64encode(payload).decode("ascii"), |
| 460 | + _TYPE_MARKER: "builtins:int", |
| 461 | + } |
| 462 | + |
| 463 | + with pytest.raises(WorkflowCheckpointException, match="deserialization blocked"): |
| 464 | + decode_checkpoint_value(checkpoint_value, allowed_types=frozenset()) |
| 465 | + |
| 466 | + |
303 | 467 | def test_restricted_decode_allows_openai_types(): |
304 | 468 | """OpenAI SDK types are always allowed during restricted deserialization.""" |
305 | 469 | from openai.types.chat.chat_completion import ChatCompletion, Choice |
|
0 commit comments