diff --git a/traffic/traffic.go b/traffic/traffic.go index 0add6303..a44050ef 100644 --- a/traffic/traffic.go +++ b/traffic/traffic.go @@ -2446,12 +2446,23 @@ 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, and a WebSocket + // over it as wss://host. + bare := strings.TrimSuffix(authority, ":"+port) + switch port { + case "80": + prefixes = append(prefixes, "http://"+bare, "ws://"+bare) + case "443": + prefixes = append(prefixes, "https://"+bare, "wss://"+bare) + } + 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..81dad35e 100644 --- a/traffic/traffic_store_test.go +++ b/traffic/traffic_store_test.go @@ -442,6 +442,51 @@ 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) + 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 + 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"}, + {host: "chat.example.com:443", want: "wss://chat.example.com/socket"}, + } { + 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