package handlers import ( "errors" "regexp" "strconv" ) const ( // appNameMinLength is the minimum allowed length for an app name. appNameMinLength = 2 // appNameMaxLength is the maximum allowed length for an app name. appNameMaxLength = 63 ) // validAppNameRe matches runs of lowercase letters and digits joined by // single dots or by hyphens, such as "my-app" or "sneak.berlin". Docker // accepts every name it allows as the app's image name, upaas-; a // dot needs a letter or digit on both sides because Docker requires it. // It also keeps the name from being "." or ".." or starting or ending // with a dot, so it is safe as a directory and file name. The pattern // attribute of the name field on the new and edit app forms is the same. var validAppNameRe = regexp.MustCompile(`^[a-z0-9]+((\.|-+)[a-z0-9]+)*$`) var ( errAppNameLength = errors.New( "app name must be between " + strconv.Itoa(appNameMinLength) + " and " + strconv.Itoa(appNameMaxLength) + " characters", ) errAppNamePattern = errors.New( "app name must contain only lowercase letters, numbers, hyphens, " + "and dots, must start and end with a letter or number, " + "and must have a letter or number on both sides of each dot", ) ) // validateAppName checks that the given app name is safe for use in Docker // container names, image tags, and file system paths. func validateAppName(name string) error { if len(name) < appNameMinLength || len(name) > appNameMaxLength { return errAppNameLength } if !validAppNameRe.MatchString(name) { return errAppNamePattern } return nil }