Skip to content

Commit

Permalink
feat: add uppercase transformation (#935)
Browse files Browse the repository at this point in the history
* add uppercase transformation
* add uppercase.json for tests

---------

Co-authored-by: Felipe Zipitría <3012076+fzipi@users.noreply.github.com>
  • Loading branch information
blotus and fzipi authored Dec 18, 2023
1 parent fc1596e commit 3b532a6
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
38 changes: 38 additions & 0 deletions internal/transformations/testdata/uppercase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"input" : "",
"name" : "uppercase",
"type" : "tfn",
"ret" : 0,
"output" : ""
},
{
"output" : "TESTCASE",
"ret" : 0,
"name" : "uppercase",
"input" : "testcase",
"type" : "tfn"
},
{
"ret" : 0,
"input" : "test\u0000case",
"type" : "tfn",
"name" : "uppercase",
"output" : "TEST\u0000CASE"
},
{
"output" : "TESTCASE",
"input" : "testcase",
"name" : "uppercase",
"type" : "tfn",
"ret" : 1
},
{
"ret" : 1,
"input" : "Test\u0000Case",
"name" : "uppercase",
"type" : "tfn",
"output" : "TEST\u0000CASE"
}
]

1 change: 1 addition & 0 deletions internal/transformations/transformations.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func init() {
Register("replaceComments", replaceComments)
Register("replaceNulls", replaceNulls)
Register("sha1", sha1T)
Register("uppercase", upperCase)
Register("urlDecode", urlDecode)
Register("urlDecodeUni", urlDecodeUni)
Register("urlEncode", urlEncode)
Expand Down
15 changes: 15 additions & 0 deletions internal/transformations/uppercase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

package transformations

import (
"strings"
)

func upperCase(data string) (string, bool, error) {
// TODO: Explicit implementation of ToUpper would allow optimizing away the byte by byte comparison for returning the changed boolean
// See https://github.com/corazawaf/coraza/pull/778#discussion_r1186963422
transformedData := strings.ToUpper(data)
return transformedData, data != transformedData, nil
}
69 changes: 69 additions & 0 deletions internal/transformations/uppercase_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

package transformations

import "testing"

func TestUpperCase(t *testing.T) {
tests := []struct {
input string
want string
}{
{
input: "TestCase",
want: "TESTCASE",
},
{
input: "test\u0000case",
want: "TEST\u0000CASE",
},
{
input: "TESTCASE",
want: "TESTCASE",
},
{
input: "",
want: "",
},
{
input: "ThIs Is A tExT fOr TeStInG uPPerCAse FuNcTiOnAlItY.",
want: "THIS IS A TEXT FOR TESTING UPPERCASE FUNCTIONALITY.",
},
}

for _, tc := range tests {
tt := tc
t.Run(tt.input, func(t *testing.T) {
have, changed, err := upperCase(tt.input)
if err != nil {
t.Error(err)
}
if tt.input == tt.want && changed || tt.input != tt.want && !changed {
t.Errorf("input %q, have %q with changed %t", tt.input, have, changed)
}
if have != tt.want {
t.Errorf("have %q, want %q", have, tt.want)
}
})
}
}

func BenchmarkUppercase(b *testing.B) {
tests := []string{
"tesTcase",
"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.",
}
for i := 0; i < b.N; i++ {
for _, tt := range tests {
b.Run(tt, func(b *testing.B) {
for j := 0; j < b.N; j++ {
_, _, err := upperCase(tt)
if err != nil {
b.Error(err)
}
}
})
}
}
}

0 comments on commit 3b532a6

Please sign in to comment.