package templates
import (
"bytes"
"regexp"
"strings"
stringsutil "github.com/projectdiscovery/utils/strings"
"github.com/segmentio/ksuid"
)
type Preprocessor interface {
ProcessNReturnData(data []byte) ([]byte, map[string]interface{})
Exists(data []byte) bool
}
var (
preprocessorRegex = regexp.MustCompile(`{{([a-z0-9_]+)}}`)
defaultPreprocessors = []Preprocessor{
&randStrPreprocessor{},
}
)
func getPreprocessors(preprocessor Preprocessor) []Preprocessor {
if preprocessor != nil {
tmp := make([]Preprocessor, 0, len(defaultPreprocessors)+1)
tmp = append(tmp, preprocessor)
tmp = append(tmp, defaultPreprocessors...)
return tmp
}
return defaultPreprocessors
}
var _ Preprocessor = &randStrPreprocessor{}
type randStrPreprocessor struct{}
func (r *randStrPreprocessor) ProcessNReturnData(data []byte) ([]byte, map[string]interface{}) {
foundMap := make(map[string]struct{})
dataMap := make(map[string]interface{})
for _, expression := range preprocessorRegex.FindAllStringSubmatch(string(data), -1) {
if len(expression) != 2 {
continue
}
value := expression[1]
if stringsutil.ContainsAny(value, "(", ")") {
continue
}
if _, ok := foundMap[value]; ok {
continue
}
foundMap[value] = struct{}{}
if strings.EqualFold(value, "randstr") || strings.HasPrefix(value, "randstr_") {
randStr := ksuid.New().String()
data = bytes.ReplaceAll(data, []byte(expression[0]), []byte(randStr))
dataMap[expression[0]] = randStr
}
}
return data, dataMap
}
func (r *randStrPreprocessor) Exists(data []byte) bool {
return bytes.Contains(data, []byte("randstr"))
}