Repository navigation
Description
Part of #13. Found by Codex (reviewer 1) in the review of PR #492. Reviewer 2 confirmed it predates the PR.
F08 · Medium · A PATCH replay bypasses the required If-Match header
cmapi/src/cmapi/plugins/namespaces/router.py:259–264, cmapi/src/cmapi/core/pipeline.py:207–212. Replay an accepted submit with the same body and idempotency key but omit If-Match. The handler returns the stored 202 before reaching the 428 check. Validate header presence before replay; keep replay ahead of ETag comparison, so a lost-response retry carrying its original ETag still works after Git moves.
Fix
diff --git a/cmapi/src/cmapi/plugins/namespaces/router.py b/cmapi/src/cmapi/plugins/namespaces/router.py
--- a/cmapi/src/cmapi/plugins/namespaces/router.py
+++ b/cmapi/src/cmapi/plugins/namespaces/router.py
@@ -256,6 +256,8 @@
for field in ("labels", "annotations"):
if getattr(body, field) is not None:
raw[field] = getattr(body, field)
+ if etag is None:
+ raise Problem(428, "precondition-required", "If-Match is required on a change; GET the namespace for its etag")
key = _idem(key, dry_run)
replay = _replay(c, who, key, raw)
if replay:Regression tests
diff --git a/cmapi/tests/core/test_review492_header.py b/cmapi/tests/core/test_review492_header.py
new file mode 100644
--- /dev/null
+++ b/cmapi/tests/core/test_review492_header.py
@@ -0,0 +1,17 @@
+from unittest.mock import Mock
+
+import pytest
+
+from cmapi.core.errors import Problem
+from cmapi.core.models import NamespacePatch
+from cmapi.plugins.namespaces import router as routes
+
+
+def test_patch_replay_still_requires_if_match(monkeypatch):
+ monkeypatch.setattr(routes, "_replay", lambda *args: {"requestId": "req-original"})
+ body = NamespacePatch(labels={"owner": "green"})
+ with pytest.raises(Problem) as caught:
+ routes.update_namespace("CRC", "ns", body, False, "same-key", None, Mock(), Mock())
+ assert (caught.value.status, caught.value.code) == (428, "precondition-required")
+ assert routes.update_namespace("CRC", "ns", body, False, "same-key", "original-etag", Mock(), Mock()) == {
+ "requestId": "req-original"}Command and observed output
PYTHONPATH=src python -m pytest -q --tb=short tests/core/test_review492_header.pyHEAD (selected failure lines and pytest summary)
E Failed: DID NOT RAISE <class 'cmapi.core.errors.Problem'>
1 failed in 1.54s
With fixes
1 passed in 1.40s
Research mandate. Read RFC 9110 (conditional requests) and the IETF draft for the Idempotency-Key header, decide the order of the idempotency replay and the precondition check, and fix it with a test.
Activity
#483 review fixes: reviewer 1 (Codex gpt-6.2-astra high) F01-F12, upd…
#499 review fixes: reviewer 1's findings 1-5 as narrowed by reviewer 2,