oficina / slides Public

Slides: a multipart preview is read to its end before it is answered

Task 3 review, fix round 2. The multipart parser stops at the closing
boundary, so a small html part followed by megabytes of epilogue was
answered. The rest of the capped body is now drained, and a request
past 2 MiB + 64 KiB is refused, with its length declared or not.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 488b3578f5f632a2da3a39682970da927f1303d0 parent 7214de9
2 files changed, +33 −0
  • internal/slides/scenes.go +6 −0
  • internal/slidestest/scenes_serving_test.go +27 −0
diff --git a/internal/slides/scenes.go b/internal/slides/scenes.go
index 130a85b..d8932eb 100644
--- a/internal/slides/scenes.go
+++ b/internal/slides/scenes.go
@@ -372,5 +372,11 @@ func previewField(r *http.Request) ([]byte, error) {
if !seen {
return nil, errPreviewFields
}
+ // The parser stops at the closing boundary, and an epilogue may
+ // follow it. The request is read through to its end, so one past the
+ // limit is refused (MaxBytesReader trips) rather than answered.
+ if _, err := io.Copy(io.Discard, r.Body); err != nil {
+ return nil, err
+ }
return html, nil
}
diff --git a/internal/slidestest/scenes_serving_test.go b/internal/slidestest/scenes_serving_test.go
index f6bf574..d208f3f 100644
--- a/internal/slidestest/scenes_serving_test.go
+++ b/internal/slidestest/scenes_serving_test.go
@@ -9,6 +9,7 @@ package slidestest
import (
"bytes"
"fmt"
+ "io"
"mime/multipart"
"net/http"
"net/http/httptest"
@@ -330,8 +331,15 @@ func TestThePreviewAnswersUnderTheSealedPolicy(t *testing.T) {
h := productionHandler(t, a)
draft := "<!doctype html><p>draft</p><script>parent.postMessage({type:'ready'}, '*')</script>"
+ // unknownLength posts with no Content-Length, as a chunked request
+ // arrives: the body is a plain reader and ContentLength is -1.
+ unknownLength := false
post := func(contentType string, body []byte, cookies []*http.Cookie, origin string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodPost, deck+"/scenes/preview", bytes.NewReader(body))
+ if unknownLength {
+ req.Body = io.NopCloser(struct{ io.Reader }{bytes.NewReader(body)})
+ req.ContentLength = -1
+ }
req.Header.Set("Content-Type", contentType)
req.Header.Set("Origin", origin)
if origin == testOrigin {
@@ -402,6 +410,25 @@ func TestThePreviewAnswersUnderTheSealedPolicy(t *testing.T) {
assertRefusal(t, "a multipart form with another field", post(otherType, otherBody, cookies, testOrigin), http.StatusBadRequest, "invalid")
twiceType, twiceBody := multipartForm(t, [2]string{"html", draft}, [2]string{"html", draft})
assertRefusal(t, "a multipart form with html twice", post(twiceType, twiceBody, cookies, testOrigin), http.StatusBadRequest, "invalid")
+ // A small html part, its closing boundary, then megabytes of
+ // epilogue: the multipart parser stops at the boundary, but the
+ // request is still over the limit and is refused, with its length
+ // declared or not. Under the limit, an epilogue is harmless.
+ epType, epBody := multipartForm(t, [2]string{"html", draft})
+ small := append(append([]byte{}, epBody...), "\r\nepilogue\r\n"...)
+ huge := append(append([]byte{}, epBody...), bytes.Repeat([]byte("x"), 3<<20)...)
+ for _, unknown := range []bool{false, true} {
+ unknownLength = unknown
+ what := fmt.Sprintf(" (length unknown: %t)", unknown)
+ assertRefusal(t, "a multipart preview with a huge epilogue"+what, post(epType, huge, cookies, testOrigin), http.StatusRequestEntityTooLarge, "too_large")
+ if rec := post(epType, small, cookies, testOrigin); rec.Code != http.StatusOK || rec.Body.String() != draft {
+ t.Errorf("a multipart preview with a small epilogue%s: status %d %q, want 200 and the draft", what, rec.Code, rec.Body)
+ }
+ if rec := post(epType, epBody, cookies, testOrigin); rec.Code != http.StatusOK {
+ t.Errorf("a multipart preview%s: status %d, want 200", what, rec.Code)
+ }
+ }
+ unknownLength = false
assertRefusal(t, "a JSON preview", post("application/json", []byte(`{"html":"x"}`), cookies, testOrigin), http.StatusUnsupportedMediaType, "unsupported_type")
if !strings.Contains(sealedPolicy, "sandbox allow-scripts;") {
t.Fatal("the sealed policy lost its sandbox")