From b8c80cb9dde2790e833ceb8bea43bcd18cccbd42 Mon Sep 17 00:00:00 2001 From: Chaitanya Deshpande Date: Thu, 26 Nov 2026 17:06:22 +0530 Subject: [PATCH] fix(traffic): match default-port captures in traffic_search Clients leave :80 and :443 out of the Host header, so a capture of https://host is recorded without a port. traffic_search with host:443, host:80 or a URL naming the default port constrained the URL authority to the explicit port and returned no traffic for those captures. Also match the scheme's portless form when the port is the default. --- traffic/traffic.go | 21 +++++++++++++------ traffic/traffic_store_test.go | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/traffic/traffic.go b/traffic/traffic.go index b84d493f..e7c7f4fb 100644 --- a/traffic/traffic.go +++ b/traffic/traffic.go @@ -2419,12 +2419,21 @@ func (t *Traffic) query(host, contains, bodyContains string, page, limit int) ([ // 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) - q += ` AND (url LIKE ? OR url LIKE ? OR url LIKE ?)` - args = append(args, - "%://"+authority+"/%", - "%://"+authority+"?%", - "%://"+authority, - ) + prefixes := []string{"%://" + authority} + // Clients leave a default port out of the Host header, so a capture + // of https://host:443 is recorded as https://host. + switch port { + case "80": + prefixes = append(prefixes, "http://"+strings.TrimSuffix(authority, ":80")) + case "443": + prefixes = append(prefixes, "https://"+strings.TrimSuffix(authority, ":443")) + } + var conds []string + for _, p := range prefixes { + conds = append(conds, `url LIKE ? OR url LIKE ? OR url LIKE ?`) + args = append(args, p+"/%", p+"?%", p) + } + q += ` AND (` + strings.Join(conds, ` OR `) + `)` } } if contains != "" { diff --git a/traffic/traffic_store_test.go b/traffic/traffic_store_test.go index a80ba0af..a1034703 100644 --- a/traffic/traffic_store_test.go +++ b/traffic/traffic_store_test.go @@ -442,6 +442,45 @@ func TestQueryHostPortAndURLForms(t *testing.T) { } } +// TestQueryHostDefaultPort covers captures of default-port targets: clients +// leave :80 and :443 out of the Host header, so the recorded URL carries no +// port, and a search naming that port must still find them. +func TestQueryHostDefaultPort(t *testing.T) { + tr, _ := openTraffic(t) + tls := newFlow("x", "GET", "/", nil, []byte("tls")) + tls.Request.URL, _ = url.Parse("https://shop.example.com/login") + tr.record(tls) + tr.record(newFlow("shop.example.com", "GET", "/plain", nil, []byte("plain"))) + tr.record(newFlow("shop.example.com:8443", "GET", "/alt", nil, []byte("alt"))) + v6 := newFlow("x", "GET", "/", nil, []byte("v6")) + v6.Request.URL, _ = url.Parse("https://[2001:db8::1]/admin") + tr.record(v6) + + for _, tc := range []struct { + host string + want string + }{ + {host: "shop.example.com:443", want: "https://shop.example.com/login"}, + {host: "https://shop.example.com:443/login", want: "https://shop.example.com/login"}, + {host: "shop.example.com:80", want: "http://shop.example.com/plain"}, + {host: "http://shop.example.com:80/", want: "http://shop.example.com/plain"}, + {host: "shop.example.com:8443", want: "http://shop.example.com:8443/alt"}, + {host: "[2001:db8::1]:443", want: "https://[2001:db8::1]/admin"}, + } { + rows, err := tr.query(tc.host, "", "", 0, 10) + if err != nil { + t.Fatal(err) + } + if len(rows) != 1 || rows[0].URL != tc.want { + var got []string + for _, r := range rows { + got = append(got, r.URL) + } + t.Errorf("query(%q) = %v, want [%s]", tc.host, got, tc.want) + } + } +} + func TestNormalizeSearchHost(t *testing.T) { for _, tc := range []struct { raw, host, port string