unnamed_argument_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package executor
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. authpublic "github.com/OliveTin/OliveTin/internal/auth/authpublic"
  8. config "github.com/OliveTin/OliveTin/internal/config"
  9. )
  10. func unnamedConfirmationAction(shell string) *config.Action {
  11. return &config.Action{
  12. ID: "unnamed-confirmation",
  13. Title: "Unnamed confirmation",
  14. Shell: shell,
  15. Arguments: []config.ActionArgument{
  16. {Type: "confirmation", Title: "Are you sure?"},
  17. {Name: "target", Type: "ascii_identifier"},
  18. },
  19. }
  20. }
  21. func TestFilterToDefinedArgumentsDropsEmptyName(t *testing.T) {
  22. req := newExecRequest()
  23. req.Binding.Action = unnamedConfirmationAction("echo test")
  24. req.Arguments = map[string]string{
  25. "": "x; touch /tmp/pwned #",
  26. "target": "server1",
  27. }
  28. filterToDefinedArgumentsOnly(req)
  29. assert.NotContains(t, req.Arguments, "", "an argument with an empty name must never be kept")
  30. assert.Equal(t, "server1", req.Arguments["target"])
  31. }
  32. func runUnnamedConfirmationAction(t *testing.T, shell string, args map[string]string) *InternalLogEntry {
  33. t.Helper()
  34. cfg := config.DefaultConfig()
  35. cfg.Actions = []*config.Action{unnamedConfirmationAction(shell)}
  36. cfg.Sanitize()
  37. ex := DefaultExecutor(cfg)
  38. ex.RebuildActionMap()
  39. req := &ExecutionRequest{
  40. Binding: ex.FindBindingByID("unnamed-confirmation"),
  41. Arguments: args,
  42. AuthenticatedUser: &authpublic.AuthenticatedUser{Username: "alice"},
  43. Cfg: cfg,
  44. }
  45. require.NotNil(t, req.Binding)
  46. wg, _ := ex.ExecRequest(req)
  47. wg.Wait()
  48. entry, ok := ex.GetLog(req.TrackingID)
  49. require.True(t, ok)
  50. return entry
  51. }
  52. func TestEmptyNameArgumentNeverReachesShellTemplate(t *testing.T) {
  53. marker := filepath.Join(t.TempDir(), "pwned")
  54. entry := runUnnamedConfirmationAction(t, `echo "value=[{{ index .Arguments "" }}] target={{ .Arguments.target }}"`, map[string]string{
  55. "": "x; touch " + marker + " #",
  56. "target": "server1",
  57. })
  58. assert.NoFileExists(t, marker, "the empty-name argument must not be rendered into the shell command")
  59. assert.NotContains(t, entry.Output, "touch")
  60. assert.Contains(t, entry.Output, "value=[]")
  61. }
  62. func TestUnnamedConfirmationActionStillRuns(t *testing.T) {
  63. entry := runUnnamedConfirmationAction(t, `echo "running against {{ .Arguments.target }}"`, map[string]string{
  64. "target": "server1",
  65. })
  66. assert.False(t, entry.Blocked)
  67. assert.Equal(t, int32(0), entry.ExitCode)
  68. assert.Contains(t, entry.Output, "running against server1")
  69. }