carlos / platform Public

console tests: expire demo API proofs on a seam (#1247)

TestDemoAPIProofRequiredClaimExpiry signed its proof 29 s in the past
against a 30 s skew. Sign truncates to whole seconds, so the proof had
between 0 and 1 s left at the door. A stall there got
requireDemoAPIProof's refusal: a 503 with Retry-After and no body, which
the test read as JSON. The 3.34 s on CI fits the second request, refused
at once after the first request's 3 s body delay.

The empty 503 is deliberate and TestDemoAPIProofRequiredRefusesStalledBody
pins it, so production behaviour does not change. The fault is the test's
timing.

Proofs are now checked against edgeProofNow, which is time.Now unless a
test sets a clock. Both expiry tests sign on that clock and move it past
the skew at the first body read, so admission no longer depends on load
and the 3 s sleeps are gone. An empty reply now reports its status and
headers.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev aa9c736a0566daa151569d36851d9f14642433f1 parent ff7fffc
6 files changed, +49 −17
  • .amadan/code-health.tsv +0 −1
  • internal/console/apidemoreports.go +1 −1
  • internal/console/demo_api_alias_integration_test.go +9 −4
  • internal/console/demo_api_origin.go +1 −1
  • internal/console/demo_api_origin_test.go +25 −9
  • internal/console/edgeproof.go +13 −1

CI cancelled — superseded by a newer push — run details

diff --git a/.amadan/code-health.tsv b/.amadan/code-health.tsv
index 638c01f4c..ae1864170 100644
--- a/.amadan/code-health.tsv
+++ b/.amadan/code-health.tsv
@@ -768,7 +768,6 @@ sleep internal/console (*claimCase).systemJob time.Sleep 1
sleep internal/console (*dbFx).waitIdle time.Sleep 1
sleep internal/console (*dbfFixture).admitWithin time.Sleep 1
sleep internal/console (*dbfHeldWriter).Write time.Sleep 1
-sleep internal/console (*demoAPIExpiryBody).Read time.Sleep 1
sleep internal/console (*reportCDP).wait time.Sleep 1
sleep internal/console (*slowCountingStore).Get time.Sleep 1
sleep internal/console (*slowGetStore).Get time.Sleep 1
diff --git a/internal/console/apidemoreports.go b/internal/console/apidemoreports.go
index 1727fb853..81487f2a4 100644
--- a/internal/console/apidemoreports.go
+++ b/internal/console/apidemoreports.go
@@ -238,5 +238,5 @@ func (s *Server) demoReportForwardedByEdge(r *http.Request, xff string) bool {
return false
}
current, next := s.edgeProofKeys(r.Context())
- return edgeproof.Verify(current, next, r.Host, xff, proof, time.Now())
+ return edgeproof.Verify(current, next, r.Host, xff, proof, s.edgeProofNow())
}
diff --git a/internal/console/demo_api_alias_integration_test.go b/internal/console/demo_api_alias_integration_test.go
index 6f3fe942a..e7a3ef51d 100644
--- a/internal/console/demo_api_alias_integration_test.go
+++ b/internal/console/demo_api_alias_integration_test.go
@@ -200,22 +200,27 @@ func TestDemoAPIProofRequiredClaimExpiry(t *testing.T) {
t.Fatal(err)
}
s.markEdgeProofSeen()
+ now, expire := demoAPIProofClock(s)
const client, peer, forged = "192.0.2.1", "127.0.0.1", "203.0.113.9"
send := func(path string, fields map[string]string) *httptest.ResponseRecorder {
b, err := json.Marshal(fields)
if err != nil {
t.Fatal(err)
}
- r := httptest.NewRequest("POST", s.cfg.Origin+path, &demoAPIExpiryBody{Reader: strings.NewReader(string(b)), delay: 3 * time.Second})
+ r := httptest.NewRequest("POST", s.cfg.Origin+path, &demoAPIExpiryBody{Reader: strings.NewReader(string(b)), expire: expire})
r.RemoteAddr = peer + ":1234"
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Forwarded", "for="+forged)
r.Header.Set("X-Real-IP", forged)
r.Header.Set("X-Forwarded-For", client)
- r.Header.Set(edgeproof.Header, edgeproof.Sign(key, r.Host, client, time.Now().Add(-29*time.Second)))
+ r.Header.Set(edgeproof.Header, edgeproof.Sign(key, r.Host, client, now()))
w := httptest.NewRecorder()
s.mux.ServeHTTP(w, r)
- if edgeproof.Verify(key, nil, r.Host, client, r.Header.Get(edgeproof.Header), time.Now()) {
+ if w.Body.Len() == 0 {
+ // requireDemoAPIProof's refusal is an empty 503 by design.
+ t.Fatalf("%s: empty reply: status = %d, headers = %v", path, w.Code, w.Header())
+ }
+ if edgeproof.Verify(key, nil, r.Host, client, r.Header.Get(edgeproof.Header), now()) {
t.Fatal("proof did not expire during body read")
}
var response map[string]json.RawMessage
@@ -297,7 +302,7 @@ func TestDemoAPIProofRequiredClaimExpiry(t *testing.T) {
r.RemoteAddr = peer + ":1234"
r.Header.Set("Authorization", "Bearer "+start.Flow)
r.Header.Set("X-Forwarded-For", client)
- r.Header.Set(edgeproof.Header, edgeproof.Sign(key, r.Host, client, time.Now()))
+ r.Header.Set(edgeproof.Header, edgeproof.Sign(key, r.Host, client, now()))
w := httptest.NewRecorder()
s.mux.ServeHTTP(w, r)
want := http.StatusAccepted
diff --git a/internal/console/demo_api_origin.go b/internal/console/demo_api_origin.go
index 5d8f75bb2..68378e252 100644
--- a/internal/console/demo_api_origin.go
+++ b/internal/console/demo_api_origin.go
@@ -49,7 +49,7 @@ func (s *Server) requireDemoAPIProof(w http.ResponseWriter, r *http.Request) boo
if err == nil && origin.Host != "" && normalizeHost(r.Host) == normalizeHost(origin.Host) && len(xff) == 1 && len(proofs) == 1 {
if ip, err := demo.CanonicalIP(xff[0]); err == nil && ip == xff[0] {
current, next := s.edgeProofKeys(r.Context())
- if edgeproof.Verify(current, next, r.Host, xff[0], proofs[0], time.Now()) {
+ if edgeproof.Verify(current, next, r.Host, xff[0], proofs[0], s.edgeProofNow()) {
return true
}
}
diff --git a/internal/console/demo_api_origin_test.go b/internal/console/demo_api_origin_test.go
index 11c716d88..423a135d7 100644
--- a/internal/console/demo_api_origin_test.go
+++ b/internal/console/demo_api_origin_test.go
@@ -10,6 +10,7 @@ import (
"net/http"
"net/http/httptest"
"strings"
+ "sync/atomic"
"testing"
"time"
@@ -329,20 +330,34 @@ func TestDemoAPIHostOwnership(t *testing.T) {
}
}
-// Delay the first body read until the admitted proof has expired.
+// demoAPIExpiryBody runs expire at the first body read, so a proof admitted
+// at the door has expired by the time the handler has the body.
type demoAPIExpiryBody struct {
io.Reader
- delay time.Duration
+ expire func()
}
func (b *demoAPIExpiryBody) Read(p []byte) (int, error) {
- if b.delay != 0 {
- time.Sleep(b.delay)
- b.delay = 0
+ if b.expire != nil {
+ b.expire()
+ b.expire = nil
}
return b.Reader.Read(p)
}
+// demoAPIProofClock gives s a proof clock that moves only when expire is
+// called, past edgeproof.Skew. A proof signed at now() is therefore valid at
+// the door however slowly the machine runs. Signing 29 s in the past on the
+// real clock left at most a second, and less the later in a second it was
+// signed: a stall there got the door's empty 503 (#1247).
+func demoAPIProofClock(s *Server) (now func() time.Time, expire func()) {
+ var at atomic.Int64
+ at.Store(time.Now().UnixNano())
+ now = func() time.Time { return time.Unix(0, at.Load()) }
+ s.edgeProof.now = now
+ return now, func() { at.Add(int64(edgeproof.Skew + time.Second)) }
+}
+
func TestDemoAPIProofRequiredPublishExpiry(t *testing.T) {
s, _ := demoJobServer(t)
s.cfg.Origin = "https://console.example.com"
@@ -351,17 +366,18 @@ func TestDemoAPIProofRequiredPublishExpiry(t *testing.T) {
if _, err := s.cfg.Releases.Put(t.Context(), edgeproof.MeshKeyObject, []byte(hex.EncodeToString(key)), ""); err != nil {
t.Fatal(err)
}
- r := httptest.NewRequest("POST", s.cfg.Origin+"/api/demo/apps", &demoAPIExpiryBody{Reader: bytes.NewReader(demoHTTPSource(t)), delay: 3 * time.Second})
+ now, expire := demoAPIProofClock(s)
+ r := httptest.NewRequest("POST", s.cfg.Origin+"/api/demo/apps", &demoAPIExpiryBody{Reader: bytes.NewReader(demoHTTPSource(t)), expire: expire})
r.RemoteAddr = "127.0.0.1:1234"
r.Header.Set("Content-Type", "application/gzip")
r.Header.Set("X-Forwarded-For", "192.0.2.1")
- r.Header.Set(edgeproof.Header, edgeproof.Sign(key, r.Host, "192.0.2.1", time.Now().Add(-29*time.Second)))
+ r.Header.Set(edgeproof.Header, edgeproof.Sign(key, r.Host, "192.0.2.1", now()))
w := newDemoRecorder()
s.mux.ServeHTTP(w, r)
if w.Code != 202 {
- t.Fatalf("publish status = %d", w.Code)
+ t.Fatalf("publish status = %d, headers = %v", w.Code, w.Header())
}
- if edgeproof.Verify(key, nil, r.Host, "192.0.2.1", r.Header.Get(edgeproof.Header), time.Now()) {
+ if edgeproof.Verify(key, nil, r.Host, "192.0.2.1", r.Header.Get(edgeproof.Header), now()) {
t.Fatal("proof did not expire during upload")
}
var ip string
diff --git a/internal/console/edgeproof.go b/internal/console/edgeproof.go
index 5c929111b..3a3f336e7 100644
--- a/internal/console/edgeproof.go
+++ b/internal/console/edgeproof.go
@@ -61,6 +61,18 @@ type edgeProofState struct {
loaded atomic.Bool
seen atomic.Bool
warned atomic.Bool
+
+ // now is the clock proofs are checked against; nil means time.Now. Tests
+ // use it to expire a proof at an exact point in a request.
+ now func() time.Time
+}
+
+// edgeProofNow is the time a proof's age is measured at.
+func (s *Server) edgeProofNow() time.Time {
+ if s.edgeProof.now != nil {
+ return s.edgeProof.now()
+ }
+ return time.Now()
}
// forwardedByEdge reports whether a loopback peer's X-Forwarded-For chain
@@ -69,7 +81,7 @@ func (s *Server) forwardedByEdge(r *http.Request, xff string) bool {
st := &s.edgeProof
if proof := r.Header.Get(edgeproof.Header); proof != "" {
current, next := s.edgeProofKeys(r.Context())
- if edgeproof.Verify(current, next, r.Host, xff, proof, time.Now()) {
+ if edgeproof.Verify(current, next, r.Host, xff, proof, s.edgeProofNow()) {
s.markEdgeProofSeen()
return true
}