arguments.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. dnsNameLabelPattern = regexp.MustCompile(`^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$`)
  25. dnsNameAllNumericPattern = regexp.MustCompile(`^[0-9]+$`)
  26. )
  27. // parseExecArray parses all exec arguments in the action.
  28. func parseExecArray(action *config.Action, values map[string]string, entity *entities.Entity) ([]string, error) {
  29. parsed := make([]string, len(action.Exec))
  30. for i, segment := range action.Exec {
  31. out, err := parseExecSegment(segment, values, entity)
  32. if err != nil {
  33. return nil, err
  34. }
  35. parsed[i] = out
  36. }
  37. return parsed, nil
  38. }
  39. func parseActionExec(values map[string]string, action *config.Action, entity *entities.Entity) ([]string, error) {
  40. if action == nil {
  41. return nil, fmt.Errorf("action is nil")
  42. }
  43. if err := validateArguments(values, action); err != nil {
  44. return nil, err
  45. }
  46. parsed, err := parseExecArray(action, values, entity)
  47. if err != nil {
  48. return nil, err
  49. }
  50. logParsedExec(action, parsed, values)
  51. return parsed, nil
  52. }
  53. func parseExecSegment(arg string, values map[string]string, entity *entities.Entity) (string, error) {
  54. return tpl.ParseTemplateWithActionContext(arg, entity, values)
  55. }
  56. func validateArguments(values map[string]string, action *config.Action) error {
  57. for _, arg := range action.Arguments {
  58. if err := typecheckActionArgument(&arg, values[arg.Name]); err != nil {
  59. return err
  60. }
  61. log.WithFields(log.Fields{"name": arg.Name, "value": values[arg.Name]}).Debugf("Arg assigned")
  62. }
  63. return nil
  64. }
  65. func logParsedExec(action *config.Action, parsed []string, values map[string]string) {
  66. redacted := redactExecArgs(parsed, action.Arguments, values)
  67. log.WithFields(log.Fields{"actionTitle": action.Title, "cmd": redacted}).Infof("Action parse args - After (Exec)")
  68. }
  69. func parseActionArguments(req *ExecutionRequest) (string, error) {
  70. log.WithFields(log.Fields{
  71. "actionTitle": req.Binding.Action.Title,
  72. "cmd": req.Binding.Action.Shell,
  73. }).Infof("Action parse args - Before")
  74. for _, arg := range req.Binding.Action.Arguments {
  75. argName := arg.Name
  76. argValue := req.Arguments[argName]
  77. err := typecheckActionArgument(&arg, argValue)
  78. if err != nil {
  79. return "", err
  80. }
  81. log.WithFields(log.Fields{
  82. "name": argName,
  83. "value": argValue,
  84. }).Debugf("Arg assigned")
  85. }
  86. parsedShellCommand, err := tpl.ParseTemplateWithActionContext(req.Binding.Action.Shell, req.Binding.Entity, req.Arguments)
  87. if err != nil {
  88. return "", err
  89. }
  90. redactedShellCommand := redactShellCommand(parsedShellCommand, req.Binding.Action.Arguments, req.Arguments)
  91. log.WithFields(log.Fields{
  92. "actionTitle": req.Binding.Action.Title,
  93. "cmd": redactedShellCommand,
  94. }).Infof("Action parse args - After")
  95. return parsedShellCommand, nil
  96. }
  97. //gocyclo:ignore
  98. func redactShellCommand(shellCommand string, arguments []config.ActionArgument, argumentValues map[string]string) string {
  99. for _, arg := range arguments {
  100. if arg.Type == "password" {
  101. argValue, exists := argumentValues[arg.Name]
  102. if !exists {
  103. log.Warnf("Redact shell command: Argument %s not found in values", arg.Name)
  104. continue
  105. }
  106. if argValue == "" {
  107. continue
  108. }
  109. shellCommand = strings.ReplaceAll(shellCommand, argValue, "<redacted>")
  110. }
  111. }
  112. return shellCommand
  113. }
  114. //gocyclo:ignore
  115. func redactExecArgs(execArgs []string, arguments []config.ActionArgument, argumentValues map[string]string) []string {
  116. redacted := make([]string, len(execArgs))
  117. for i, arg := range execArgs {
  118. redacted[i] = redactShellCommand(arg, arguments, argumentValues)
  119. }
  120. return redacted
  121. }
  122. func argumentSkipsValidation(arg *config.ActionArgument) bool {
  123. return arg.Type == "html"
  124. }
  125. func typecheckActionArgument(arg *config.ActionArgument, value string) error {
  126. if argumentSkipsValidation(arg) {
  127. return nil
  128. }
  129. if arg.Type == "confirmation" {
  130. return typecheckConfirmation(arg, value)
  131. }
  132. if arg.Name == "" {
  133. return fmt.Errorf("argument name cannot be empty")
  134. }
  135. return typecheckActionArgumentFound(value, arg)
  136. }
  137. // typecheckConfirmation allows unnamed confirmation args as UI-only gates.
  138. // Named confirmation values are only ever "0" or "1", matching the web UI.
  139. func typecheckConfirmation(arg *config.ActionArgument, value string) error {
  140. if arg.Name == "" {
  141. return nil
  142. }
  143. if value == "0" || value == "1" {
  144. return nil
  145. }
  146. return fmt.Errorf("argument %q of type confirmation must be \"0\" or \"1\"", arg.Name)
  147. }
  148. // ValidateArgument validates a single argument value using the same logic as the executor.
  149. // It applies mangling transformations and performs full validation including null checks,
  150. // choice validation, and type safety checks.
  151. func ValidateArgument(arg *config.ActionArgument, value string, action *config.Action) error {
  152. if arg == nil {
  153. return fmt.Errorf("ValidateArgument: arg is nil")
  154. }
  155. if action == nil {
  156. return fmt.Errorf("ValidateArgument: action is nil")
  157. }
  158. // Apply mangling transformations
  159. mangledValue := MangleArgumentValue(arg, value, action.Title)
  160. // Use the same validation path as the executor
  161. return typecheckActionArgument(arg, mangledValue)
  162. }
  163. func typecheckActionArgumentFound(value string, arg *config.ActionArgument) error {
  164. if value == "" {
  165. return typecheckNull(arg)
  166. }
  167. if arg.Type == "checklist" {
  168. return typecheckChecklist(value, arg)
  169. }
  170. if len(arg.Choices) > 0 {
  171. return typecheckChoice(value, arg)
  172. }
  173. return TypeSafetyCheck(arg.Name, value, arg.Type)
  174. }
  175. // TypeSafetyCheck checks argument values match a specific type. The types are
  176. // defined in typecheckRegex, and, you guessed it, uses regex to check for allowed
  177. // characters.
  178. //
  179. //gocyclo:ignore
  180. func TypeSafetyCheck(name string, value string, argumentType string) error {
  181. switch argumentType {
  182. case "password":
  183. return nil
  184. case "raw_string_multiline":
  185. return nil
  186. case "checkbox":
  187. return nil
  188. case "checklist":
  189. return nil
  190. case "email":
  191. return typeSafetyCheckEmail(value)
  192. case "url":
  193. return typeSafetyCheckUrl(value)
  194. case "datetime":
  195. return typeSafetyCheckDatetime(value)
  196. case "dnsname":
  197. return typeSafetyCheckDnsName(value)
  198. }
  199. return typeSafetyCheckRegex(name, value, argumentType)
  200. }
  201. func typecheckNull(arg *config.ActionArgument) error {
  202. if arg.RejectNull {
  203. return fmt.Errorf("null values are not allowed")
  204. }
  205. return nil
  206. }
  207. func typecheckChecklist(value string, arg *config.ActionArgument) error {
  208. if len(arg.Choices) == 0 {
  209. return fmt.Errorf("checklist argument %q requires choices", arg.Name)
  210. }
  211. segments, err := config.ParseChecklistValue(value)
  212. if err != nil {
  213. return err
  214. }
  215. return typecheckChecklistSegments(segments, arg)
  216. }
  217. func typecheckChecklistSegments(segments []string, arg *config.ActionArgument) error {
  218. for _, segment := range segments {
  219. if err := typecheckChecklistSegment(segment, arg); err != nil {
  220. return err
  221. }
  222. }
  223. return nil
  224. }
  225. func typecheckChecklistSegment(segment string, arg *config.ActionArgument) error {
  226. if segment == "" {
  227. return fmt.Errorf("checklist argument %q contains an empty segment", arg.Name)
  228. }
  229. return typecheckChoice(segment, arg)
  230. }
  231. func typecheckChoice(value string, arg *config.ActionArgument) error {
  232. if arg.Entity != "" {
  233. return typecheckChoiceEntity(value, arg)
  234. }
  235. for _, choice := range arg.Choices {
  236. if value == choice.Value {
  237. return nil
  238. }
  239. }
  240. return fmt.Errorf("argument value is not one of the predefined choices")
  241. }
  242. func typecheckChoiceEntity(value string, arg *config.ActionArgument) error {
  243. templateChoice := arg.Choices[0].Value
  244. for _, ent := range entities.GetEntityInstances(arg.Entity) {
  245. choice := tpl.ParseTemplateOfActionBeforeExec(templateChoice, ent)
  246. if value == choice {
  247. return nil
  248. }
  249. }
  250. return fmt.Errorf("argument value cannot be found in entities")
  251. }
  252. func typeSafetyCheckEmail(value string) error {
  253. _, err := mail.ParseAddress(value)
  254. if err != nil {
  255. log.WithField("type", "email").Debugf("Email argument type check failed")
  256. return err
  257. }
  258. return nil
  259. }
  260. // typeSafetyCheckDnsName validates a DNS hostname (RFC 1123 LDH labels).
  261. // Accepts short names (e.g. webserver) and FQDNs (e.g. webserver.example.com).
  262. // An optional trailing dot is allowed.
  263. func typeSafetyCheckDnsName(value string) error {
  264. hostname := strings.TrimSuffix(value, ".")
  265. if hostname == "" || len(hostname) > 253 {
  266. return fmt.Errorf("invalid dnsname length")
  267. }
  268. return typeSafetyCheckDnsNameLabels(strings.Split(hostname, "."))
  269. }
  270. func typeSafetyCheckDnsNameLabels(labels []string) error {
  271. for _, label := range labels {
  272. if !dnsNameLabelPattern.MatchString(label) {
  273. return fmt.Errorf("invalid dnsname label %q", label)
  274. }
  275. }
  276. tld := labels[len(labels)-1]
  277. if dnsNameAllNumericPattern.MatchString(tld) {
  278. return fmt.Errorf("dnsname top-level label must not be all-numeric")
  279. }
  280. return nil
  281. }
  282. func typeSafetyCheckDatetime(value string) error {
  283. _, err := time.Parse("2006-01-02T15:04:05", value)
  284. if err != nil {
  285. return err
  286. }
  287. return nil
  288. }
  289. func anchorCustomRegexPattern(pattern string) string {
  290. return "^(?:" + pattern + ")$"
  291. }
  292. func typeSafetyCheckRegex(name string, value string, argumentType string) error {
  293. pattern := ""
  294. isCustomRegex := strings.HasPrefix(argumentType, "regex:")
  295. if isCustomRegex {
  296. pattern = strings.TrimPrefix(argumentType, "regex:")
  297. pattern = anchorCustomRegexPattern(pattern)
  298. } else {
  299. found := false
  300. pattern, found = typecheckRegex[argumentType]
  301. if !found {
  302. return fmt.Errorf("argument type not implemented %v for arg: %v", argumentType, name)
  303. }
  304. }
  305. matches, _ := regexp.MatchString(pattern, value)
  306. if !matches {
  307. log.WithFields(log.Fields{
  308. "name": name,
  309. "value": value,
  310. "type": argumentType,
  311. "pattern": pattern,
  312. }).Warn("Arg type check safety failure")
  313. return fmt.Errorf("invalid argument %v, doesn't match %v", name, argumentType)
  314. }
  315. return nil
  316. }
  317. func typeSafetyCheckUrl(value string) error {
  318. parsed, err := url.ParseRequestURI(value)
  319. if err != nil {
  320. return err
  321. }
  322. scheme := strings.ToLower(parsed.Scheme)
  323. if scheme != "http" && scheme != "https" {
  324. return fmt.Errorf("url scheme %q is not allowed; only http and https are permitted", parsed.Scheme)
  325. }
  326. return nil
  327. }
  328. var shellUnsafeArgumentTypes = map[string]struct{}{
  329. "url": {},
  330. "email": {},
  331. "raw_string_multiline": {},
  332. "very_dangerous_raw_string": {},
  333. "password": {},
  334. "html": {},
  335. }
  336. func isUnsafeShellArgumentType(arg *config.ActionArgument) bool {
  337. if strings.HasPrefix(arg.Type, "regex:") {
  338. return true
  339. }
  340. _, inMap := shellUnsafeArgumentTypes[arg.Type]
  341. return inMap || (arg.Type == "checkbox" && len(arg.Choices) == 0)
  342. }
  343. func checkShellArgumentSafety(action *config.Action) error {
  344. if action.Shell == "" {
  345. return nil
  346. }
  347. for i := range action.Arguments {
  348. arg := &action.Arguments[i]
  349. if isUnsafeShellArgumentType(arg) {
  350. 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)
  351. }
  352. }
  353. return nil
  354. }
  355. func mangleInvalidArgumentValues(req *ExecutionRequest) {
  356. for _, arg := range req.Binding.Action.Arguments {
  357. if arg.Type == "datetime" {
  358. mangleInvalidDatetimeValues(req, &arg)
  359. }
  360. mangleCheckboxValues(req, &arg)
  361. mangleChecklistValues(req, &arg)
  362. }
  363. }
  364. func mangleCheckboxValues(req *ExecutionRequest, arg *config.ActionArgument) {
  365. if arg.Type != "checkbox" {
  366. return
  367. }
  368. log.Infof("Checking checkbox values for argument %s in action %s", arg.Name, req.Binding.Action.Title)
  369. for i, v := range arg.Choices {
  370. choice := &arg.Choices[i]
  371. if req.Arguments[arg.Name] == choice.Title {
  372. log.WithFields(log.Fields{
  373. "arg": arg.Name,
  374. "choice": v,
  375. "oldValue": req.Arguments[arg.Name],
  376. "newValue": choice.Value,
  377. "actionTitle": req.Binding.Action.Title,
  378. }).Infof("Mangled checkbox value")
  379. req.Arguments[arg.Name] = choice.Value
  380. }
  381. }
  382. }
  383. func mangleInvalidDatetimeValues(req *ExecutionRequest, arg *config.ActionArgument) {
  384. value, exists := req.Arguments[arg.Name]
  385. if !exists || value == "" {
  386. return
  387. }
  388. timestamp, err := time.Parse("2006-01-02T15:04", value)
  389. if err == nil {
  390. log.WithFields(log.Fields{
  391. "arg": arg.Name,
  392. "value": value,
  393. "actionTitle": req.Binding.Action.Title,
  394. }).Warnf("Mangled invalid datetime value without seconds to :00 seconds, this issue is commonly caused by Android browsers.")
  395. req.Arguments[arg.Name] = timestamp.Format("2006-01-02T15:04:05")
  396. }
  397. }
  398. // MangleArgumentValue applies mangling transformations to a single argument value.
  399. // This is used by the validation API to ensure the value matches what would be
  400. // used during actual execution.
  401. func MangleArgumentValue(arg *config.ActionArgument, value string, actionTitle string) string {
  402. if arg == nil {
  403. log.Debugf("MangleArgumentValue called with nil arg, returning value unchanged")
  404. return value
  405. }
  406. return mangleArgumentValueByType(arg, value, actionTitle)
  407. }
  408. func mangleArgumentValueByType(arg *config.ActionArgument, value string, actionTitle string) string {
  409. switch arg.Type {
  410. case "datetime":
  411. return mangleDatetimeValue(arg, value, actionTitle)
  412. case "checkbox":
  413. return mangleCheckboxValue(arg, value, actionTitle)
  414. case "checklist":
  415. return mangleChecklistValue(arg, value, actionTitle)
  416. default:
  417. return value
  418. }
  419. }
  420. func mangleDatetimeValue(arg *config.ActionArgument, value string, actionTitle string) string {
  421. if arg == nil {
  422. log.Debugf("mangleDatetimeValue called with nil arg, returning value unchanged")
  423. return value
  424. }
  425. if value == "" {
  426. return value
  427. }
  428. timestamp, err := time.Parse("2006-01-02T15:04", value)
  429. if err != nil {
  430. return value
  431. }
  432. log.WithFields(log.Fields{
  433. "arg": arg.Name,
  434. "value": value,
  435. "actionTitle": actionTitle,
  436. }).Warnf("Mangled invalid datetime value without seconds to :00 seconds, this issue is commonly caused by Android browsers.")
  437. return timestamp.Format("2006-01-02T15:04:05")
  438. }
  439. func mangleCheckboxValue(arg *config.ActionArgument, value string, actionTitle string) string {
  440. if arg == nil {
  441. log.Debugf("mangleCheckboxValue called with nil arg, returning value unchanged")
  442. return value
  443. }
  444. return mangleChoiceSegment(arg, value, actionTitle)
  445. }
  446. func mangleChecklistValues(req *ExecutionRequest, arg *config.ActionArgument) {
  447. if arg.Type != "checklist" {
  448. return
  449. }
  450. value, exists := req.Arguments[arg.Name]
  451. if !exists || value == "" {
  452. return
  453. }
  454. req.Arguments[arg.Name] = mangleChecklistValue(arg, value, req.Binding.Action.Title)
  455. }
  456. func mangleChecklistValue(arg *config.ActionArgument, value string, actionTitle string) string {
  457. if arg == nil || value == "" {
  458. return value
  459. }
  460. segments, err := config.ParseChecklistValue(value)
  461. if err != nil {
  462. return value
  463. }
  464. return mangleChecklistSegments(arg, segments, value, actionTitle)
  465. }
  466. func mangleChecklistSegments(arg *config.ActionArgument, segments []string, fallback string, actionTitle string) string {
  467. mangled := make([]string, len(segments))
  468. for i, segment := range segments {
  469. mangled[i] = mangleChecklistSegment(arg, segment, actionTitle)
  470. }
  471. formatted, err := config.FormatChecklistValue(mangled)
  472. if err != nil {
  473. return fallback
  474. }
  475. return formatted
  476. }
  477. func mangleChecklistSegment(arg *config.ActionArgument, segment string, actionTitle string) string {
  478. trimmed := strings.TrimSpace(segment)
  479. if trimmed == "" {
  480. return ""
  481. }
  482. return mangleChoiceSegment(arg, trimmed, actionTitle)
  483. }
  484. func mangleChoiceSegment(arg *config.ActionArgument, value string, actionTitle string) string {
  485. if mapped, ok := mangleChoiceSegmentEntity(arg, value, actionTitle); ok {
  486. return mapped
  487. }
  488. return mangleChoiceSegmentStatic(arg, value, actionTitle)
  489. }
  490. func mangleChoiceSegmentEntity(arg *config.ActionArgument, value string, actionTitle string) (string, bool) {
  491. if arg.Entity == "" || len(arg.Choices) == 0 {
  492. return value, false
  493. }
  494. return mangleEntityTemplateChoiceSegment(arg.Choices[0], arg.Entity, arg.Name, value, actionTitle)
  495. }
  496. func mangleEntityTemplateChoiceSegment(templateChoice config.ActionArgumentChoice, entityName string, argName string, value string, actionTitle string) (string, bool) {
  497. for _, ent := range entities.GetEntityInstancesOrdered(entityName) {
  498. expandedTitle := tpl.ParseTemplateOfActionBeforeExec(templateChoice.Title, ent)
  499. if value != expandedTitle {
  500. continue
  501. }
  502. expandedValue := tpl.ParseTemplateOfActionBeforeExec(templateChoice.Value, ent)
  503. log.WithFields(log.Fields{
  504. "arg": argName,
  505. "oldValue": value,
  506. "newValue": expandedValue,
  507. "actionTitle": actionTitle,
  508. }).Infof("Mangled entity choice segment")
  509. return expandedValue, true
  510. }
  511. return value, false
  512. }
  513. func mangleChoiceSegmentStatic(arg *config.ActionArgument, value string, actionTitle string) string {
  514. for _, choice := range arg.Choices {
  515. if value == choice.Title {
  516. log.WithFields(log.Fields{
  517. "arg": arg.Name,
  518. "oldValue": value,
  519. "newValue": choice.Value,
  520. "actionTitle": actionTitle,
  521. }).Infof("Mangled choice segment")
  522. return choice.Value
  523. }
  524. }
  525. return value
  526. }