executor_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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 TestFilterToDefinedArgumentsOnly(t *testing.T) {
  370. req := newExecRequest()
  371. req.Binding.Action = &config.Action{
  372. Title: "Filter test",
  373. Shell: "echo '{{ name }}'",
  374. Arguments: []config.ActionArgument{
  375. {Name: "name", Type: "ascii"},
  376. },
  377. }
  378. req.Arguments = map[string]string{
  379. "name": "Alice",
  380. "webhook_path": "/malicious/$(id)",
  381. "extra_undefined": "ignored",
  382. }
  383. filterToDefinedArgumentsOnly(req)
  384. assert.Equal(t, "Alice", req.Arguments["name"])
  385. assert.Empty(t, req.Arguments["webhook_path"])
  386. assert.Empty(t, req.Arguments["extra_undefined"])
  387. }
  388. func TestFilterToDefinedArgumentsDropsReservedPrefixArgs(t *testing.T) {
  389. req := newExecRequest()
  390. req.Binding.Action = &config.Action{
  391. Title: "Filter test",
  392. Shell: "echo test",
  393. Arguments: []config.ActionArgument{},
  394. }
  395. req.Arguments = map[string]string{
  396. "ot_executionTrackingId": "track-123",
  397. "ot_username": "webhook",
  398. }
  399. filterToDefinedArgumentsOnly(req)
  400. assert.Empty(t, req.Arguments["ot_executionTrackingId"])
  401. assert.Empty(t, req.Arguments["ot_username"])
  402. }
  403. func TestStepParseArgsInjectsSystemArgsAfterFiltering(t *testing.T) {
  404. req := newExecRequest()
  405. req.TrackingID = "server-track-456"
  406. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice"}
  407. req.Binding.Action = &config.Action{
  408. Title: "Filter then inject",
  409. Shell: "echo test",
  410. Arguments: []config.ActionArgument{
  411. {Name: "name", Type: "ascii"},
  412. },
  413. }
  414. req.Arguments = map[string]string{
  415. "name": "Alice",
  416. "ot_executionTrackingId": "attacker-track",
  417. "ot_username": "mallory",
  418. "ot_custom": "polluted",
  419. }
  420. assert.True(t, stepParseArgs(req))
  421. assert.Equal(t, "Alice", req.Arguments["name"])
  422. assert.Equal(t, "server-track-456", req.Arguments["ot_executionTrackingId"])
  423. assert.Equal(t, "alice", req.Arguments["ot_username"])
  424. assert.Empty(t, req.Arguments["ot_custom"])
  425. }
  426. func TestStepParseArgsDropsReservedPrefixArgsFromEnvironment(t *testing.T) {
  427. req := newExecRequest()
  428. req.TrackingID = "server-track-456"
  429. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  430. req.Binding.Action = &config.Action{
  431. Title: "No reserved prefix pollution",
  432. Shell: "echo test",
  433. Arguments: []config.ActionArgument{},
  434. }
  435. req.Arguments = map[string]string{
  436. "ot_custom": "polluted",
  437. }
  438. assert.True(t, stepParseArgs(req))
  439. env := buildEnv(req.Arguments)
  440. assert.False(t, containsEnvPrefix(env, "OT_CUSTOM="))
  441. assert.True(t, containsEnvPrefix(env, "OT_USERNAME=alice@example.com"))
  442. assert.True(t, containsEnvPrefix(env, "OT_EXECUTIONTRACKINGID=server-track-456"))
  443. }
  444. func TestSystemArgumentDefinitionsAreReservedAndShellSafe(t *testing.T) {
  445. unsafeTypes := map[string]struct{}{
  446. "email": {},
  447. "password": {},
  448. "raw_string_multiline": {},
  449. "url": {},
  450. "very_dangerous_raw_string": {},
  451. }
  452. seen := map[string]struct{}{}
  453. for _, arg := range systemArgumentDefinitions {
  454. assert.True(t, strings.HasPrefix(arg.Name, config.ReservedArgumentNamePrefix))
  455. assert.NotEmpty(t, arg.Type)
  456. assert.True(t, arg.RejectNull)
  457. _, duplicate := seen[arg.Name]
  458. assert.False(t, duplicate, "duplicate system argument definition %q", arg.Name)
  459. seen[arg.Name] = struct{}{}
  460. _, unsafe := unsafeTypes[arg.Type]
  461. assert.False(t, unsafe, "system argument %q uses unsafe type %q", arg.Name, arg.Type)
  462. }
  463. }
  464. func TestValidatedSystemArgsMatchesSystemArgumentDefinitions(t *testing.T) {
  465. req := newExecRequest()
  466. req.TrackingID = "server-track-456"
  467. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  468. args, err := validatedSystemArgs(req)
  469. assert.Nil(t, err)
  470. assert.Len(t, args, len(systemArgumentDefinitions))
  471. for _, arg := range systemArgumentDefinitions {
  472. assert.Contains(t, args, arg.Name)
  473. }
  474. }
  475. func TestBuildShellAfterArgsOnlyAddsExpectedNonSystemArgs(t *testing.T) {
  476. req := newExecRequest()
  477. req.logEntry = &InternalLogEntry{
  478. Output: "hello",
  479. ExitCode: 7,
  480. }
  481. req.TrackingID = "server-track-456"
  482. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  483. req.Binding.Action = &config.Action{ShellAfterCompleted: "echo test"}
  484. args, err := buildShellAfterArgs(req)
  485. assert.Nil(t, err)
  486. assert.Len(t, args, len(systemArgumentDefinitions)+2)
  487. assert.Contains(t, args, "output")
  488. assert.Contains(t, args, "exitCode")
  489. for _, arg := range systemArgumentDefinitions {
  490. assert.Contains(t, args, arg.Name)
  491. }
  492. }
  493. func TestStepParseArgsAllowsEmailUsernameSystemArg(t *testing.T) {
  494. req := newExecRequest()
  495. req.logEntry = &InternalLogEntry{}
  496. req.TrackingID = "server-track-456"
  497. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  498. req.Binding.Action = &config.Action{
  499. Title: "Email username",
  500. Shell: "echo test",
  501. Arguments: []config.ActionArgument{},
  502. }
  503. assert.True(t, stepParseArgs(req))
  504. assert.Equal(t, "alice@example.com", req.Arguments["ot_username"])
  505. }
  506. func TestStepParseArgsFailsWhenUsernameSystemArgIsInvalid(t *testing.T) {
  507. req := newExecRequest()
  508. req.logEntry = &InternalLogEntry{}
  509. req.TrackingID = "server-track-456"
  510. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice;id"}
  511. req.Binding.Action = &config.Action{
  512. Title: "Invalid system arg",
  513. Shell: "echo test",
  514. Arguments: []config.ActionArgument{},
  515. }
  516. assert.False(t, stepParseArgs(req))
  517. assert.Contains(t, req.logEntry.Output, `system argument "ot_username" failed validation`)
  518. assert.Empty(t, req.Arguments["ot_username"])
  519. }
  520. func TestStepParseArgsFailsWhenTrackingIDSystemArgIsInvalid(t *testing.T) {
  521. req := newExecRequest()
  522. req.logEntry = &InternalLogEntry{}
  523. req.TrackingID = "track/../../bad"
  524. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice"}
  525. req.Binding.Action = &config.Action{
  526. Title: "Invalid tracking ID",
  527. Shell: "echo test",
  528. Arguments: []config.ActionArgument{},
  529. }
  530. assert.False(t, stepParseArgs(req))
  531. assert.Contains(t, req.logEntry.Output, `system argument "ot_executionTrackingId" failed validation`)
  532. assert.Empty(t, req.Arguments["ot_executionTrackingId"])
  533. }
  534. func TestBuildShellAfterArgsUsesValidatedSystemArgs(t *testing.T) {
  535. req := newExecRequest()
  536. req.logEntry = &InternalLogEntry{
  537. Output: "hello",
  538. ExitCode: 7,
  539. }
  540. req.TrackingID = "server-track-456"
  541. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice@example.com"}
  542. req.Binding.Action = &config.Action{
  543. Title: "Shell after",
  544. ShellAfterCompleted: "echo test",
  545. }
  546. args, err := buildShellAfterArgs(req)
  547. assert.Nil(t, err)
  548. assert.Equal(t, "alice@example.com", args["ot_username"])
  549. assert.Equal(t, "server-track-456", args["ot_executionTrackingId"])
  550. assert.Equal(t, "hello", args["output"])
  551. assert.Equal(t, "7", args["exitCode"])
  552. }
  553. func TestBuildShellAfterArgsFailsWhenSystemArgIsInvalid(t *testing.T) {
  554. req := newExecRequest()
  555. req.logEntry = &InternalLogEntry{}
  556. req.TrackingID = "server-track-456"
  557. req.AuthenticatedUser = &authpublic.AuthenticatedUser{Username: "alice;id"}
  558. req.Binding.Action = &config.Action{
  559. Title: "Shell after invalid username",
  560. ShellAfterCompleted: "echo test",
  561. }
  562. args, err := buildShellAfterArgs(req)
  563. assert.Nil(t, args)
  564. assert.NotNil(t, err)
  565. assert.Contains(t, err.Error(), `system argument "ot_username" failed validation`)
  566. }
  567. func containsEnvPrefix(env []string, prefix string) bool {
  568. for _, item := range env {
  569. if strings.HasPrefix(item, prefix) {
  570. return true
  571. }
  572. }
  573. return false
  574. }
  575. func TestTriggerExecutesTriggeredAction(t *testing.T) {
  576. cfg := config.DefaultConfig()
  577. e := DefaultExecutor(cfg)
  578. helloAction := &config.Action{
  579. Title: "Hello world",
  580. Shell: "echo 'Hello World!'",
  581. }
  582. triggerAction := &config.Action{
  583. Title: "Simple action that triggers another action",
  584. Shell: "echo 'Hi'",
  585. Triggers: []string{"Hello world"},
  586. }
  587. cfg.Actions = append(cfg.Actions, helloAction, triggerAction)
  588. cfg.Sanitize()
  589. e.RebuildActionMap()
  590. finishedTitles := make(chan string, 4)
  591. collector := &executionFinishedCollector{ch: finishedTitles}
  592. e.AddListener(collector)
  593. req := &ExecutionRequest{
  594. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  595. Cfg: cfg,
  596. Binding: e.FindBindingWithNoEntity(triggerAction),
  597. }
  598. wg, _ := e.ExecRequest(req)
  599. wg.Wait()
  600. var got []string
  601. for i := 0; i < 2; i++ {
  602. select {
  603. case title := <-finishedTitles:
  604. got = append(got, title)
  605. case <-time.After(2 * time.Second):
  606. t.Fatalf("timed out waiting for execution %d; got %v", i+1, got)
  607. }
  608. }
  609. assert.Contains(t, got, "Hello world", "triggered action must run")
  610. assert.Contains(t, got, "Simple action that triggers another action", "triggering action must run")
  611. }
  612. func TestTriggerUnknownActionTitleSkipsWithoutPanic(t *testing.T) {
  613. cfg := config.DefaultConfig()
  614. e := DefaultExecutor(cfg)
  615. triggerAction := &config.Action{
  616. Title: "Action with bad trigger",
  617. Shell: "echo 'ok'",
  618. Triggers: []string{"Nonexistent action"},
  619. }
  620. cfg.Actions = append(cfg.Actions, triggerAction)
  621. cfg.Sanitize()
  622. e.RebuildActionMap()
  623. finishedTitles := make(chan string, 4)
  624. collector := &executionFinishedCollector{ch: finishedTitles}
  625. e.AddListener(collector)
  626. req := &ExecutionRequest{
  627. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  628. Cfg: cfg,
  629. Binding: e.FindBindingWithNoEntity(triggerAction),
  630. }
  631. wg, _ := e.ExecRequest(req)
  632. wg.Wait()
  633. var got []string
  634. select {
  635. case title := <-finishedTitles:
  636. got = append(got, title)
  637. case <-time.After(500 * time.Millisecond):
  638. }
  639. assert.Len(t, got, 1, "only the triggering action runs; unknown trigger is skipped")
  640. if len(got) > 0 {
  641. assert.Equal(t, "Action with bad trigger", got[0])
  642. }
  643. }
  644. type executionFinishedCollector struct {
  645. ch chan string
  646. }
  647. func (c *executionFinishedCollector) OnExecutionStarted(_ *InternalLogEntry) {}
  648. func (c *executionFinishedCollector) OnExecutionFinished(entry *InternalLogEntry) {
  649. c.ch <- entry.ActionTitle
  650. }
  651. func (c *executionFinishedCollector) OnOutputChunk(_ []byte, _ string) {}
  652. func (c *executionFinishedCollector) OnActionMapRebuilt() {}
  653. func TestSanitizeLogFilename(t *testing.T) {
  654. tests := []struct {
  655. title string
  656. want string
  657. }{
  658. {"Echo Test", "Echo Test"},
  659. {"Create/update Monthly Report", "Create_update Monthly Report"},
  660. {`path\with\backslashes`, "path_with_backslashes"},
  661. {`a:b*c?d"e<f>g|h`, "a_b_c_d_e_f_g_h"},
  662. {"has\x00nul", "has_nul"},
  663. {"tab\there\nand\rreturn", "tab_here_and_return"},
  664. }
  665. for _, tt := range tests {
  666. assert.Equal(t, tt.want, sanitizeLogFilename(tt.title), "title=%q", tt.title)
  667. }
  668. }
  669. func TestStepSaveLogSanitizesSlashInTitle(t *testing.T) {
  670. resultsDir := t.TempDir()
  671. outputDir := t.TempDir()
  672. started := time.Unix(1714333384, 0)
  673. trackingID := "5e2dc9e5-b6b3-445b-bff9-c2082b0bbbb2"
  674. title := "Create/update Monthly Report"
  675. req := &ExecutionRequest{
  676. Cfg: &config.Config{
  677. SaveLogs: config.SaveLogsConfig{
  678. ResultsDirectory: resultsDir,
  679. OutputDirectory: outputDir,
  680. },
  681. },
  682. Binding: &ActionBinding{
  683. Action: &config.Action{},
  684. },
  685. logEntry: &InternalLogEntry{
  686. ActionTitle: title,
  687. DatetimeStarted: started,
  688. ExecutionTrackingID: trackingID,
  689. Output: "report ok",
  690. },
  691. }
  692. assert.True(t, stepSaveLog(req))
  693. expectedBase := "Create_update Monthly Report.1714333384." + trackingID
  694. resultsPath := filepath.Join(resultsDir, expectedBase+".yaml")
  695. outputPath := filepath.Join(outputDir, expectedBase+".log")
  696. assert.FileExists(t, resultsPath)
  697. assert.FileExists(t, outputPath)
  698. resultsEntries, err := os.ReadDir(resultsDir)
  699. assert.NoError(t, err)
  700. assert.Len(t, resultsEntries, 1, "results file must be flat under resultsDirectory, not a subdirectory")
  701. data, err := os.ReadFile(resultsPath)
  702. assert.NoError(t, err)
  703. assert.Contains(t, string(data), title, "YAML content keeps the original action title")
  704. output, err := os.ReadFile(outputPath)
  705. assert.NoError(t, err)
  706. assert.Equal(t, "report ok", string(output))
  707. }
  708. func TestStepSaveLogKeepsSafeTitleFilename(t *testing.T) {
  709. resultsDir := t.TempDir()
  710. started := time.Unix(1714333384, 0)
  711. trackingID := "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
  712. req := &ExecutionRequest{
  713. Cfg: &config.Config{
  714. SaveLogs: config.SaveLogsConfig{
  715. ResultsDirectory: resultsDir,
  716. },
  717. },
  718. Binding: &ActionBinding{
  719. Action: &config.Action{},
  720. },
  721. logEntry: &InternalLogEntry{
  722. ActionTitle: "Echo Test",
  723. DatetimeStarted: started,
  724. ExecutionTrackingID: trackingID,
  725. },
  726. }
  727. assert.True(t, stepSaveLog(req))
  728. expectedPath := filepath.Join(resultsDir, "Echo Test.1714333384."+trackingID+".yaml")
  729. assert.FileExists(t, expectedPath)
  730. }
  731. func TestStepSaveLogSanitizesNULInTitle(t *testing.T) {
  732. resultsDir := t.TempDir()
  733. outputDir := t.TempDir()
  734. started := time.Unix(1714333384, 0)
  735. trackingID := "bbbbbbbb-cccc-dddd-eeee-ffffffffffff"
  736. title := "Bad\x00Title"
  737. req := &ExecutionRequest{
  738. Cfg: &config.Config{
  739. SaveLogs: config.SaveLogsConfig{
  740. ResultsDirectory: resultsDir,
  741. OutputDirectory: outputDir,
  742. },
  743. },
  744. Binding: &ActionBinding{
  745. Action: &config.Action{},
  746. },
  747. logEntry: &InternalLogEntry{
  748. ActionTitle: title,
  749. DatetimeStarted: started,
  750. ExecutionTrackingID: trackingID,
  751. Output: "nul ok",
  752. },
  753. }
  754. assert.True(t, stepSaveLog(req))
  755. expectedBase := "Bad_Title.1714333384." + trackingID
  756. resultsPath := filepath.Join(resultsDir, expectedBase+".yaml")
  757. outputPath := filepath.Join(outputDir, expectedBase+".log")
  758. assert.FileExists(t, resultsPath)
  759. assert.FileExists(t, outputPath)
  760. assert.NotContains(t, resultsPath, "\x00")
  761. assert.NotContains(t, outputPath, "\x00")
  762. output, err := os.ReadFile(outputPath)
  763. assert.NoError(t, err)
  764. assert.Equal(t, "nul ok", string(output))
  765. }
  766. func TestStepSaveLogReturnsFalseWhenDependenciesMissing(t *testing.T) {
  767. started := time.Unix(1714333384, 0)
  768. valid := &ExecutionRequest{
  769. Cfg: &config.Config{},
  770. Binding: &ActionBinding{
  771. Action: &config.Action{},
  772. },
  773. logEntry: &InternalLogEntry{
  774. ActionTitle: "Echo",
  775. DatetimeStarted: started,
  776. ExecutionTrackingID: "cccccccc-dddd-eeee-ffff-000000000000",
  777. },
  778. }
  779. assert.False(t, stepSaveLog(nil))
  780. assert.False(t, stepSaveLog(&ExecutionRequest{}))
  781. missingLog := *valid
  782. missingLog.logEntry = nil
  783. assert.False(t, stepSaveLog(&missingLog))
  784. missingBinding := *valid
  785. missingBinding.Binding = nil
  786. assert.False(t, stepSaveLog(&missingBinding))
  787. missingAction := *valid
  788. missingAction.Binding = &ActionBinding{}
  789. assert.False(t, stepSaveLog(&missingAction))
  790. missingCfg := *valid
  791. missingCfg.Cfg = nil
  792. assert.False(t, stepSaveLog(&missingCfg))
  793. }
  794. func TestLogEntryOutputAvailableWhileRunning(t *testing.T) {
  795. cfg := config.DefaultConfig()
  796. e := DefaultExecutor(cfg)
  797. action := &config.Action{
  798. Title: "Slow output",
  799. Shell: "echo hello-mid-run; sleep 2",
  800. }
  801. cfg.Actions = append(cfg.Actions, action)
  802. cfg.Sanitize()
  803. e.RebuildActionMap()
  804. binding := e.FindBindingWithNoEntity(action)
  805. require.NotNil(t, binding)
  806. wg, trackingID := e.ExecRequest(&ExecutionRequest{
  807. Binding: binding,
  808. Cfg: cfg,
  809. AuthenticatedUser: auth.UserFromSystem(cfg, "testuser"),
  810. })
  811. var sawOutputWhileRunning bool
  812. require.Eventually(t, func() bool {
  813. snapshot, ok := e.SnapshotLog(trackingID)
  814. if !ok {
  815. return false
  816. }
  817. if snapshot.ExecutionFinished {
  818. return false
  819. }
  820. if strings.Contains(snapshot.Output, "hello-mid-run") {
  821. sawOutputWhileRunning = true
  822. return true
  823. }
  824. return false
  825. }, 2*time.Second, 10*time.Millisecond)
  826. wg.Wait()
  827. require.True(t, sawOutputWhileRunning, "expected Output to contain printed text before ExecutionFinished")
  828. snapshot, ok := e.SnapshotLog(trackingID)
  829. require.True(t, ok)
  830. assert.True(t, snapshot.ExecutionFinished)
  831. assert.Contains(t, snapshot.Output, "hello-mid-run")
  832. }