arguments.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package executor
  2. import (
  3. config "github.com/OliveTin/OliveTin/internal/config"
  4. "github.com/OliveTin/OliveTin/internal/entities"
  5. log "github.com/sirupsen/logrus"
  6. "fmt"
  7. "net/mail"
  8. "net/url"
  9. "regexp"
  10. "strings"
  11. "time"
  12. )
  13. var (
  14. typecheckRegex = map[string]string{
  15. "very_dangerous_raw_string": "",
  16. "int": `^\d+$`,
  17. "unicode_identifier": `^[\w\-\.\_\d]+$`,
  18. "ascii": `^[a-zA-Z0-9]+$`,
  19. "ascii_identifier": `^[a-zA-Z0-9\-\._]+$`,
  20. "ascii_sentence": `^[a-zA-Z0-9\-\._, ]+$`,
  21. }
  22. )
  23. func parseCommandForReplacements(shellCommand string, values map[string]string, entity any) (string, error) {
  24. r := regexp.MustCompile(`{{ *?([a-zA-Z0-9_]+?) *?}}`)
  25. foundArgumentNames := r.FindAllStringSubmatch(shellCommand, -1)
  26. for _, match := range foundArgumentNames {
  27. argName := match[1]
  28. argValue, argProvided := values[argName]
  29. if !argProvided {
  30. return "", fmt.Errorf("required arg not provided: %v", argName)
  31. }
  32. shellCommand = strings.ReplaceAll(shellCommand, match[0], argValue)
  33. }
  34. return shellCommand, nil
  35. }
  36. func parseActionExec(values map[string]string, action *config.Action, entity *entities.Entity) ([]string, error) {
  37. if action == nil {
  38. return nil, fmt.Errorf("action is nil")
  39. }
  40. if err := validateArguments(values, action); err != nil {
  41. return nil, err
  42. }
  43. parsed := make([]string, len(action.Exec))
  44. for i, a := range action.Exec {
  45. out, err := parseSingleExec(a, values, entity)
  46. if err != nil {
  47. return nil, err
  48. }
  49. parsed[i] = out
  50. }
  51. logParsedExec(action, parsed, values)
  52. return parsed, nil
  53. }
  54. func parseSingleExec(a string, values map[string]string, entity *entities.Entity) (string, error) {
  55. arg, err := parseCommandForReplacements(a, values, entity)
  56. if err != nil {
  57. return "", err
  58. }
  59. return entities.ParseTemplateWithArgs(arg, entity, values), nil
  60. }
  61. func validateArguments(values map[string]string, action *config.Action) error {
  62. for _, arg := range action.Arguments {
  63. if err := typecheckActionArgument(&arg, values[arg.Name], action); err != nil {
  64. return err
  65. }
  66. log.WithFields(log.Fields{"name": arg.Name, "value": values[arg.Name]}).Debugf("Arg assigned")
  67. }
  68. return nil
  69. }
  70. func logParsedExec(action *config.Action, parsed []string, values map[string]string) {
  71. redacted := redactExecArgs(parsed, action.Arguments, values)
  72. log.WithFields(log.Fields{"actionTitle": action.Title, "cmd": redacted}).Infof("Action parse args - After (Exec)")
  73. }
  74. func parseActionArguments(values map[string]string, action *config.Action, entity *entities.Entity) (string, error) {
  75. log.WithFields(log.Fields{
  76. "actionTitle": action.Title,
  77. "cmd": action.Shell,
  78. }).Infof("Action parse args - Before")
  79. rawShellCommand, err := parseCommandForReplacements(action.Shell, values, entity)
  80. for _, arg := range action.Arguments {
  81. argName := arg.Name
  82. argValue := values[argName]
  83. err := typecheckActionArgument(&arg, argValue, action)
  84. if err != nil {
  85. return "", err
  86. }
  87. log.WithFields(log.Fields{
  88. "name": argName,
  89. "value": argValue,
  90. }).Debugf("Arg assigned")
  91. }
  92. parsedShellCommand := entities.ParseTemplateWithArgs(rawShellCommand, entity, values)
  93. redactedShellCommand := redactShellCommand(parsedShellCommand, action.Arguments, values)
  94. if err != nil {
  95. return "", err
  96. }
  97. log.WithFields(log.Fields{
  98. "actionTitle": action.Title,
  99. "cmd": redactedShellCommand,
  100. }).Infof("Action parse args - After")
  101. return parsedShellCommand, nil
  102. }
  103. //gocyclo:ignore
  104. func redactShellCommand(shellCommand string, arguments []config.ActionArgument, argumentValues map[string]string) string {
  105. for _, arg := range arguments {
  106. if arg.Type == "password" {
  107. argValue, exists := argumentValues[arg.Name]
  108. if !exists {
  109. log.Warnf("Redact shell command: Argument %s not found in values", arg.Name)
  110. continue
  111. }
  112. if argValue == "" {
  113. continue
  114. }
  115. shellCommand = strings.ReplaceAll(shellCommand, argValue, "<redacted>")
  116. }
  117. }
  118. return shellCommand
  119. }
  120. //gocyclo:ignore
  121. func redactExecArgs(execArgs []string, arguments []config.ActionArgument, argumentValues map[string]string) []string {
  122. redacted := make([]string, len(execArgs))
  123. for i, arg := range execArgs {
  124. redacted[i] = redactShellCommand(arg, arguments, argumentValues)
  125. }
  126. return redacted
  127. }
  128. func typecheckActionArgument(arg *config.ActionArgument, value string, action *config.Action) error {
  129. if arg.Type == "confirmation" {
  130. return nil
  131. }
  132. if arg.Name == "" {
  133. return fmt.Errorf("argument name cannot be empty")
  134. }
  135. return typecheckActionArgumentFound(value, action, arg)
  136. }
  137. func typecheckActionArgumentFound(value string, action *config.Action, arg *config.ActionArgument) error {
  138. if value == "" {
  139. return typecheckNull(arg)
  140. }
  141. if len(arg.Choices) > 0 {
  142. return typecheckChoice(value, arg)
  143. }
  144. return TypeSafetyCheck(arg.Name, value, arg.Type)
  145. }
  146. // TypeSafetyCheck checks argument values match a specific type. The types are
  147. // defined in typecheckRegex, and, you guessed it, uses regex to check for allowed
  148. // characters.
  149. //
  150. //gocyclo:ignore
  151. func TypeSafetyCheck(name string, value string, argumentType string) error {
  152. switch argumentType {
  153. case "password":
  154. return nil
  155. case "raw_string_multiline":
  156. return nil
  157. case "email":
  158. return typeSafetyCheckEmail(value)
  159. case "url":
  160. return typeSafetyCheckUrl(value)
  161. case "datetime":
  162. return typeSafetyCheckDatetime(value)
  163. }
  164. return typeSafetyCheckRegex(name, value, argumentType)
  165. }
  166. func typecheckNull(arg *config.ActionArgument) error {
  167. if arg.RejectNull {
  168. return fmt.Errorf("null values are not allowed")
  169. }
  170. return nil
  171. }
  172. func typecheckChoice(value string, arg *config.ActionArgument) error {
  173. if arg.Entity != "" {
  174. return typecheckChoiceEntity(value, arg)
  175. }
  176. for _, choice := range arg.Choices {
  177. if value == choice.Value {
  178. return nil
  179. }
  180. }
  181. return fmt.Errorf("argument value is not one of the predefined choices")
  182. }
  183. func typecheckChoiceEntity(value string, arg *config.ActionArgument) error {
  184. templateChoice := arg.Choices[0].Value
  185. for _, ent := range entities.GetEntityInstances(arg.Entity) {
  186. choice := entities.ParseTemplateWith(templateChoice, ent)
  187. if value == choice {
  188. return nil
  189. }
  190. }
  191. return fmt.Errorf("argument value cannot be found in entities")
  192. }
  193. func typeSafetyCheckEmail(value string) error {
  194. _, err := mail.ParseAddress(value)
  195. log.Errorf("Email check: %v, %v", err, value)
  196. if err != nil {
  197. return err
  198. }
  199. return nil
  200. }
  201. func typeSafetyCheckDatetime(value string) error {
  202. _, err := time.Parse("2006-01-02T15:04:05", value)
  203. if err != nil {
  204. return err
  205. }
  206. return nil
  207. }
  208. func typeSafetyCheckRegex(name string, value string, argumentType string) error {
  209. pattern := ""
  210. if strings.HasPrefix(argumentType, "regex:") {
  211. pattern = strings.Replace(argumentType, "regex:", "", 1)
  212. } else {
  213. found := false
  214. pattern, found = typecheckRegex[argumentType]
  215. if !found {
  216. return fmt.Errorf("argument type not implemented %v for arg: %v", argumentType, name)
  217. }
  218. }
  219. matches, _ := regexp.MatchString(pattern, value)
  220. if !matches {
  221. log.WithFields(log.Fields{
  222. "name": name,
  223. "value": value,
  224. "type": argumentType,
  225. "pattern": pattern,
  226. }).Warn("Arg type check safety failure")
  227. return fmt.Errorf("invalid argument %v, doesn't match %v", name, argumentType)
  228. }
  229. return nil
  230. }
  231. func typeSafetyCheckUrl(value string) error {
  232. _, err := url.ParseRequestURI(value)
  233. return err
  234. }
  235. func checkShellArgumentSafety(action *config.Action) error {
  236. if action.Shell == "" {
  237. return nil
  238. }
  239. unsafe := map[string]struct{}{"url": {}, "email": {}, "raw_string_multiline": {}, "very_dangerous_raw_string": {}}
  240. for _, arg := range action.Arguments {
  241. if _, bad := unsafe[arg.Type]; bad {
  242. return fmt.Errorf("unsafe argument type '%s' cannot be used with Shell execution. Use 'exec' instead. See https://docs.olivetin.app/action_execution/shellvsexec.html", arg.Type)
  243. }
  244. }
  245. return nil
  246. }
  247. func mangleInvalidArgumentValues(req *ExecutionRequest) {
  248. for _, arg := range req.Binding.Action.Arguments {
  249. if arg.Type == "datetime" {
  250. mangleInvalidDatetimeValues(req, &arg)
  251. }
  252. mangleCheckboxValues(req, &arg)
  253. }
  254. }
  255. func mangleCheckboxValues(req *ExecutionRequest, arg *config.ActionArgument) {
  256. if arg.Type != "checkbox" {
  257. return
  258. }
  259. log.Infof("Checking checkbox values for argument %s in action %s", arg.Name, req.Binding.Action.Title)
  260. for i, _ := range arg.Choices {
  261. choice := &arg.Choices[i]
  262. if req.Arguments[arg.Name] == choice.Title {
  263. log.WithFields(log.Fields{
  264. "arg": arg.Name,
  265. "oldValue": req.Arguments[arg.Name],
  266. "newValue": choice.Value,
  267. "actionTitle": req.Binding.Action.Title,
  268. }).Infof("Mangled checkbox value")
  269. req.Arguments[arg.Name] = choice.Value
  270. }
  271. }
  272. }
  273. func mangleInvalidDatetimeValues(req *ExecutionRequest, arg *config.ActionArgument) {
  274. value, exists := req.Arguments[arg.Name]
  275. if !exists || value == "" {
  276. return
  277. }
  278. timestamp, err := time.Parse("2006-01-02T15:04", value)
  279. if err == nil {
  280. log.WithFields(log.Fields{
  281. "arg": arg.Name,
  282. "value": value,
  283. "actionTitle": req.Binding.Action.Title,
  284. }).Warnf("Mangled invalid datetime value without seconds to :00 seconds, this issue is commonly caused by Android browsers.")
  285. req.Arguments[arg.Name] = timestamp.Format("2006-01-02T15:04:05")
  286. }
  287. }