mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-30 17:53:21 -04:00 
			
		
		
		
	plugin/loop: avoid panic on invalid server block (#7568)
Ignore invalid ServerBlockKeys in loop plugin that fail normalization. Retain the default “.” zone instead of indexing into an empty slice. This prevents an index-out-of-range panic triggered by malformed inputs such as "unix://". Added tests to validate and increase test coverage. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
This commit is contained in:
		| @@ -17,3 +17,32 @@ func TestSetup(t *testing.T) { | ||||
| 		t.Fatal("Expected errors, but got none") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestParseServerBlockKeys(t *testing.T) { | ||||
| 	tests := []struct { | ||||
| 		name   string | ||||
| 		key    string | ||||
| 		want   string | ||||
| 		wantOk bool | ||||
| 	}{ | ||||
| 		{name: "valid domain", key: "example.org", want: "example.org.", wantOk: true}, | ||||
| 		{name: "invalid scheme", key: "unix://", want: ".", wantOk: true}, | ||||
| 		{name: "empty", key: "", want: ".", wantOk: true}, | ||||
| 	} | ||||
|  | ||||
| 	for _, tt := range tests { | ||||
| 		t.Run(tt.name, func(t *testing.T) { | ||||
| 			c := caddy.NewTestController("dns", `loop`) | ||||
| 			if tt.key != "" { | ||||
| 				c.ServerBlockKeys = []string{tt.key} | ||||
| 			} | ||||
| 			l, err := parse(c) | ||||
| 			if (err == nil) != tt.wantOk { | ||||
| 				t.Fatalf("parse err=%v, wantOk=%v", err, tt.wantOk) | ||||
| 			} | ||||
| 			if l.zone != tt.want { | ||||
| 				t.Fatalf("zone=%q, want %q", l.zone, tt.want) | ||||
| 			} | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user