From b8c80cb9dde2790e833ceb8bea43bcd18cccbd42 Mon Sep 17 00:00:00 2001 From: Chaitanya Deshpande Date: Thu, 26 Nov 2026 17:06:22 +0530 Subject: [PATCH 1/2] 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 From d3b2dc7eb057fa74dc19e2cb266d136ab3bc4a1c Mon Sep 17 00:00:00 2001 From: Chaitanya Deshpande Date: Thu, 26 Nov 2026 17:14:38 +0530 Subject: [PATCH 2/2] fix(traffic): match default-port WebSocket captures in traffic_search A WebSocket over a default port is recorded as ws://host or wss://host, so host:80 and host:443 searches still missed it. Also match the ws and wss portless forms. --- traffic/traffic.go | 8 +++++--- traffic/traffic_store_test.go | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/traffic/traffic.go b/traffic/traffic.go index e7c7f4fb..73a9cea6 100644 --- a/traffic/traffic.go +++ b/traffic/traffic.go @@ -2421,12 +2421,14 @@ func (t *Traffic) query(host, contains, bodyContains string, page, limit int) ([ authority := net.JoinHostPort(hostName, port) 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. + // of https://host:443 is recorded as https://host, and a WebSocket + // over it as wss://host. + bare := strings.TrimSuffix(authority, ":"+port) switch port { case "80": - prefixes = append(prefixes, "http://"+strings.TrimSuffix(authority, ":80")) + prefixes = append(prefixes, "http://"+bare, "ws://"+bare) case "443": - prefixes = append(prefixes, "https://"+strings.TrimSuffix(authority, ":443")) + prefixes = append(prefixes, "https://"+bare, "wss://"+bare) } var conds []string for _, p := range prefixes { diff --git a/traffic/traffic_store_test.go b/traffic/traffic_store_test.go index a1034703..81dad35e 100644 --- a/traffic/traffic_store_test.go +++ b/traffic/traffic_store_test.go @@ -455,6 +455,11 @@ func TestQueryHostDefaultPort(t *testing.T) { v6 := newFlow("x", "GET", "/", nil, []byte("v6")) v6.Request.URL, _ = url.Parse("https://[2001:db8::1]/admin") tr.record(v6) + ws := newFlow("x", "GET", "/", nil, nil) + ws.Request.URL, _ = url.Parse("https://chat.example.com/socket") + if _, err := tr.recordStreamStart(flowRequestView(ws.Request), responseView{}, "websocket", nil); err != nil { + t.Fatal(err) + } for _, tc := range []struct { host string @@ -466,6 +471,7 @@ func TestQueryHostDefaultPort(t *testing.T) { {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"}, + {host: "chat.example.com:443", want: "wss://chat.example.com/socket"}, } { rows, err := tr.query(tc.host, "", "", 0, 10) if err != nil {