arguments.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. redactedShellCommand := redactShellCommand(parsedShellCommand, action.Arguments, values)
  57. if err != nil {
  58. return "", err
  59. }
  60. log.WithFields(log.Fields{
  61. "actionTitle": action.Title,
  62. "cmd": redactedShellCommand,
  63. }).Infof("Action parse args - After")
  64. return parsedShellCommand, nil
  65. }
  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 errors.New("Action arg not defined: " + 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 errors.New("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 errors.New("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 errors.New("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 errors.New("argument type not implemented " + 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 errors.New(fmt.Sprintf("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. mangleInvalidDatetimeValues(req)
  186. }
  187. func mangleInvalidDatetimeValues(req *ExecutionRequest) {
  188. for _, arg := range req.Action.Arguments {
  189. if arg.Type == "datetime" {
  190. value, exists := req.Arguments[arg.Name]
  191. if !exists || value == "" {
  192. continue
  193. }
  194. timestamp, err := time.Parse("2006-01-02T15:04", value)
  195. if err == nil {
  196. log.WithFields(log.Fields {
  197. "arg": arg.Name,
  198. "value": value,
  199. "actionTitle": req.Action.Title,
  200. }).Warnf("Mangled invalid datetime value without seconds to :00 seconds, this issue is commonly caused by Android browsers.")
  201. req.Arguments[arg.Name] = timestamp.Format("2006-01-02T15:04:05")
  202. }
  203. }
  204. }
  205. }