From 6bd6dd342dee54dc45e433aefda9c9ac6b9ec8f9 Mon Sep 17 00:00:00 2001 From: Evan Shaw Date: Fri, 29 Jul 2016 14:00:35 +1200 Subject: [PATCH] Restrict app name App name must match the regular expression: [\w\-]{1, 30} --- api/models/app.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/api/models/app.go b/api/models/app.go index b203cc94a..9fdbbb6c7 100644 --- a/api/models/app.go +++ b/api/models/app.go @@ -1,6 +1,9 @@ package models -import "errors" +import ( + "errors" + "fmt" +) type Apps []*App @@ -19,13 +22,27 @@ type App struct { Routes Routes `json:"routes,omitempty"` } +const ( + maxAppName = 30 +) + var ( - ErrAppsValidationName = errors.New("Missing app name") + ErrAppsValidationMissingName = errors.New("Missing app name") + ErrAppsValidationTooLongName = fmt.Errorf("App name must be %v characters or less", maxAppName) + ErrAppsValidationInvalidName = errors.New("Invalid app name") ) func (a *App) Validate() error { if a.Name == "" { - return ErrAppsValidationName + return ErrAppsValidationMissingName + } + if len(a.Name) > maxAppName { + return ErrAppsValidationTooLongName + } + for _, c := range a.Name { + if (c < '0' || '9' < c) && (c < 'A' || 'Z' > c) && (c < 'a' || 'z' < c) && c != '_' && c != '-' { + return ErrAppsValidationInvalidName + } } return nil }