Skip to content

Commit

Permalink
broader restrictions via allow-list
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Mason <andrew@planetscale.com>
  • Loading branch information
Andrew Mason committed Mar 28, 2023
1 parent 966e4df commit 5354d4f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
3 changes: 1 addition & 2 deletions changelog/17.0/17.0.0/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ Prior to v17, it was possible to create a keyspace with invalid characters, whic

Now, the TopoServer's `GetKeyspace` and `CreateKeyspace` methods return an error if given an invalid name.

Keyspace names may no longer contain the following characters:
- Forward slash ("/").
Keyspace names may now only contain alphanumerics, dashes (`-`), and underscores (`_`), and must begin with an alphabetical character.

### <a id="new-flag"/> New command line flags and behavior

Expand Down
8 changes: 4 additions & 4 deletions go/vt/topo/keyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package topo

import (
"path"
"strings"
"regexp"

"context"

Expand Down Expand Up @@ -54,15 +54,15 @@ func (ki *KeyspaceInfo) SetKeyspaceName(name string) {
ki.keyspace = name
}

var invalidKeyspaceNameChars = "/"
var keyspaceNameRegexp = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9_-]*$")

// ValidateKeyspaceNames checks if the provided name is a valid name for a
// keyspace.
//
// As of v17, "all invalid characters" is just the forward slash ("/").
func ValidateKeyspaceName(name string) error {
if strings.ContainsAny(name, invalidKeyspaceNameChars) {
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "keyspace name %s contains invalid characters; may not contain any of the following: %+v", name, strings.Split(invalidKeyspaceNameChars, ""))
if !keyspaceNameRegexp.MatchString(name) {
return vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "keyspace name %s contains invalid characters; may only contain alphanumerics, dashes and underscores", name)
}

return nil
Expand Down
48 changes: 48 additions & 0 deletions go/vt/topo/keyspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"

topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)

Expand Down Expand Up @@ -175,3 +177,49 @@ func TestComputeCellServedFrom(t *testing.T) {
t.Fatalf("c2 failed: %v", m)
}
}

func TestValidateKeyspaceName(t *testing.T) {
t.Parallel()

cases := []struct {
name string
valid bool
}{
{
name: "testks",
valid: true,
},
{
name: "alphnum_ks1-with_a_dash",
valid: true,
},
{
name: "no/slashes/allowed",
valid: false,
},
{
name: "wë!rd ch@r@ct3r$",
valid: false,
},
{
name: "0_cant-start_with-a-number",
valid: false,
},
}

for _, tcase := range cases {
tcase := tcase
t.Run(tcase.name, func(t *testing.T) {
t.Parallel()

var assertion func(t assert.TestingT, err error, msgAndArgs ...any) bool
if tcase.valid {
assertion = assert.NoError
} else {
assertion = assert.Error
}

assertion(t, ValidateKeyspaceName(tcase.name))
})
}
}

0 comments on commit 5354d4f

Please sign in to comment.