arguments.go 2.9 KB

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