arguments.go 3.9 KB

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