Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,29 @@ def _resolve_execution_dir(
return os.path.join(base_path, _encode_segment(arn, path_encoding))


def _write_to_file(
def _resolve_file_path(
base_path: str,
value: Any,
context: SerDesContext,
path_encoding: FileSystemPathEncoding,
value_serdes: SerDes[Any],
) -> str:
"""Write value as serialized string to a file and return the file path."""
"""Path for one entity of one execution; used by serialize and deserialize."""
dir_path: str = _resolve_execution_dir(
base_path, context.durable_execution_arn, path_encoding
)
os.makedirs(dir_path, exist_ok=True)
file_name: str = f"{_encode_segment(context.operation_id, path_encoding)}.json"
file_path: str = os.path.join(dir_path, file_name)
return os.path.join(dir_path, file_name)


def _write_to_file(
base_path: str,
value: Any,
context: SerDesContext,
path_encoding: FileSystemPathEncoding,
value_serdes: SerDes[Any],
) -> str:
"""Write value as serialized string to a file and return the file path."""
file_path: str = _resolve_file_path(base_path, context, path_encoding)
os.makedirs(os.path.dirname(file_path), exist_ok=True)
serialized: str = value_serdes.serialize(value, context)
with open(file_path, "w", encoding="utf-8") as f:
f.write(serialized)
Expand Down Expand Up @@ -187,6 +196,8 @@ class FileSystemSerDes(SerDes[Any]):
- {"file": "<path>"} — value stored in a file
- {"file": "<path>", "preview": {...}} — file pointer with inline preview

The "file" value is informational; deserialize rebuilds the path from the context.

Example:
# Always write to S3 Files mount (default)
fs_serdes = FileSystemSerDes("/mnt/s3")
Expand Down Expand Up @@ -252,12 +263,15 @@ def deserialize(self, data: str, serdes_context: SerDesContext) -> Any:
"""Deserialize from a JSON envelope string.

Reads from file if envelope contains a file pointer, otherwise parses
inline data.
inline data. Ignores the envelope's file value and rebuilds the path.
"""
envelope: dict[str, Any] = json.loads(data)

if "file" in envelope:
with open(envelope["file"], encoding="utf-8") as f:
file_path: str = _resolve_file_path(
self._base_path, serdes_context, self._path_encoding
)
with open(file_path, encoding="utf-8") as f:
file_content: str = f.read()
return self._value_serdes.deserialize(file_content, serdes_context)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
FileSystemSerDesMode,
_encode_segment,
_resolve_execution_dir,
_resolve_file_path,
_write_to_file,
FileSystemSerDes,
)
Expand Down Expand Up @@ -204,6 +205,20 @@ def test_write_to_file_with_hash_encoding(tmp_path):
assert file_name == expected_name


@pytest.mark.parametrize(
"path_encoding", [FileSystemPathEncoding.URI, FileSystemPathEncoding.HASH]
)
def test_resolve_file_path_matches_write_to_file(tmp_path, path_encoding):
"""_resolve_file_path returns the path _write_to_file writes to."""
context = SerDesContext(operation_id="my-step", durable_execution_arn=TEST_ARN)

written = _write_to_file(
str(tmp_path), {"id": 1}, context, path_encoding, EXTENDED_TYPES_SERDES
)

assert _resolve_file_path(str(tmp_path), context, path_encoding) == written


def test_write_to_file_unsafe_operation_id(tmp_path):
"""Test _write_to_file with an unsafe operation ID containing path traversal."""

Expand Down Expand Up @@ -651,13 +666,89 @@ def test_deserialization_error_handling(self, tmp_path):
from aws_durable_execution_sdk_python.serdes import deserialize

fs_serdes = FileSystemSerDes(str(tmp_path))
# Invalid envelope pointing to nonexistent file
# Envelope file value is ignored; no file exists at the rebuilt path
invalid_data = json.dumps({"file": "/nonexistent/file.json"})

with pytest.raises(SerDesError, match="Deserialization failed"):
deserialize(fs_serdes, invalid_data, TEST_OPERATION_ID, TEST_ARN)


class TestDeserializeIgnoresEnvelopePath:
"""deserialize must rebuild the file path itself and never trust the
path stored in the envelope. A callback result is written by an
external caller, so the envelope's file value is untrusted input."""

@pytest.mark.parametrize(
"path_encoding", [FileSystemPathEncoding.URI, FileSystemPathEncoding.HASH]
)
def test_envelope_pointing_at_another_execution_reads_own_file(
self, tmp_path, path_encoding
):
"""A forged envelope naming another execution's file must not be
read. deserialize reads this execution's own file instead."""
serdes = FileSystemSerDes(
str(tmp_path), FileSystemSerDesConfig(path_encoding=path_encoding)
)
own_ctx = SerDesContext(operation_id="op-1", durable_execution_arn=TEST_ARN)
other_arn = (
"arn:aws:lambda:us-east-1:123456789012:function:test-function:1"
"/durable-execution/other-exec-id/other-invocation-id"
)
other_ctx = SerDesContext(operation_id="op-1", durable_execution_arn=other_arn)

serdes.serialize({"owner": "self"}, own_ctx)
other_envelope = serdes.serialize({"owner": "other"}, other_ctx)

# Forge: an envelope claiming to be own_ctx's result, but naming the
# other execution's file.
forged = json.dumps({"file": json.loads(other_envelope)["file"]})

result = serdes.deserialize(forged, own_ctx)
assert result == {"owner": "self"}

@pytest.mark.parametrize(
"path_encoding", [FileSystemPathEncoding.URI, FileSystemPathEncoding.HASH]
)
def test_envelope_pointing_at_another_entity_reads_own_file(
self, tmp_path, path_encoding
):
"""A forged envelope naming another entity's file, in the same
execution, must not be read."""
serdes = FileSystemSerDes(
str(tmp_path), FileSystemSerDesConfig(path_encoding=path_encoding)
)
ctx_a = SerDesContext(operation_id="op-a", durable_execution_arn=TEST_ARN)
ctx_b = SerDesContext(operation_id="op-b", durable_execution_arn=TEST_ARN)

serdes.serialize({"owner": "a"}, ctx_a)
envelope_b = serdes.serialize({"owner": "b"}, ctx_b)

forged = json.dumps({"file": json.loads(envelope_b)["file"]})

result = serdes.deserialize(forged, ctx_a)
assert result == {"owner": "a"}

@pytest.mark.parametrize(
"path_encoding", [FileSystemPathEncoding.URI, FileSystemPathEncoding.HASH]
)
def test_envelope_pointing_outside_base_path_fails(self, tmp_path, path_encoding):
"""A forged envelope naming a path outside base_path must not be
read: deserialize never looks at that path at all."""
serdes = FileSystemSerDes(
str(tmp_path / "base"),
FileSystemSerDesConfig(path_encoding=path_encoding),
)
outside_file = tmp_path / "outside-secret.json"
outside_file.write_text(
json.dumps({"t": "m", "v": {"secret": {"t": "s", "v": "leak"}}})
)

forged = json.dumps({"file": str(outside_file)})

with pytest.raises(FileNotFoundError):
serdes.deserialize(forged, MOCK_CONTEXT)


class TestEdgeCases:
"""Tests for edge cases and error scenarios."""

Expand Down