Skip to content

Commit

Permalink
Fix bucket name validation to allow underscores (#330)
Browse files Browse the repository at this point in the history
Also prevent non-alphanumeric characters at start/end of bucket name
  • Loading branch information
gh2k authored Sep 17, 2020
1 parent 71c9024 commit bb30ba5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion fakestorage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/gorilla/mux"
)

var bucketRegexp = regexp.MustCompile(`^[a-zA-Z0-9-][a-zA-Z0-9.-]*[a-zA-Z0-9-]$`)
var bucketRegexp = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]$`)

// CreateBucket creates a bucket inside the server, so any API calls that
// require the bucket name will recognize this bucket.
Expand Down
14 changes: 10 additions & 4 deletions fakestorage/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func TestServerClientBucketAttrs(t *testing.T) {
{BucketName: "some-bucket", Name: "img/hi-res/party-01.jpg"},
{BucketName: "some-bucket", Name: "img/hi-res/party-02.jpg"},
{BucketName: "some-bucket", Name: "img/hi-res/party-03.jpg"},
{BucketName: "other-bucket", Name: "static/css/website.css"},
{BucketName: "other_bucket", Name: "static/css/website.css"},
{BucketName: "dot.bucket", Name: "static/js/app.js"},
}
startTime := time.Now()
runServersTest(t, objs, func(t *testing.T, server *Server) {
Expand Down Expand Up @@ -96,8 +97,12 @@ func TestServerClientBucketAttrsAfterCreateBucketByPost(t *testing.T) {
func TestServerClientBucketCreateValidation(t *testing.T) {
bucketNames := []string{
"..what-is-this",
"-hostname-cant-start-with-dash",
"_hostname-cant-start-with-underscore",
"hostname-cant-end-with-dash-",
"hostname-cant-end-with-underscore_",
".host.name.cant.start.with.dot",
"no_underlines_either",
"host.name.cant.end.with.dot.",
"or spaces",
"don't even try",
"no/slashes/either",
Expand Down Expand Up @@ -133,7 +138,8 @@ func TestServerClientListBuckets(t *testing.T) {
{BucketName: "some-bucket", Name: "img/hi-res/party-01.jpg"},
{BucketName: "some-bucket", Name: "img/hi-res/party-02.jpg"},
{BucketName: "some-bucket", Name: "img/hi-res/party-03.jpg"},
{BucketName: "other-bucket", Name: "static/css/website.css"},
{BucketName: "other_bucket", Name: "static/css/website.css"},
{BucketName: "dot.bucket", Name: "static/js/app.js"},
}

runServersTest(t, objs, func(t *testing.T, server *Server) {
Expand All @@ -147,7 +153,7 @@ func TestServerClientListBuckets(t *testing.T) {
}
it := client.Buckets(context.Background(), "whatever")
expectedBuckets := map[string]bool{
"other-bucket": false, "some-bucket": false, versionedBucketName: true,
"other_bucket": false, "dot.bucket": false, "some-bucket": false, versionedBucketName: true,
}
b, err := it.Next()
numberOfBuckets := 0
Expand Down

0 comments on commit bb30ba5

Please sign in to comment.