arguments.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. )
  11. var (
  12. typecheckRegex = map[string]string{
  13. "very_dangerous_raw_string": "",
  14. "int": "^[\\d]+$",
  15. "ascii": "^[a-zA-Z0-9]+$",
  16. "ascii_identifier": "^[a-zA-Z0-9\\-\\.\\_]+$",
  17. "ascii_sentence": "^[a-zA-Z0-9 \\,\\.]+$",
  18. }
  19. )
  20. func parseActionArguments(rawShellCommand string, values map[string]string, action *config.Action, actionTitle string, entityPrefix string) (string, error) {
  21. log.WithFields(log.Fields{
  22. "actionTitle": actionTitle,
  23. "cmd": rawShellCommand,
  24. }).Infof("Action parse args - Before")
  25. r := regexp.MustCompile("{{ *?([a-zA-Z0-9_]+?) *?}}")
  26. matches := r.FindAllStringSubmatch(rawShellCommand, -1)
  27. for _, match := range matches {
  28. argValue, argProvided := values[match[1]]
  29. if !argProvided {
  30. log.Infof("%v", values)
  31. return "", errors.New("Required arg not provided: " + match[1])
  32. }
  33. err := typecheckActionArgument(match[1], argValue, action)
  34. if err != nil {
  35. return "", err
  36. }
  37. log.WithFields(log.Fields{
  38. "name": match[1],
  39. "value": argValue,
  40. }).Debugf("Arg assigned")
  41. rawShellCommand = strings.ReplaceAll(rawShellCommand, match[0], argValue)
  42. }
  43. rawShellCommand = sv.ReplaceEntityVars(entityPrefix, rawShellCommand)
  44. log.WithFields(log.Fields{
  45. "actionTitle": actionTitle,
  46. "cmd": rawShellCommand,
  47. }).Infof("Action parse args - After")
  48. return rawShellCommand, nil
  49. }
  50. func typecheckActionArgument(name string, value string, action *config.Action) error {
  51. arg := action.FindArg(name)
  52. if arg == nil {
  53. return errors.New("Action arg not defined: " + name)
  54. }
  55. if len(arg.Choices) > 0 {
  56. return typecheckChoice(value, arg)
  57. }
  58. return TypeSafetyCheck(name, value, arg.Type)
  59. }
  60. func typecheckChoice(value string, arg *config.ActionArgument) error {
  61. for _, choice := range arg.Choices {
  62. if value == choice.Value {
  63. return nil
  64. }
  65. }
  66. return errors.New("argument value is not one of the predefined choices")
  67. }
  68. // TypeSafetyCheck checks argument values match a specific type. The types are
  69. // defined in typecheckRegex, and, you guessed it, uses regex to check for allowed
  70. // characters.
  71. func TypeSafetyCheck(name string, value string, argumentType string) error {
  72. if argumentType == "url" {
  73. return typeSafetyCheckUrl(name, value)
  74. }
  75. return typeSafetyCheckRegex(name, value, argumentType)
  76. }
  77. func typeSafetyCheckRegex(name string, value string, argumentType string) error {
  78. pattern, found := typecheckRegex[argumentType]
  79. if !found {
  80. return errors.New("argument type not implemented " + argumentType)
  81. }
  82. matches, _ := regexp.MatchString(pattern, value)
  83. if !matches {
  84. log.WithFields(log.Fields{
  85. "name": name,
  86. "value": value,
  87. "type": argumentType,
  88. }).Warn("Arg type check safety failure")
  89. return errors.New("invalid argument, doesn't match " + argumentType)
  90. }
  91. return nil
  92. }
  93. func typeSafetyCheckUrl(name string, value string) error {
  94. _, err := url.ParseRequestURI(value)
  95. return err
  96. }