hub: review fix 5 — a window request the hub cannot read is 400; every answer says accepts_ended (version skew)

- POST /offsite/window-open answered 200 on a body it could not parse and
  decided the window on „count 0”; now 400 (the empty body of the oldest boxes
  is still „no count”).
- Every answer carries accepts_ended, granted or not: a box sends its end
  records only to a hub that said so, so a new box never sends a large request
  to an older hub that reads 4 KiB.

Carries the red-proof of both halves (the controller's is the next commit
there). Tests seen red; 3 hub mutations seen red, 1 after a redo.

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
admin committed 2026-11-15 14:57:24 +01:00
1 parent 6d2d4bd09d
commit 45848f6ee4
8 files changed
+168 -5

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
)
// Review of 2026-11-15, finding 5 — version skew. A hub that cannot read a window request must say so (400), never
// decide a window on a count of 0; and it tells every box, in every answer, that it understands `ended` — a box sends
// its end records only to a hub that said so.
// RED-PROOF: documentation/audits/build-release-4-2026-11-15/A/redproof-review-5.txt.
func TestD84_WindowOpen_ABodyTheHubCannotReadIs400(t *testing.T) {
h, st, _ := newTestHandler(t)
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "ckey", RetrievalPassword: "pp"})
f := &fakeKeySvc{}
h.SetOffsiteKeyService(f)
post := func(body string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodPost, "/api/v1/offsite/window-open/c1", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer ckey")
rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
return rr
}
for name, body := range map[string]string{
"cut in the middle (a read limit)": `{"count_before":20,"ended":[{"app":"vikunja","snapshot_ids":["aaaa","bb`,
"not JSON": `count_before=20`,
"a list, not an object": `[20]`,
} {
f.opened = 0
if rr := post(body); rr.Code != http.StatusBadRequest {
t.Errorf("%s: HTTP %d, want 400 — the hub must not decide a window on a body it could not read", name, rr.Code)
}
if f.opened != 0 {
t.Errorf("%s: the window service was asked although the body could not be read", name)
}
}
// what every box has always sent still works — also the empty body of the oldest ones
for _, body := range []string{`{"count_before":20}`, ``} {
rr := post(body)
if rr.Code != http.StatusOK {
t.Errorf("body %q: HTTP %d, want 200", body, rr.Code)
}
// … and EVERY answer says that this hub understands ended apps — here a window that is NOT granted
var ans map[string]any
_ = json.Unmarshal(rr.Body.Bytes(), &ans)
if ans["accepts_ended"] != true || ans["granted"] != false {
t.Errorf("body %q: the answer %s does not say accepts_ended — a new box would never send its end records", body, rr.Body.String())
}
}
}
+12 -3
View File
@@ -6,6 +6,7 @@ import (
"errors"
"io"
"net/http"
"strings"
"time"
"gitea.dooplex.hu/admin/felhom-hub/internal/offsitekeys"
@@ -141,10 +142,17 @@ func (h *Handler) handleOffsiteWindowOpen(w http.ResponseWriter, r *http.Request
// agrees per id from its OWN records (offsitekeys/ended.go); the box's dates decide nothing.
Ended []offsitekeys.EndedClaim `json:"ended"`
}
// 1 MiB: a snapshot id is 64 characters, and a removed app can hold a few hundred snapshots. A body that is cut
// does not parse, and then nothing is claimed — the window is still decided on count_before 0, as before.
// 1 MiB: a snapshot id is 64 characters, and a removed app can hold a few hundred snapshots.
// A body the hub cannot read is REFUSED (review of 2026-11-15): until then a body that was cut at the read limit,
// or was not JSON, was decided as „count_before 0” — a window opened with the smallest cap, which then refused
// the honest plan every week. The empty body of the oldest boxes is still „no count”.
body, _ := io.ReadAll(io.LimitReader(r.Body, 1<<20))
_ = json.Unmarshal(body, &req)
if len(strings.TrimSpace(string(body))) > 0 {
if err := json.Unmarshal(body, &req); err != nil {
http.Error(w, "the window request could not be read (not JSON, or cut)", http.StatusBadRequest)
return
}
}
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Minute)
defer cancel()
g, err := h.offsiteKeys.OpenWindowFor(ctx, customerID, req.CountBefore, req.Ended...)
@@ -152,6 +160,7 @@ func (h *Handler) handleOffsiteWindowOpen(w http.ResponseWriter, r *http.Request
offsiteKeyErr(w, err)
return
}
g.AcceptsEnded = true // every answer of this hub, granted or not
writeJSON(w, http.StatusOK, g)
}
+2
View File
@@ -55,6 +55,7 @@ type fakeKeySvc struct {
gotPub string
gotFP string
gotEnded []offsitekeys.EndedClaim
opened int
err error
}
@@ -68,6 +69,7 @@ func (f *fakeKeySvc) ConfirmKey(_ context.Context, _ string, fp string) (int, er
}
func (f *fakeKeySvc) OpenWindowFor(_ context.Context, _ string, _ int, ended ...offsitekeys.EndedClaim) (offsitekeys.WindowGrant, error) {
f.gotEnded = ended
f.opened++
return offsitekeys.WindowGrant{Granted: false, Reason: "not due"}, f.err
}
func (f *fakeKeySvc) CloseWindowFor(context.Context, string, offsitekeys.WindowResult) error {