arguments.go 12 KB

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