diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d58c11c7..b7148ccb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ All notable changes to this project are documented in this file. The format is b ### Fixed +- **`traffic_search` host matching**: a port search such as `host:8443` matched any capture of that host whose URL quoted `host:8443` in its query string (`?next=http://host:8443/`, typical of open-redirect and SSRF probes), because the authority prefix was not anchored to the scheme. It is now anchored to `http`, `https`, `ws` and `wss`. The recorded host keeps the client's case while the search is lowercased, so a capture of `https://Mixed.Example.com/` was not found by any host search; the comparison now ignores case. A compressed body with a bad checksum or trailing garbage now keeps the text decoded before the error instead of dropping out of the index, and the store directory guard on host delete ignores case for case-insensitive filesystems. - **Browser capture keeps a bounded request map**: unfinished debugger requests are capped at 500, and a request that fails to load is dropped instead of waiting for eviction. A stored body longer than 256K JavaScript characters is cut and marked `[truncated by extension]`. That count is UTF-16 code units, not bytes; the server still enforces its byte cap on ingest. - **Browser capture keeps a bounded request map**: unfinished debugger requests are capped at 500, and a stored request body is cut at the same 256 KiB limit as ingest. - **The capture proxy no longer holds a response while indexing it** (#542): recording wrote the trigram full-text index of each body (up to 4 MiB) before the client got its response, under the recorder's global lock, so a 1 MB script added about 160 ms to its own response and to every response recorded behind it. The index is now written by a background worker. Body searches wait for entries queued before them, so they still find every exchange recorded before the search. An entry whose exchange was deleted in the meantime is dropped. diff --git a/traffic/encoding.go b/traffic/encoding.go index 127d9098d..ee19cf401 100644 --- a/traffic/encoding.go +++ b/traffic/encoding.go @@ -5,7 +5,6 @@ import ( "compress/flate" "compress/gzip" "compress/zlib" - "errors" "io" "net/http" "strings" @@ -17,8 +16,8 @@ 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. A truncated stream -// yields what decoded before the cut. The output is bounded because the body +// unsupported or stacked, or nothing decodes. A truncated or corrupt stream +// yields what decoded before the error. 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"))) @@ -46,7 +45,7 @@ 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 && (!errors.Is(err, io.ErrUnexpectedEOF) || len(out) == 0) { + if err != nil && len(out) == 0 { return nil, false } return out, true diff --git a/traffic/encoding_test.go b/traffic/encoding_test.go index 93d21a853..142e49cb7 100644 --- a/traffic/encoding_test.go +++ b/traffic/encoding_test.go @@ -143,3 +143,17 @@ func TestIndexBodySkipsBinaryContentType(t *testing.T) { t.Fatalf("indexBody on an image/png body = (%q, %v), want (\"\", false)", text, ok) } } + +// A gzip body with a bad checksum still yields the text that decoded. +func TestDecodeBodyKeepsTextOnBadGzipChecksum(t *testing.T) { + var buf bytes.Buffer + gz := gzip.NewWriter(&buf) + io.WriteString(gz, compressedPage) + gz.Close() + b := buf.Bytes() + b[len(b)-8] ^= 0xff + out, ok := decodeBody(http.Header{"Content-Encoding": {"gzip"}}, b, maxIndexBody) + if !ok || string(out) != compressedPage { + t.Fatalf("decodeBody on a bad gzip checksum = (%q, %v), want the decoded text", out, ok) + } +} diff --git a/traffic/traffic.go b/traffic/traffic.go index d4be9c7a6..07bf7bd8e 100644 --- a/traffic/traffic.go +++ b/traffic/traffic.go @@ -1115,7 +1115,7 @@ func sanitize(s string) string { // Host trees never use these names. DeleteAll skips every underscore-prefixed // entry for the same reason. func storeDir(name string) bool { - switch name { + switch strings.ToLower(name) { case "_index", "_blobs", "_ca", "_delete_staging": return true default: @@ -2504,14 +2504,20 @@ func (t *Traffic) query(host, contains, bodyContains string, page, limit int) ([ if err != nil { return nil, err } - q += ` AND host=?` + // Recorded hosts keep the client's case; hostName is already lowercase. + q += ` AND lower(host)=?` args = append(args, hostName) if port != "" { // Recorded rows historically store URL.Hostname() (without the port), // so constrain the original URL authority as a compatibility fallback. // New and old captures therefore share the same search contract. authority := net.JoinHostPort(hostName, port) - prefixes := []string{"%://" + authority} + var prefixes []string + // Anchored per scheme, so a URL quoted in the query string (an + // open-redirect or SSRF probe) can't match. + for _, scheme := range []string{"http", "https", "ws", "wss"} { + prefixes = append(prefixes, scheme+"://"+authority) + } // Clients leave a default port out of the Host header, so a capture // of https://host:443 is recorded as https://host, and a WebSocket // over it as wss://host. diff --git a/traffic/traffic_store_test.go b/traffic/traffic_store_test.go index 81dad35e0..4a98d8561 100644 --- a/traffic/traffic_store_test.go +++ b/traffic/traffic_store_test.go @@ -6,6 +6,7 @@ import ( "net/url" "os" "path/filepath" + "slices" "strings" "sync" "testing" @@ -487,6 +488,40 @@ func TestQueryHostDefaultPort(t *testing.T) { } } +// A URL quoted in the query string must not satisfy a port search, and the +// recorded host keeps the client's case while the search is lowercased. +func TestQueryHostAnchoredAndCaseInsensitive(t *testing.T) { + tr, _ := openTraffic(t) + redir := newFlow("x", "GET", "/", nil, []byte("redir")) + redir.Request.URL, _ = url.Parse("http://go.example.com/r?next=http://go.example.com:8443/x") + tr.record(redir) + caps := newFlow("x", "GET", "/", nil, []byte("caps")) + caps.Request.URL, _ = url.Parse("https://Mixed.Example.com/") + tr.record(caps) + + for _, tc := range []struct { + host string + want []string + }{ + {host: "go.example.com:8443"}, + {host: "go.example.com:80", want: []string{"http://go.example.com/r?next=http://go.example.com:8443/x"}}, + {host: "mixed.example.com", want: []string{"https://Mixed.Example.com/"}}, + {host: "Mixed.Example.com:443", want: []string{"https://Mixed.Example.com/"}}, + } { + rows, err := tr.query(tc.host, "", "", 0, 10) + if err != nil { + t.Fatal(err) + } + var got []string + for _, r := range rows { + got = append(got, r.URL) + } + if !slices.Equal(got, tc.want) { + t.Errorf("query(%q) = %v, want %v", tc.host, got, tc.want) + } + } +} + func TestNormalizeSearchHost(t *testing.T) { for _, tc := range []struct { raw, host, port string diff --git a/traffic/traffic_test.go b/traffic/traffic_test.go index e767a88d2..75ea0819d 100644 --- a/traffic/traffic_test.go +++ b/traffic/traffic_test.go @@ -182,7 +182,7 @@ VALUES(?,?,?,?,?,?,?,?,?,?,?)`, func TestHostDirSkipsStorePaths(t *testing.T) { root := t.TempDir() - for _, name := range []string{"_index", "_blobs", "_ca", "_delete_staging", ".", ".."} { + for _, name := range []string{"_index", "_blobs", "_ca", "_delete_staging", "_INDEX", ".", ".."} { if _, ok := hostDir(root, name); ok { t.Fatalf("hostDir(%q) was accepted", name) }