arguments.go 12 KB

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