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 @@ -252,12 +261,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 @@ -658,6 +658,98 @@ def test_deserialization_error_handling(self, tmp_path):
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), FileSystemSerDesConfig(path_encoding=path_encoding)
)
outside_file = tmp_path / "outside" / "outside-secret.json"
outside_file.parent.mkdir()
outside_file.write_text(
json.dumps({"t": "m", "v": {"secret": {"t": "s", "v": "leak"}}})
)

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

try:
# The step has no file of its own yet, so the read fails with
# the rebuilt path, not the outside file.
with pytest.raises(FileNotFoundError):
serdes.deserialize(forged, MOCK_CONTEXT)
finally:
outside_file.unlink()

def test_round_trip_still_reads_back_what_serialize_wrote(self, tmp_path):
"""Control case: deserialize reads back what serialize wrote for
this same context, via the rebuilt path."""
serdes = FileSystemSerDes(str(tmp_path))
value = {"ssn": "123-45-6789"}

envelope = serdes.serialize(value, MOCK_CONTEXT)
result = serdes.deserialize(envelope, MOCK_CONTEXT)

assert result == value


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

Expand Down
Loading