From 33cc8a46edd54f6e732d023cdb5adef8ccced60f Mon Sep 17 00:00:00 2001 From: cedricve Date: Mon, 23 Nov 2026 20:06:08 +0000 Subject: [PATCH] mqtt: keep commas that are part of a single broker URI Splitting the MQTT URI on every comma broke previously valid single-broker URIs with commas in the query string, credentials or path (for example wss://broker/mqtt?token=a,b). A comma now only separates brokers when the text after it starts with a URI scheme ("scheme://"); other commas stay in the current broker URI. Adds regression cases for these URIs. Co-Authored-By: Claude Opus 5.5 --- machinery/src/routers/mqtt/main.go | 20 +++++++++++++++++++- machinery/src/routers/mqtt/main_test.go | 9 +++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/machinery/src/routers/mqtt/main.go b/machinery/src/routers/mqtt/main.go index 9fd8a27..ea139df 100644 --- a/machinery/src/routers/mqtt/main.go +++ b/machinery/src/routers/mqtt/main.go @@ -14,6 +14,7 @@ import ( "net" "net/url" "os" + "regexp" "strconv" "strings" "sync" @@ -67,6 +68,11 @@ func init() { // and a plain comma-separated list of broker URIs. Transport options after // the closing parenthesis (e.g. "?randomize=false") are ignored: paho tries // the brokers in the order they are listed, on connect and on reconnect. +// +// A comma only separates brokers when the text after it starts with a URI +// scheme ("scheme://"). Any other comma belongs to the current broker URI, +// so existing single-broker URIs with commas in their credentials, path or +// query string (e.g. "wss://broker/mqtt?token=a,b") keep working. func parseMQTTBrokers(raw string) []string { raw = strings.TrimSpace(raw) if len(raw) >= len("failover:") && strings.EqualFold(raw[:len("failover:")], "failover:") { @@ -80,8 +86,17 @@ func parseMQTTBrokers(raw string) []string { } } + segments := []string{} + for _, segment := range strings.Split(raw, ",") { + if len(segments) > 0 && !mqttBrokerSchemePattern.MatchString(strings.TrimSpace(segment)) { + segments[len(segments)-1] += "," + segment + continue + } + segments = append(segments, segment) + } + brokers := []string{} - for _, broker := range strings.Split(raw, ",") { + for _, broker := range segments { if broker = strings.TrimSpace(broker); broker != "" { brokers = append(brokers, broker) } @@ -89,6 +104,9 @@ func parseMQTTBrokers(raw string) []string { return brokers } +// mqttBrokerSchemePattern matches the start of a URI with a scheme (RFC 3986). +var mqttBrokerSchemePattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9+.-]*://`) + // enableMQTTConnectionDiagnostics installs our instrumented TCP/TLS dialer. // The custom dialer is used for every broker, so it is only installed when // all brokers use a TCP-based scheme (WebSocket brokers keep paho's dialer). diff --git a/machinery/src/routers/mqtt/main_test.go b/machinery/src/routers/mqtt/main_test.go index e13d129..8eb2f57 100644 --- a/machinery/src/routers/mqtt/main_test.go +++ b/machinery/src/routers/mqtt/main_test.go @@ -230,6 +230,15 @@ func TestParseMQTTBrokers(t *testing.T) { }, {name: "failover without parentheses", raw: "failover:tcp://b-1.example:1883", want: []string{"tcp://b-1.example:1883"}}, {name: "comma separated", raw: "tcp://b-1.example:1883,tcp://b-2.example:1883", want: []string{"tcp://b-1.example:1883", "tcp://b-2.example:1883"}}, + {name: "comma in query string", raw: "wss://broker.example/mqtt?token=a,b", want: []string{"wss://broker.example/mqtt?token=a,b"}}, + {name: "comma in password", raw: "mqtt+ssl://user:p,ss@broker.example:8883", want: []string{"mqtt+ssl://user:p,ss@broker.example:8883"}}, + {name: "comma in path", raw: "wss://broker.example/a,b/mqtt", want: []string{"wss://broker.example/a,b/mqtt"}}, + {name: "comma followed by text that looks like a scheme without slashes", raw: "wss://broker.example/mqtt?list=a,b:c", want: []string{"wss://broker.example/mqtt?list=a,b:c"}}, + { + name: "failover with commas inside a broker URI", + raw: "failover:(wss://b-1.example/mqtt?token=a,b,wss://b-2.example/mqtt?token=c,d)", + want: []string{"wss://b-1.example/mqtt?token=a,b", "wss://b-2.example/mqtt?token=c,d"}, + }, } for _, test := range tests {