arguments.go 4.6 KB

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