Merge pull request #23965 from tpaschalis/disallow-s3-backend-key-trailing-slash

S3 Backend : Bucket key should not contain trailing slash
This commit is contained in:
Graham Davison 2022-11-01 13:56:43 -07:00 committed by GitHub
commit 6663cde619
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -37,6 +37,11 @@ func New() backend.Backend {
if strings.HasPrefix(v.(string), "/") {
return nil, []error{errors.New("key must not start with '/'")}
}
// s3 will recognize objects with a trailing slash as a directory
// so they should not be valid keys
if strings.HasSuffix(v.(string), "/") {
return nil, []error{errors.New("key must not end with '/'")}
}
return nil, nil
},
},

View File

@ -326,6 +326,19 @@ func TestBackendConfig_invalidKey(t *testing.T) {
if !diags.HasErrors() {
t.Fatal("expected config validation error")
}
cfg = hcl2shim.HCL2ValueFromConfigValue(map[string]interface{}{
"region": "us-west-1",
"bucket": "tf-test",
"key": "trailing-slash/",
"encrypt": true,
"dynamodb_table": "dynamoTable",
})
_, diags = New().PrepareConfig(cfg)
if !diags.HasErrors() {
t.Fatal("expected config validation error")
}
}
func TestBackendConfig_invalidSSECustomerKeyLength(t *testing.T) {