Merge pull request #1645 from projectdiscovery/gzip-decode

Added gzip_decode helper function
dev
Sandeep Singh 2022-02-28 12:33:56 +05:30 committed by GitHub
commit 909c9765d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 7 deletions

View File

@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"html"
"io"
"math"
"math/rand"
"net/url"
@ -101,12 +102,26 @@ func init() {
buffer := &bytes.Buffer{}
writer := gzip.NewWriter(buffer)
if _, err := writer.Write([]byte(args[0].(string))); err != nil {
_ = writer.Close()
return "", err
}
_ = writer.Close()
return buffer.String(), nil
}),
"gzip_decode": makeDslFunction(1, func(args ...interface{}) (interface{}, error) {
reader, err := gzip.NewReader(strings.NewReader(args[0].(string)))
if err != nil {
return "", err
}
data, err := io.ReadAll(reader)
if err != nil {
_ = reader.Close()
return "", err
}
_ = reader.Close()
return string(data), nil
}),
"base64_py": makeDslFunction(1, func(args ...interface{}) (interface{}, error) {
// python encodes to base64 with lines of 76 bytes terminated by new line "\n"
stdBase64 := base64.StdEncoding.EncodeToString([]byte(types.ToString(args[0])))

View File

@ -1,12 +1,9 @@
package dsl
import (
"compress/gzip"
"fmt"
"io/ioutil"
"math"
"regexp"
"strings"
"testing"
"time"
@ -40,15 +37,18 @@ func TestDSLTimeComparison(t *testing.T) {
func TestDSLGzipSerialize(t *testing.T) {
compiled, err := govaluate.NewEvaluableExpressionWithFunctions("gzip(\"hello world\")", HelperFunctions())
require.Nil(t, err, "could not compare time")
require.Nil(t, err, "could not compile encoder")
result, err := compiled.Evaluate(make(map[string]interface{}))
require.Nil(t, err, "could not evaluate compare time")
reader, _ := gzip.NewReader(strings.NewReader(types.ToString(result)))
data, _ := ioutil.ReadAll(reader)
compiled, err = govaluate.NewEvaluableExpressionWithFunctions("gzip_decode(data)", HelperFunctions())
require.Nil(t, err, "could not compile decoder")
require.Equal(t, "hello world", string(data), "could not get gzip encoded data")
data, err := compiled.Evaluate(map[string]interface{}{"data": result})
require.Nil(t, err, "could not evaluate decoded data")
require.Equal(t, "hello world", data.(string), "could not get gzip encoded data")
}
func TestDslFunctionSignatures(t *testing.T) {
@ -102,6 +102,7 @@ func TestGetPrintableDslFunctionSignatures(t *testing.T) {
contains(arg1, arg2 interface{}) interface{}
generate_java_gadget(arg1, arg2, arg3 interface{}) interface{}
gzip(arg1 interface{}) interface{}
gzip_decode(arg1 interface{}) interface{}
hex_decode(arg1 interface{}) interface{}
hex_encode(arg1 interface{}) interface{}
html_escape(arg1 interface{}) interface{}