Skip to content

Commit 0916177

Browse files
authored
S3 Driver requires hash algorithm and value on claim payloads (#1443)
1 parent 160bfce commit 0916177

2 files changed

Lines changed: 27 additions & 21 deletions

File tree

temporalio/contrib/aws/s3driver/_driver.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,28 @@ async def _download(claim: StorageDriverClaim) -> Payload:
188188
f"S3StorageDriver retrieve failed [bucket={bucket}, key={key}]"
189189
) from e
190190

191-
expected_hash = claim.claim_data.get("hash_value")
192191
hash_algorithm = claim.claim_data.get("hash_algorithm")
193-
if expected_hash and hash_algorithm:
194-
if hash_algorithm != "sha256":
195-
raise ValueError(
196-
f"S3StorageDriver unsupported hash algorithm "
197-
f"[bucket={bucket}, key={key}]: "
198-
f"expected sha256, got {hash_algorithm}"
199-
)
200-
actual_hash = hashlib.sha256(payload_bytes).hexdigest().lower()
201-
if actual_hash != expected_hash:
202-
raise ValueError(
203-
f"S3StorageDriver integrity check failed "
204-
f"[bucket={bucket}, key={key}]: "
205-
f"expected {hash_algorithm}:{expected_hash}, "
206-
f"got {hash_algorithm}:{actual_hash}"
207-
)
192+
expected_hash = claim.claim_data.get("hash_value")
193+
if not hash_algorithm or not expected_hash:
194+
raise ValueError(
195+
f"S3StorageDriver claim is missing required content hash information "
196+
f"[bucket={bucket}, key={key}]: "
197+
f"claim_data must contain 'hash_algorithm' and 'hash_value'"
198+
)
199+
if hash_algorithm != "sha256":
200+
raise ValueError(
201+
f"S3StorageDriver unsupported hash algorithm "
202+
f"[bucket={bucket}, key={key}]: "
203+
f"expected sha256, got {hash_algorithm}"
204+
)
205+
actual_hash = hashlib.sha256(payload_bytes).hexdigest().lower()
206+
if actual_hash != expected_hash:
207+
raise ValueError(
208+
f"S3StorageDriver integrity check failed "
209+
f"[bucket={bucket}, key={key}]: "
210+
f"expected {hash_algorithm}:{expected_hash}, "
211+
f"got {hash_algorithm}:{actual_hash}"
212+
)
208213

209214
payload = Payload()
210215
payload.ParseFromString(payload_bytes)

tests/contrib/aws/s3driver/test_s3driver.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ async def test_retrieve_rejects_unsupported_hash_algorithm(
489489
async def test_retrieve_without_hash_in_claim(
490490
self, driver_client: S3StorageDriverClient
491491
) -> None:
492-
"""Claims without hash fields still retrieve successfully (backward compat)."""
492+
"""Claims missing content hash fields raise ValueError on retrieve."""
493493
driver = S3StorageDriver(client=driver_client, bucket=BUCKET)
494494
payload = make_payload("no-hash-claim")
495495
[claim] = await driver.store(make_store_context(), [payload])
@@ -500,10 +500,11 @@ async def test_retrieve_without_hash_in_claim(
500500
"key": claim.claim_data["key"],
501501
},
502502
)
503-
[retrieved] = await driver.retrieve(
504-
StorageDriverRetrieveContext(), [legacy_claim]
505-
)
506-
assert retrieved == payload
503+
with pytest.raises(
504+
ValueError,
505+
match=r"S3StorageDriver claim is missing required content hash information",
506+
):
507+
await driver.retrieve(StorageDriverRetrieveContext(), [legacy_claim])
507508

508509

509510
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)