diff --git a/traffic/encoding.go b/traffic/encoding.go index e93f99fd..127d9098 100644 --- a/traffic/encoding.go +++ b/traffic/encoding.go @@ -5,6 +5,7 @@ import ( "compress/flate" "compress/gzip" "compress/zlib" + "errors" "io" "net/http" "strings" @@ -16,8 +17,9 @@ import ( // returns at most limit decoded bytes. The proxy records bodies as they crossed // the wire, so a compressed body has to be decoded before its text can be // indexed or matched. ok is false when the body is not encoded, the encoding is -// unsupported or stacked, or the data does not decode. The output is bounded -// because the body comes from the target. +// unsupported or stacked, or the data does not decode. A truncated stream +// yields what decoded before the cut. The output is bounded because the body +// comes from the target. func decodeBody(header http.Header, body []byte, limit int) (out []byte, ok bool) { enc := strings.ToLower(strings.TrimSpace(header.Get("Content-Encoding"))) if enc == "" || enc == "identity" || len(body) == 0 || strings.Contains(enc, ",") { @@ -44,21 +46,27 @@ func decodeBody(header http.Header, body []byte, limit int) (out []byte, ok bool return nil, false } out, err := io.ReadAll(io.LimitReader(zr, int64(limit))) - if err != nil { + if err != nil && (!errors.Is(err, io.ErrUnexpectedEOF) || len(out) == 0) { return nil, false } return out, true } -// indexBody returns the full-text index text of a recorded body: the decoded -// text of a compressed body, otherwise what spill computed. -func indexBody(header http.Header, body []byte, spilled string) string { +// indexBody returns the full-text index text of a compressed body. ok is false +// when the body is not one decodeBody handles, and the index text spill +// computes stands. It does no locking, so callers run it before taking wmu. +func indexBody(header http.Header, body []byte) (text string, ok bool) { + ct := header.Get("Content-Type") + if isBinaryBody(ct, nil) { + // A binary content type is never indexed, encoded or not. + return "", false + } dec, ok := decodeBody(header, body, maxIndexBody) if !ok { - return spilled + return "", false } - if isBinaryBody(header.Get("Content-Type"), dec) { - return "" + if isBinaryBody(ct, dec) { + return "", true } - return string(dec) + return string(dec), true } diff --git a/traffic/encoding_test.go b/traffic/encoding_test.go index c540e8db..93d21a85 100644 --- a/traffic/encoding_test.go +++ b/traffic/encoding_test.go @@ -4,6 +4,7 @@ import ( "bytes" "compress/gzip" "compress/zlib" + "fmt" "io" "net/http" "net/http/httptest" @@ -106,3 +107,39 @@ func TestDecodeBodyIsBounded(t *testing.T) { t.Fatalf("decodeBody returned %d bytes, ok=%v; want %d", len(out), ok, maxIndexBody) } } + +// TestTruncatedCompressedBodyIsIndexed covers a capture cut short mid-stream: +// the text decoded before the cut is still indexed. +func TestTruncatedCompressedBodyIsIndexed(t *testing.T) { + var buf bytes.Buffer + gz := gzip.NewWriter(&buf) + io.WriteString(gz, compressedPage) + for i := range 4096 { + fmt.Fprintf(gz, "row %d\n", i) + } + gz.Close() + cut := buf.Bytes()[:buf.Len()/2] + + tr, _ := openTraffic(t) + f := newFlow("api.example.com", "GET", "/cut", nil, cut) + f.Response.Header.Set("Content-Type", "text/html") + f.Response.Header.Set("Content-Encoding", "gzip") + tr.record(f) + hits, err := tr.query("api.example.com", "", "root:x:0", 0, 10) + if err != nil || len(hits) != 1 { + t.Fatalf("body search for text of a truncated gzip response: %d rows, err=%v; want 1", len(hits), err) + } +} + +// TestIndexBodySkipsBinaryContentType keeps an encoded body with a binary +// content type out of the index without decoding it. +func TestIndexBodySkipsBinaryContentType(t *testing.T) { + var buf bytes.Buffer + gz := gzip.NewWriter(&buf) + io.WriteString(gz, compressedPage) + gz.Close() + h := http.Header{"Content-Encoding": {"gzip"}, "Content-Type": {"image/png"}} + if text, ok := indexBody(h, buf.Bytes()); ok || text != "" { + t.Fatalf("indexBody on an image/png body = (%q, %v), want (\"\", false)", text, ok) + } +} diff --git a/traffic/traffic.go b/traffic/traffic.go index ddd9fe10..0add6303 100644 --- a/traffic/traffic.go +++ b/traffic/traffic.go @@ -711,6 +711,10 @@ const unavailableMarker = "[original request unavailable]\n" // so there is never a rewritten exchange without its trace. Without trace // lines this is exactly what record always wrote. func (t *Traffic) recordViews(rq requestView, rs responseView, lines []rewrite.TraceLine) { + // Decoding a compressed body is CPU work on up to maxIndexBody bytes each; + // it runs before the write lock so concurrent records do not wait on it. + reqIdx, reqDecoded := indexBody(rq.header, rq.body) + respIdx, respDecoded := indexBody(rs.header, rs.body) // The write lock covers blob + index writes, so DeleteHost (and its blob GC) // can run under the same lock without racing a concurrent record. t.wmu.Lock() @@ -733,8 +737,12 @@ func (t *Traffic) recordViews(rq requestView, rs responseView, lines []rewrite.T // work and must not sit inside the SQLite write lock. reqB := t.spill(rq.body, rq.header.Get("Content-Type")) respB := t.spill(rs.body, ct) - reqB.index = indexBody(rq.header, rq.body, reqB.index) - respB.index = indexBody(rs.header, rs.body, respB.index) + if reqDecoded { + reqB.index = reqIdx + } + if respDecoded { + respB.index = respIdx + } trace := t.spillTrace(lines) // A rewritten exchange is logged by host, step and rule ids only: its URL