Repository: minio/minio (language: go)

Issue:
Panic in dns_path.go
Community report here: https://minio.slack.com/archives/C3NDUB8UA/p1730712383739299

```
minio cluster 6 nodes MINIO_STORAGE_CLASS_STANDARD="EC:2" Due to a power outage, 6 servers were shut down at the same time. Now after startup, only 3 node minio can be started. If forced to start the fourth node. The first three nodes will bring down one node. The error log is as follows:Nov  4 17:22:43 prod-es-120 run.sh[1611136]: ---------------------------
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: MinIO Object Storage Server
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: Copyright: 2015-2024 MinIO, Inc.
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: License: GNU AGPLv3 - https://www.gnu.org/licenses/agpl-3.0.html
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: Version: RELEASE.2024-10-02T17-50-41Z (go1.22.8 linux/amd64)
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: API: http://10.1.16.120:9000/
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: WebUI: https://zbcg-oss.hbcoal.com/minio/ui
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: Docs: https://docs.min.io/
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: ---------------------------
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: panic: runtime error: slice bounds out of range [1:0]
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: goroutine 5590 [running]:
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: [github.com/minio/minio/internal/config/dns.msgUnPath({0xc0036e02be](http://github.com/minio/minio/internal/config/dns.msgUnPath(%7B0xc0036e02be)?, 0xc0036e0180?})
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: #[011github.com/minio/minio/internal/config/dns/dns_path.go:57](http://011github.com/minio/minio/internal/config/dns/dns_path.go:57) +0x10f
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: [github.com/minio/minio/internal/config/dns.(*CoreDNS).list(0xc001343d60](http://github.com/minio/minio/internal/config/dns.(*CoreDNS).list(0xc001343d60), {0xc00e97cf20, 0x1e}, 0x1)
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: #[011github.com/minio/minio/internal/config/dns/etcd_dns.go:144](http://011github.com/minio/minio/internal/config/dns/etcd_dns.go:144) +0x57a
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: [github.com/minio/minio/internal/config/dns.(*CoreDNS).List(0xc001343d60)](http://github.com/minio/minio/internal/config/dns.(*CoreDNS).List(0xc001343d60))
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: #[011github.com/minio/minio/internal/config/dns/etcd_dns.go:63](http://011github.com/minio/minio/internal/config/dns/etcd_dns.go:63) +0x10f
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: [github.com/minio/minio/cmd.initFederatorBackend({0xc001210040](http://github.com/minio/minio/cmd.initFederatorBackend(%7B0xc001210040), 0x2, 0x4?}, {0x4f1edb0?, 0xc0012bf558?})
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: #[011github.com/minio/minio/cmd/bucket-handlers.go:102](http://011github.com/minio/minio/cmd/bucket-handlers.go:102) +0x52
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: created by [github.com/minio/minio/cmd.serverMain.func15.12](http://github.com/minio/minio/cmd.serverMain.func15.12) in goroutine 5084
Nov  4 17:22:43 prod-es-120 run.sh[1611136]: #[011github.com/minio/minio/cmd/server-main.go:1100](http://011github.com/minio/minio/cmd/server-main.go:1100) +0xae (edited)
```

`RELEASE.2024-10-02T17-50-41Z` is not the most recent version but I looked at the git history and release notes and guessed that it's probably not fixed in most recent version?




Discussion / hints:
@marktheunissen please ask the user to start the server settings i.e /etc/default/minio or whatever how they deployed. 

