| 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") |