From 10cf1053f85394aa52192609c11109b4bf643bc1 Mon Sep 17 00:00:00 2001 From: forgedhallpass <13679401+forgedhallpass@users.noreply.github.com> Date: Thu, 9 Dec 2021 10:32:01 +0200 Subject: [PATCH] feat: Improve DSL function UX #1295 (#1351) * feat: Improve DSL function UX #1295 Sort the output signatures * feat: Improve DSL function UX #1295 Sort the output signatures. Lint: simplified the sorting. --- v2/pkg/operators/common/dsl/dsl.go | 3 + v2/pkg/operators/common/dsl/dsl_test.go | 95 ++++++++++++++----------- 2 files changed, 55 insertions(+), 43 deletions(-) diff --git a/v2/pkg/operators/common/dsl/dsl.go b/v2/pkg/operators/common/dsl/dsl.go index 5b443102..2f005ad3 100644 --- a/v2/pkg/operators/common/dsl/dsl.go +++ b/v2/pkg/operators/common/dsl/dsl.go @@ -15,6 +15,7 @@ import ( "math/rand" "net/url" "regexp" + "sort" "strconv" "strings" "time" @@ -387,6 +388,8 @@ func AddHelperFunction(key string, value func(args ...interface{}) (interface{}, func GetPrintableDslFunctionSignatures(noColor bool) string { aggregateSignatures := func(values []string) string { + sort.Strings(values) + builder := &strings.Builder{} for _, value := range values { builder.WriteRune('\t') diff --git a/v2/pkg/operators/common/dsl/dsl_test.go b/v2/pkg/operators/common/dsl/dsl_test.go index f50447d6..bd2200c7 100644 --- a/v2/pkg/operators/common/dsl/dsl_test.go +++ b/v2/pkg/operators/common/dsl/dsl_test.go @@ -4,6 +4,7 @@ import ( "compress/gzip" "fmt" "io/ioutil" + "regexp" "strings" "testing" "time" @@ -92,47 +93,55 @@ func createSignatureError(signature string) string { return fmt.Errorf(invalidDslFunctionMessageTemplate, invalidDslFunctionError, signature).Error() } -func Test(t *testing.T) { - expectedColorizedSignatures := []string{ - "\x1b[93mbase64_py\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mprint_debug\x1b[0m(args \x1b[38;5;208m...interface{}\x1b[0m)\x1b[38;5;208m\x1b[0m", - "\x1b[93mregex\x1b[0m(arg1, arg2 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mmmh3\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mto_lower\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mmd5\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mreplace_regex\x1b[0m(arg1, arg2, arg3 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mhtml_unescape\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mhex_encode\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mrand_base\x1b[0m(length \x1b[38;5;208muint\x1b[0m, optionalCharSet \x1b[38;5;208mstring\x1b[0m)\x1b[38;5;208m string\x1b[0m", - "\x1b[93msha1\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mtrim_right\x1b[0m(arg1, arg2 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mwait_for\x1b[0m(seconds \x1b[38;5;208muint\x1b[0m)\x1b[38;5;208m\x1b[0m", - "\x1b[93mtrim\x1b[0m(arg1, arg2 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93murl_encode\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mto_upper\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mrand_text_alpha\x1b[0m(length \x1b[38;5;208muint\x1b[0m, optionalBadChars \x1b[38;5;208mstring\x1b[0m)\x1b[38;5;208m string\x1b[0m", - "\x1b[93msha256\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mgzip\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mlen\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mtrim_space\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mrand_int\x1b[0m(optionalMin, optionalMax \x1b[38;5;208muint\x1b[0m)\x1b[38;5;208m int\x1b[0m", - "\x1b[93mremove_bad_chars\x1b[0m(arg1, arg2 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mrand_char\x1b[0m(optionalCharSet \x1b[38;5;208mstring\x1b[0m)\x1b[38;5;208m string\x1b[0m", - "\x1b[93mreverse\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mhtml_escape\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mbase64\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mbase64_decode\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mhex_decode\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mtrim_prefix\x1b[0m(arg1, arg2 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93murl_decode\x1b[0m(arg1 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mreplace\x1b[0m(arg1, arg2, arg3 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mtrim_suffix\x1b[0m(arg1, arg2 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mrand_text_numeric\x1b[0m(length \x1b[38;5;208muint\x1b[0m, optionalBadNumbers \x1b[38;5;208mstring\x1b[0m)\x1b[38;5;208m string\x1b[0m", - "\x1b[93mcontains\x1b[0m(arg1, arg2 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mgenerate_java_gadget\x1b[0m(arg1, arg2, arg3 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93munix_time\x1b[0m(optionalSeconds \x1b[38;5;208muint\x1b[0m)\x1b[38;5;208m float64\x1b[0m", - "\x1b[93mtrim_left\x1b[0m(arg1, arg2 \x1b[38;5;208minterface{}\x1b[0m)\x1b[38;5;208m interface{}\x1b[0m", - "\x1b[93mrand_text_alphanumeric\x1b[0m(length \x1b[38;5;208muint\x1b[0m, optionalBadChars \x1b[38;5;208mstring\x1b[0m)\x1b[38;5;208m string\x1b[0m", - } - assert.ElementsMatch(t, expectedColorizedSignatures, colorizeDslFunctionSignatures()) +func TestGetPrintableDslFunctionSignatures(t *testing.T) { + expected := ` base64(arg1 interface{}) interface{} + base64_decode(arg1 interface{}) interface{} + base64_py(arg1 interface{}) interface{} + contains(arg1, arg2 interface{}) interface{} + generate_java_gadget(arg1, arg2, arg3 interface{}) interface{} + gzip(arg1 interface{}) interface{} + hex_decode(arg1 interface{}) interface{} + hex_encode(arg1 interface{}) interface{} + html_escape(arg1 interface{}) interface{} + html_unescape(arg1 interface{}) interface{} + len(arg1 interface{}) interface{} + md5(arg1 interface{}) interface{} + mmh3(arg1 interface{}) interface{} + print_debug(args ...interface{}) + rand_base(length uint, optionalCharSet string) string + rand_char(optionalCharSet string) string + rand_int(optionalMin, optionalMax uint) int + rand_text_alpha(length uint, optionalBadChars string) string + rand_text_alphanumeric(length uint, optionalBadChars string) string + rand_text_numeric(length uint, optionalBadNumbers string) string + regex(arg1, arg2 interface{}) interface{} + remove_bad_chars(arg1, arg2 interface{}) interface{} + replace(arg1, arg2, arg3 interface{}) interface{} + replace_regex(arg1, arg2, arg3 interface{}) interface{} + reverse(arg1 interface{}) interface{} + sha1(arg1 interface{}) interface{} + sha256(arg1 interface{}) interface{} + to_lower(arg1 interface{}) interface{} + to_upper(arg1 interface{}) interface{} + trim(arg1, arg2 interface{}) interface{} + trim_left(arg1, arg2 interface{}) interface{} + trim_prefix(arg1, arg2 interface{}) interface{} + trim_right(arg1, arg2 interface{}) interface{} + trim_space(arg1 interface{}) interface{} + trim_suffix(arg1, arg2 interface{}) interface{} + unix_time(optionalSeconds uint) float64 + url_decode(arg1 interface{}) interface{} + url_encode(arg1 interface{}) interface{} + wait_for(seconds uint) +` + t.Run("with coloring", func(t *testing.T) { + assert.Equal(t, expected, GetPrintableDslFunctionSignatures(false)) + }) + + t.Run("without coloring", func(t *testing.T) { + var decolorizerRegex = regexp.MustCompile(`\x1B\[[0-9;]*[a-zA-Z]`) + expectedSignaturesWithoutColor := decolorizerRegex.ReplaceAllString(expected, "") + + assert.Equal(t, expectedSignaturesWithoutColor, GetPrintableDslFunctionSignatures(true)) + }) }