all the MINIO_ env vars 
> Nov  4 17:22:43 prod-es-120 run.sh[1611136]: [github.com/minio/minio/internal/config/dns.msgUnPath({0xc0036e02be](http://github.com/minio/minio/internal/config/dns.msgUnPath(%7B0xc0036e02be)?, 0xc0036e0180?})

this code is only used in one place which is when you have etcd_dns configured we do not really support this model of federated deployments anymore. 

it looks like we are getting some results from etcd which are not exactly what we expect and its failing. 
```
#!/bin/sh
export INTERNAL_IP=10.1.16.117
export MINIO_UPDATE="on"
export MINIO_ACCESS_KEY="admin"
export MINIO_SECRET_KEY=""
export MINIO_REGION="yzc"
export MINIO_BROWSER="on"
export MINIO_ETCD_ENDPOINTS="http://10.1.16.117:2379/,http://10.1.16.118:2379/,http://10.1.16.119:2379/"
export MINIO_ETCD_PATH_PREFIX="minio/"
export MINIO_ETCD_COREDNS_PATH="/miniodns"
export MINIO_DOMAIN="[zbcg-oss.hbcoal.com](http://zbcg-oss.hbcoal.com/)"
export MINIO_BROWSER_REDIRECT_URL="https://zbcg-oss.hbcoal.com/minio/ui"
export MINIO_STORAGE_CLASS_STANDARD="EC:2"
export MINIO_CI_CD="true"
export CI="true"
/usr/local/bin/minio server --config-dir /etc/minio \
--address "10.1.16.117:9000" \
--console-address "10.1.16.117:19000" \
http://10.1.16.117/data/minio/data \
http://10.1.16.118/data/minio/data \
http://10.1.16.119/data/minio/data \
http://10.1.16.120/data/minio/data \
http://10.1.16.121/data/minio/data \
http://10.1.16.122/data/minio/data
```
etcd:
```
ExecStart=/usr/bin/etcd \
  --name prod-midtool-117 \
  --peer-client-cert-auth \
  --client-cert-auth \
  --initial-advertise-peer-urls http://10.1.16.117:2380/ \
  --listen-peer-urls http://10.1.16.117:2380/ \
  --listen-client-urls http://10.1.16.117:2379/,http://127.0.0.1:2379/ \
  --advertise-client-urls http://10.1.16.117:2379/ \
  --initial-cluster-token etcd-cluster-0 \
  --initial-cluster prod-midtool-117=http://prod-midtool-117:2380/,prod-midtool-118=http://prod-midtool-118:2380/,prod-midtool-119=http://prod-midtool-119:2380/ \
  --initial-cluster-state new \
  --data-dir=/data/etcd/
```
The MINIO_DOMAIN value looks wrong
@jiuker this looks like hbcoal.com - can you try to reach out to them. They should ideally become a customer here. It's a production deployment.
> The MINIO_DOMAIN value looks wrong

Looks like it's been auto-formatted as Markdown, probably because it was copy-pasted through Slack.
We can add protections against nefarious data but there is not much we can do here though.

It looks like the etcd store has been messed around with and it's sending us some incorrect data at the /miniodns path

Perhaps something to do with domain and number of domain fields we expect.

We hardly support this anymore but it's worth to add protections around to avoid a potential random crash.

Fix patch:
diff --git a/internal/config/dns/dns_path.go b/internal/config/dns/dns_path.go
index 94b238045d92f..c648aed2d7ffa 100644
--- a/internal/config/dns/dns_path.go
+++ b/internal/config/dns/dns_path.go
@@ -37,6 +37,9 @@ func msgPath(s, prefix string) string {
 // dnsJoin joins labels to form a fully qualified domain name. If the last label is
 // the root label it is ignored. Not other syntax checks are performed.
 func dnsJoin(labels ...string) string {
+	if len(labels) == 0 {
+		return ""
+	}
 	ll := len(labels)
 	if labels[ll-1] == "." {
 		return strings.Join(labels[:ll-1], ".") + "."
@@ -50,6 +53,9 @@ func msgUnPath(s string) string {
 	if l[len(l)-1] == "" {
 		l = l[:len(l)-1]
 	}
+	if len(l) < 2 {
+		return s
+	}
 	// start with 1, to strip /skydns
 	for i, j := 1, len(l)-1; i < j; i, j = i+1, j-1 {
 		l[i], l[j] = l[j], l[i]


Test patch: