Repository navigation
router, sdk/js: a browser reaches a router by IP over WebTransport - #920
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds WebTransport support to the router and JS SDK, enabling browser clients to connect over WebTransport in addition to WebSockets. It updates the router to share its QUIC socket with WebTransport, resolve its own WebTransport addresses, and advertise them. However, there are critical compile-time errors in internal/router/router.go where multiaddr.Multiaddr and multiaddr.Component are incorrectly treated as slices or having non-existent methods. These must be resolved using the proper Protocols() and Protocol().Code APIs as suggested.
| for _, addr := range external { | ||
| if addr[len(addr)-1].Code() != multiaddr.P_QUIC_V1 || seen[addr.String()] { | ||
| continue | ||
| } | ||
| seen[addr.String()] = true | ||
| if suffix := webTransportSuffixFor(suffixes, addr[0].Code()); suffix != nil { | ||
| addrs = append(addrs, addr.Encapsulate(suffix)) | ||
| } | ||
| } |
There was a problem hiding this comment.
The multiaddr.Multiaddr type is an interface, not a slice, so indexing it directly (e.g., addr[len(addr)-1] or addr[0]) and calling len(addr) will result in compile-time errors. Instead, use addr.Protocols() to inspect the protocols of the multiaddr.
| for _, addr := range external { | |
| if addr[len(addr)-1].Code() != multiaddr.P_QUIC_V1 || seen[addr.String()] { | |
| continue | |
| } | |
| seen[addr.String()] = true | |
| if suffix := webTransportSuffixFor(suffixes, addr[0].Code()); suffix != nil { | |
| addrs = append(addrs, addr.Encapsulate(suffix)) | |
| } | |
| } | |
| for _, addr := range external { | |
| protocols := addr.Protocols() | |
| if len(protocols) == 0 || protocols[len(protocols)-1].Code != multiaddr.P_QUIC_V1 || seen[addr.String()] { | |
| continue | |
| } | |
| seen[addr.String()] = true | |
| if suffix := webTransportSuffixFor(suffixes, protocols[0].Code); suffix != nil { | |
| addrs = append(addrs, addr.Encapsulate(suffix)) | |
| } | |
| } |
| func (r *Router) webTransportSuffixesByIPVersion() map[int]multiaddr.Multiaddr { | ||
| listen := r.Host.Network().ListenAddresses() | ||
| suffixes := map[int]multiaddr.Multiaddr{} | ||
| for _, addr := range listen { | ||
| // Cut at /webtransport: the QUIC address before it, and the rest with the certhashes. | ||
| quic, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Code() == multiaddr.P_WEBTRANSPORT }) | ||
| if len(suffix) == 0 || !multiaddr.Contains(listen, quic) { | ||
| continue | ||
| } | ||
| suffixes[quic[0].Code()] = suffix | ||
| } | ||
| return suffixes | ||
| } |
There was a problem hiding this comment.
The multiaddr.Component interface does not have a Code() method; use c.Protocol().Code instead. Additionally, quic is a multiaddr.Multiaddr interface, not a slice, so quic[0] is invalid. Use quic.Protocols() to access the protocol codes.
| func (r *Router) webTransportSuffixesByIPVersion() map[int]multiaddr.Multiaddr { | |
| listen := r.Host.Network().ListenAddresses() | |
| suffixes := map[int]multiaddr.Multiaddr{} | |
| for _, addr := range listen { | |
| // Cut at /webtransport: the QUIC address before it, and the rest with the certhashes. | |
| quic, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Code() == multiaddr.P_WEBTRANSPORT }) | |
| if len(suffix) == 0 || !multiaddr.Contains(listen, quic) { | |
| continue | |
| } | |
| suffixes[quic[0].Code()] = suffix | |
| } | |
| return suffixes | |
| } | |
| func (r *Router) webTransportSuffixesByIPVersion() map[int]multiaddr.Multiaddr { | |
| listen := r.Host.Network().ListenAddresses() | |
| suffixes := map[int]multiaddr.Multiaddr{} | |
| for _, addr := range listen { | |
| // Cut at /webtransport: the QUIC address before it, and the rest with the certhashes. | |
| quic, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Protocol().Code == multiaddr.P_WEBTRANSPORT }) | |
| if len(suffix) == 0 || !multiaddr.Contains(listen, quic) { | |
| continue | |
| } | |
| quicProtos := quic.Protocols() | |
| if len(quicProtos) > 0 { | |
| suffixes[quicProtos[0].Code] = suffix | |
| } | |
| } | |
| return suffixes | |
| } |
| for _, s := range r.config.ExternalAddrs { | ||
| ma, err := multiaddr.NewMultiaddr(s) | ||
| if err != nil || ma[0].Code() != multiaddr.P_DNSADDR { | ||
| continue | ||
| } |
There was a problem hiding this comment.
The multiaddr.Multiaddr interface does not support indexing, so ma[0] will cause a compile-time error. Use ma.Protocols() to check the first protocol code.
| for _, s := range r.config.ExternalAddrs { | |
| ma, err := multiaddr.NewMultiaddr(s) | |
| if err != nil || ma[0].Code() != multiaddr.P_DNSADDR { | |
| continue | |
| } | |
| for _, s := range r.config.ExternalAddrs { | |
| ma, err := multiaddr.NewMultiaddr(s) | |
| if err != nil { | |
| continue | |
| } | |
| protocols := ma.Protocols() | |
| if len(protocols) == 0 || protocols[0].Code != multiaddr.P_DNSADDR { | |
| continue | |
| } |
2fa260f to
71488af
Compare
| for _, addr := range listen { | ||
| // Cut at /webtransport: the QUIC address before it, and the rest with the certhashes. | ||
| _, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Code() == multiaddr.P_WEBTRANSPORT }) | ||
| ipVersion := addr[0].Code() |
There was a problem hiding this comment.
are these indexes safe or should we check that value exist?
|
/gemini review , are all indexes [0] safe or should we check |
There was a problem hiding this comment.
Code Review
This pull request introduces WebTransport support to allow browser clients to connect to routers directly by IP, sharing the QUIC port and validating self-signed certificates via certhashes. It updates the router's address resolution logic, Helm chart documentation, JS SDK, and integration tests to support WebTransport. The code review feedback suggests adding defensive checks to ensure multiaddr.Multiaddr instances are not empty before indexing them in internal/router/address.go, preventing potential out-of-bounds panics.
| for _, addr := range external { | ||
| transport := addr[len(addr)-1].Code() | ||
| if transport != multiaddr.P_QUIC_V1 { | ||
| continue | ||
| } | ||
| // ip4, ip6, dns4, dns6 or dns. | ||
| hostProtocol := addr[0].Code() |
There was a problem hiding this comment.
To prevent potential out-of-bounds panics when processing resolved or configured multiaddrs, we should defensively check that the multiaddr is not empty (len(addr) > 0) before indexing it. This is especially important for correctness and defensive programming when handling dynamic inputs like DNS-resolved addresses.
for _, addr := range external {
if len(addr) == 0 {
continue
}
transport := addr[len(addr)-1].Code()
if transport != multiaddr.P_QUIC_V1 {
continue
}
// ip4, ip6, dns4, dns6 or dns.
hostProtocol := addr[0].Code()| for _, addr := range listen { | ||
| // Cut at /webtransport: the QUIC address before it, and the rest with the certhashes. | ||
| _, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Code() == multiaddr.P_WEBTRANSPORT }) | ||
| ipVersion := addr[0].Code() |
There was a problem hiding this comment.
Defensively check that the listen multiaddr is not empty (len(addr) > 0) before indexing it to avoid potential out-of-bounds panics.
| for _, addr := range listen { | |
| // Cut at /webtransport: the QUIC address before it, and the rest with the certhashes. | |
| _, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Code() == multiaddr.P_WEBTRANSPORT }) | |
| ipVersion := addr[0].Code() | |
| for _, addr := range listen { | |
| if len(addr) == 0 { | |
| continue | |
| } | |
| // Cut at /webtransport: the QUIC address before it, and the rest with the certhashes. | |
| _, suffix := multiaddr.SplitFunc(addr, func(c multiaddr.Component) bool { return c.Code() == multiaddr.P_WEBTRANSPORT }) | |
| ipVersion := addr[0].Code() |
| for _, ma := range r.externalAddrs() { | ||
| if hostProtocol := ma[0].Code(); hostProtocol != multiaddr.P_DNSADDR { | ||
| continue | ||
| } |
There was a problem hiding this comment.
Defensively check that the external multiaddr is not empty (len(ma) > 0) before indexing it to avoid potential out-of-bounds panics.
| for _, ma := range r.externalAddrs() { | |
| if hostProtocol := ma[0].Code(); hostProtocol != multiaddr.P_DNSADDR { | |
| continue | |
| } | |
| for _, ma := range r.externalAddrs() { | |
| if len(ma) == 0 { | |
| continue | |
| } | |
| if hostProtocol := ma[0].Code(); hostProtocol != multiaddr.P_DNSADDR { | |
| continue | |
| } |
| for _, addr := range external { | ||
| transport := addr[len(addr)-1].Code() | ||
| hostProtocol := addr[0].Code() |
There was a problem hiding this comment.
Defensively check that the external multiaddr is not empty (len(addr) > 0) before indexing it to avoid potential out-of-bounds panics.
| for _, addr := range external { | |
| transport := addr[len(addr)-1].Code() | |
| hostProtocol := addr[0].Code() | |
| for _, addr := range external { | |
| if len(addr) == 0 { | |
| continue | |
| } | |
| transport := addr[len(addr)-1].Code() | |
| hostProtocol := addr[0].Code() |
A browser could not join a mesh whose routers have no hostname and CA certificate. A page reaches a router over WebSocket or WebTransport only, and a router with --external-addr leased the addresses the operator gave, which cannot include a WebTransport address: its certhash changes with the certificate. The router now leases a /webtransport address for each of its external QUIC addresses. It appends the "/webtransport/certhash/..." end of its own WebTransport listen address to the QUIC address, when it listens for WebTransport on the IP version the address reaches. An external QUIC address is one given in --external-addr, by IP or by name, or one found under the router's peer ID in a /dnsaddr --external-addr. The router looks the /dnsaddr up before each lease, with a 5-second limit. A failed lookup keeps the last result and warns once, then counts at debug until DNS answers again. A record that is gone withdraws the address. The router warns about an external QUIC address that gets no WebTransport address because of its IP version. A router that does not listen on WebTransport makes no lookup and leases what it leased before. The address code moves from router.go to address.go. The chart README says what a chart operator adds to let a browser in. The testnet routers, on Kubernetes and on the GCE VM, listen on /udp/4501/quic-v1/webtransport, which shares the QUIC socket. The browser build of the JS SDK dials WebTransport beside WebSocket. The tests pin: the derived address for an external QUIC address by IP, by name and from a /dnsaddr; none for TCP, for an IP version without a WebTransport listener, and without a WebTransport listener; a failed lookup keeps the address and is counted until DNS answers again, a lookup with no answer ends at the limit, a changed record replaces it, a removed record withdraws it; no DNS lookup unless a /dnsaddr and a WebTransport listener are both present; an empty address is skipped and does not stop the router; and a lease renewal sends an address that a second host dials. A browser test (tests/ui/browser-sdk.spec.js) starts a router with QUIC and WebTransport on one port and no WebSocket listener: a Chromium page joins, holds an open connection on the leased WebTransport address and passes the mesh handshake with the router.
71488af to
2362700
Compare
A browser can now join the testnets. A page reaches a router only over WebSocket or WebTransport, and the testnet routers offered neither: they listen on TCP and QUIC and lease only their
--external-addr/dnsaddr, whose TXT records hold TCP and QUIC addresses.wsswould need a hostname and a CA certificate per router.WebTransport needs neither. The browser checks the router's self-signed certificate against the
certhashin the router's address, so it dials the router by IP.What changes
--external-addr, the router also leases a/webtransportaddress for each of its external QUIC addresses. It appends the/webtransport/certhash/…end of its own WebTransport listen address to the QUIC address. The certhash changes with the certificate about every two weeks, so no operator can configure the address. The relay circuit addresses the router hands to members follow the lease, so they carry this address too.--external-addr, by IP or by name, or one found under the router's peer ID in a/dnsaddr--external-addr. The router looks up the/dnsaddrbefore each lease, with a 5-second limit. A failed lookup keeps the last result, so a DNS outage does not withdraw a leased address./udp/4501/quic-v1/webtransport. It shares the QUIC socket, so no new port or firewall rule is needed.@libp2p/webtransport).Nothing changes for a router that does not listen on WebTransport: it makes no DNS lookup and leases the same addresses as before. The router's default
--listenis TCP only. A router without--external-addrleases all its host addresses as before, and those include its WebTransport listen addresses.Testing
TestRouterLeasesWebTransportForItsExternalQUICAddrschecks the leased WebTransport addresses for an external QUIC address by public IP, by private IP and by name, for the router's own entry in a/dnsaddr, and for the same address given twice. It also checks that the router leases none for a/dnsaddrwithout a record, for TCP-only addresses, for an IPv6 address with an IPv4 listener, and without a WebTransport listener.TestRouterCircuitAddrsFollowTheExternalAddrschecks that the circuit addresses carry the WebTransport address.agentmesh-one) and oneagentmesh-routerthat listens on QUIC and WebTransport on one UDP port, with--external-addr /ip4/<LAN IP>/udp/<port>/quic-v1and no other member. The router log and/infolisted/ip4/<LAN IP>/udp/<port>/quic-v1/webtransport/certhash/…. A headless Chromium 158 page running the JS SDK browser example joined, held an open WebTransport connection to that router and passed the mesh handshake./dnsaddrpath is covered by the unit test with a mock resolver only. The JS transport change has no automated test.Notes
/mesh/auth, so unauthenticated peers would count as observers.--control-plane-sync-interval./dnsaddrrecord.@libp2p/webtransportdepends on@libp2p/noise, the same project as@chainsafe/libp2p-noiseunder its new name, so the browser bundle carries both.https:inconnect-src: WebTransport dialshttps://<ip>:<port>.