Files
felhom.eu/hub/internal/api/d84_window_skew_test.go
T
admin 45848f6ee4 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
2026-11-15 14:57:24 +01:00

56 lines
2.2 KiB
Go

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())
}
}
}