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 reverse transformation support #870

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 3 additions & 1 deletion experimental/plugins/plugintypes/transformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ package plugintypes
// If a transformation fails to run it will return the same string
// and an error, errors are only used for logging, it won't stop
// the execution of the rule
type Transformation = func(input string) (string, bool, error)
// "updated" is used for transformation cache, if true, the cache
// will be updated
type Transformation = func(input string) (result string, updated bool, err error)
18 changes: 18 additions & 0 deletions internal/transformations/reverse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

package transformations

func reverse(data string) (string, bool, error) {
if len(data) <= 1 {
return data, false, nil
}
rns := []rune(data) // convert to rune, not bytes
for i, j := 0, len(rns)-1; i < j; i, j = i+1, j-1 {
// swap the letters of the string,
// like first with last and so on.
rns[i], rns[j] = rns[j], rns[i]
}
res := string(rns)
return res, res != data, nil
}
38 changes: 38 additions & 0 deletions internal/transformations/testdata/reverse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"output": "desrever si siht",
"name": "reverse",
"input": "this is reversed",
"type": "tfn",
"ret": 0
},
{
"output": "síht sì dèsrèvèr",
"name": "reverse",
"input": "rèvèrsèd ìs thís",
"type": "tfn",
"ret": 0
},
{
"output": "esaC\u0000tseT",
"name": "reverse",
"input": "Test\u0000Case",
"type": "tfn",
"ret": 0
},
{
"output": "",
"name": "reverse",
"input": "",
"type": "tfn",
"ret": 0
},
{
"output": "a",
"name": "reverse",
"input": "a",
"type": "tfn",
"ret": 0
}

]
1 change: 1 addition & 0 deletions internal/transformations/transformations.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func init() {
Register("removeWhitespace", removeWhitespace)
Register("replaceComments", replaceComments)
Register("replaceNulls", replaceNulls)
Register("reverse", reverse)
Register("sha1", sha1T)
Register("uppercase", upperCase)
Register("urlDecode", urlDecode)
Expand Down
Loading