Skip to content

A PATCH idempotency replay bypasses the required If-Match header #495

Closed

Description

@ephico2real2

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.py
HEAD (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

  1. added
    bug
    status:open
    sev:medium
    Backlog
    on Nov 20, 2026
  2. added
    In-Progress
    and removed
    status:open
    Backlog
    on Nov 20, 2026
  3. added 2 commits that reference this issue on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-Progressbugsev:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions