arguments.go 12 KB

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