arguments.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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(values map[string]string, action *config.Action, actionTitle string, entityPrefix string) (string, error) {
  39. log.WithFields(log.Fields{
  40. "actionTitle": actionTitle,
  41. "cmd": action.Shell,
  42. }).Infof("Action parse args - Before")
  43. rawShellCommand, usedArgs, err := parseCommandForReplacements(action.Shell, 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. //
  108. //gocyclo:ignore
  109. func TypeSafetyCheck(name string, value string, argumentType string) error {
  110. switch argumentType {
  111. case "password":
  112. return nil
  113. case "raw_string_multiline":
  114. return nil
  115. case "email":
  116. return typeSafetyCheckEmail(name, value)
  117. case "url":
  118. return typeSafetyCheckUrl(name, value)
  119. case "datetime":
  120. return typeSafetyCheckDatetime(name, value)
  121. }
  122. return typeSafetyCheckRegex(name, value, argumentType)
  123. }
  124. func typeSafetyCheckEmail(name string, value string) error {
  125. _, err := mail.ParseAddress(value)
  126. log.Errorf("Email check: %v, %v", err, value)
  127. if err != nil {
  128. return err
  129. }
  130. return nil
  131. }
  132. func typeSafetyCheckDatetime(name string, value string) error {
  133. _, err := time.Parse("2006-01-02T15:04:05", value)
  134. if err != nil {
  135. return err
  136. }
  137. return nil
  138. }
  139. func typeSafetyCheckRegex(name string, value string, argumentType string) error {
  140. pattern := ""
  141. if strings.HasPrefix(argumentType, "regex:") {
  142. pattern = strings.Replace(argumentType, "regex:", "", 1)
  143. } else {
  144. found := false
  145. pattern, found = typecheckRegex[argumentType]
  146. if !found {
  147. return errors.New("argument type not implemented " + argumentType)
  148. }
  149. }
  150. matches, _ := regexp.MatchString(pattern, value)
  151. if !matches {
  152. log.WithFields(log.Fields{
  153. "name": name,
  154. "value": value,
  155. "type": argumentType,
  156. "pattern": pattern,
  157. }).Warn("Arg type check safety failure")
  158. return errors.New("invalid argument, doesn't match " + argumentType)
  159. }
  160. return nil
  161. }
  162. func typeSafetyCheckUrl(name string, value string) error {
  163. _, err := url.ParseRequestURI(value)
  164. return err
  165. }