Skip to content

Commit

Permalink
refactor(fuzz): use mapsutil.Map type (#5558)
Browse files Browse the repository at this point in the history
* refactor(fuzz): use `mapsutil.Map` type

Signed-off-by: Dwi Siswanto <[email protected]>

* fix(headless): handle empty `key` in `*Value.SetParsedValue`

Signed-off-by: Dwi Siswanto <[email protected]>

* feat(fuzz): add type assertion checks

Signed-off-by: Dwi Siswanto <[email protected]>

---------

Signed-off-by: Dwi Siswanto <[email protected]>
  • Loading branch information
dwisiswant0 authored Aug 28, 2024
1 parent 4a85e73 commit aac1af1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
21 changes: 18 additions & 3 deletions pkg/fuzz/component/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/projectdiscovery/nuclei/v3/pkg/fuzz/dataformat"
"github.com/projectdiscovery/retryablehttp-go"
"github.com/projectdiscovery/utils/maps"
urlutil "github.com/projectdiscovery/utils/url"
)

Expand Down Expand Up @@ -81,7 +82,7 @@ func (q *Path) Delete(key string) error {
// Rebuild returns a new request with the
// component rebuilt
func (q *Path) Rebuild() (*retryablehttp.Request, error) {
originalValues := make(map[string]interface{})
originalValues := mapsutil.Map[string, any]{}
splitted := strings.Split(q.req.URL.Path, "/")
for i := range splitted {
pathTillNow := strings.Join(splitted[:i+1], "/")
Expand All @@ -95,8 +96,22 @@ func (q *Path) Rebuild() (*retryablehttp.Request, error) {
lengthSplitted := len(q.value.parsed.Map)
for i := lengthSplitted; i > 0; i-- {
key := strconv.Itoa(i)
original := originalValues[key].(string)
new := q.value.parsed.Map[key].(string)

original, ok := originalValues.GetOrDefault(key, "").(string)
if !ok {
continue
}

new, ok := q.value.parsed.Map.GetOrDefault(key, "").(string)
if !ok {
continue
}

if new == original {
// no need to replace
continue
}

originalPath = strings.Replace(originalPath, original, new, 1)
}

Expand Down
12 changes: 11 additions & 1 deletion pkg/fuzz/component/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,22 @@ func (v *Value) SetParsed(data dataformat.KV, dataFormat string) {

// SetParsedValue sets the parsed value for a key
// in the parsed map
func (v *Value) SetParsedValue(key string, value string) bool {
func (v *Value) SetParsedValue(key, value string) bool {
if key == "" {
return false
}

origValue := v.parsed.Get(key)
if origValue == nil {
v.parsed.Set(key, value)
return true
}

// TODO(dwisiswant0): I'm sure that this can be simplified because
// `dataformat.KV.*` is a type of `mapsutil.*` where the value is `any`. So,
// it looks like we won't type conversion here or even have its own methods
// inside `dataformat.KV`.

// If the value is a list, append to it
// otherwise replace it
switch v := origValue.(type) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/fuzz/dataformat/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// if it's not important/significant (ex: json,xml) we use map
// this also allows us to iteratively implement ordered map
type KV struct {
Map map[string]interface{}
Map mapsutil.Map[string, any]
OrderedMap *mapsutil.OrderedMap[string, any]
}

Expand Down

0 comments on commit aac1af1

Please sign in to comment.