Skip to content

Commit

Permalink
Merge pull request #896 from traPtitech/chores/validate-env-var-name
Browse files Browse the repository at this point in the history
Add env var name validation
  • Loading branch information
motoki317 authored Apr 17, 2024
2 parents febf874 + ce8c602 commit 84bd6c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/domain/app_environment.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package domain

import (
"fmt"
"regexp"
)

type Environment struct {
ApplicationID string
Key string
Expand All @@ -10,3 +15,12 @@ type Environment struct {
func (e *Environment) GetKV() (string, string) {
return e.Key, e.Value
}

var environmentVariableKeyFormat = regexp.MustCompile(`^[A-Z_][A-Z0-9_]*$`)

func (e *Environment) Validate() error {
if !environmentVariableKeyFormat.MatchString(e.Key) {
return fmt.Errorf("bad key format: %s", e.Key)
}
return nil
}
6 changes: 6 additions & 0 deletions pkg/usecase/apiserver/app_config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ func (s *Service) SetEnvironmentVariable(ctx context.Context, applicationID stri
return err
}

// Validate
env := &domain.Environment{ApplicationID: applicationID, Key: key, Value: value, System: false}
err = env.Validate()
if err != nil {
return newError(ErrorTypeBadRequest, "invalid environment variable", err)
}

return s.envRepo.SetEnv(ctx, env)
}

Expand Down

0 comments on commit 84bd6c4

Please sign in to comment.