-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(vardump): use
godump
lib (#5676)
* refactor(vardump): use `godump` lib also increate limit char to `255`. Signed-off-by: Dwi Siswanto <git@dw1.io> * feat(vardump): add global var `Limit` Signed-off-by: Dwi Siswanto <git@dw1.io> * chore(protocols): rm newline Signed-off-by: Dwi Siswanto <git@dw1.io> * feat(types): add `VarDumpLimit` option Signed-off-by: Dwi Siswanto <git@dw1.io> * test(vardump): add test cases Signed-off-by: Dwi Siswanto <git@dw1.io> * chore: tidy up mod Signed-off-by: Dwi Siswanto <git@dw1.io> --------- Signed-off-by: Dwi Siswanto <git@dw1.io>
- Loading branch information
1 parent
53f56e1
commit 2c832f5
Showing
19 changed files
with
131 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,67 @@ | ||
package vardump | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/projectdiscovery/nuclei/v3/pkg/types" | ||
mapsutil "github.com/projectdiscovery/utils/maps" | ||
"github.com/yassinebenaid/godump" | ||
) | ||
|
||
// EnableVarDump enables var dump for debugging optionally | ||
var EnableVarDump bool | ||
// variables is a map of variables | ||
type variables = map[string]any | ||
|
||
// DumpVariables writes the truncated dump of variables to a string | ||
// in a formatted key-value manner. | ||
// | ||
// The values are truncated to return 50 characters from start and end. | ||
func DumpVariables(data map[string]interface{}) string { | ||
var counter int | ||
// DumpVariables dumps the variables in a pretty format | ||
func DumpVariables(data variables) string { | ||
if !EnableVarDump { | ||
return "" | ||
} | ||
|
||
d := godump.Dumper{ | ||
Indentation: " ", | ||
HidePrivateFields: false, | ||
ShowPrimitiveNamedTypes: true, | ||
} | ||
|
||
d.Theme = godump.Theme{ | ||
String: godump.RGB{R: 138, G: 201, B: 38}, | ||
Quotes: godump.RGB{R: 112, G: 214, B: 255}, | ||
Bool: godump.RGB{R: 249, G: 87, B: 56}, | ||
Number: godump.RGB{R: 10, G: 178, B: 242}, | ||
Types: godump.RGB{R: 0, G: 150, B: 199}, | ||
Address: godump.RGB{R: 205, G: 93, B: 0}, | ||
PointerTag: godump.RGB{R: 110, G: 110, B: 110}, | ||
Nil: godump.RGB{R: 219, G: 57, B: 26}, | ||
Func: godump.RGB{R: 160, G: 90, B: 220}, | ||
Fields: godump.RGB{R: 189, G: 176, B: 194}, | ||
Chan: godump.RGB{R: 195, G: 154, B: 76}, | ||
UnsafePointer: godump.RGB{R: 89, G: 193, B: 180}, | ||
Braces: godump.RGB{R: 185, G: 86, B: 86}, | ||
} | ||
|
||
buffer := &strings.Builder{} | ||
buffer.Grow(len(data) * 78) // grow buffer to an approximate size | ||
return d.Sprint(process(data, Limit)) | ||
} | ||
|
||
builder := &strings.Builder{} | ||
// sort keys for deterministic output | ||
// process is a helper function that processes the variables | ||
// and returns a new map of variables | ||
func process(data variables, limit int) variables { | ||
keys := mapsutil.GetSortedKeys(data) | ||
vars := make(variables) | ||
|
||
if limit == 0 { | ||
limit = 255 | ||
} | ||
|
||
for _, k := range keys { | ||
v := data[k] | ||
valueString := types.ToString(v) | ||
|
||
counter++ | ||
if len(valueString) > 50 { | ||
builder.Grow(56) | ||
builder.WriteString(valueString[0:25]) | ||
builder.WriteString(" .... ") | ||
builder.WriteString(valueString[len(valueString)-25:]) | ||
valueString = builder.String() | ||
builder.Reset() | ||
v := types.ToString(data[k]) | ||
v = strings.ReplaceAll(strings.ReplaceAll(v, "\r", " "), "\n", " ") | ||
if len(v) > limit { | ||
v = v[:limit] | ||
v += " [...]" | ||
} | ||
valueString = strings.ReplaceAll(strings.ReplaceAll(valueString, "\r", " "), "\n", " ") | ||
|
||
buffer.WriteString("\t") | ||
buffer.WriteString(strconv.Itoa(counter)) | ||
buffer.WriteString(". ") | ||
buffer.WriteString(k) | ||
buffer.WriteString(" => ") | ||
buffer.WriteString(valueString) | ||
buffer.WriteString("\n") | ||
|
||
vars[k] = v | ||
} | ||
final := buffer.String() | ||
return final | ||
|
||
return vars | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package vardump | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDumpVariables(t *testing.T) { | ||
// Enable var dump for testing | ||
EnableVarDump = true | ||
|
||
// Test case | ||
testVars := variables{ | ||
"string": "test", | ||
"int": 42, | ||
"bool": true, | ||
"slice": []string{"a", "b", "c"}, | ||
} | ||
|
||
result := DumpVariables(testVars) | ||
|
||
// Assertions | ||
assert.NotEmpty(t, result) | ||
assert.Contains(t, result, "string") | ||
assert.Contains(t, result, "test") | ||
assert.Contains(t, result, "int") | ||
assert.Contains(t, result, "42") | ||
assert.Contains(t, result, "bool") | ||
assert.Contains(t, result, "true") | ||
assert.Contains(t, result, "slice") | ||
assert.Contains(t, result, "a") | ||
assert.Contains(t, result, "b") | ||
assert.Contains(t, result, "c") | ||
|
||
// Test with EnableVarDump set to false | ||
EnableVarDump = false | ||
result = DumpVariables(testVars) | ||
assert.Empty(t, result) | ||
} | ||
|
||
func TestProcess(t *testing.T) { | ||
testVars := variables{ | ||
"short": "short string", | ||
"long": strings.Repeat("a", 300), | ||
"number": 42, | ||
} | ||
|
||
processed := process(testVars, 255) | ||
|
||
assert.Equal(t, "short string", processed["short"]) | ||
assert.Equal(t, strings.Repeat("a", 255)+" [...]", processed["long"]) | ||
assert.Equal(t, "42", processed["number"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package vardump | ||
|
||
var ( | ||
// EnableVarDump enables var dump for debugging optionally | ||
EnableVarDump bool | ||
// Limit is the maximum characters to be dumped | ||
Limit int = 255 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters