github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/templates/preprocessors.go (about)

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