4
0

executor_test.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. package executor
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "github.com/OliveTin/OliveTin/internal/auth"
  11. authpublic "github.com/OliveTin/OliveTin/internal/auth/authpublic"
  12. config "github.com/OliveTin/OliveTin/internal/config"
  13. "github.com/OliveTin/OliveTin/internal/entities"
  14. )
  15. func testingExecutor() (*Executor, *config.Config) {
  16. cfg := config.DefaultConfig()
  17. e := DefaultExecutor(cfg)
  18. a1 := &config.Action{
  19. Title: "Do some tickles",
  20. Shell: "echo 'Tickling {{ person }}'",
  21. Arguments: []config.ActionArgument{
  22. {
  23. Name: "person",
  24. Type: "ascii",
  25. },
  26. },
  27. }
  28. cfg.Actions = append(cfg.Actions, a1)
  29. cfg.Sanitize()
  30. return e, cfg
  31. }
  32. func TestCreateExecutorAndExec(t *testing.T) {
  33. e, cfg := testingExecutor()
  34. req := ExecutionRequest{
  35. AuthenticatedUser: &authpublic.AuthenticatedUser{Username: "MrTickle"},
  36. Cfg: cfg,
  37. Arguments: map[string]string{
  38. "person": "yourself",
  39. },
  40. }
  41. // Ensure bindings are available and set the binding to the only configured action
  42. e.RebuildActionMap()
  43. if len(cfg.Actions) > 0 {
  44. req.Binding = e.FindBindingWithNoEntity(cfg.Actions[0])
  45. }
  46. assert.NotNil(t, e, "Create an executor")
  47. wg, _ := e.ExecRequest(&req)
  48. wg.Wait()
  49. assert.Equal(t, int32(0), req.logEntry.ExitCode, "Exit code is zero")
  50. }
  51. func TestStepRequestActionPopulateLogEntryResolvesEntityTemplates(t *testing.T) {
  52. req := &ExecutionRequest{
  53. logEntry: &InternalLogEntry{},
  54. Binding: &ActionBinding{
  55. Action: &config.Action{
  56. Title: "Do something with {{ project.name }}",
  57. Icon: "{{ project.icon }}",
  58. },
  59. Entity: &entities.Entity{
  60. Data: map[string]any{
  61. "name": "foo",
  62. "icon": "🐰",
  63. },
  64. UniqueKey: "foo-key",
  65. },
  66. },
  67. }
  68. stepRequestActionPopulateLogEntry(req)
  69. assert.Equal(t, "Do something with foo", req.logEntry.ActionTitle)
  70. assert.Equal(t, "🐰", req.logEntry.ActionIcon)
  71. assert.Equal(t, "Do something with {{ project.name }}", req.logEntry.ActionConfigTitle)
  72. assert.Equal(t, "foo-key", req.logEntry.EntityPrefix)
  73. }
  74. func TestExecNonExistant(t *testing.T) {
  75. e, cfg := testingExecutor()
  76. req := ExecutionRequest{
  77. // Binding: e.FindBindingWithNoEntity("waffles"),
  78. logEntry: &InternalLogEntry{},
  79. Cfg: cfg,
  80. }
  81. wg, _ := e.ExecRequest(&req)
  82. wg.Wait()
  83. assert.Equal(t, int32(-1337), req.logEntry.ExitCode, "Log entry is set to an internal error code")
  84. assert.Equal(t, "💩", req.logEntry.ActionIcon, "Log entry icon is a poop (not found)")
  85. }
  86. func TestArgumentNameCamelCase(t *testing.T) {
  87. req := newExecRequest()
  88. req.Binding.Action = &config.Action{
  89. Title: "Do some tickles",
  90. Shell: "echo 'Tickling {{ personName }}'",
  91. Arguments: []config.ActionArgument{
  92. {
  93. Name: "personName",
  94. Type: "ascii",
  95. },
  96. },
  97. }
  98. req.Arguments = map[string]string{
  99. "personName": "Fred",
  100. }
  101. out, err := parseActionArguments(req)
  102. assert.Equal(t, "echo 'Tickling Fred'", out)
  103. assert.Nil(t, err)
  104. }
  105. func TestArgumentNameSnakeCase(t *testing.T) {
  106. req := newExecRequest()
  107. req.Binding.Action = &config.Action{
  108. Title: "Do some tickles",
  109. Shell: "echo 'Tickling {{ person_name }}'",
  110. Arguments: []config.ActionArgument{
  111. {
  112. Name: "person_name",
  113. Type: "ascii",
  114. },
  115. },
  116. }
  117. req.Arguments = map[string]string{
  118. "person_name": "Fred",
  119. }
  120. out, err := parseActionArguments(req)
  121. assert.Equal(t, "echo 'Tickling Fred'", out)
  122. assert.Nil(t, err)
  123. }
  124. func TestGetLogsEmpty(t *testing.T) {
  125. e, cfg := testingExecutor()
  126. assert.Equal(t, int64(10), cfg.LogHistoryPageSize, "Logs page size should be 10")
  127. logs, paging := e.GetLogTrackingIds(0, 10)
  128. assert.NotNil(t, logs, "Logs should not be nil")
  129. assert.Equal(t, 0, len(logs), "No logs yet")
  130. assert.Equal(t, int64(0), paging.CountRemaining, "There should be no remaining logs")
  131. }
  132. func TestGetLogsLessThanPageSize(t *testing.T) {
  133. e, cfg := testingExecutor()
  134. cfg.Actions = append(cfg.Actions, &config.Action{
  135. Title: "blat",
  136. Shell: "date",
  137. })
  138. cfg.Sanitize()
  139. // Rebuild action map to include newly added action
  140. e.RebuildActionMap()
  141. assert.Equal(t, int64(10), cfg.LogHistoryPageSize, "Logs page size should be 10")
  142. logEntries, paging := e.GetLogTrackingIds(0, 10)
  143. assert.Equal(t, 0, len(logEntries), "There should be 0 logs")
  144. assert.Zero(t, paging.CountRemaining, "There should be no remaining logs")
  145. execNewReqAndWait(e, "blat", cfg)
  146. execNewReqAndWait(e, "blat", cfg)
  147. execNewReqAndWait(e, "blat", cfg)
  148. execNewReqAndWait(e, "blat", cfg)
  149. execNewReqAndWait(e, "blat", cfg)
  150. execNewReqAndWait(e, "blat", cfg)
  151. execNewReqAndWait(e, "blat", cfg)
  152. logEntries, paging = e.GetLogTrackingIds(0, 10)
  153. assert.Equal(t, 7, len(logEntries), "There should be 7 logs")
  154. assert.Zero(t, paging.CountRemaining, "There should be no remaining logs")
  155. execNewReqAndWait(e, "blat", cfg)
  156. execNewReqAndWait(e, "blat", cfg)
  157. execNewReqAndWait(e, "blat", cfg)
  158. execNewReqAndWait(e, "blat", cfg)
  159. execNewReqAndWait(e, "blat", cfg)
  160. logEntries, paging = e.GetLogTrackingIds(0, 10)
  161. assert.Equal(t, 10, len(logEntries), "There should be 10 logs")
  162. assert.Equal(t, int64(2), paging.CountRemaining, "There should be 1 remaining logs")
  163. }
  164. func execNewReqAndWait(e *Executor, title string, cfg *config.Config) {
  165. req := &ExecutionRequest{
  166. // ActionTitle: title,
  167. Cfg: cfg,
  168. }
  169. // Ensure we have a binding for the requested title
  170. e.RebuildActionMap()
  171. var action *config.Action
  172. for _, a := range cfg.Actions {
  173. if a.Title == title {
  174. action = a
  175. break
  176. }
  177. }
  178. if action != nil {
  179. req.Binding = e.FindBindingWithNoEntity(action)
  180. }
  181. wg, _ := e.ExecRequest(req)
  182. wg.Wait()
  183. }
  184. func TestGetPagingIndexes(t *testing.T) {
  185. assert.Zero(t, getPagingStartIndex(5, 0), "Testing start index from empty list")
  186. assert.Equal(t, int64(4), getPagingStartIndex(5, 10), "Testing start index from mid point")
  187. assert.Equal(t, int64(9), getPagingStartIndex(-1, 10), "Testing start index with negative offset")
  188. assert.Equal(t, int64(0), getPagingStartIndex(15, 10), "Testing start index with large offset")
  189. assert.Equal(t, int64(9), getPagingStartIndex(0, 10), "Testing start index with zero count")
  190. }
  191. func TestUnsetRequiredArgument(t *testing.T) {
  192. req := newExecRequest()
  193. req.Binding.Action = &config.Action{
  194. Title: "Print your name",
  195. Shell: "echo 'Your name is: {{ name }}'",
  196. Arguments: []config.ActionArgument{
  197. {
  198. Name: "name",
  199. Type: "ascii",
  200. },
  201. },
  202. }
  203. req.Arguments = map[string]string{}
  204. out, err := parseActionArguments(req)
  205. assert.Equal(t, "", out)
  206. assert.NotNil(t, err)
  207. }
  208. func TestUnusedArgumentStillPassesTypeSafetyCheck(t *testing.T) {
  209. req := newExecRequest()
  210. req.Binding.Action = &config.Action{
  211. Title: "Print your name",
  212. Shell: "echo 'Your name is: {{ name }}'",
  213. Arguments: []config.ActionArgument{
  214. {
  215. Name: "name",
  216. Type: "ascii",
  217. },
  218. {
  219. Name: "age",
  220. Type: "int",
  221. },
  222. },
  223. }
  224. req.Arguments = map[string]string{
  225. "name": "Fred",
  226. "age": "Not an integer",
  227. }
  228. out, err := parseActionArguments(req)
  229. assert.Equal(t, "", out)
  230. assert.NotNil(t, err)
  231. }
  232. // https://github.com/OliveTin/OliveTin/issues/564
  233. func TestMangleInvalidArgumentValues(t *testing.T) {
  234. e, cfg := testingExecutor()
  235. a1 := &config.Action{
  236. Title: "Validate my date without seconds because I am from an Android phone",
  237. Shell: "echo 'The date is: {{ date }}'",
  238. Arguments: []config.ActionArgument{
  239. {
  240. Name: "date",
  241. Type: "datetime",
  242. },
  243. },
  244. }
  245. cfg.Actions = append(cfg.Actions, a1)
  246. cfg.Sanitize()
  247. // Build bindings for newly added action
  248. e.RebuildActionMap()
  249. req := ExecutionRequest{
  250. // Action: a1,
  251. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  252. Cfg: cfg,
  253. Arguments: map[string]string{
  254. "date": "1990-01-10T12:00", // Invalid format, should be without seconds
  255. },
  256. }
  257. // Set binding to our appended action
  258. req.Binding = e.FindBindingWithNoEntity(a1)
  259. wg, _ := e.ExecRequest(&req)
  260. wg.Wait()
  261. assert.NotNil(t, req.logEntry, "Log entry should not be nil")
  262. assert.Equal(t, req.logEntry.Output, "The date is: 1990-01-10T12:00:00\n", "Date should be mangled to a valid format")
  263. }
  264. func TestWebhookRejectsShellExecution(t *testing.T) {
  265. cfg := config.DefaultConfig()
  266. e := DefaultExecutor(cfg)
  267. a1 := &config.Action{
  268. Title: "Webhook Shell Reject",
  269. Shell: "echo '{{ msg }}'",
  270. Arguments: []config.ActionArgument{
  271. {Name: "msg", Type: "ascii"},
  272. },
  273. }
  274. cfg.Actions = append(cfg.Actions, a1)
  275. cfg.Sanitize()
  276. e.RebuildActionMap()
  277. req := ExecutionRequest{
  278. Tags: []string{"webhook"},
  279. AuthenticatedUser: auth.UserFromSystem(cfg, "webhook"),
  280. Cfg: cfg,
  281. Arguments: map[string]string{"msg": "hello"},
  282. Binding: e.FindBindingWithNoEntity(a1),
  283. }
  284. wg, _ := e.ExecRequest(&req)
  285. wg.Wait()
  286. assert.NotNil(t, req.logEntry)
  287. assert.Equal(t, int32(-1337), req.logEntry.ExitCode)
  288. assert.Contains(t, req.logEntry.Output, "webhooks cannot use Shell execution")
  289. }
  290. func TestWebhookAllowsExecExecution(t *testing.T) {
  291. cfg := config.DefaultConfig()
  292. e := DefaultExecutor(cfg)
  293. a1 := &config.Action{
  294. Title: "Webhook Exec OK",
  295. Exec: []string{"echo", "{{ msg }}"},
  296. Arguments: []config.ActionArgument{
  297. {Name: "msg", Type: "ascii"},
  298. },
  299. }
  300. cfg.Actions = append(cfg.Actions, a1)
  301. cfg.Sanitize()
  302. e.RebuildActionMap()
  303. req := ExecutionRequest{
  304. Tags: []string{"webhook"},
  305. AuthenticatedUser: auth.UserFromSystem(cfg, "webhook"),
  306. Cfg: cfg,
  307. Arguments: map[string]string{"msg": "hello"},
  308. Binding: e.FindBindingWithNoEntity(a1),
  309. }
  310. wg, _ := e.ExecRequest(&req)
  311. wg.Wait()
  312. assert.NotNil(t, req.logEntry)
  313. assert.Equal(t, int32(0), req.logEntry.ExitCode)
  314. assert.Contains(t, req.logEntry.Output, "hello")
  315. }
  316. func TestWebhookRejectsShellAfterCompleted(t *testing.T) {
  317. cfg := config.DefaultConfig()
  318. e := DefaultExecutor(cfg)
  319. a1 := &config.Action{
  320. Title: "Webhook After Shell Reject",
  321. Exec: []string{"echo", "{{ msg }}"},
  322. ShellAfterCompleted: "echo after",
  323. Arguments: []config.ActionArgument{
  324. {Name: "msg", Type: "ascii"},
  325. },
  326. }
  327. cfg.Actions = append(cfg.Actions, a1)
  328. cfg.Sanitize()
  329. e.RebuildActionMap()
  330. req := ExecutionRequest{
  331. Tags: []string{"webhook"},
  332. AuthenticatedUser: auth.UserFromSystem(cfg, "webhook"),
  333. Cfg: cfg,
  334. Arguments: map[string]string{"msg": "hello"},
  335. Binding: e.FindBindingWithNoEntity(a1),
  336. }
  337. wg, _ := e.ExecRequest(&req)
  338. wg.Wait()
  339. assert.NotNil(t, req.logEntry)
  340. assert.Contains(t, req.logEntry.Output, "webhooks cannot use shellAfterCompleted")
  341. }
  342. func TestShellAfterCompletedUsesOutputEnvSafely(t *testing.T) {
  343. cfg := config.DefaultConfig()
  344. e := DefaultExecutor(cfg)
  345. injectedPath := filepath.Join(t.TempDir(), "olivetin-injected")
  346. expectedMainOutput := "'; touch " + injectedPath + "; echo '"
  347. a1 := &config.Action{
  348. Title: "After completion escape",
  349. Shell: "printf %s \"" + expectedMainOutput + "\"",
  350. ShellAfterCompleted: "printf %s {{ output }}",
  351. }
  352. cfg.Actions = append(cfg.Actions, a1)
  353. cfg.Sanitize()
  354. e.RebuildActionMap()
  355. req := ExecutionRequest{
  356. AuthenticatedUser: auth.UserFromSystem(cfg, "cron"),
  357. Cfg: cfg,
  358. Binding: e.FindBindingWithNoEntity(a1),
  359. }
  360. wg, _ := e.ExecRequest(&req)
  361. wg.Wait()
  362. assert.NotNil(t, req.logEntry)
  363. assert.Equal(t, int32(0), req.logEntry.ExitCode)
  364. assert.True(t, strings.HasPrefix(req.logEntry.Output, expectedMainOutput))
  365. assert.Contains(t, req.logEntry.Output, "OliveTin::shellAfterCompleted stdout\n"+expectedMainOutput)
  366. _, err := os.Stat(injectedPath)
  367. assert.True(t, os.IsNotExist(err), "shellAfterCompleted must not execute injected commands from output")
  368. }
  369. func TestShellAfterCompletedExpandsQuotedPlaceholders(t *testing.T) {
  370. cases := []struct {
  371. name string
  372. sac string
  373. }{
  374. {"legacy single-quoted", `printf '%s' '{{ output }}'`},
  375. {"modern single-quoted", `printf '%s' '{{ .Arguments.output }}'`},
  376. }
  377. for _, tc := range cases {
  378. t.Run(tc.name, func(t *testing.T) {
  379. cfg := config.DefaultConfig()
  380. executor := DefaultExecutor(cfg)
  381. mainOutput := "quoted-output-ok"
  382. action := &config.Action{
  383. Title: "sac-quoted-" + tc.name,
  384. Shell: "printf %s \"" + mainOutput + "\"",
  385. ShellAfterCompleted: tc.sac,
  386. }
  387. cfg.Actions = append(cfg.Actions, action)
  388. cfg.Sanitize()
  389. executor.RebuildActionMap()
  390. req := ExecutionRequest{
  391. AuthenticatedUser: auth.UserFromSystem(cfg, "cron"),
  392. Cfg: cfg,
  393. Binding: executor.FindBindingWithNoEntity(action),
  394. }
  395. wg, _ := executor.ExecRequest(&req)
  396. wg.Wait()
  397. require.NotNil(t, req.logEntry)
  398. assert.Equal(t, int32(0), req.logEntry.ExitCode)
  399. assert.Contains(t, req.logEntry.Output, "OliveTin::shellAfterCompleted stdout\n"+mainOutput)
  400. })
  401. }
  402. }
  403. func TestShellAfterCompletedBlocksArgumentsOutputInjection(t *testing.T) {
  404. payload := func(injectedPath string) string {
  405. return "x; touch " + injectedPath + "; #"
  406. }
  407. cases := []struct {
  408. name string
  409. sac string
  410. }{
  411. {"legacy", "printf %s {{ output }}"},
  412. {"legacy compact", "printf %s {{output}}"},
  413. {"legacy extra spaces", "printf %s {{ output }}"},
  414. {"modern Arguments", "printf %s {{ .Arguments.output }}"},
  415. {"modern compact", "printf %s {{.Arguments.output}}"},
  416. {"modern exitCode still env", "printf %s {{ .Arguments.exitCode }}"},
  417. }
  418. for _, tc := range cases {
  419. t.Run(tc.name, func(t *testing.T) {
  420. cfg := config.DefaultConfig()
  421. executor := DefaultExecutor(cfg)
  422. injectedPath := filepath.Join(t.TempDir(), "injected")
  423. mainPayload := payload(injectedPath)
  424. action := &config.Action{
  425. Title: "sac-injection-" + tc.name,
  426. Shell: "printf %s \"" + mainPayload + "\"",
  427. ShellAfterCompleted: tc.sac,
  428. }
  429. cfg.Actions = append(cfg.Actions, action)
  430. cfg.Sanitize()
  431. executor.RebuildActionMap()
  432. req := ExecutionRequest{
  433. AuthenticatedUser: auth.UserFromSystem(cfg, "cron"),
  434. Cfg: cfg,
  435. Binding: executor.FindBindingWithNoEntity(action),
  436. }
  437. wg, _ := executor.ExecRequest(&req)
  438. wg.Wait()
  439. _, err := os.Stat(injectedPath)
  440. assert.True(t, os.IsNotExist(err), "shellAfterCompleted must not execute injected commands via %q", tc.sac)
  441. })
  442. }
  443. }
  444. func TestSubstituteShellAfterCompletedEnvRefs(t *testing.T) {
  445. cases := []struct {
  446. in string
  447. want string
  448. }{
  449. {`printf %s {{ output }}`, `printf %s "$OUTPUT"`},
  450. {`printf %s {{output}}`, `printf %s "$OUTPUT"`},
  451. {`printf %s {{ output }}`, `printf %s "$OUTPUT"`},
  452. {`printf %s {{ .Arguments.output }}`, `printf %s "$OUTPUT"`},
  453. {`printf %s {{.Arguments.output}}`, `printf %s "$OUTPUT"`},
  454. {`echo {{ exitCode }}`, `echo "$EXITCODE"`},
  455. {`echo {{ .Arguments.exitCode }}`, `echo "$EXITCODE"`},
  456. {`echo {{ .Arguments.exitCode }}`, `echo "$EXITCODE"`},
  457. {`printf '%s' '{{ output }}'`, `printf '%s' ''"$OUTPUT"''`},
  458. {`printf '%s' '{{ .Arguments.output }}'`, `printf '%s' ''"$OUTPUT"''`},
  459. {`printf '%s' '{{ exitCode }}'`, `printf '%s' ''"$EXITCODE"''`},
  460. {`printf '%s' '{{ .Arguments.exitCode }}'`, `printf '%s' ''"$EXITCODE"''`},
  461. }
  462. for _, tc := range cases {
  463. assert.Equal(t, tc.want, substituteShellAfterCompletedEnvRefs(tc.in))
  464. }
  465. }
  466. func TestShellAfterTemplateArgsOmitsOutputAndExitCode(t *testing.T) {
  467. args := map[string]string{
  468. "output": "evil; id",
  469. "exitCode": "1",
  470. "ot_username": "alice",
  471. "ot_executionTrackingId": "track-1",
  472. }
  473. templateArgs := shellAfterTemplateArgs(args)
  474. assert.NotContains(t, templateArgs, "output")
  475. assert.NotContains(t, templateArgs, "exitCode")
  476. assert.Equal(t, "alice", templateArgs["ot_username"])
  477. assert.Equal(t, "track-1", templateArgs["ot_executionTrackingId"])
  478. assert.Equal(t, "evil; id", args["output"], "env args map must keep output for OUTPUT=")
  479. }
  480. func TestFilterToDefinedArgumentsOnly(t *testing.T) {
  481. req := newExecRequest()
  482. req.Binding.Action = &config.Action{
  483. Title: "Filter test",
  484. Shell: "echo '{{ name }}'",
  485. Arguments: []config.ActionArgument{
  486. {Name: "name", Type: "ascii"},
  487. },
  488. }
  489. req.Arguments = map[string]string{
  490. "name": "Alice",
  491. "webhook_path": "/malicious/$(id)",
  492. "extra_undefined": "ignored",
  493. }
  494. filterToDefinedArgumentsOnly(req)
  495. assert.Equal(t, "Alice", req.Arguments["name"])
  496. assert.Empty(t, req.Arguments["webhook_path"])
  497. assert.Empty(t, req.Arguments["extra_undefined"])
  498. }
  499. func TestFilterToDefinedArgumentsDropsReservedPrefixArgs(t *testing.T) {
  500. req := newExecRequest()
  501. req.Binding.Action = &config.Action{
  502. Title: "Filter test",
  503. Shell: "echo test",
  504. Arguments: []config.ActionArgument{},
  505. }
  506. req.Arguments = map[string]string{
  507. "ot_executionTrackingId": "track-123",
  508. "ot_username": "webhook",
  509. }
  510. filterToDefinedArgumentsOnly(req)
  511. assert.Empty(t, req.Arguments["ot_executionTrackingId"])
  512. assert.Empty(t, req.Arguments["ot_username"])
  513. }
  514. func TestStepParseArgsInjectsSystemArgsAfterFiltering(t *testing.T) {
  515. req := newExecRequest()
  516. req.TrackingID = "server-track-456"
  517. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice"}
  518. req.Binding.Action = &config.Action{
  519. Title: "Filter then inject",
  520. Shell: "echo test",
  521. Arguments: []config.ActionArgument{
  522. {Name: "name", Type: "ascii"},
  523. },
  524. }
  525. req.Arguments = map[string]string{
  526. "name": "Alice",
  527. "ot_executionTrackingId": "attacker-track",
  528. "ot_username": "mallory",
  529. "ot_custom": "polluted",
  530. }
  531. assert.True(t, stepParseArgs(req))
  532. assert.Equal(t, "Alice", req.Arguments["name"])
  533. assert.Equal(t, "server-track-456", req.Arguments["ot_executionTrackingId"])
  534. assert.Equal(t, "alice", req.Arguments["ot_username"])
  535. assert.Empty(t, req.Arguments["ot_custom"])
  536. }
  537. func TestStepParseArgsDropsReservedPrefixArgsFromEnvironment(t *testing.T) {
  538. req := newExecRequest()
  539. req.TrackingID = "server-track-456"
  540. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  541. req.Binding.Action = &config.Action{
  542. Title: "No reserved prefix pollution",
  543. Shell: "echo test",
  544. Arguments: []config.ActionArgument{},
  545. }
  546. req.Arguments = map[string]string{
  547. "ot_custom": "polluted",
  548. }
  549. assert.True(t, stepParseArgs(req))
  550. env := buildEnv(req.Arguments)
  551. assert.False(t, containsEnvPrefix(env, "OT_CUSTOM="))
  552. assert.True(t, containsEnvPrefix(env, "OT_USERNAME=alice@example.com"))
  553. assert.True(t, containsEnvPrefix(env, "OT_EXECUTIONTRACKINGID=server-track-456"))
  554. }
  555. func TestSystemArgumentDefinitionsAreReservedAndShellSafe(t *testing.T) {
  556. unsafeTypes := map[string]struct{}{
  557. "email": {},
  558. "password": {},
  559. "raw_string_multiline": {},
  560. "url": {},
  561. "very_dangerous_raw_string": {},
  562. }
  563. seen := map[string]struct{}{}
  564. for _, arg := range systemArgumentDefinitions {
  565. assert.True(t, strings.HasPrefix(arg.Name, config.ReservedArgumentNamePrefix))
  566. assert.NotEmpty(t, arg.Type)
  567. assert.True(t, arg.RejectNull)
  568. _, duplicate := seen[arg.Name]
  569. assert.False(t, duplicate, "duplicate system argument definition %q", arg.Name)
  570. seen[arg.Name] = struct{}{}
  571. _, unsafe := unsafeTypes[arg.Type]
  572. assert.False(t, unsafe, "system argument %q uses unsafe type %q", arg.Name, arg.Type)
  573. }
  574. }
  575. func TestValidatedSystemArgsMatchesSystemArgumentDefinitions(t *testing.T) {
  576. req := newExecRequest()
  577. req.TrackingID = "server-track-456"
  578. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  579. args, err := validatedSystemArgs(req)
  580. assert.Nil(t, err)
  581. assert.Len(t, args, len(systemArgumentDefinitions))
  582. for _, arg := range systemArgumentDefinitions {
  583. assert.Contains(t, args, arg.Name)
  584. }
  585. }
  586. func TestBuildShellAfterArgsOnlyAddsExpectedNonSystemArgs(t *testing.T) {
  587. req := newExecRequest()
  588. req.logEntry = &InternalLogEntry{
  589. Output: "hello",
  590. ExitCode: 7,
  591. }
  592. req.TrackingID = "server-track-456"
  593. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  594. req.Binding.Action = &config.Action{ShellAfterCompleted: "echo test"}
  595. args, err := buildShellAfterArgs(req)
  596. assert.Nil(t, err)
  597. assert.Len(t, args, len(systemArgumentDefinitions)+2)
  598. assert.Contains(t, args, "output")
  599. assert.Contains(t, args, "exitCode")
  600. for _, arg := range systemArgumentDefinitions {
  601. assert.Contains(t, args, arg.Name)
  602. }
  603. }
  604. func TestStepParseArgsAllowsEmailUsernameSystemArg(t *testing.T) {
  605. req := newExecRequest()
  606. req.logEntry = &InternalLogEntry{}
  607. req.TrackingID = "server-track-456"
  608. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  609. req.Binding.Action = &config.Action{
  610. Title: "Email username",
  611. Shell: "echo test",
  612. Arguments: []config.ActionArgument{},
  613. }
  614. assert.True(t, stepParseArgs(req))
  615. assert.Equal(t, "alice@example.com", req.Arguments["ot_username"])
  616. }
  617. func TestStepParseArgsFailsWhenUsernameSystemArgIsInvalid(t *testing.T) {
  618. req := newExecRequest()
  619. req.logEntry = &InternalLogEntry{}
  620. req.TrackingID = "server-track-456"
  621. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice;id"}
  622. req.Binding.Action = &config.Action{
  623. Title: "Invalid system arg",
  624. Shell: "echo test",
  625. Arguments: []config.ActionArgument{},
  626. }
  627. assert.False(t, stepParseArgs(req))
  628. assert.Contains(t, req.logEntry.Output, `system argument "ot_username" failed validation`)
  629. assert.Empty(t, req.Arguments["ot_username"])
  630. }
  631. func TestStepParseArgsFailsWhenTrackingIDSystemArgIsInvalid(t *testing.T) {
  632. req := newExecRequest()
  633. req.logEntry = &InternalLogEntry{}
  634. req.TrackingID = "track/../../bad"
  635. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice"}
  636. req.Binding.Action = &config.Action{
  637. Title: "Invalid tracking ID",
  638. Shell: "echo test",
  639. Arguments: []config.ActionArgument{},
  640. }
  641. assert.False(t, stepParseArgs(req))
  642. assert.Contains(t, req.logEntry.Output, `system argument "ot_executionTrackingId" failed validation`)
  643. assert.Empty(t, req.Arguments["ot_executionTrackingId"])
  644. }
  645. func TestBuildShellAfterArgsUsesValidatedSystemArgs(t *testing.T) {
  646. req := newExecRequest()
  647. req.logEntry = &InternalLogEntry{
  648. Output: "hello",
  649. ExitCode: 7,
  650. }
  651. req.TrackingID = "server-track-456"
  652. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  653. req.Binding.Action = &config.Action{
  654. Title: "Shell after",
  655. ShellAfterCompleted: "echo test",
  656. }
  657. args, err := buildShellAfterArgs(req)
  658. assert.Nil(t, err)
  659. assert.Equal(t, "alice@example.com", args["ot_username"])
  660. assert.Equal(t, "server-track-456", args["ot_executionTrackingId"])
  661. assert.Equal(t, "hello", args["output"])
  662. assert.Equal(t, "7", args["exitCode"])
  663. }
  664. func TestBuildShellAfterArgsFailsWhenSystemArgIsInvalid(t *testing.T) {
  665. req := newExecRequest()
  666. req.logEntry = &InternalLogEntry{}
  667. req.TrackingID = "server-track-456"
  668. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice;id"}
  669. req.Binding.Action = &config.Action{
  670. Title: "Shell after invalid username",
  671. ShellAfterCompleted: "echo test",
  672. }
  673. args, err := buildShellAfterArgs(req)
  674. assert.Nil(t, args)
  675. assert.NotNil(t, err)
  676. assert.Contains(t, err.Error(), `system argument "ot_username" failed validation`)
  677. }
  678. func containsEnvPrefix(env []string, prefix string) bool {
  679. for _, item := range env {
  680. if strings.HasPrefix(item, prefix) {
  681. return true
  682. }
  683. }
  684. return false
  685. }
  686. func TestTriggerExecutesTriggeredAction(t *testing.T) {
  687. cfg := config.DefaultConfig()
  688. e := DefaultExecutor(cfg)
  689. helloAction := &config.Action{
  690. Title: "Hello world",
  691. Shell: "echo 'Hello World!'",
  692. }
  693. triggerAction := &config.Action{
  694. Title: "Simple action that triggers another action",
  695. Shell: "echo 'Hi'",
  696. Triggers: []string{"Hello world"},
  697. }
  698. cfg.Actions = append(cfg.Actions, helloAction, triggerAction)
  699. cfg.Sanitize()
  700. e.RebuildActionMap()
  701. finishedTitles := make(chan string, 4)
  702. collector := &executionFinishedCollector{ch: finishedTitles}
  703. e.AddListener(collector)
  704. req := &ExecutionRequest{
  705. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  706. Cfg: cfg,
  707. Binding: e.FindBindingWithNoEntity(triggerAction),
  708. }
  709. wg, _ := e.ExecRequest(req)
  710. wg.Wait()
  711. var got []string
  712. for i := 0; i < 2; i++ {
  713. select {
  714. case title := <-finishedTitles:
  715. got = append(got, title)
  716. case <-time.After(2 * time.Second):
  717. t.Fatalf("timed out waiting for execution %d; got %v", i+1, got)
  718. }
  719. }
  720. assert.Contains(t, got, "Hello world", "triggered action must run")
  721. assert.Contains(t, got, "Simple action that triggers another action", "triggering action must run")
  722. }
  723. func TestTriggerUnknownActionTitleSkipsWithoutPanic(t *testing.T) {
  724. cfg := config.DefaultConfig()
  725. e := DefaultExecutor(cfg)
  726. triggerAction := &config.Action{
  727. Title: "Action with bad trigger",
  728. Shell: "echo 'ok'",
  729. Triggers: []string{"Nonexistent action"},
  730. }
  731. cfg.Actions = append(cfg.Actions, triggerAction)
  732. cfg.Sanitize()
  733. e.RebuildActionMap()
  734. finishedTitles := make(chan string, 4)
  735. collector := &executionFinishedCollector{ch: finishedTitles}
  736. e.AddListener(collector)
  737. req := &ExecutionRequest{
  738. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  739. Cfg: cfg,
  740. Binding: e.FindBindingWithNoEntity(triggerAction),
  741. }
  742. wg, _ := e.ExecRequest(req)
  743. wg.Wait()
  744. var got []string
  745. select {
  746. case title := <-finishedTitles:
  747. got = append(got, title)
  748. case <-time.After(500 * time.Millisecond):
  749. }
  750. assert.Len(t, got, 1, "only the triggering action runs; unknown trigger is skipped")
  751. if len(got) > 0 {
  752. assert.Equal(t, "Action with bad trigger", got[0])
  753. }
  754. }
  755. type executionFinishedCollector struct {
  756. ch chan string
  757. }
  758. func (c *executionFinishedCollector) OnExecutionStarted(_ *InternalLogEntry) {}
  759. func (c *executionFinishedCollector) OnExecutionFinished(entry *InternalLogEntry) {
  760. c.ch <- entry.ActionTitle
  761. }
  762. func (c *executionFinishedCollector) OnOutputChunk(_ []byte, _ string) {}
  763. func (c *executionFinishedCollector) OnActionMapRebuilt() {}
  764. func TestSanitizeLogFilename(t *testing.T) {
  765. tests := []struct {
  766. title string
  767. want string
  768. }{
  769. {"Echo Test", "Echo Test"},
  770. {"Create/update Monthly Report", "Create_update Monthly Report"},
  771. {`path\with\backslashes`, "path_with_backslashes"},
  772. {`a:b*c?d"e<f>g|h`, "a_b_c_d_e_f_g_h"},
  773. {"has\x00nul", "has_nul"},
  774. {"tab\there\nand\rreturn", "tab_here_and_return"},
  775. }
  776. for _, tt := range tests {
  777. assert.Equal(t, tt.want, sanitizeLogFilename(tt.title), "title=%q", tt.title)
  778. }
  779. }
  780. func TestStepSaveLogSanitizesSlashInTitle(t *testing.T) {
  781. resultsDir := t.TempDir()
  782. outputDir := t.TempDir()
  783. started := time.Unix(1714333384, 0)
  784. trackingID := "5e2dc9e5-b6b3-445b-bff9-c2082b0bbbb2"
  785. title := "Create/update Monthly Report"
  786. req := &ExecutionRequest{
  787. Cfg: &config.Config{
  788. SaveLogs: config.SaveLogsConfig{
  789. ResultsDirectory: resultsDir,
  790. OutputDirectory: outputDir,
  791. },
  792. },
  793. Binding: &ActionBinding{
  794. Action: &config.Action{},
  795. },
  796. logEntry: &InternalLogEntry{
  797. ActionTitle: title,
  798. DatetimeStarted: started,
  799. ExecutionTrackingID: trackingID,
  800. Output: "report ok",
  801. },
  802. }
  803. assert.True(t, stepSaveLog(req))
  804. expectedBase := "Create_update Monthly Report.1714333384." + trackingID
  805. resultsPath := filepath.Join(resultsDir, expectedBase+".yaml")
  806. outputPath := filepath.Join(outputDir, expectedBase+".log")
  807. assert.FileExists(t, resultsPath)
  808. assert.FileExists(t, outputPath)
  809. resultsEntries, err := os.ReadDir(resultsDir)
  810. assert.NoError(t, err)
  811. assert.Len(t, resultsEntries, 1, "results file must be flat under resultsDirectory, not a subdirectory")
  812. data, err := os.ReadFile(resultsPath)
  813. assert.NoError(t, err)
  814. assert.Contains(t, string(data), title, "YAML content keeps the original action title")
  815. output, err := os.ReadFile(outputPath)
  816. assert.NoError(t, err)
  817. assert.Equal(t, "report ok", string(output))
  818. }
  819. func TestStepSaveLogKeepsSafeTitleFilename(t *testing.T) {
  820. resultsDir := t.TempDir()
  821. started := time.Unix(1714333384, 0)
  822. trackingID := "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
  823. req := &ExecutionRequest{
  824. Cfg: &config.Config{
  825. SaveLogs: config.SaveLogsConfig{
  826. ResultsDirectory: resultsDir,
  827. },
  828. },
  829. Binding: &ActionBinding{
  830. Action: &config.Action{},
  831. },
  832. logEntry: &InternalLogEntry{
  833. ActionTitle: "Echo Test",
  834. DatetimeStarted: started,
  835. ExecutionTrackingID: trackingID,
  836. },
  837. }
  838. assert.True(t, stepSaveLog(req))
  839. expectedPath := filepath.Join(resultsDir, "Echo Test.1714333384."+trackingID+".yaml")
  840. assert.FileExists(t, expectedPath)
  841. }
  842. func TestStepSaveLogSanitizesNULInTitle(t *testing.T) {
  843. resultsDir := t.TempDir()
  844. outputDir := t.TempDir()
  845. started := time.Unix(1714333384, 0)
  846. trackingID := "bbbbbbbb-cccc-dddd-eeee-ffffffffffff"
  847. title := "Bad\x00Title"
  848. req := &ExecutionRequest{
  849. Cfg: &config.Config{
  850. SaveLogs: config.SaveLogsConfig{
  851. ResultsDirectory: resultsDir,
  852. OutputDirectory: outputDir,
  853. },
  854. },
  855. Binding: &ActionBinding{
  856. Action: &config.Action{},
  857. },
  858. logEntry: &InternalLogEntry{
  859. ActionTitle: title,
  860. DatetimeStarted: started,
  861. ExecutionTrackingID: trackingID,
  862. Output: "nul ok",
  863. },
  864. }
  865. assert.True(t, stepSaveLog(req))
  866. expectedBase := "Bad_Title.1714333384." + trackingID
  867. resultsPath := filepath.Join(resultsDir, expectedBase+".yaml")
  868. outputPath := filepath.Join(outputDir, expectedBase+".log")
  869. assert.FileExists(t, resultsPath)
  870. assert.FileExists(t, outputPath)
  871. assert.NotContains(t, resultsPath, "\x00")
  872. assert.NotContains(t, outputPath, "\x00")
  873. output, err := os.ReadFile(outputPath)
  874. assert.NoError(t, err)
  875. assert.Equal(t, "nul ok", string(output))
  876. }
  877. func TestBlockedExecutionPersistsSaveLogs(t *testing.T) {
  878. t.Parallel()
  879. resultsDir := t.TempDir()
  880. outputDir := t.TempDir()
  881. action := &config.Action{
  882. Title: "Blocked report",
  883. Shell: "sleep 1",
  884. MaxConcurrent: 1,
  885. SaveLogs: config.SaveLogsConfig{
  886. ResultsDirectory: resultsDir,
  887. OutputDirectory: outputDir,
  888. },
  889. }
  890. e, cfg := testGroupExecutor([]*config.Action{action}, nil)
  891. binding := e.FindBindingWithNoEntity(action)
  892. wg1, tracking1 := e.ExecRequest(&ExecutionRequest{
  893. Binding: binding,
  894. Cfg: cfg,
  895. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  896. })
  897. waitUntilExecutionStarted(t, e, tracking1)
  898. wg2, tracking2 := e.ExecRequest(&ExecutionRequest{
  899. Binding: binding,
  900. Cfg: cfg,
  901. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  902. })
  903. wg1.Wait()
  904. wg2.Wait()
  905. snapshot, ok := e.SnapshotLog(tracking2)
  906. require.True(t, ok)
  907. require.True(t, snapshot.Blocked)
  908. resultsEntries, err := os.ReadDir(resultsDir)
  909. require.NoError(t, err)
  910. var resultsPath string
  911. for _, entry := range resultsEntries {
  912. if strings.Contains(entry.Name(), tracking2) {
  913. resultsPath = filepath.Join(resultsDir, entry.Name())
  914. break
  915. }
  916. }
  917. require.NotEmpty(t, resultsPath)
  918. outputEntries, err := os.ReadDir(outputDir)
  919. require.NoError(t, err)
  920. var outputPath string
  921. for _, entry := range outputEntries {
  922. if strings.Contains(entry.Name(), tracking2) {
  923. outputPath = filepath.Join(outputDir, entry.Name())
  924. break
  925. }
  926. }
  927. require.NotEmpty(t, outputPath)
  928. resultsData, err := os.ReadFile(resultsPath)
  929. require.NoError(t, err)
  930. assert.Contains(t, string(resultsData), "blocked: true")
  931. assert.Contains(t, string(resultsData), tracking2)
  932. outputData, err := os.ReadFile(outputPath)
  933. require.NoError(t, err)
  934. assert.Contains(t, string(outputData), "Blocked from executing due to concurrency limit")
  935. }
  936. func TestStepSaveLogReturnsFalseWhenDependenciesMissing(t *testing.T) {
  937. started := time.Unix(1714333384, 0)
  938. valid := &ExecutionRequest{
  939. Cfg: &config.Config{},
  940. Binding: &ActionBinding{
  941. Action: &config.Action{},
  942. },
  943. logEntry: &InternalLogEntry{
  944. ActionTitle: "Echo",
  945. DatetimeStarted: started,
  946. ExecutionTrackingID: "cccccccc-dddd-eeee-ffff-000000000000",
  947. },
  948. }
  949. assert.False(t, stepSaveLog(nil))
  950. assert.False(t, stepSaveLog(&ExecutionRequest{}))
  951. missingLog := *valid
  952. missingLog.logEntry = nil
  953. assert.False(t, stepSaveLog(&missingLog))
  954. missingBinding := *valid
  955. missingBinding.Binding = nil
  956. assert.False(t, stepSaveLog(&missingBinding))
  957. missingAction := *valid
  958. missingAction.Binding = &ActionBinding{}
  959. assert.False(t, stepSaveLog(&missingAction))
  960. missingCfg := *valid
  961. missingCfg.Cfg = nil
  962. assert.False(t, stepSaveLog(&missingCfg))
  963. }
  964. func TestLogEntryOutputAvailableWhileRunning(t *testing.T) {
  965. cfg := config.DefaultConfig()
  966. e := DefaultExecutor(cfg)
  967. action := &config.Action{
  968. Title: "Slow output",
  969. Shell: "echo hello-mid-run; sleep 2",
  970. }
  971. cfg.Actions = append(cfg.Actions, action)
  972. cfg.Sanitize()
  973. e.RebuildActionMap()
  974. binding := e.FindBindingWithNoEntity(action)
  975. require.NotNil(t, binding)
  976. wg, trackingID := e.ExecRequest(&ExecutionRequest{
  977. Binding: binding,
  978. Cfg: cfg,
  979. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  980. })
  981. var sawOutputWhileRunning bool
  982. require.Eventually(t, func() bool {
  983. snapshot, ok := e.SnapshotLog(trackingID)
  984. if !ok {
  985. return false
  986. }
  987. if snapshot.ExecutionFinished {
  988. return false
  989. }
  990. if strings.Contains(snapshot.Output, "hello-mid-run") {
  991. sawOutputWhileRunning = true
  992. return true
  993. }
  994. return false
  995. }, 2*time.Second, 10*time.Millisecond)
  996. wg.Wait()
  997. require.True(t, sawOutputWhileRunning, "expected Output to contain printed text before ExecutionFinished")
  998. snapshot, ok := e.SnapshotLog(trackingID)
  999. require.True(t, ok)
  1000. assert.True(t, snapshot.ExecutionFinished)
  1001. assert.Contains(t, snapshot.Output, "hello-mid-run")
  1002. }