modsec_rule_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package waap_rule
  2. import "testing"
  3. func TestVPatchRuleString(t *testing.T) {
  4. tests := []struct {
  5. name string
  6. rule CustomRule
  7. expected string
  8. }{
  9. {
  10. name: "Base Rule",
  11. rule: CustomRule{
  12. Zones: []string{"ARGS"},
  13. Variables: []string{"foo"},
  14. Match: match{Type: "regex", Value: "[^a-zA-Z]"},
  15. Transform: []string{"lowercase"},
  16. },
  17. expected: `SecRule ARGS_GET:foo "@rx [^a-zA-Z]" "id:1136235475,phase:2,deny,log,msg:'Base Rule',t:lowercase"`,
  18. },
  19. {
  20. name: "Multiple Zones",
  21. rule: CustomRule{
  22. Zones: []string{"ARGS", "BODY_ARGS"},
  23. Variables: []string{"foo"},
  24. Match: match{Type: "regex", Value: "[^a-zA-Z]"},
  25. Transform: []string{"lowercase"},
  26. },
  27. expected: `SecRule ARGS_GET:foo|ARGS_POST:foo "@rx [^a-zA-Z]" "id:2088895799,phase:2,deny,log,msg:'Multiple Zones',t:lowercase"`,
  28. },
  29. {
  30. name: "Basic AND",
  31. rule: CustomRule{
  32. And: []CustomRule{
  33. {
  34. Zones: []string{"ARGS"},
  35. Variables: []string{"foo"},
  36. Match: match{Type: "regex", Value: "[^a-zA-Z]"},
  37. Transform: []string{"lowercase"},
  38. },
  39. {
  40. Zones: []string{"ARGS"},
  41. Variables: []string{"bar"},
  42. Match: match{Type: "regex", Value: "[^a-zA-Z]"},
  43. Transform: []string{"lowercase"},
  44. },
  45. },
  46. },
  47. expected: `SecRule ARGS_GET:foo "@rx [^a-zA-Z]" "id:2323451654,phase:2,deny,log,msg:'Basic AND_and_0',t:lowercase,chain"
  48. SecRule ARGS_GET:bar "@rx [^a-zA-Z]" "id:2075918819,phase:2,deny,log,msg:'Basic AND_and_1',t:lowercase"`,
  49. },
  50. {
  51. name: "Basic OR",
  52. rule: CustomRule{
  53. Or: []CustomRule{
  54. {
  55. Zones: []string{"ARGS"},
  56. Variables: []string{"foo"},
  57. Match: match{Type: "regex", Value: "[^a-zA-Z]"},
  58. Transform: []string{"lowercase"},
  59. },
  60. {
  61. Zones: []string{"ARGS"},
  62. Variables: []string{"bar"},
  63. Match: match{Type: "regex", Value: "[^a-zA-Z]"},
  64. Transform: []string{"lowercase"},
  65. },
  66. },
  67. },
  68. expected: `SecRule ARGS_GET:foo "@rx [^a-zA-Z]" "id:2720972114,phase:2,deny,log,msg:'Basic OR_or_0',t:lowercase,skip:1"
  69. SecRule ARGS_GET:bar "@rx [^a-zA-Z]" "id:2638639999,phase:2,deny,log,msg:'Basic OR_or_1',t:lowercase"`,
  70. },
  71. {
  72. name: "OR AND mix",
  73. rule: CustomRule{
  74. And: []CustomRule{
  75. {
  76. Zones: []string{"ARGS"},
  77. Variables: []string{"foo"},
  78. Match: match{Type: "regex", Value: "[^a-zA-Z]"},
  79. Transform: []string{"lowercase"},
  80. Or: []CustomRule{
  81. {
  82. Zones: []string{"ARGS"},
  83. Variables: []string{"foo"},
  84. Match: match{Type: "regex", Value: "[^a-zA-Z]"},
  85. Transform: []string{"lowercase"},
  86. },
  87. {
  88. Zones: []string{"ARGS"},
  89. Variables: []string{"bar"},
  90. Match: match{Type: "regex", Value: "[^a-zA-Z]"},
  91. Transform: []string{"lowercase"},
  92. },
  93. },
  94. },
  95. },
  96. },
  97. expected: `SecRule ARGS_GET:foo "@rx [^a-zA-Z]" "id:2720972114,phase:2,deny,log,msg:'Basic OR_or_0',t:lowercase,skip:1"
  98. SecRule ARGS_GET:bar "@rx [^a-zA-Z]" "id:2638639999,phase:2,deny,log,msg:'Basic OR_or_1',t:lowercase"`,
  99. },
  100. }
  101. for _, tt := range tests {
  102. t.Run(tt.name, func(t *testing.T) {
  103. actual, _, err := tt.rule.Convert(ModsecurityRuleType, tt.name)
  104. if err != nil {
  105. t.Errorf("Error converting rule: %s", err)
  106. }
  107. if actual != tt.expected {
  108. t.Errorf("Expected:\n%s\nGot:\n%s", tt.expected, actual)
  109. }
  110. })
  111. }
  112. }