-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check that requests to docker endpoint are against rdns-valid hostname (
- Loading branch information
Ian Whitlock
authored
Apr 12, 2018
1 parent
68a3060
commit e6e9ada
Showing
6 changed files
with
267 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2018 VMware, Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"golang.org/x/net/context" | ||
|
||
vicdns "github.com/vmware/vic/lib/dns" | ||
) | ||
|
||
// HostCheckMiddleware provides middleware for Host header correctness enforcement | ||
type HostCheckMiddleware struct { | ||
ValidDomains vicdns.SetOfDomains | ||
} | ||
|
||
// validateHostname trims the port off the Host header in an HTTP request and returns either the bare IP (v4 or v6) or the FQDN with the port truncated. Returns non-nil error if Host field doesn't make sense. | ||
func validateHostname(r *http.Request) (hostname string, err error) { | ||
if r.Host == "" { | ||
// this really shouldn't be necessary https://tools.ietf.org/html/rfc2616#section-14.23 | ||
// you can delete this if stanza if you're braver than me. | ||
return "", fmt.Errorf("empty host header from %s", r.RemoteAddr) | ||
} | ||
|
||
if r.Host[len(r.Host)-1] == ']' { | ||
// ipv6 w/o specified port | ||
return r.Host, nil | ||
} | ||
|
||
// trim port if it's there. r.Host should never contain a scheme | ||
hostnameSplit := strings.Split(r.Host, ":") | ||
|
||
if len(hostnameSplit) <= 2 { | ||
// ipv4 or dns hostname with or without port, first element is hostname | ||
return hostnameSplit[0], nil | ||
} | ||
|
||
// if we see >2 colons in the hostname, it's an ipv6 address w/ port | ||
// unfortunately that means we have to recombine the rest.. | ||
return fmt.Sprintf("%s", strings.Join(hostnameSplit[:len(hostnameSplit)-1], ":")), nil | ||
} | ||
|
||
// WrapHandler satisfies the Docker middleware interface for HostCheckMiddleware to reject http requests that do not specify a known DNS name for this endpoint in the Host: field of the request | ||
func (h HostCheckMiddleware) WrapHandler(f func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error) func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { | ||
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) (err error) { | ||
var hostname string | ||
if hostname, err = validateHostname(r); err != nil { | ||
return err | ||
} | ||
|
||
if h.ValidDomains[hostname] { | ||
return f(ctx, w, r, vars) | ||
} | ||
|
||
return fmt.Errorf("invalid host header from %s to requested host %s", r.RemoteAddr, r.Host) | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
lib/apiservers/engine/backends/middleware/middleware_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2018 VMware, Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidateHostname(t *testing.T) { | ||
r := &http.Request{} | ||
hostname, err := validateHostname(r) | ||
assert.Error(t, err) | ||
assert.EqualValues(t, "", hostname) | ||
|
||
r.Host = "" | ||
hostname, err = validateHostname(r) | ||
assert.Error(t, err) | ||
assert.EqualValues(t, "", hostname) | ||
|
||
r.Host = "localname" | ||
hostname, err = validateHostname(r) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, "localname", hostname) | ||
|
||
r.Host = "localname:4567" | ||
hostname, err = validateHostname(r) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, "localname", hostname) | ||
|
||
r.Host = "[2605:a601:1119:6800:c69b:b2ec:eefa:ef4b]" | ||
hostname, err = validateHostname(r) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, "[2605:a601:1119:6800:c69b:b2ec:eefa:ef4b]", hostname) | ||
|
||
r.Host = "[2605:a601:1119:6800:c69b:b2ec:eefa:ef4b]:8080" | ||
hostname, err = validateHostname(r) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, "[2605:a601:1119:6800:c69b:b2ec:eefa:ef4b]", hostname) | ||
|
||
r.Host = "127.0.0.1:8080" | ||
hostname, err = validateHostname(r) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, "127.0.0.1", hostname) | ||
|
||
r.Host = "127.0.0.1" | ||
hostname, err = validateHostname(r) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, "127.0.0.1", hostname) | ||
|
||
r.Host = "foo.com:80" | ||
hostname, err = validateHostname(r) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, "foo.com", hostname) | ||
|
||
r.Host = "foo.com" | ||
hostname, err = validateHostname(r) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, "foo.com", hostname) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters