64 lines
1.6 KiB
Go
Raw Normal View History

package extractors
import (
"fmt"
"regexp"
2021-01-12 11:21:32 +05:30
"strings"
2021-08-01 14:42:04 +02:00
2022-04-20 11:32:13 +02:00
"github.com/Knetic/govaluate"
2021-08-01 14:42:04 +02:00
"github.com/itchyny/gojq"
2022-04-20 11:32:13 +02:00
"github.com/projectdiscovery/nuclei/v2/pkg/operators/common/dsl"
)
2021-09-07 17:31:46 +03:00
// CompileExtractors performs the initial setup operation on an extractor
func (e *Extractor) CompileExtractors() error {
2021-09-07 17:31:46 +03:00
// Set up the extractor type
computedType, err := toExtractorTypes(e.GetType().String())
if err != nil {
return fmt.Errorf("unknown extractor type specified: %s", e.Type)
}
e.extractorType = computedType
// Compile the regexes
for _, regex := range e.Regex {
compiled, err := regexp.Compile(regex)
if err != nil {
return fmt.Errorf("could not compile regex: %s", regex)
}
e.regexCompiled = append(e.regexCompiled, compiled)
}
2021-01-12 11:21:32 +05:30
for i, kval := range e.KVal {
e.KVal[i] = strings.ToLower(kval)
}
2021-08-01 14:42:04 +02:00
for _, query := range e.JSON {
2021-07-31 22:49:23 +02:00
query, err := gojq.Parse(query)
if err != nil {
return fmt.Errorf("could not parse json: %s", query)
}
compiled, err := gojq.Compile(query)
if err != nil {
return fmt.Errorf("could not compile json: %s", query)
}
e.jsonCompiled = append(e.jsonCompiled, compiled)
}
2022-04-20 11:32:13 +02:00
for _, dslExp := range e.DSL {
compiled, err := govaluate.NewEvaluableExpressionWithFunctions(dslExp, dsl.HelperFunctions)
2022-04-20 11:32:13 +02:00
if err != nil {
return &dsl.CompilationError{DslSignature: dslExp, WrappedError: err}
2022-04-20 11:32:13 +02:00
}
e.dslCompiled = append(e.dslCompiled, compiled)
}
if e.CaseInsensitive {
if e.GetType() != KValExtractor {
return fmt.Errorf("case-insensitive flag is supported only for 'kval' extractors (not '%s')", e.Type)
}
for i := range e.KVal {
e.KVal[i] = strings.ToLower(e.KVal[i])
}
}
return nil
}