forked from projectdiscovery/nuclei
-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocessors.go
39 lines (32 loc) · 920 Bytes
/
preprocessors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package templates
import (
"bytes"
"regexp"
"strings"
"github.com/segmentio/ksuid"
)
type Preprocessor interface {
Process(data []byte) []byte
}
var preprocessorRegex = regexp.MustCompile(`{{([a-z0-9_]+)}}`)
// expandPreprocessors expands the pre-processors if any for a template data.
func (template *Template) expandPreprocessors(data []byte) []byte {
foundMap := make(map[string]struct{})
for _, expression := range preprocessorRegex.FindAllStringSubmatch(string(data), -1) {
if len(expression) != 2 {
continue
}
value := expression[1]
if strings.Contains(value, "(") || strings.Contains(value, ")") {
continue
}
if _, ok := foundMap[value]; ok {
continue
}
foundMap[value] = struct{}{}
if strings.EqualFold(value, "randstr") || strings.HasPrefix(value, "randstr_") {
data = bytes.ReplaceAll(data, []byte(expression[0]), []byte(ksuid.New().String()))
}
}
return data
}