webhook_unsigned_args_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package configcheck_test
  2. import (
  3. "testing"
  4. "github.com/OliveTin/OliveTin/internal/config"
  5. "github.com/OliveTin/OliveTin/internal/configcheck"
  6. "github.com/OliveTin/OliveTin/internal/configissues"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func unsignedArgIssues(webhook config.WebhookConfig, argNames ...string) []configissues.Issue {
  11. configissues.BeginConfigLoad()
  12. args := make([]config.ActionArgument, 0, len(argNames))
  13. for _, name := range argNames {
  14. args = append(args, config.ActionArgument{Name: name, Type: "ascii_identifier"})
  15. }
  16. cfg := config.DefaultConfig()
  17. cfg.Actions = []*config.Action{{
  18. ID: "deploy",
  19. Title: "Deploy",
  20. Exec: []string{"true"},
  21. Arguments: args,
  22. ExecOnWebhook: []config.WebhookConfig{webhook},
  23. }}
  24. configcheck.Rebuild(cfg)
  25. return issuesWithCode(configissues.List(), configissues.CodeWebhookUnsignedArg)
  26. }
  27. func hmacWebhook() config.WebhookConfig {
  28. return config.WebhookConfig{AuthType: "hmac-sha256", Secret: "s3cret"}
  29. }
  30. func TestRebuildWarnsForUnsignedArgumentsOnHmacWebhook(t *testing.T) {
  31. issues := unsignedArgIssues(hmacWebhook(), "webhook_header_x-target", "webhook_query", "webhook_path", "branch")
  32. require.Len(t, issues, 3)
  33. names := []string{issues[0].ArgumentName, issues[1].ArgumentName, issues[2].ArgumentName}
  34. assert.ElementsMatch(t, []string{"webhook_header_x-target", "webhook_query", "webhook_path"}, names)
  35. assert.Equal(t, configissues.SeverityWarning, issues[0].Severity)
  36. assert.Contains(t, issues[0].Message, "extract")
  37. }
  38. func TestRebuildWarnsForUnsignedArgumentsOnEveryHmacAuthTypeAndTemplate(t *testing.T) {
  39. for _, authType := range config.HmacWebhookAuthTypes {
  40. assert.Len(t, unsignedArgIssues(config.WebhookConfig{AuthType: authType, Secret: "s"}, "webhook_query"), 1, authType)
  41. }
  42. assert.Len(t, unsignedArgIssues(config.WebhookConfig{Template: "github-push", Secret: "s"}, "webhook_query"), 1)
  43. }
  44. func TestRebuildDoesNotWarnForArgumentsTakenFromTheBody(t *testing.T) {
  45. assert.Empty(t, unsignedArgIssues(hmacWebhook(), "branch", "webhook_method"))
  46. }
  47. func TestRebuildDoesNotWarnForWebhooksThatAuthenticateTheWholeRequest(t *testing.T) {
  48. for _, webhook := range []config.WebhookConfig{
  49. {AuthType: "bearer", Secret: "token"},
  50. {AuthType: "basic", Secret: "user:pass"},
  51. {AuthType: config.WebhookAuthTypeNone},
  52. } {
  53. assert.Empty(t, unsignedArgIssues(webhook, "webhook_query"), webhook.AuthType)
  54. }
  55. }
  56. func TestRebuildDoesNotTreatUnknownWebhookTemplateAsHmac(t *testing.T) {
  57. assert.Empty(t, unsignedArgIssues(config.WebhookConfig{Template: "github-pus", Secret: "s"}, "webhook_query"))
  58. }