Skip to content

Commit 3b532a6

Browse files
blotusfzipi
andauthored
feat: add uppercase transformation (#935)
* add uppercase transformation * add uppercase.json for tests --------- Co-authored-by: Felipe Zipitría <[email protected]>
1 parent fc1596e commit 3b532a6

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

Diff for: internal/transformations/testdata/uppercase.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
"input" : "",
4+
"name" : "uppercase",
5+
"type" : "tfn",
6+
"ret" : 0,
7+
"output" : ""
8+
},
9+
{
10+
"output" : "TESTCASE",
11+
"ret" : 0,
12+
"name" : "uppercase",
13+
"input" : "testcase",
14+
"type" : "tfn"
15+
},
16+
{
17+
"ret" : 0,
18+
"input" : "test\u0000case",
19+
"type" : "tfn",
20+
"name" : "uppercase",
21+
"output" : "TEST\u0000CASE"
22+
},
23+
{
24+
"output" : "TESTCASE",
25+
"input" : "testcase",
26+
"name" : "uppercase",
27+
"type" : "tfn",
28+
"ret" : 1
29+
},
30+
{
31+
"ret" : 1,
32+
"input" : "Test\u0000Case",
33+
"name" : "uppercase",
34+
"type" : "tfn",
35+
"output" : "TEST\u0000CASE"
36+
}
37+
]
38+

Diff for: internal/transformations/transformations.go

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func init() {
5151
Register("replaceComments", replaceComments)
5252
Register("replaceNulls", replaceNulls)
5353
Register("sha1", sha1T)
54+
Register("uppercase", upperCase)
5455
Register("urlDecode", urlDecode)
5556
Register("urlDecodeUni", urlDecodeUni)
5657
Register("urlEncode", urlEncode)

Diff for: internal/transformations/uppercase.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package transformations
5+
6+
import (
7+
"strings"
8+
)
9+
10+
func upperCase(data string) (string, bool, error) {
11+
// TODO: Explicit implementation of ToUpper would allow optimizing away the byte by byte comparison for returning the changed boolean
12+
// See https://github.com/corazawaf/coraza/pull/778#discussion_r1186963422
13+
transformedData := strings.ToUpper(data)
14+
return transformedData, data != transformedData, nil
15+
}

Diff for: internal/transformations/uppercase_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package transformations
5+
6+
import "testing"
7+
8+
func TestUpperCase(t *testing.T) {
9+
tests := []struct {
10+
input string
11+
want string
12+
}{
13+
{
14+
input: "TestCase",
15+
want: "TESTCASE",
16+
},
17+
{
18+
input: "test\u0000case",
19+
want: "TEST\u0000CASE",
20+
},
21+
{
22+
input: "TESTCASE",
23+
want: "TESTCASE",
24+
},
25+
{
26+
input: "",
27+
want: "",
28+
},
29+
{
30+
input: "ThIs Is A tExT fOr TeStInG uPPerCAse FuNcTiOnAlItY.",
31+
want: "THIS IS A TEXT FOR TESTING UPPERCASE FUNCTIONALITY.",
32+
},
33+
}
34+
35+
for _, tc := range tests {
36+
tt := tc
37+
t.Run(tt.input, func(t *testing.T) {
38+
have, changed, err := upperCase(tt.input)
39+
if err != nil {
40+
t.Error(err)
41+
}
42+
if tt.input == tt.want && changed || tt.input != tt.want && !changed {
43+
t.Errorf("input %q, have %q with changed %t", tt.input, have, changed)
44+
}
45+
if have != tt.want {
46+
t.Errorf("have %q, want %q", have, tt.want)
47+
}
48+
})
49+
}
50+
}
51+
52+
func BenchmarkUppercase(b *testing.B) {
53+
tests := []string{
54+
"tesTcase",
55+
"ThIs Is A tExT fOr TeStInG lOwErCaSe FuNcTiOnAlItY.ThIs Is A tExT fOr TeStInG lOwErCaSe FuNcTiOnAlItY. ThIs Is A tExT fOr TeStInG lOwErCaSe FuNcTiOnAlItY.ThIs Is A tExT fOr TeStInG lOwErCaSe FuNcTiOnAlItY.",
56+
}
57+
for i := 0; i < b.N; i++ {
58+
for _, tt := range tests {
59+
b.Run(tt, func(b *testing.B) {
60+
for j := 0; j < b.N; j++ {
61+
_, _, err := upperCase(tt)
62+
if err != nil {
63+
b.Error(err)
64+
}
65+
}
66+
})
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)