Skip to content

Commit

Permalink
Validate TXT prefix and suffix
Browse files Browse the repository at this point in the history
Signed-off-by: Ruud Kamphuis <ruudk@users.noreply.github.com>
  • Loading branch information
ruudk committed Jul 14, 2020
1 parent 7505f29 commit e5fce25
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
25 changes: 24 additions & 1 deletion registry/txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -52,8 +53,30 @@ func NewTXTRegistry(provider provider.Provider, txtPrefix, txtSuffix, ownerID st
return nil, errors.New("txt-prefix and txt-suffix are mutual exclusive")
}

txtPrefix = strings.ToLower(txtPrefix)
txtSuffix = strings.ToLower(txtSuffix)
mapper := newaffixNameMapper(txtPrefix, txtSuffix)

if txtPrefix != "" {
matched, err := regexp.MatchString(`^[a-z0-9-.]+$`, txtPrefix)
if err != nil {
return nil, err
}
if !matched {
return nil, fmt.Errorf(`invalid TXT prefix provided, expected "[a-z0-9-.]+" got "%s"`, txtPrefix)
}
}

if txtSuffix != "" {
matched, err := regexp.MatchString(`^[a-z0-9-.]+$`, txtSuffix)
if err != nil {
return nil, err
}
if !matched {
return nil, fmt.Errorf(`invalid TXT suffix provided, expected "[a-z0-9-.]+" got "%s"`, txtSuffix)
}
}

return &TXTRegistry{
provider: provider,
ownerID: ownerID,
Expand Down Expand Up @@ -218,7 +241,7 @@ type affixNameMapper struct {
var _ nameMapper = affixNameMapper{}

func newaffixNameMapper(prefix string, suffix string) affixNameMapper {
return affixNameMapper{prefix: strings.ToLower(prefix), suffix: strings.ToLower(suffix)}
return affixNameMapper{prefix: prefix, suffix: suffix}
}

func (pr affixNameMapper) toEndpointName(txtDNSName string) string {
Expand Down
20 changes: 20 additions & 0 deletions registry/txt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ func testTXTRegistryNew(t *testing.T) {
_, err = NewTXTRegistry(p, "txt", "txt", "owner", time.Hour)
require.Error(t, err)

_, err = NewTXTRegistry(p, "@invalid#", "", "owner", time.Hour)
require.Error(t, err, `invalid TXT prefix provided, expected "[a-z0-9-.]+" got "@invalid#"`)
_, err = NewTXTRegistry(p, "", "@invalid#", "owner", time.Hour)
require.Error(t, err, `invalid TXT suffix provided, expected "[a-z0-9-.]+" got "@invalid#"`)

_, err = NewTXTRegistry(p, "txt", "", "owner", time.Hour)
require.NoError(t, err)
_, err = NewTXTRegistry(p, "", "txt", "owner", time.Hour)
require.NoError(t, err)

_, err = NewTXTRegistry(p, "TxT", "", "owner", time.Hour)
require.NoError(t, err)
_, err = NewTXTRegistry(p, "", "TxT", "owner", time.Hour)
require.NoError(t, err)

r, err = NewTXTRegistry(p, "-sub-.", "", "owner", time.Hour)
require.NoError(t, err)
r, err = NewTXTRegistry(p, "", "-sub-.", "owner", time.Hour)
require.NoError(t, err)

_, ok := r.mapper.(affixNameMapper)
require.True(t, ok)
assert.Equal(t, "owner", r.ownerID)
Expand Down

0 comments on commit e5fce25

Please sign in to comment.