arguments.go 2.9 KB

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