|
8 | 8 | "compress/gzip" |
9 | 9 | "compress/zlib" |
10 | 10 | "context" |
| 11 | + "encoding/binary" |
11 | 12 | "errors" |
12 | 13 | "fmt" |
13 | 14 | "io" |
@@ -961,6 +962,41 @@ func TestSnappyBlockRejectsOversizedDecodedLen(t *testing.T) { |
961 | 962 | assert.False(t, downstreamCalled, "downstream handler must not run when request is rejected") |
962 | 963 | } |
963 | 964 |
|
| 965 | +func TestSnappyBlockRejectsOversizedDecodedLenBeforeCompressedBodyLimit(t *testing.T) { |
| 966 | + t.Parallel() |
| 967 | + |
| 968 | + const maxBody = 1024 |
| 969 | + |
| 970 | + payload := make([]byte, binary.MaxVarintLen64+maxBody+1) |
| 971 | + n := binary.PutUvarint(payload, maxBody+1) |
| 972 | + payload = payload[:n+maxBody+1] |
| 973 | + require.Greater(t, len(payload), maxBody) |
| 974 | + |
| 975 | + downstreamCalled := false |
| 976 | + h := maxRequestBodySizeInterceptor( |
| 977 | + httpContentDecompressor( |
| 978 | + http.HandlerFunc(func(http.ResponseWriter, *http.Request) { |
| 979 | + downstreamCalled = true |
| 980 | + }), |
| 981 | + maxBody, |
| 982 | + defaultErrorHandler, |
| 983 | + defaultCompressionAlgorithms(), |
| 984 | + nil, |
| 985 | + ), |
| 986 | + maxBody, |
| 987 | + ) |
| 988 | + |
| 989 | + req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(payload)) |
| 990 | + req.Header.Set("Content-Encoding", "snappy") |
| 991 | + |
| 992 | + resp := httptest.NewRecorder() |
| 993 | + h.ServeHTTP(resp, req) |
| 994 | + |
| 995 | + assert.Equal(t, http.StatusBadRequest, resp.Code) |
| 996 | + assert.Contains(t, resp.Body.String(), "decoded size exceeds max request body size") |
| 997 | + assert.False(t, downstreamCalled, "downstream handler must not run when request is rejected") |
| 998 | +} |
| 999 | + |
964 | 1000 | func TestPooledZstdReadCloserReadAfterClose(t *testing.T) { |
965 | 1001 | h := httpContentDecompressor( |
966 | 1002 | http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
0 commit comments