Repository: coredns/coredns (go)

Issue:
plugin/forward: crash if using https://
<!-- Please use this template while reporting a bug and provide as much info as possible. Not doing so may result in your bug not being addressed in a timely manner. Thanks!

If the matter is security related, please disclose it privately via security@coredns.io
-->

**What happened**:

```
jbelamaric@jbelamaric:~/proj/gh/coredns/coredns$ ./coredns -conf Corefile.client.doh
[WARNING] plugin/forward: No healthchecker for transport "https"
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0xc56bf7]

goroutine 1 [running]:
github.com/coredns/coredns/plugin/forward.parseStanza(0xc0000ed320, 0x203000, 0x203000, 0x203000)
	/usr/local/google/home/jbelamaric/proj/gh/coredns/coredns/plugin/forward/setup.go:124 +0x3d7
github.com/coredns/coredns/plugin/forward.parseForward(0xc0000ed320, 0x41327b, 0xc00064f8b8, 0x89f6bc6c)
	/usr/local/google/home/jbelamaric/proj/gh/coredns/coredns/plugin/forward/setup.go:75 +0x6d
github.com/coredns/coredns/plugin/forward.setup(0xc0000ed320, 0x3, 0x1beaebd)
	/usr/local/google/home/jbelamaric/proj/gh/coredns/coredns/plugin/forward/setup.go:23 +0x40
github.com/caddyserver/caddy.executeDirectives(0xc0001a6100, 0x7ffc08f41ed6, 0x13, 0x2d824c0, 0x2d, 0x2d, 0xc000587000, 0x1, 0x1, 0x0, ...)
	/usr/local/google/home/jbelamaric/go/pkg/mod/github.com/caddyserver/caddy@v1.0.5/caddy.go:665 +0x69e
github.com/caddyserver/caddy.ValidateAndExecuteDirectives(0x1fbd5e0, 0xc0001d60c0, 0xc0001a6100, 0x0, 0x1, 0xc00020a0a8)
	/usr/local/google/home/jbelamaric/go/pkg/mod/github.com/caddyserver/caddy@v1.0.5/caddy.go:621 +0x54f
github.com/caddyserver/caddy.startWithListenerFds(0x1fbd5e0, 0xc0001d60c0, 0xc0001a6100, 0x0, 0x0, 0x0)
	/usr/local/google/home/jbelamaric/go/pkg/mod/github.com/caddyserver/caddy@v1.0.5/caddy.go:516 +0x136
github.com/caddyserver/caddy.Start(0x1fbd5e0, 0xc0001d60c0, 0x1fbd5e0, 0xc0001d60c0, 0x0)
	/usr/local/google/home/jbelamaric/go/pkg/mod/github.com/caddyserver/caddy@v1.0.5/caddy.go:473 +0xea
github.com/coredns/coredns/coremain.Run()
	/usr/local/google/home/jbelamaric/proj/gh/coredns/coredns/coremain/run.go:79 +0x27c
main.main()
	/usr/local/google/home/jbelamaric/proj/gh/coredns/coredns/coredns.go:12 +0x20
```

**What you expected to happen**:
Error message that `https://` is not supported as a destination protocol in `forward`.

**How to reproduce it (as minimally and precisely as possible)**:
See above plus Corefile:

```
foo.com:5301 {
  forward foo.com https://127.0.0.1:5300
}
```

**Anything else we need to know?**:
Nope. Should be a quick fix but don't have time to do it right now.

**Environment**:

- the version of CoreDNS: master branch build: CoreDNS-1.6.9, linux/amd64, go1.14, 2a0862a5
- Corefile: see above
- logs, if applicable:
- OS (e.g: `cat /etc/os-release`):
- Others:


Pull request description:
plugin/forward: crash if using https
Signed-off-by: kadern0 <kerno@gmail.com>

<!--
Thank you for contributing to CoreDNS!
Please provide the following information to help us make the most of your pull request:
-->

### 1. Why is this pull request needed and what does it do?

Verifies 'https' is not being used as destination protocol within the forward plugin.

### 2. Which issues (if any) are related?

Fixes #3816 

### 3. Which documentation changes (if any) need to be made?
N/A
### 4. Does this introduce a backward incompatible change or deprecation?
N/A

Patch:
diff --git a/plugin/forward/setup.go b/plugin/forward/setup.go
index 8070369c9..b5eeab912 100644
--- a/plugin/forward/setup.go
+++ b/plugin/forward/setup.go
@@ -99,8 +99,13 @@ func parseStanza(c *caddy.Controller) (*Forward, error) {
 	}
 
 	transports := make([]string, len(toHosts))
+	allowedTrans := map[string]bool{"dns": true, "tls": true}
 	for i, host := range toHosts {
 		trans, h := parse.Transport(host)
+
+		if !allowedTrans[trans] {
+			return f, fmt.Errorf("'%s' is not supported as a destination protocol in forward: %s", trans, host)
+		}
 		p := NewProxy(h, trans)
 		f.proxies = append(f.proxies, p)
 		transports[i] = trans


Test patch:
diff --git a/plugin/forward/setup_test.go b/plugin/forward/setup_test.go
index 0949e0935..6864136e4 100644
--- a/plugin/forward/setup_test.go
+++ b/plugin/forward/setup_test.go
@@ -37,6 +37,7 @@ func TestSetup(t *testing.T) {
 		{"forward . 127.0.0.1 {\nblaatl\n}\n", true, "", nil, 0, options{hcRecursionDesired: true}, "unknown property"},
 		{`forward . ::1
 		forward com ::2`, true, "", nil, 0, options{hcRecursionDesired: true}, "plugin"},
+		{"forward . https://127.0.0.1 \n", true, ".", nil, 2, options{hcRecursionDesired: true}, "'https' is not supported as a destination protocol in forward: https://127.0.0.1"},
 	}
 
 	for i, test := range tests {