Skip to content

Commit

Permalink
Test hash uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
piyushroshan committed Jan 23, 2025
1 parent ab2d3e6 commit c21c47d
Showing 1 changed file with 121 additions and 17 deletions.
138 changes: 121 additions & 17 deletions internal/corazawaf/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package corazawaf

import (
"errors"
"hash/fnv"
"strconv"
"strings"
"testing"

"github.com/corazawaf/coraza/v3/debuglog"
Expand All @@ -14,6 +16,7 @@ import (
"github.com/corazawaf/coraza/v3/internal/corazarules"
"github.com/corazawaf/coraza/v3/types"
"github.com/corazawaf/coraza/v3/types/variables"
"github.com/stretchr/testify/assert"
)

func TestMatchEvaluate(t *testing.T) {
Expand Down Expand Up @@ -384,23 +387,6 @@ func TestAddTransformation(t *testing.T) {
}
}

func BenchmarkAddTransformationUnique(b *testing.B) {
transformation := func(input string) (string, bool, error) {
return "Test", true, nil
}
b.ResetTimer()
b.RunParallel(func(p *testing.PB) {
rule := NewRule()
for p.Next() {
transformationName := "transformation" + b.Name()
err := rule.AddTransformation(transformationName, transformation)
if err != nil {
b.Fatalf("Failed to add a transformation: %s", err.Error())
}
}
})
}

func TestAddTransformationEmpty(t *testing.T) {
rule := NewRule()
transformationName := ""
Expand Down Expand Up @@ -633,3 +619,121 @@ func TestExpandMacroAfterWholeRuleEvaluation(t *testing.T) {
t.Errorf("Expected ArgsGet-data, got %s", matchdata[0].Data())
}
}

func BenchmarkAddTransformationSame(b *testing.B) {
transformation := func(input string) (string, bool, error) {
return "Test", true, nil
}
b.ResetTimer()
rule := NewRule()
for i := 0; i < b.N; i++ {
transformationName := "transformation"
err := rule.AddTransformation(transformationName, transformation)
if err != nil {
b.Fatalf("Failed to add a transformation: %s", err.Error())
}
}
}

func BenchmarkAddTransformationUnique(b *testing.B) {
transformation := func(input string) (string, bool, error) {
return "Test", true, nil
}
b.ResetTimer()
rule := NewRule()
for i := 0; i < b.N; i++ {
transformationName := "transformation" + b.Name()
err := rule.AddTransformation(transformationName, transformation)
if err != nil {
b.Fatalf("Failed to add a transformation: %s", err.Error())
}
}
}

func BenchmarkAddTransformationUniqueParallel(b *testing.B) {
transformation := func(input string) (string, bool, error) {
return "Test", true, nil
}
b.ResetTimer()
b.RunParallel(func(p *testing.PB) {
rule := NewRule()
for p.Next() {
transformationName := "transformation" + b.Name()
err := rule.AddTransformation(transformationName, transformation)
if err != nil {
b.Fatalf("Failed to add a transformation: %s", err.Error())
}
}
})
}


func BenchmarkAddTransformationSameParallel(b *testing.B) {
transformation := func(input string) (string, bool, error) {
return "Test", true, nil
}
b.ResetTimer()
b.RunParallel(func(p *testing.PB) {
for p.Next() {
rule := NewRule()
transformationName := "transformation"
err := rule.AddTransformation(transformationName, transformation)
if err != nil {
b.Fatalf("Failed to add a transformation: %s", err.Error())
}
}
})
}

func TestGetTransformationID(t *testing.T) {
// create an array of transformations using following string
transformations_values := []string{
"t:none,t:lowercase",
"t:none",
"t:none,t:htmlEntityDecode",
"t:none,t:lowercase",
"t:none,t:lowercase,t:removeWhiteSpace",
"t:none,t:urlDecode",
"t:none,t:urlDecode,t:urlDecodeUni",
"t:none,t:urlDecodeUni",
"t:none,t:urlDecodeUni,t:base64Decode",
"t:none,t:urlDecodeUni,t:cmdLine",
"t:none,t:urlDecodeUni,t:cmdLine,t:lowercase,t:removeWhiteSpace",
"t:none,t:urlDecodeUni,t:cmdLine,t:normalizePath,t:lowercase,t:removeWhiteSpace",
"t:none,t:urlDecodeUni,t:compressWhitespace",
"t:none,t:urlDecodeUni,t:htmlEntityDecode",
"t:none,t:urlDecodeUni,t:htmlEntityDecode,t:lowercase",
"t:none,t:urlDecodeUni,t:lowercase",
"t:none,t:urlDecodeUni,t:lowercase,t:urlDecode,t:htmlEntityDecode,t:jsDecode",
"t:none,t:urlDecodeUni,t:replaceComments",
"t:none,t:urlDecodeUni,t:urlDecode,t:htmlEntityDecode,t:jsDecode",
"t:none,t:utf8toUnicode,t:urlDecodeUni,t:compressWhitespace",
"t:none,t:utf8toUnicode,t:urlDecodeUni,t:htmlEntityDecode,t:jsDecode,t:cssDecode,t:lowercase,t:removeNulls",
"t:none,t:utf8toUnicode,t:urlDecodeUni,t:htmlEntityDecode,t:jsDecode,t:cssDecode,t:removeNulls",
"t:none,t:utf8toUnicode,t:urlDecodeUni,t:lowercase",
"t:none,t:utf8toUnicode,t:urlDecodeUni,t:normalizePathWin,t:lowercase,t:removeWhiteSpace",
"t:none,t:utf8toUnicode,t:urlDecodeUni,t:removeNulls",
"t:none,t:utf8toUnicode,t:urlDecodeUni,t:removeNulls,t:cmdLine",
}
hashes := make(map[uint64]string)
for _, value := range transformations_values {
// split the string by comma
transformations := strings.Split(value, ",")
currentID := 0
// iterate over the transformations
for _, transformation := range transformations {
transformation = strings.Split(strings.Trim(transformation, " "), ":")[1]
nextName := strconv.Itoa(currentID) + "+" + transformation
hasher := fnv.New64a()
hasher.Write([]byte(nextName))
id := hasher.Sum64()
oldname, ok := hashes[id]
if ok {
assert.Equal(t, oldname, nextName, "Hash collision detected for %s and %s:: %d", oldname, nextName, id)
}
hashes[id] = nextName
currentID = int(id)
assert.NotEqual(t, id, 0)
}
}
}

0 comments on commit c21c47d

Please sign in to comment.