2023-08-31 18:03:01 +05:30
|
|
|
package builtin
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/md5"
|
|
|
|
|
"reflect"
|
|
|
|
|
|
2025-07-09 14:47:26 -05:00
|
|
|
"github.com/Mzack9999/goja"
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
2023-08-31 18:03:01 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Dedupe is a javascript builtin for deduping values
|
|
|
|
|
type Dedupe struct {
|
|
|
|
|
m map[string]goja.Value
|
|
|
|
|
VM *goja.Runtime
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add adds a value to the dedupe
|
|
|
|
|
func (d *Dedupe) Add(call goja.FunctionCall) goja.Value {
|
|
|
|
|
allVars := []any{}
|
|
|
|
|
for _, v := range call.Arguments {
|
|
|
|
|
if v.Export() == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if v.ExportType().Kind() == reflect.Slice {
|
|
|
|
|
// convert []datatype to []interface{}
|
|
|
|
|
// since it cannot be type asserted to []interface{} directly
|
|
|
|
|
rfValue := reflect.ValueOf(v.Export())
|
|
|
|
|
for i := 0; i < rfValue.Len(); i++ {
|
|
|
|
|
allVars = append(allVars, rfValue.Index(i).Interface())
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
allVars = append(allVars, v.Export())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, v := range allVars {
|
|
|
|
|
hash := hashValue(v)
|
|
|
|
|
if _, ok := d.m[hash]; ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
d.m[hash] = d.VM.ToValue(v)
|
|
|
|
|
}
|
|
|
|
|
return d.VM.ToValue(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Values returns all values from the dedupe
|
|
|
|
|
func (d *Dedupe) Values(call goja.FunctionCall) goja.Value {
|
|
|
|
|
tmp := []goja.Value{}
|
|
|
|
|
for _, v := range d.m {
|
|
|
|
|
tmp = append(tmp, v)
|
|
|
|
|
}
|
|
|
|
|
return d.VM.ToValue(tmp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewDedupe creates a new dedupe builtin object
|
|
|
|
|
func NewDedupe(vm *goja.Runtime) *Dedupe {
|
|
|
|
|
return &Dedupe{
|
|
|
|
|
m: make(map[string]goja.Value),
|
|
|
|
|
VM: vm,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// hashValue returns a hash of the value
|
|
|
|
|
func hashValue(value interface{}) string {
|
|
|
|
|
res := types.ToString(value)
|
|
|
|
|
md5sum := md5.Sum([]byte(res))
|
|
|
|
|
return string(md5sum[:])
|
|
|
|
|
}
|