nuclei/v2/pkg/templates/preprocessors.go

40 lines
920 B
Go
Raw Normal View History

2021-02-16 09:48:57 +00:00
package templates
import (
"bytes"
"regexp"
"strings"
"github.com/segmentio/ksuid"
)
2021-07-21 05:32:44 +00:00
type Preprocessor interface {
Process(data []byte) []byte
}
var preprocessorRegex = regexp.MustCompile(`{{([a-z0-9_]+)}}`)
2021-02-16 09:48:57 +00:00
// expandPreprocessors expands the pre-processors if any for a template data.
2021-11-25 14:57:43 +00:00
func (template *Template) expandPreprocessors(data []byte) []byte {
2021-02-16 09:48:57 +00:00
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
}
2021-02-16 09:48:57 +00:00
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
}