carlos / platform Public

console: an empty chunked body is no body on the offline doors

Astra gate review P2 on Task 3: readJSONAllowEmpty only knows a
known-length empty body, so an empty chunked POST to seal, flip, open,
finish or abort was refused where it used to be accepted.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 0596aedb6d24503fa6085fa4a4379f6526360818 parent ae7310b
2 files changed, +38 −0
  • internal/console/apiinstancemove_offline.go +14 −0
  • internal/console/apiinstancemove_offline_test.go +24 −0

CI failed — run details

diff --git a/internal/console/apiinstancemove_offline.go b/internal/console/apiinstancemove_offline.go
index 61e67d0a0..11728701a 100644
--- a/internal/console/apiinstancemove_offline.go
+++ b/internal/console/apiinstancemove_offline.go
@@ -1,6 +1,7 @@
package console
import (
+ "bufio"
"context"
"crypto/rand"
"crypto/sha256"
@@ -8,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "io"
"log"
"net/http"
"os"
@@ -148,6 +150,18 @@ func (s *Server) offlineExpects(w http.ResponseWriter, r *http.Request, d offlin
var req struct {
ID string `json:"id"`
}
+ if r.ContentLength < 0 && r.Body != nil {
+ // A body of unknown length (chunked) that turns out empty is no
+ // body, as it always was for these doors.
+ br := bufio.NewReader(r.Body)
+ if _, err := br.Peek(1); errors.Is(err, io.EOF) {
+ return s.offlineMoveIs(w, d, "")
+ }
+ r.Body = struct {
+ io.Reader
+ io.Closer
+ }{br, r.Body}
+ }
if !readJSONAllowEmpty(w, r, &req) {
return false
}
diff --git a/internal/console/apiinstancemove_offline_test.go b/internal/console/apiinstancemove_offline_test.go
index 273f17d0f..a4745b8d9 100644
--- a/internal/console/apiinstancemove_offline_test.go
+++ b/internal/console/apiinstancemove_offline_test.go
@@ -7,7 +7,9 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
+ "io"
"net/http"
+ "net/http/httptest"
"strings"
"sync"
"testing"
@@ -1750,3 +1752,25 @@ func TestOfflineNudgeDoor(t *testing.T) {
// Another move's id: MV-O35, and not even the serial moves.
w.refused(http.MethodPost, "/nudge", withID("", otherMoveID(t)), http.StatusConflict, "the record carries a different offline move")
}
+
+// An empty body of unknown length (chunked framing) is no body: the doors
+// that never read one answer as before (Astra gate review, Task 3 P2).
+func TestOfflineExpectsEmptyChunkedBody(t *testing.T) {
+ s := &Server{}
+ for _, ctype := range []string{"", "application/json"} {
+ for _, body := range []string{"", `{"id":""}`} {
+ if ctype == "" && body != "" {
+ continue // a typed body is refused without its type, as for every JSON door
+ }
+ r := httptest.NewRequest(http.MethodPost, "/", io.NopCloser(strings.NewReader(body)))
+ r.ContentLength = -1
+ if ctype != "" {
+ r.Header.Set("Content-Type", ctype)
+ }
+ rec := httptest.NewRecorder()
+ if !s.offlineExpects(rec, r, offlineDoor{}) {
+ t.Fatalf("chunked %q (%q) refused: %d %s", body, ctype, rec.Code, rec.Body)
+ }
+ }
+ }
+}