Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add uppercase transformation #935

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
})
}
}
}
Loading