Skip to content

Commit

Permalink
Add port validation for configured server hosts #59
Browse files Browse the repository at this point in the history
Fixup comments
  • Loading branch information
Alex Schneider authored and Marcel Ludwig committed Nov 9, 2020
1 parent ddcb015 commit 3cd26b1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
19 changes: 12 additions & 7 deletions config/runtime/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ var (
errorMissingServer = fmt.Errorf("missing server definitions")
)

var reCleanPattern = regexp.MustCompile(`{([^}]+)}`)
var (
// reValidFormat validates the format only, validating for a valid host or port is out of scope.
reValidFormat = regexp.MustCompile(`^([a-z0-9.-]+|\*)(:\*|:\d{1,5})?$`)
reCleanPattern = regexp.MustCompile(`{([^}]+)}`)
rePortCheck = regexp.MustCompile(`^(0|[1-9][0-9]{1,4})$`)
)

type backendDefinition struct {
conf *config.Backend
Expand Down Expand Up @@ -293,9 +298,6 @@ func mapPortRoutes(configuredPort int, server *config.Server, mux *MuxOptions, s
// "host:*" equals to "host:configuredPort"
// "host" listen on configured default port for given host
func validatePortHosts(conf *config.Gateway, configuredPort int) error {
// validate the format, validating for a valid host or port is out of scope.
validFormat := regexp.MustCompile(`^([a-z0-9.-]+|\*)(:\*|:\d{1,5})?$`)

type hosts map[string]bool
type ports map[int]hosts

Expand All @@ -309,7 +311,7 @@ func validatePortHosts(conf *config.Gateway, configuredPort int) error {

srvPortMap := make(ports)
for _, host := range srv.Hosts {
if !validFormat.MatchString(host) {
if !reValidFormat.MatchString(host) {
return fmt.Errorf("host format is invalid: %q", host)
}

Expand All @@ -325,8 +327,8 @@ func validatePortHosts(conf *config.Gateway, configuredPort int) error {
srvPortMap[po][ho] = true
}

// srvPortsMap contains all unique host port combinations
// of current server and should not exist multiple.
// srvPortMap contains all unique host port combinations for
// the current server and should not exist multiple times.
for po, ho := range srvPortMap {
if _, ok := portMap[po]; !ok {
portMap[po] = make(hosts)
Expand Down Expand Up @@ -358,6 +360,9 @@ func splitWildcardHostPort(host string, configuredPort int) (string, int, error)
}
ho = h
if p != "" && p != "*" {
if !rePortCheck.MatchString(p) {
return "", -1, fmt.Errorf("invalid port given: %s", p)
}
po, err = strconv.Atoi(p)
if err != nil {
return "", -1, err
Expand Down
11 changes: 11 additions & 0 deletions config/runtime/server_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"fmt"
"testing"

"github.com/avenga/couper/config"
Expand Down Expand Up @@ -149,3 +150,13 @@ func TestServer_validatePortHosts(t *testing.T) {
})
}
}

func TestServer_splitWildcardHostPort(t *testing.T) {
for _, port := range []string{"01234", "00", "123456"} {
host := fmt.Sprintf("foo.de:%s", port)
_, _, err := splitWildcardHostPort(host, 8080)
if err == nil {
t.Errorf("Expected en error for %q, NIL given.", host)
}
}
}

0 comments on commit 3cd26b1

Please sign in to comment.