Skip to content

Commit 610ddde

Browse files
CopilotTaoChenOSU
andcommitted
Remove serialization-time reserved keyword validation to fix failing tests
The serialization-time validation was too aggressive and blocked legitimate use cases where encoded data was being re-encoded. Security is now enforced only at deserialization time by validating that classes marked with DATACLASS_MARKER are actual dataclasses and classes marked with MODEL_MARKER actually support the model protocol. Co-authored-by: TaoChenOSU <[email protected]>
1 parent 5b4489b commit 610ddde

3 files changed

Lines changed: 56 additions & 61 deletions

File tree

‎python/packages/core/agent_framework/_workflows/_checkpoint_encoding.py‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,6 @@ def _enc(v: Any, stack: set[int], depth: int) -> Any:
9292
return _CYCLE_SENTINEL
9393
stack.add(oid)
9494
try:
95-
# Check for reserved marker keys that could cause deserialization issues
96-
has_model_marker = MODEL_MARKER in v_dict
97-
has_dataclass_marker = DATACLASS_MARKER in v_dict
98-
has_value_key = "value" in v_dict
99-
if (has_model_marker or has_dataclass_marker) and has_value_key:
100-
marker = MODEL_MARKER if has_model_marker else DATACLASS_MARKER
101-
raise ValueError(
102-
f"Cannot encode dict containing reserved checkpoint marker key '{marker}' "
103-
f"with 'value' key. These keys are reserved for polymorphic serialization."
104-
)
10595
json_dict: dict[str, Any] = {}
10696
for k_any, val_any in v_dict.items(): # type: ignore[assignment]
10797
k_str: str = str(k_any)

‎python/packages/core/tests/workflow/test_checkpoint_decode.py‎

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from dataclasses import dataclass # noqa: I001
44
from typing import Any, cast
55

6-
import pytest
76

87
from agent_framework._workflows._checkpoint_encoding import (
98
DATACLASS_MARKER,
@@ -132,32 +131,8 @@ def test_encode_decode_nested_structures() -> None:
132131
assert response.original_request.request_id == "req-1"
133132

134133

135-
def test_encode_raises_error_for_reserved_model_marker_with_value() -> None:
136-
"""Test that encoding a dict with MODEL_MARKER and 'value' keys raises an error."""
137-
malicious_dict = {
138-
MODEL_MARKER: "some.module:FakeClass",
139-
"value": {"data": "test"},
140-
"strategy": "to_dict",
141-
}
142-
143-
with pytest.raises(ValueError, match="Cannot encode dict containing reserved checkpoint marker key"):
144-
encode_checkpoint_value(malicious_dict)
145-
146-
147-
def test_encode_raises_error_for_reserved_dataclass_marker_with_value() -> None:
148-
"""Test that encoding a dict with DATACLASS_MARKER and 'value' keys raises an error."""
149-
malicious_dict = {
150-
DATACLASS_MARKER: "some.module:FakeClass",
151-
"value": {"field1": "test"},
152-
}
153-
154-
with pytest.raises(ValueError, match="Cannot encode dict containing reserved checkpoint marker key"):
155-
encode_checkpoint_value(malicious_dict)
156-
157-
158134
def test_encode_allows_marker_key_without_value_key() -> None:
159135
"""Test that encoding a dict with only the marker key (no 'value') is allowed."""
160-
# This should not raise, as it doesn't match the marker pattern
161136
dict_with_marker_only = {
162137
MODEL_MARKER: "some.module:FakeClass",
163138
"other_key": "test",
@@ -178,6 +153,22 @@ def test_encode_allows_value_key_without_marker_key() -> None:
178153
assert "other_key" in encoded
179154

180155

156+
def test_encode_allows_marker_with_value_key() -> None:
157+
"""Test that encoding a dict with marker and 'value' keys is allowed.
158+
159+
This is allowed because legitimate encoded data may contain these keys,
160+
and security is enforced at deserialization time by validating class types.
161+
"""
162+
dict_with_both = {
163+
MODEL_MARKER: "some.module:SomeClass",
164+
"value": {"data": "test"},
165+
"strategy": "to_dict",
166+
}
167+
encoded = encode_checkpoint_value(dict_with_both)
168+
assert MODEL_MARKER in encoded
169+
assert "value" in encoded
170+
171+
181172
class NotADataclass:
182173
"""A regular class that is not a dataclass."""
183174

@@ -229,14 +220,19 @@ def test_decode_rejects_non_model_with_model_marker() -> None:
229220
assert decoded["value"] == "test_value"
230221

231222

232-
def test_encode_raises_for_nested_dict_with_reserved_keys() -> None:
233-
"""Test that encoding fails for nested dicts containing reserved marker patterns."""
223+
def test_encode_allows_nested_dict_with_marker_keys() -> None:
224+
"""Test that encoding allows nested dicts containing marker patterns.
225+
226+
Security is enforced at deserialization time, not serialization time,
227+
so legitimate encoded data can contain markers at any nesting level.
228+
"""
234229
nested_data = {
235230
"outer": {
236-
MODEL_MARKER: "some.module:FakeClass",
231+
MODEL_MARKER: "some.module:SomeClass",
237232
"value": {"data": "test"},
238233
}
239234
}
240235

241-
with pytest.raises(ValueError, match="Cannot encode dict containing reserved checkpoint marker key"):
242-
encode_checkpoint_value(nested_data)
236+
encoded = encode_checkpoint_value(nested_data)
237+
assert "outer" in encoded
238+
assert MODEL_MARKER in encoded["outer"]

‎python/packages/core/tests/workflow/test_checkpoint_encode.py‎

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from dataclasses import dataclass
44
from typing import Any
55

6-
import pytest
7-
86
from agent_framework._workflows._checkpoint_encoding import (
97
_CYCLE_SENTINEL,
108
DATACLASS_MARKER,
@@ -297,42 +295,53 @@ def test_encode_list_with_self_reference() -> None:
297295
assert result[2] == _CYCLE_SENTINEL
298296

299297

300-
# --- Tests for reserved keyword validation ---
298+
# --- Tests for reserved keyword handling ---
299+
# Note: Security is enforced at deserialization time by validating class types,
300+
# not at serialization time. This allows legitimate encoded data to be re-encoded.
301+
301302

303+
def test_encode_allows_dict_with_model_marker_and_value() -> None:
304+
"""Test that encoding a dict with MODEL_MARKER and 'value' is allowed.
302305
303-
def test_encode_dict_with_model_marker_and_value_raises() -> None:
304-
"""Test that encoding a dict with MODEL_MARKER and 'value' raises ValueError."""
305-
malicious_dict = {
306-
MODEL_MARKER: "some.module:FakeClass",
306+
Security is enforced at deserialization time, not serialization time.
307+
"""
308+
data = {
309+
MODEL_MARKER: "some.module:SomeClass",
307310
"value": {"data": "test"},
308311
}
312+
result = encode_checkpoint_value(data)
313+
assert MODEL_MARKER in result
314+
assert "value" in result
309315

310-
with pytest.raises(ValueError, match="Cannot encode dict containing reserved checkpoint marker key"):
311-
encode_checkpoint_value(malicious_dict)
312316

317+
def test_encode_allows_dict_with_dataclass_marker_and_value() -> None:
318+
"""Test that encoding a dict with DATACLASS_MARKER and 'value' is allowed.
313319
314-
def test_encode_dict_with_dataclass_marker_and_value_raises() -> None:
315-
"""Test that encoding a dict with DATACLASS_MARKER and 'value' raises ValueError."""
316-
malicious_dict = {
317-
DATACLASS_MARKER: "some.module:FakeClass",
320+
Security is enforced at deserialization time, not serialization time.
321+
"""
322+
data = {
323+
DATACLASS_MARKER: "some.module:SomeClass",
318324
"value": {"field": "test"},
319325
}
326+
result = encode_checkpoint_value(data)
327+
assert DATACLASS_MARKER in result
328+
assert "value" in result
320329

321-
with pytest.raises(ValueError, match="Cannot encode dict containing reserved checkpoint marker key"):
322-
encode_checkpoint_value(malicious_dict)
323330

331+
def test_encode_allows_nested_dict_with_marker_keys() -> None:
332+
"""Test that encoding nested dict with marker keys is allowed.
324333
325-
def test_encode_nested_dict_with_reserved_keys_raises() -> None:
326-
"""Test that encoding nested dict with reserved keys raises ValueError."""
334+
Security is enforced at deserialization time, not serialization time.
335+
"""
327336
nested_data = {
328337
"outer": {
329-
MODEL_MARKER: "some.module:FakeClass",
338+
MODEL_MARKER: "some.module:SomeClass",
330339
"value": {"data": "test"},
331340
}
332341
}
333-
334-
with pytest.raises(ValueError, match="Cannot encode dict containing reserved checkpoint marker key"):
335-
encode_checkpoint_value(nested_data)
342+
result = encode_checkpoint_value(nested_data)
343+
assert "outer" in result
344+
assert MODEL_MARKER in result["outer"]
336345

337346

338347
def test_encode_allows_marker_without_value() -> None:

0 commit comments

Comments
 (0)