arguments.go 4.8 KB

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