Add db/rp name validation
This change adds some very basic name validation with the following plain-english description: names must be non-zero sequence of printable characters that do not contain slashes ('/' or '\') and are not equal to either "." or "..". The intent is that, since we currently just use database and retention policy names directly as path elements, these rules will hopefully leave us with names that should be at least close to valid directory names. Ideally, we would restrict names even further or not use them as path elements directly, but this should be a step towards the former without restricting names "too much"pull/7829/head
parent
b19260fb26
commit
2db0250b22
|
@ -244,6 +244,12 @@ func (e *StatementExecutor) executeCreateContinuousQueryStatement(q *influxql.Cr
|
|||
}
|
||||
|
||||
func (e *StatementExecutor) executeCreateDatabaseStatement(stmt *influxql.CreateDatabaseStatement) error {
|
||||
if !meta.ValidName(stmt.Name) {
|
||||
// TODO This should probably be in `(*meta.Data).CreateDatabase`
|
||||
// but can't go there until 1.1 is used everywhere
|
||||
return meta.ErrInvalidName
|
||||
}
|
||||
|
||||
if !stmt.RetentionPolicyCreate {
|
||||
_, err := e.MetaClient.CreateDatabase(stmt.Name)
|
||||
return err
|
||||
|
@ -260,6 +266,12 @@ func (e *StatementExecutor) executeCreateDatabaseStatement(stmt *influxql.Create
|
|||
}
|
||||
|
||||
func (e *StatementExecutor) executeCreateRetentionPolicyStatement(stmt *influxql.CreateRetentionPolicyStatement) error {
|
||||
if !meta.ValidName(stmt.Name) {
|
||||
// TODO This should probably be in `(*meta.Data).CreateRetentionPolicy`
|
||||
// but can't go there until 1.1 is used everywhere
|
||||
return meta.ErrInvalidName
|
||||
}
|
||||
|
||||
spec := meta.RetentionPolicySpec{
|
||||
Name: stmt.Name,
|
||||
Duration: &stmt.Duration,
|
||||
|
@ -352,7 +364,6 @@ func (e *StatementExecutor) executeDropShardStatement(stmt *influxql.DropShardSt
|
|||
}
|
||||
|
||||
func (e *StatementExecutor) executeDropRetentionPolicyStatement(stmt *influxql.DropRetentionPolicyStatement) error {
|
||||
|
||||
dbi := e.MetaClient.Database(stmt.Database)
|
||||
if dbi == nil {
|
||||
return nil
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"github.com/influxdata/influxdb"
|
||||
|
@ -1553,3 +1554,17 @@ func UnmarshalTime(v int64) time.Time {
|
|||
}
|
||||
return time.Unix(0, v).UTC()
|
||||
}
|
||||
|
||||
// ValidName checks to see if the given name can would be valid for DB/RP name
|
||||
func ValidName(name string) bool {
|
||||
for _, r := range name {
|
||||
if !unicode.IsPrint(r) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return name != "" &&
|
||||
name != "." &&
|
||||
name != ".." &&
|
||||
!strings.ContainsAny(name, `/\`)
|
||||
}
|
||||
|
|
|
@ -22,6 +22,9 @@ var (
|
|||
|
||||
// ErrDatabaseNameRequired is returned when creating a database without a name.
|
||||
ErrDatabaseNameRequired = errors.New("database name required")
|
||||
|
||||
// ErrInvalidName is returned when attempting to create a database or retention policy with an invalid name
|
||||
ErrInvalidName = errors.New("invalid name")
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
|
@ -438,6 +438,7 @@ func (s *Store) DeleteDatabase(name string) error {
|
|||
func (s *Store) DeleteRetentionPolicy(database, name string) error {
|
||||
s.mu.RLock()
|
||||
if s.databaseIndexes[database] == nil {
|
||||
s.mu.RUnlock()
|
||||
// unknown database, nothing to do
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue