console: decide if_version once from the raw body; refuse If-Match
Paul's ruling under stopping rule 2, after a supplied precondition vanished in two rounds (round 2 P1: a JSON null, or a repeated member ending in null, made the write unconditional). The request struct no longer has an if_version field. configIfVersion reads the raw body with a json token pass: absent is unconditional; otherwise exactly one member (matched ignoring case, as encoding/json does) holding a non-empty string. null, "", blank, any other type and a repeated member are 400s before any store read. The If-Match synonym is removed: a request with the header is refused with 400 so a client relying on it learns. One table-driven guard test covers every form, plus the stale 412 under another case of the name and the valid and absent cases. The spec's header paragraph is marked superseded.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+132
−129
docs/superpowers/specs/2026-11-02-config-receipts-design.md+6 −2internal/console/apiconfig.go+80 −59internal/console/apiconfig_conditional_test.go+2 −46internal/console/apiconfig_witness_test.go+44 −22
diff --git a/docs/superpowers/specs/2026-11-02-config-receipts-design.md b/docs/superpowers/specs/2026-11-02-config-receipts-design.md| index 05f7f4d8b..5545448af 100644 |
| --- a/docs/superpowers/specs/2026-11-02-config-receipts-design.md |
| +++ b/docs/superpowers/specs/2026-11-02-config-receipts-design.md |
| @@ -319,9 +319,13 @@ are unchanged on every store. |
| Header vs body: the field is in the body because the CLI's door client sends |
| JSON bodies everywhere and a body field shows up in the request log the |
| -console already keeps. An `If-Match` header is accepted as a synonym if the |
| +console already keeps. ~~An `If-Match` header is accepted as a synonym if the |
| builder finds it cheap; the body field wins if both are sent and differ (400). |
| -(My call.) |
| +(My call.)~~ Superseded by Paul's ruling after branch review round 2: there |
| +is no header synonym. A request carrying `If-Match` is refused with 400, and |
| +`if_version` is accepted only as one non-empty JSON string member; null, an |
| +empty or blank string, any other type and a repeated member are 400s before |
| +any read. Absent means unconditional. |
| The expected version is carried into **both** write paths **(R1-3)**: |
| `applyAt` (default bundle, non-owned environments) and `ApplyEnvironmentOwned` |
diff --git a/internal/console/apiconfig.go b/internal/console/apiconfig.go| index 7320e7de0..52579c4df 100644 |
| --- a/internal/console/apiconfig.go |
| +++ b/internal/console/apiconfig.go |
| @@ -26,9 +26,12 @@ |
| package console |
| import ( |
| + "bytes" |
| "context" |
| + "encoding/json" |
| "errors" |
| "fmt" |
| + "io" |
| "log" |
| "net/http" |
| "sort" |
| @@ -328,7 +331,7 @@ func (s *Server) handleAPIConfigGet(w http.ResponseWriter, r *http.Request) { |
| // write for the set/unset half (appconfig.ApplyIf) — two would mean a |
| // half-applied request whenever the second lost a race. |
| // |
| -// With if_version (or If-Match) that write is conditional on the bundle's |
| +// With if_version that write is conditional on the bundle's |
| // version (#522 §4.5): a bundle not at it is a 412 naming the version it is |
| // at, nothing written. Every set/unset write answers with a receipt (§4.6) |
| // saying exactly what it did, and the version the whole request left the |
| @@ -383,13 +386,18 @@ func (s *Server) handleAPIConfigPut(w http.ResponseWriter, r *http.Request) { |
| // the field would otherwise be indistinguishable from one asking to |
| // turn the flag OFF. |
| ExecDelivery *string `json:"exec_delivery"` |
| - // IfVersion makes the set/unset half conditional on the bundle's |
| - // version (#522 §4.5). A POINTER, like ExecDelivery: an absent field |
| - // is today's unconditional write, and a present empty one is a |
| - // precondition that must not quietly become none (configIfVersion). |
| - // The If-Match header says the same thing. |
| - IfVersion *string `json:"if_version"` |
| - } |
| + // if_version is deliberately NOT a field here. Whether a request |
| + // carries a precondition is decided in one place, configIfVersion, |
| + // from the raw body: a struct field cannot tell an absent member |
| + // from a null one, and keeps only the last of a repeated one. |
| + } |
| + // The bytes the decoder reads are kept for configIfVersion. The tee |
| + // sits under readJSONLimit's own byte cap, so it holds at most that. |
| + var rawBody bytes.Buffer |
| + r.Body = struct { |
| + io.Reader |
| + io.Closer |
| + }{io.TeeReader(r.Body, &rawBody), r.Body} |
| if !readJSONLimit(w, r, &req, configPutMaxBytes) { |
| return |
| } |
| @@ -526,7 +534,7 @@ func (s *Server) handleAPIConfigPut(w http.ResponseWriter, r *http.Request) { |
| // The precondition's refusals come last among the body checks and, |
| // like them, before the first store call: a request refused here has |
| // read nothing, so it cannot have learnt a version either. |
| - ifVersion, ok := configIfVersion(w, r, req.IfVersion) |
| + ifVersion, ok := configIfVersion(w, r, rawBody.Bytes()) |
| if !ok { |
| return |
| } |
| @@ -1014,67 +1022,80 @@ func (s *Server) teamApplyEnvironment(ctx context.Context, tv *teamView, envName |
| return rc, false, err |
| } |
| -// configIfVersion is the PUT's precondition, from the body's if_version or |
| -// the If-Match header, answering the 400 itself when the two disagree. "" |
| -// means none. The header is the HTTP spelling for clients that already |
| -// speak it; an entity tag arrives quoted, so one pair of quotes is dropped. |
| -// The body wins nothing on a disagreement: a caller who said two things has |
| -// to say which one it meant. |
| +// configIfVersion is the PUT's precondition, and the ONE place that decides |
| +// whether the request supplied one (round 2 ruling, after the same fault |
| +// recurred in rounds 1 and 2): a supplied precondition must never become |
| +// none on the way in. It reads the raw body, which readJSONLimit has |
| +// already decoded as a JSON object, and answers the 400 itself. |
| // |
| -// A precondition that is SUPPLIED never becomes none (round 1, P1-1): only |
| -// an absent field and an absent header mean an unconditional write. A |
| -// present one that names no version (empty, blank or `""`), or names one in |
| -// a form this door does not take (`*`, a weak `W/` tag, a list, a second |
| -// header, a stray quote), is a 400 before anything is read. Falling back to |
| -// an unconditional write would overwrite exactly the concurrent change the |
| -// caller asked to be protected from, and a 412 would claim a comparison |
| -// that was never made. |
| -func configIfVersion(w http.ResponseWriter, r *http.Request, bodyField *string) (string, bool) { |
| - body := "" |
| - if bodyField != nil { |
| - body = strings.TrimSpace(*bodyField) |
| - if body == "" { |
| - jsonError(w, http.StatusBadRequest, configCopyEmptyIfVersion) |
| - return "", false |
| - } |
| - } |
| - values := r.Header.Values("If-Match") |
| - if len(values) == 0 { |
| - return body, true |
| - } |
| - header, ok := configIfMatchTag(values) |
| - if !ok { |
| - jsonError(w, http.StatusBadRequest, configCopyBadIfMatch) |
| +// Absent means an unconditional write, as it always did. Otherwise |
| +// if_version must be exactly one top-level member holding a non-empty JSON |
| +// string. null, an empty or blank string, any other JSON type, and a |
| +// member that appears more than once are refused: decoded into a struct, |
| +// null reads as absent and a repeated member keeps only its last value, so |
| +// either could turn a conditional write into an unconditional one. The |
| +// member is matched as encoding/json matches names, ignoring case, so |
| +// "IF_VERSION" is the precondition too and never an ignored field. |
| +// |
| +// The If-Match header is not a synonym (it was until round 2): a request |
| +// carrying one is refused, so a client that relied on it learns rather |
| +// than writing unconditionally. All of this runs before any store read. |
| +func configIfVersion(w http.ResponseWriter, r *http.Request, raw []byte) (string, bool) { |
| + if len(r.Header.Values("If-Match")) > 0 { |
| + jsonError(w, http.StatusBadRequest, configCopyIfMatch) |
| return "", false |
| } |
| - if body == "" || body == header { |
| - return header, true |
| + version, refusal := configIfVersionMember(raw) |
| + if refusal != "" { |
| + jsonError(w, http.StatusBadRequest, refusal) |
| + return "", false |
| } |
| - jsonError(w, http.StatusBadRequest, configCopyIfVersionDisagree) |
| - return "", false |
| + return version, true |
| } |
| -// configIfMatchTag is the one version an If-Match header names, quoted or |
| -// not, and false for any other form. |
| -func configIfMatchTag(values []string) (string, bool) { |
| - if len(values) != 1 { |
| - return "", false |
| - } |
| - tag := strings.TrimSpace(values[0]) |
| - if len(tag) >= 2 && tag[0] == '"' && tag[len(tag)-1] == '"' { |
| - tag = tag[1 : len(tag)-1] |
| - } |
| - if tag == "" || tag == "*" || strings.HasPrefix(tag, "W/") || strings.ContainsAny(tag, "\", \t") { |
| - return "", false |
| +// configIfVersionMember is the version the body's if_version member names, |
| +// "" when there is no such member, or the refusal for any other shape. |
| +func configIfVersionMember(raw []byte) (version, refusal string) { |
| + dec := json.NewDecoder(bytes.NewReader(raw)) |
| + if tok, err := dec.Token(); err != nil || tok != json.Delim('{') { |
| + return "", configCopyIfVersionUnreadable |
| + } |
| + seen := false |
| + for dec.More() { |
| + tok, err := dec.Token() |
| + if err != nil { |
| + return "", configCopyIfVersionUnreadable |
| + } |
| + name, _ := tok.(string) |
| + var value json.RawMessage |
| + if err := dec.Decode(&value); err != nil { |
| + return "", configCopyIfVersionUnreadable |
| + } |
| + if !strings.EqualFold(name, "if_version") { |
| + continue |
| + } |
| + if seen { |
| + return "", configCopyIfVersionTwice |
| + } |
| + seen = true |
| + var s string |
| + if len(value) == 0 || value[0] != '"' || json.Unmarshal(value, &s) != nil { |
| + return "", configCopyIfVersionNotString |
| + } |
| + if version = strings.TrimSpace(s); version == "" { |
| + return "", configCopyEmptyIfVersion |
| + } |
| } |
| - return tag, true |
| + return version, "" |
| } |
| // The precondition's 400s. |
| const ( |
| - configCopyEmptyIfVersion = "if_version is empty. send the version to write against, or leave if_version out" |
| - configCopyBadIfMatch = "If-Match must name one version, quoted or not. an empty tag, \"*\", a weak tag and a list are not accepted" |
| - configCopyIfVersionDisagree = "if_version and the If-Match header name different versions. send one, or send the same version in both" |
| + configCopyEmptyIfVersion = "if_version is empty. send the version to write against, or leave the field out" |
| + configCopyIfVersionNotString = "if_version must be a version string. send the version to write against, or leave the field out" |
| + configCopyIfVersionTwice = "if_version is in the body more than once. send it once" |
| + configCopyIfVersionUnreadable = "if_version could not be read from the body. send the body as one JSON object" |
| + configCopyIfMatch = "this door takes if_version in the body, not an If-Match header" |
| ) |
| // configSetAndUnset is the first of keys (sorted) that unsets also names, |
diff --git a/internal/console/apiconfig_conditional_test.go b/internal/console/apiconfig_conditional_test.go| index a997397ca..a3198d1dd 100644 |
| --- a/internal/console/apiconfig_conditional_test.go |
| +++ b/internal/console/apiconfig_conditional_test.go |
| @@ -1,8 +1,8 @@ |
| package console |
| // Conditional config writes and receipts on the PUT door (#522 plan 1, spec |
| -// §4.5-4.6): if_version and its If-Match synonym, the 412, the refusals that |
| -// answer before anything is read, and the receipt every write now carries. |
| +// §4.5-4.6): if_version, the 412, the refusals that answer before anything |
| +// is read, and the receipt every write now carries. |
| import ( |
| "bytes" |
| @@ -340,50 +340,6 @@ func TestAPIConfigPutIfVersionWithExecDelivery(t *testing.T) { |
| } |
| } |
| -// TestAPIConfigPutIfMatchHeaderConflict: If-Match is the body field's |
| -// synonym; the two disagreeing is a 400 before any read. |
| -func TestAPIConfigPutIfMatchHeaderConflict(t *testing.T) { |
| - fx, token := configFixture(t, "code-config-ifmatch") |
| - seedDefault(t, fx, token, envSet("A", "1")) |
| - key := appconfig.Key(release.OpsAccount, "titogo") |
| - cur := storedVersion(t, fx, key) |
| - cs := useCfgCountStore(fx, true) |
| - u := configURL(fx, "titogo") |
| - |
| - body := putBody(t, map[string]any{"if_version": cur, "set": envSet("B", "2")}) |
| - status, _, out := rawReqHeader(t, http.MethodPut, u, token, body, map[string]string{"If-Match": `"cv1:00"`}) |
| - if status != http.StatusBadRequest { |
| - t.Fatalf("header and body differ = %d %s, want 400", status, out) |
| - } |
| - if n := cs.getCount(); n != 0 { |
| - t.Errorf("the refusal read the store %d times, want 0", n) |
| - } |
| - |
| - // The header alone, stale: 412, as the body field would be. |
| - body = putBody(t, map[string]any{"set": envSet("B", "2")}) |
| - if status, _, out := rawReqHeader(t, http.MethodPut, u, token, body, map[string]string{"If-Match": "cv1:00"}); status != http.StatusPreconditionFailed { |
| - t.Fatalf("stale header = %d %s, want 412", status, out) |
| - } |
| - // The header alone, current and quoted as an HTTP entity tag: it lands. |
| - status, _, out = rawReqHeader(t, http.MethodPut, u, token, body, map[string]string{"If-Match": `"` + cur + `"`}) |
| - if status != http.StatusOK { |
| - t.Fatalf("current header = %d %s, want 200", status, out) |
| - } |
| - var res map[string]any |
| - if err := json.Unmarshal([]byte(out), &res); err != nil { |
| - t.Fatal(err) |
| - } |
| - if r := receiptOf(t, res); str(t, r, "version_before") != cur { |
| - t.Errorf("version_before = %v, want %q", r["version_before"], cur) |
| - } |
| - // Header and body agreeing is the same as either. |
| - now := storedVersion(t, fx, key) |
| - body = putBody(t, map[string]any{"if_version": now, "set": envSet("C", "3")}) |
| - if status, _, out := rawReqHeader(t, http.MethodPut, u, token, body, map[string]string{"If-Match": now}); status != http.StatusOK { |
| - t.Fatalf("agreeing header and body = %d %s, want 200", status, out) |
| - } |
| -} |
| - |
| // TestAPIConfigPutIfVersionOnDirStore: a store that cannot promise an |
| // atomic conditional write refuses if_version before any read (R2-2), and |
| // keeps unconditional writes as they were. |
diff --git a/internal/console/apiconfig_witness_test.go b/internal/console/apiconfig_witness_test.go| index 72efe4498..789fee92b 100644 |
| --- a/internal/console/apiconfig_witness_test.go |
| +++ b/internal/console/apiconfig_witness_test.go |
| @@ -104,10 +104,12 @@ func reqWithHeaders(t *testing.T, method, u, bearer, body string, hdr http.Heade |
| return res.StatusCode, string(out) |
| } |
| -// TestAPIConfigPutRefusesUnusablePrecondition: an If-Match or if_version |
| -// that is present but empty, a wildcard, a weak tag or a list is refused |
| -// with 400 before anything is read. None of them may fall back to an |
| -// unconditional write, and none is a 412: they are not versions at all. |
| +// TestAPIConfigPutRefusesUnusablePrecondition is the guard on the one place |
| +// the PUT decides whether a precondition was supplied (configIfVersion, |
| +// round 2 ruling): if_version is a non-empty JSON string or absent. Every |
| +// other form of the member, a repeated member, and an If-Match header |
| +// (no longer a synonym) is a 400 before anything is read. None may fall |
| +// back to an unconditional write, and none is a 412: no version was given. |
| func TestAPIConfigPutRefusesUnusablePrecondition(t *testing.T) { |
| fx, token := configFixture(t, "code-config-precondition-forms") |
| seedDefault(t, fx, token, envSet("A", "1")) |
| @@ -117,24 +119,31 @@ func TestAPIConfigPutRefusesUnusablePrecondition(t *testing.T) { |
| gen := genEtag(t, fx, "titogo") |
| cs := useCfgCountStore(fx, true) |
| u := configURL(fx, "titogo") |
| - plain := putBody(t, map[string]any{"set": envSet("B", "2")}) |
| + const set = `"set":{"B":{"kind":"env","value":"2"}}` |
| + plain := "{" + set + "}" |
| + stale := `"cv1:` + strings.Repeat("0", 64) + `"` |
| for _, c := range []struct { |
| what string |
| body string |
| hdr http.Header |
| }{ |
| - {"an empty quoted If-Match", plain, http.Header{"If-Match": {`""`}}}, |
| - {"an empty If-Match", plain, http.Header{"If-Match": {""}}}, |
| - {"a blank If-Match", plain, http.Header{"If-Match": {" "}}}, |
| - {"a wildcard If-Match", plain, http.Header{"If-Match": {"*"}}}, |
| - {"a weak If-Match of the current version", plain, http.Header{"If-Match": {`W/"` + cur + `"`}}}, |
| - {"an If-Match list", plain, http.Header{"If-Match": {`"` + cur + `", "cv1:00"`}}}, |
| - {"two If-Match headers", plain, http.Header{"If-Match": {`"` + cur + `"`, `"` + cur + `"`}}}, |
| - {"an unbalanced quote", plain, http.Header{"If-Match": {`"` + cur}}}, |
| - {"an empty if_version", `{"if_version":"","set":{"B":{"kind":"env","value":"2"}}}`, nil}, |
| - {"a blank if_version", `{"if_version":" ","set":{"B":{"kind":"env","value":"2"}}}`, nil}, |
| - {"an empty if_version beside a good header", `{"if_version":"","set":{"B":{"kind":"env","value":"2"}}}`, http.Header{"If-Match": {cur}}}, |
| + {"null", `{"if_version":null,` + set + `}`, nil}, |
| + {"empty string", `{"if_version":"",` + set + `}`, nil}, |
| + {"blank string", `{"if_version":" ",` + set + `}`, nil}, |
| + {"number", `{"if_version":0,` + set + `}`, nil}, |
| + {"boolean", `{"if_version":true,` + set + `}`, nil}, |
| + {"object", `{"if_version":{},` + set + `}`, nil}, |
| + {"array", `{"if_version":[],` + set + `}`, nil}, |
| + {"a version then null", `{"if_version":` + stale + `,"if_version":null,` + set + `}`, nil}, |
| + {"null then a version", `{"if_version":null,"if_version":"` + cur + `",` + set + `}`, nil}, |
| + {"two versions", `{"if_version":` + stale + `,"if_version":"` + cur + `",` + set + `}`, nil}, |
| + {"the same version twice", `{"if_version":"` + cur + `","if_version":"` + cur + `",` + set + `}`, nil}, |
| + {"null under another case", `{"IF_VERSION":null,` + set + `}`, nil}, |
| + {"two cases of the member", `{"if_version":"` + cur + `","If_Version":null,` + set + `}`, nil}, |
| + {"an If-Match header", plain, http.Header{"If-Match": {`"` + cur + `"`}}}, |
| + {"an If-Match header beside the field", `{"if_version":"` + cur + `",` + set + `}`, http.Header{"If-Match": {cur}}}, |
| + {"an empty If-Match header", plain, http.Header{"If-Match": {""}}}, |
| } { |
| before := cs.getCount() |
| status, out := reqWithHeaders(t, http.MethodPut, u, token, c.body, c.hdr) |
| @@ -149,16 +158,29 @@ func TestAPIConfigPutRefusesUnusablePrecondition(t *testing.T) { |
| t.Errorf("%s read the store %d times, want 0", c.what, n) |
| } |
| if !bytes.Equal(rawAtKey(t, fx, key), stored) { |
| - t.Fatalf("%s changed the stored bytes", c.what) |
| + t.Errorf("%s changed the stored bytes", c.what) |
| + _, etag, err := fx.rel.Get(context.Background(), key) |
| + if err == nil { |
| + _, err = fx.rel.Put(context.Background(), key, stored, etag) |
| + } |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| } |
| - if genEtag(t, fx, "titogo") != gen { |
| - t.Fatalf("%s bumped the generation", c.what) |
| + if g := genEtag(t, fx, "titogo"); g != gen { |
| + t.Errorf("%s bumped the generation", c.what) |
| + gen = g |
| } |
| } |
| - // An absent precondition is today's unconditional write, and a present |
| - // usable one still works, quoted or not. |
| - if status, out := reqWithHeaders(t, http.MethodPut, u, token, putBody(t, map[string]any{"if_version": cur, "set": envSet("B", "2")}), nil); status != http.StatusOK { |
| + // A version under another case of the name is still the precondition: |
| + // a stale one is a 412, not an unconditional write. |
| + if status, out := reqWithHeaders(t, http.MethodPut, u, token, `{"If_Version":`+stale+`,`+set+`}`, nil); status != http.StatusPreconditionFailed { |
| + t.Fatalf("a stale If_Version = %d %s, want 412", status, out) |
| + } |
| + // An absent precondition is today's unconditional write, and a usable |
| + // one still works. |
| + if status, out := reqWithHeaders(t, http.MethodPut, u, token, `{"if_version":"`+cur+`",`+set+`}`, nil); status != http.StatusOK { |
| t.Fatalf("a usable if_version = %d %s", status, out) |
| } |
| if status, out := reqWithHeaders(t, http.MethodPut, u, token, plain, nil); status != http.StatusOK { |