-
Notifications
You must be signed in to change notification settings - Fork 555
misc: added validation on create environment #6859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,6 +25,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/devtron-labs/devtron/pkg/cluster/environment/read" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/devtron-labs/devtron/util/commonEnforcementFunctionsUtil" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "regexp" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "sync" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -106,6 +107,34 @@ func NewEnvironmentRestHandlerImpl(svc request.EnvironmentService, environmentRe | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Regex patterns for environment name validation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| envNameAlphanumericRegex = regexp.MustCompile(`^[a-z0-9-]+$`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| envNameNoStartEndHyphen = regexp.MustCompile(`^(?![-]).*[^-]$`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| envNameLengthRegex = regexp.MustCompile(`^.{1,16}$`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // validateEnvironmentName validates the environment name against multiple regex patterns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Note: Required validation is already handled by struct validation tag | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (impl EnvironmentRestHandlerImpl) validateEnvironmentName(envName string) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Validation 1: Use only lowercase alphanumeric characters or '-' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !envNameAlphanumericRegex.MatchString(envName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return errors.New("Use only lowercase alphanumeric characters or '-'") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Validation 2: Cannot start/end with '-' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !envNameNoStartEndHyphen.MatchString(envName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return errors.New("Cannot start/end with '-'") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Validation 3: Minimum 1 and Maximum 16 characters required | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !envNameLengthRegex.MatchString(envName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return errors.New("Minimum 1 and Maximum 16 characters required") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+130
to
+131
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Regex patterns for environment name validation | |
| envNameAlphanumericRegex = regexp.MustCompile(`^[a-z0-9-]+$`) | |
| envNameNoStartEndHyphen = regexp.MustCompile(`^(?![-]).*[^-]$`) | |
| envNameLengthRegex = regexp.MustCompile(`^.{1,16}$`) | |
| ) | |
| // validateEnvironmentName validates the environment name against multiple regex patterns | |
| // Note: Required validation is already handled by struct validation tag | |
| func (impl EnvironmentRestHandlerImpl) validateEnvironmentName(envName string) error { | |
| // Validation 1: Use only lowercase alphanumeric characters or '-' | |
| if !envNameAlphanumericRegex.MatchString(envName) { | |
| return errors.New("Use only lowercase alphanumeric characters or '-'") | |
| } | |
| // Validation 2: Cannot start/end with '-' | |
| if !envNameNoStartEndHyphen.MatchString(envName) { | |
| return errors.New("Cannot start/end with '-'") | |
| } | |
| // Validation 3: Minimum 1 and Maximum 16 characters required | |
| if !envNameLengthRegex.MatchString(envName) { | |
| return errors.New("Minimum 1 and Maximum 16 characters required") | |
| } | |
| // Comprehensive regex pattern for environment name validation: | |
| // - Only lowercase alphanumeric and hyphens | |
| // - Must start and end with alphanumeric | |
| // - Hyphens allowed only in the middle | |
| // - Length: 1-16 characters | |
| envNameComprehensiveRegex = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]{0,14}[a-z0-9])?$`) | |
| ) | |
| // validateEnvironmentName validates the environment name against multiple regex patterns | |
| // Note: Required validation is already handled by struct validation tag | |
| func (impl EnvironmentRestHandlerImpl) validateEnvironmentName(envName string) error { | |
| // Validate using a single comprehensive regex | |
| if !envNameComprehensiveRegex.MatchString(envName) { | |
| return errors.New("Environment name must be 1-16 characters, use only lowercase alphanumeric characters or '-', cannot start or end with '-'") | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex pattern
^(?![-]).*[^-]$will fail for single-character names (e.g., 'a') because it requires at least two characters: one that doesn't match the negative lookahead and one that isn't a hyphen at the end. Consider using^[^-].*[^-]$|^[^-]$to handle single non-hyphen characters, or simplify to^[^-]([a-z0-9-]*[^-])?$.