Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/tmplexec/flow/builtin/dedupe.go
2073 views
1
package builtin
2
3
import (
4
"crypto/md5"
5
"reflect"
6
7
"github.com/Mzack9999/goja"
8
"github.com/projectdiscovery/nuclei/v3/pkg/types"
9
)
10
11
// Dedupe is a javascript builtin for deduping values
12
type Dedupe struct {
13
m map[string]goja.Value
14
VM *goja.Runtime
15
}
16
17
// Add adds a value to the dedupe
18
func (d *Dedupe) Add(call goja.FunctionCall) goja.Value {
19
allVars := []any{}
20
for _, v := range call.Arguments {
21
if v.Export() == nil {
22
continue
23
}
24
if v.ExportType().Kind() == reflect.Slice {
25
// convert []datatype to []interface{}
26
// since it cannot be type asserted to []interface{} directly
27
rfValue := reflect.ValueOf(v.Export())
28
for i := 0; i < rfValue.Len(); i++ {
29
allVars = append(allVars, rfValue.Index(i).Interface())
30
}
31
} else {
32
allVars = append(allVars, v.Export())
33
}
34
}
35
for _, v := range allVars {
36
hash := hashValue(v)
37
if _, ok := d.m[hash]; ok {
38
continue
39
}
40
d.m[hash] = d.VM.ToValue(v)
41
}
42
return d.VM.ToValue(true)
43
}
44
45
// Values returns all values from the dedupe
46
func (d *Dedupe) Values(call goja.FunctionCall) goja.Value {
47
tmp := []goja.Value{}
48
for _, v := range d.m {
49
tmp = append(tmp, v)
50
}
51
return d.VM.ToValue(tmp)
52
}
53
54
// NewDedupe creates a new dedupe builtin object
55
func NewDedupe(vm *goja.Runtime) *Dedupe {
56
return &Dedupe{
57
m: make(map[string]goja.Value),
58
VM: vm,
59
}
60
}
61
62
// hashValue returns a hash of the value
63
func hashValue(value interface{}) string {
64
res := types.ToString(value)
65
md5sum := md5.Sum([]byte(res))
66
return string(md5sum[:])
67
}
68
69