arguments.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. "fmt"
  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(shellCommand string, values map[string]string) (string, error) {
  24. r := regexp.MustCompile("{{ *?([a-zA-Z0-9_]+?) *?}}")
  25. foundArgumentNames := r.FindAllStringSubmatch(shellCommand, -1)
  26. for _, match := range foundArgumentNames {
  27. argName := match[1]
  28. argValue, argProvided := values[argName]
  29. if !argProvided {
  30. return "", fmt.Errorf("required arg not provided: %v", argName)
  31. }
  32. shellCommand = strings.ReplaceAll(shellCommand, match[0], argValue)
  33. }
  34. return shellCommand, nil
  35. }
  36. func parseActionArguments(values map[string]string, action *config.Action, entityPrefix string) (string, error) {
  37. log.WithFields(log.Fields{
  38. "actionTitle": action.Title,
  39. "cmd": action.Shell,
  40. }).Infof("Action parse args - Before")
  41. for _, arg := range action.Arguments {
  42. argName := arg.Name
  43. argValue := values[argName]
  44. err := typecheckActionArgument(argName, argValue, action)
  45. if err != nil {
  46. return "", err
  47. }
  48. log.WithFields(log.Fields{
  49. "name": argName,
  50. "value": argValue,
  51. }).Debugf("Arg assigned")
  52. }
  53. parsedShellCommand, err := parseCommandForReplacements(action.Shell, values)
  54. parsedShellCommand = sv.ReplaceEntityVars(entityPrefix, parsedShellCommand)
  55. redactedShellCommand := redactShellCommand(parsedShellCommand, action.Arguments, values)
  56. if err != nil {
  57. return "", err
  58. }
  59. log.WithFields(log.Fields{
  60. "actionTitle": action.Title,
  61. "cmd": redactedShellCommand,
  62. }).Infof("Action parse args - After")
  63. return parsedShellCommand, nil
  64. }
  65. //gocyclo:ignore
  66. func redactShellCommand(shellCommand string, arguments []config.ActionArgument, argumentValues map[string]string) string {
  67. for _, arg := range arguments {
  68. if arg.Type == "password" {
  69. argValue, exists := argumentValues[arg.Name]
  70. if !exists {
  71. log.Warnf("Redact shell command: Argument %s not found in values", arg.Name)
  72. continue
  73. }
  74. if argValue == "" {
  75. continue
  76. }
  77. shellCommand = strings.ReplaceAll(shellCommand, argValue, "<redacted>")
  78. }
  79. }
  80. return shellCommand
  81. }
  82. func typecheckActionArgument(name string, value string, action *config.Action) error {
  83. arg := action.FindArg(name)
  84. if arg == nil {
  85. return fmt.Errorf("action arg not defined: %v", name)
  86. }
  87. if value == "" {
  88. return typecheckNull(arg)
  89. }
  90. if len(arg.Choices) > 0 {
  91. return typecheckChoice(value, arg)
  92. }
  93. return TypeSafetyCheck(name, value, arg.Type)
  94. }
  95. // TypeSafetyCheck checks argument values match a specific type. The types are
  96. // defined in typecheckRegex, and, you guessed it, uses regex to check for allowed
  97. // characters.
  98. //
  99. //gocyclo:ignore
  100. func TypeSafetyCheck(name string, value string, argumentType string) error {
  101. switch argumentType {
  102. case "password":
  103. return nil
  104. case "raw_string_multiline":
  105. return nil
  106. case "email":
  107. return typeSafetyCheckEmail(value)
  108. case "url":
  109. return typeSafetyCheckUrl(value)
  110. case "datetime":
  111. return typeSafetyCheckDatetime(value)
  112. }
  113. return typeSafetyCheckRegex(name, value, argumentType)
  114. }
  115. func typecheckNull(arg *config.ActionArgument) error {
  116. if arg.RejectNull {
  117. return fmt.Errorf("null values are not allowed")
  118. }
  119. return nil
  120. }
  121. func typecheckChoice(value string, arg *config.ActionArgument) error {
  122. if arg.Entity != "" {
  123. return typecheckChoiceEntity(value, arg)
  124. }
  125. for _, choice := range arg.Choices {
  126. if value == choice.Value {
  127. return nil
  128. }
  129. }
  130. return fmt.Errorf("argument value is not one of the predefined choices")
  131. }
  132. func typecheckChoiceEntity(value string, arg *config.ActionArgument) error {
  133. templateChoice := arg.Choices[0].Value
  134. for _, ent := range sv.GetEntities(arg.Entity) {
  135. choice := sv.ReplaceEntityVars(ent, templateChoice)
  136. if value == choice {
  137. return nil
  138. }
  139. }
  140. return fmt.Errorf("argument value cannot be found in entities")
  141. }
  142. func typeSafetyCheckEmail(value string) error {
  143. _, err := mail.ParseAddress(value)
  144. log.Errorf("Email check: %v, %v", err, value)
  145. if err != nil {
  146. return err
  147. }
  148. return nil
  149. }
  150. func typeSafetyCheckDatetime(value string) error {
  151. _, err := time.Parse("2006-01-02T15:04:05", value)
  152. if err != nil {
  153. return err
  154. }
  155. return nil
  156. }
  157. func typeSafetyCheckRegex(name string, value string, argumentType string) error {
  158. pattern := ""
  159. if strings.HasPrefix(argumentType, "regex:") {
  160. pattern = strings.Replace(argumentType, "regex:", "", 1)
  161. } else {
  162. found := false
  163. pattern, found = typecheckRegex[argumentType]
  164. if !found {
  165. return fmt.Errorf("argument type not implemented %v", argumentType)
  166. }
  167. }
  168. matches, _ := regexp.MatchString(pattern, value)
  169. if !matches {
  170. log.WithFields(log.Fields{
  171. "name": name,
  172. "value": value,
  173. "type": argumentType,
  174. "pattern": pattern,
  175. }).Warn("Arg type check safety failure")
  176. return fmt.Errorf("invalid argument %v, doesn't match %v", name, argumentType)
  177. }
  178. return nil
  179. }
  180. func typeSafetyCheckUrl(value string) error {
  181. _, err := url.ParseRequestURI(value)
  182. return err
  183. }
  184. func mangleInvalidArgumentValues(req *ExecutionRequest) {
  185. for _, arg := range req.Action.Arguments {
  186. if arg.Type == "datetime" {
  187. mangleInvalidDatetimeValues(req, &arg)
  188. }
  189. }
  190. }
  191. func mangleInvalidDatetimeValues(req *ExecutionRequest, arg *config.ActionArgument) {
  192. value, exists := req.Arguments[arg.Name]
  193. if !exists || value == "" {
  194. return
  195. }
  196. timestamp, err := time.Parse("2006-01-02T15:04", value)
  197. if err == nil {
  198. log.WithFields(log.Fields{
  199. "arg": arg.Name,
  200. "value": value,
  201. "actionTitle": req.Action.Title,
  202. }).Warnf("Mangled invalid datetime value without seconds to :00 seconds, this issue is commonly caused by Android browsers.")
  203. req.Arguments[arg.Name] = timestamp.Format("2006-01-02T15:04:05")
  204. }
  205. }