|
| 1 | +// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//go:build coraza.rule.no_regex_multiline |
| 5 | + |
| 6 | +package operators |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/corazawaf/coraza/v3/experimental/plugins/plugintypes" |
| 13 | + "github.com/corazawaf/coraza/v3/internal/corazawaf" |
| 14 | +) |
| 15 | + |
| 16 | +func TestRx(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + pattern string |
| 19 | + input string |
| 20 | + want bool |
| 21 | + }{ |
| 22 | + { |
| 23 | + pattern: "som(.*)ta", |
| 24 | + input: "somedata", |
| 25 | + want: true, |
| 26 | + }, |
| 27 | + { |
| 28 | + pattern: "som(.*)ta", |
| 29 | + input: "notdata", |
| 30 | + want: false, |
| 31 | + }, |
| 32 | + { |
| 33 | + pattern: "ハロー", |
| 34 | + input: "ハローワールド", |
| 35 | + want: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + pattern: "ハロー", |
| 39 | + input: "グッバイワールド", |
| 40 | + want: false, |
| 41 | + }, |
| 42 | + { |
| 43 | + pattern: `\xac\xed\x00\x05`, |
| 44 | + input: "\xac\xed\x00\x05t\x00\x04test", |
| 45 | + want: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + pattern: `\xac\xed\x00\x05`, |
| 49 | + input: "\xac\xed\x00t\x00\x04test", |
| 50 | + want: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + // Requires dotall |
| 54 | + pattern: `hello.*world`, |
| 55 | + input: "hello\nworld", |
| 56 | + want: true, |
| 57 | + }, |
| 58 | + { |
| 59 | + // Requires multiline disabled by default |
| 60 | + pattern: `^hello.*world`, |
| 61 | + input: "test\nhello\nworld", |
| 62 | + want: false, |
| 63 | + }, |
| 64 | + { |
| 65 | + // Makes sure multiline can be enabled by the user |
| 66 | + pattern: `(?m)^hello.*world`, |
| 67 | + input: "test\nhello\nworld", |
| 68 | + want: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + // Makes sure, (?s) passed by the user does not |
| 72 | + // break the regex. |
| 73 | + pattern: `(?s)hello.*world`, |
| 74 | + input: "hello\nworld", |
| 75 | + want: true, |
| 76 | + }, |
| 77 | + { |
| 78 | + // Make sure user flags are also applied |
| 79 | + pattern: `(?i)hello.*world`, |
| 80 | + input: "testHELLO\nworld", |
| 81 | + want: true, |
| 82 | + }, |
| 83 | + { |
| 84 | + // The so called DOLLAR_ENDONLY modifier in PCRE2 is meant to tweak the meaning of dollar '$' |
| 85 | + // so that it matches only at the very end of the string (see: https://www.pcre.org/current/doc/html/pcre2pattern.html#SEC6) |
| 86 | + // It seems that re2 already behaves like that by default. |
| 87 | + pattern: `123$`, |
| 88 | + input: "123\n", |
| 89 | + want: false, |
| 90 | + }, |
| 91 | + { |
| 92 | + // Dollar endonly match |
| 93 | + pattern: `123$`, |
| 94 | + input: "test123", |
| 95 | + want: true, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + for _, tc := range tests { |
| 100 | + tt := tc |
| 101 | + t.Run(fmt.Sprintf("%s/%s", tt.pattern, tt.input), func(t *testing.T) { |
| 102 | + |
| 103 | + opts := plugintypes.OperatorOptions{ |
| 104 | + Arguments: tt.pattern, |
| 105 | + } |
| 106 | + rx, err := newRX(opts) |
| 107 | + if err != nil { |
| 108 | + t.Error(err) |
| 109 | + } |
| 110 | + waf := corazawaf.NewWAF() |
| 111 | + tx := waf.NewTransaction() |
| 112 | + tx.Capture = true |
| 113 | + res := rx.Evaluate(tx, tt.input) |
| 114 | + if res != tt.want { |
| 115 | + t.Errorf("want %v, got %v", tt.want, res) |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
0 commit comments