arguments.go 3.8 KB

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