arguments.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package executor
  2. import (
  3. config "github.com/OliveTin/OliveTin/internal/config"
  4. sv "github.com/OliveTin/OliveTin/internal/stringvariables"
  5. log "github.com/sirupsen/logrus"
  6. "errors"
  7. "net/mail"
  8. "net/url"
  9. "regexp"
  10. "strings"
  11. "time"
  12. )
  13. var (
  14. typecheckRegex = map[string]string{
  15. "very_dangerous_raw_string": "",
  16. "int": "^[\\d]+$",
  17. "unicode_identifier": "^[\\w\\/\\\\.\\_ \\d]+$",
  18. "ascii": "^[a-zA-Z0-9]+$",
  19. "ascii_identifier": "^[a-zA-Z0-9\\-\\.\\_]+$",
  20. "ascii_sentence": "^[a-zA-Z0-9 \\,\\.]+$",
  21. }
  22. )
  23. func parseCommandForReplacements(rawShellCommand string, values map[string]string) (string, map[string]string, error) {
  24. r := regexp.MustCompile("{{ *?([a-zA-Z0-9_]+?) *?}}")
  25. foundArgumentNames := r.FindAllStringSubmatch(rawShellCommand, -1)
  26. usedArguments := make(map[string]string)
  27. for _, match := range foundArgumentNames {
  28. argName := match[1]
  29. argValue, argProvided := values[argName]
  30. if !argProvided {
  31. return "", nil, errors.New("Required arg not provided: " + argName)
  32. }
  33. usedArguments[argName] = argValue
  34. rawShellCommand = strings.ReplaceAll(rawShellCommand, match[0], argValue)
  35. }
  36. return rawShellCommand, usedArguments, nil
  37. }
  38. func parseActionArguments(rawShellCommand string, values map[string]string, action *config.Action, actionTitle string, entityPrefix string) (string, error) {
  39. log.WithFields(log.Fields{
  40. "actionTitle": actionTitle,
  41. "cmd": rawShellCommand,
  42. }).Infof("Action parse args - Before")
  43. rawShellCommand, usedArgs, err := parseCommandForReplacements(rawShellCommand, values)
  44. if err != nil {
  45. return "", err
  46. }
  47. for argName, argValue := range usedArgs {
  48. err := typecheckActionArgument(argName, argValue, action)
  49. if err != nil {
  50. return "", err
  51. }
  52. log.WithFields(log.Fields{
  53. "name": argName,
  54. "value": argValue,
  55. }).Debugf("Arg assigned")
  56. }
  57. rawShellCommand = sv.ReplaceEntityVars(entityPrefix, rawShellCommand)
  58. log.WithFields(log.Fields{
  59. "actionTitle": actionTitle,
  60. "cmd": rawShellCommand,
  61. }).Infof("Action parse args - After")
  62. return rawShellCommand, nil
  63. }
  64. func typecheckActionArgument(name string, value string, action *config.Action) error {
  65. arg := action.FindArg(name)
  66. if arg == nil {
  67. return errors.New("Action arg not defined: " + name)
  68. }
  69. if value == "" {
  70. return typecheckNull(arg)
  71. }
  72. if len(arg.Choices) > 0 {
  73. return typecheckChoice(value, arg)
  74. }
  75. return TypeSafetyCheck(name, value, arg.Type)
  76. }
  77. func typecheckNull(arg *config.ActionArgument) error {
  78. if arg.RejectNull {
  79. return errors.New("Null values are not allowed")
  80. }
  81. return nil
  82. }
  83. func typecheckChoice(value string, arg *config.ActionArgument) error {
  84. if arg.Entity != "" {
  85. return typecheckChoiceEntity(value, arg)
  86. }
  87. for _, choice := range arg.Choices {
  88. if value == choice.Value {
  89. return nil
  90. }
  91. }
  92. return errors.New("argument value is not one of the predefined choices")
  93. }
  94. func typecheckChoiceEntity(value string, arg *config.ActionArgument) error {
  95. templateChoice := arg.Choices[0].Value
  96. for _, ent := range sv.GetEntities(arg.Entity) {
  97. choice := sv.ReplaceEntityVars(ent, templateChoice)
  98. if value == choice {
  99. return nil
  100. }
  101. }
  102. return errors.New("argument value cannot be found in entities")
  103. }
  104. // TypeSafetyCheck checks argument values match a specific type. The types are
  105. // defined in typecheckRegex, and, you guessed it, uses regex to check for allowed
  106. // characters.
  107. //gocyclo:ignore
  108. func TypeSafetyCheck(name string, value string, argumentType string) error {
  109. switch argumentType {
  110. case "password":
  111. return nil
  112. case "email":
  113. return typeSafetyCheckEmail(name, value)
  114. case "url":
  115. return typeSafetyCheckUrl(name, value)
  116. case "datetime":
  117. return typeSafetyCheckDatetime(name, value)
  118. }
  119. return typeSafetyCheckRegex(name, value, argumentType)
  120. }
  121. func typeSafetyCheckEmail(name string, value string) error {
  122. _, err := mail.ParseAddress(value)
  123. log.Errorf("Email check: %v, %v", err, value)
  124. if err != nil {
  125. return err
  126. }
  127. return nil
  128. }
  129. func typeSafetyCheckDatetime(name string, value string) error {
  130. _, err := time.Parse("2006-01-02T15:04:05", value)
  131. if err != nil {
  132. return err
  133. }
  134. return nil
  135. }
  136. func typeSafetyCheckRegex(name string, value string, argumentType string) error {
  137. pattern := ""
  138. if strings.HasPrefix(argumentType, "regex:") {
  139. pattern = strings.Replace(argumentType, "regex:", "", 1)
  140. } else {
  141. found := false
  142. pattern, found = typecheckRegex[argumentType]
  143. if !found {
  144. return errors.New("argument type not implemented " + argumentType)
  145. }
  146. }
  147. matches, _ := regexp.MatchString(pattern, value)
  148. if !matches {
  149. log.WithFields(log.Fields{
  150. "name": name,
  151. "value": value,
  152. "type": argumentType,
  153. "pattern": pattern,
  154. }).Warn("Arg type check safety failure")
  155. return errors.New("invalid argument, doesn't match " + argumentType)
  156. }
  157. return nil
  158. }
  159. func typeSafetyCheckUrl(name string, value string) error {
  160. _, err := url.ParseRequestURI(value)
  161. return err
  162. }