Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

Guard StoragePayloadCodec.decode against URIs outside the configured prefix

Louis Choquel1032d1571f20
diff --git a/pipelex/temporal/codec/storage_payload_codec.py b/pipelex/temporal/codec/storage_payload_codec.py
index 9ec643c5b..315641bec 100644
--- a/pipelex/temporal/codec/storage_payload_codec.py
+++ b/pipelex/temporal/codec/storage_payload_codec.py
@@ -10,6 +10,8 @@ from temporalio.converter import PayloadCodec
from typing_extensions import override
from pipelex import log
+from pipelex.tools.storage.exceptions import StorageInvalidUriError
+from pipelex.tools.storage.storage_provider_abstract import PIPELEX_STORAGE_SCHEME
if TYPE_CHECKING:
from collections.abc import Sequence
@@ -41,6 +43,7 @@ class StoragePayloadCodec(PayloadCodec):
self._storage = storage_provider
self._size_threshold = size_threshold
self._storage_prefix = f"{storage_prefix}/" if storage_prefix and not storage_prefix.endswith("/") else storage_prefix
+ self._expected_uri_prefix = f"{PIPELEX_STORAGE_SCHEME}{self._storage_prefix}"
@staticmethod
def _extract_job_routing(payload: Payload) -> tuple[str, str] | None:
@@ -110,6 +113,9 @@ class StoragePayloadCodec(PayloadCodec):
decoded.append(payload)
continue
uri = payload.data.decode()
+ if not uri.startswith(self._expected_uri_prefix):
+ msg = f"Refusing to load storage reference '{uri}': URI does not start with expected prefix '{self._expected_uri_prefix}'"
+ raise StorageInvalidUriError(msg)
original_bytes = await self._storage.load(uri)
original_payload = Payload()
original_payload.ParseFromString(original_bytes)
diff --git a/tests/unit/pipelex/temporal/test_storage_payload_codec.py b/tests/unit/pipelex/temporal/test_storage_payload_codec.py
index f931cc8c0..f96189b10 100644
--- a/tests/unit/pipelex/temporal/test_storage_payload_codec.py
+++ b/tests/unit/pipelex/temporal/test_storage_payload_codec.py
@@ -4,6 +4,7 @@ import pytest
from temporalio.api.common.v1 import Payload
from pipelex.temporal.codec.storage_payload_codec import STORAGE_REF_ENCODING, StoragePayloadCodec
+from pipelex.tools.storage.exceptions import StorageInvalidUriError
from pipelex.tools.storage.in_memory_storage_provider import InMemoryStorageProvider
TEST_THRESHOLD = 1024
@@ -274,3 +275,36 @@ class TestStoragePayloadCodec:
assert len(segments) == 4, f"Expected routed key for {topic}, got: {key_after_scheme}"
assert segments[1] == expected_user, f"User segment mismatch for {topic}"
assert segments[2] == expected_run, f"Run segment mismatch for {topic}"
+
+ # -------------------------------------------------------------------
+ # decode() URI prefix guard tests
+ # -------------------------------------------------------------------
+
+ @pytest.mark.parametrize(
+ ("topic", "tampered_uri"),
+ [
+ ("foreign_prefix", "pipelex-storage://other-tenant/abc123"),
+ ("missing_scheme", "test-payloads/abc123"),
+ ("foreign_scheme", "file:///etc/passwd"),
+ ("absolute_path_no_scheme", "/etc/passwd"),
+ ("empty_uri", ""),
+ ],
+ )
+ async def test_decode_rejects_uri_outside_configured_prefix(
+ self,
+ codec: StoragePayloadCodec,
+ storage: InMemoryStorageProvider,
+ topic: str,
+ tampered_uri: str,
+ ) -> None:
+ """decode() refuses storage references whose URI does not start with the configured prefix."""
+ tampered_payload = Payload(
+ metadata={"encoding": STORAGE_REF_ENCODING},
+ data=tampered_uri.encode(),
+ )
+
+ with pytest.raises(StorageInvalidUriError, match="does not start with expected prefix"):
+ await codec.decode([tampered_payload])
+
+ # Storage must not be touched when the URI is rejected by the prefix guard
+ assert len(storage.root) == 0, f"Storage should not be accessed for tampered URI ({topic})"