2024-03-13 02:27:15 +01:00
|
|
|
package templates
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2024-03-13 21:02:36 +01:00
|
|
|
"io"
|
2025-07-09 14:47:26 -05:00
|
|
|
"strings"
|
|
|
|
|
"sync"
|
2024-03-13 02:27:15 +01:00
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/utils"
|
2025-02-11 04:31:37 +07:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
|
2024-03-13 02:27:15 +01:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/utils/stats"
|
2024-03-13 21:02:36 +01:00
|
|
|
yamlutil "github.com/projectdiscovery/nuclei/v3/pkg/utils/yaml"
|
|
|
|
|
fileutil "github.com/projectdiscovery/utils/file"
|
2025-07-09 14:47:26 -05:00
|
|
|
|
2024-03-13 02:27:15 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Parser struct {
|
2024-03-13 21:02:36 +01:00
|
|
|
ShouldValidate bool
|
|
|
|
|
NoStrictSyntax bool
|
|
|
|
|
// this cache can be copied safely between ephemeral instances
|
|
|
|
|
parsedTemplatesCache *Cache
|
|
|
|
|
// this cache might potentially contain references to heap objects
|
|
|
|
|
// it's recommended to always empty it at the end of execution
|
2024-03-13 02:27:15 +01:00
|
|
|
compiledTemplatesCache *Cache
|
2025-07-09 14:47:26 -05:00
|
|
|
sync.Mutex
|
2024-03-13 02:27:15 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-15 13:36:57 +01:00
|
|
|
func NewParser() *Parser {
|
2024-03-13 02:27:15 +01:00
|
|
|
p := &Parser{
|
|
|
|
|
parsedTemplatesCache: NewCache(),
|
|
|
|
|
compiledTemplatesCache: NewCache(),
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-15 13:36:57 +01:00
|
|
|
return p
|
2024-03-13 02:27:15 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-31 18:53:55 +05:30
|
|
|
func NewParserWithParsedCache(cache *Cache) *Parser {
|
|
|
|
|
return &Parser{
|
|
|
|
|
parsedTemplatesCache: cache,
|
|
|
|
|
compiledTemplatesCache: NewCache(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-04 21:11:39 +02:00
|
|
|
// Cache returns the parsed templates cache
|
|
|
|
|
func (p *Parser) Cache() *Cache {
|
|
|
|
|
return p.parsedTemplatesCache
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-18 13:40:58 -05:00
|
|
|
// CompiledCache returns the compiled templates cache
|
|
|
|
|
func (p *Parser) CompiledCache() *Cache {
|
|
|
|
|
return p.compiledTemplatesCache
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Parser) ParsedCount() int {
|
|
|
|
|
p.Lock()
|
|
|
|
|
defer p.Unlock()
|
|
|
|
|
return len(p.parsedTemplatesCache.items.Map)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Parser) CompiledCount() int {
|
|
|
|
|
p.Lock()
|
|
|
|
|
defer p.Unlock()
|
|
|
|
|
return len(p.compiledTemplatesCache.items.Map)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 14:47:26 -05:00
|
|
|
func checkOpenFileError(err error) bool {
|
|
|
|
|
if err != nil && strings.Contains(err.Error(), "too many open files") {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 02:27:15 +01:00
|
|
|
// LoadTemplate returns true if the template is valid and matches the filtering criteria.
|
|
|
|
|
func (p *Parser) LoadTemplate(templatePath string, t any, extraTags []string, catalog catalog.Catalog) (bool, error) {
|
|
|
|
|
tagFilter, ok := t.(*TagFilter)
|
|
|
|
|
if !ok {
|
|
|
|
|
panic("not a *TagFilter")
|
|
|
|
|
}
|
|
|
|
|
t, templateParseError := p.ParseTemplate(templatePath, catalog)
|
|
|
|
|
if templateParseError != nil {
|
2025-07-09 14:47:26 -05:00
|
|
|
checkOpenFileError(templateParseError)
|
2025-08-20 05:28:23 +05:30
|
|
|
return false, ErrCouldNotLoadTemplate(templatePath, templateParseError.Error())
|
2024-03-13 02:27:15 +01:00
|
|
|
}
|
|
|
|
|
template, ok := t.(*Template)
|
|
|
|
|
if !ok {
|
|
|
|
|
panic("not a template")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(template.Workflows) > 0 {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validationError := validateTemplateMandatoryFields(template)
|
|
|
|
|
if validationError != nil {
|
|
|
|
|
stats.Increment(SyntaxErrorStats)
|
2025-08-20 05:28:23 +05:30
|
|
|
return false, ErrCouldNotLoadTemplate(templatePath, validationError.Error())
|
2024-03-13 02:27:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret, err := isTemplateInfoMetadataMatch(tagFilter, template, extraTags)
|
|
|
|
|
if err != nil {
|
2025-07-09 14:47:26 -05:00
|
|
|
checkOpenFileError(err)
|
2025-08-20 05:28:23 +05:30
|
|
|
return ret, ErrCouldNotLoadTemplate(templatePath, err.Error())
|
2024-03-13 02:27:15 +01:00
|
|
|
}
|
|
|
|
|
// if template loaded then check the template for optional fields to add warnings
|
|
|
|
|
if ret {
|
|
|
|
|
validationWarning := validateTemplateOptionalFields(template)
|
|
|
|
|
if validationWarning != nil {
|
|
|
|
|
stats.Increment(SyntaxWarningStats)
|
2025-07-09 14:47:26 -05:00
|
|
|
checkOpenFileError(validationWarning)
|
2025-08-20 05:28:23 +05:30
|
|
|
return ret, ErrCouldNotLoadTemplate(templatePath, validationWarning.Error())
|
2024-03-13 02:27:15 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ParseTemplate parses a template and returns a *templates.Template structure
|
|
|
|
|
func (p *Parser) ParseTemplate(templatePath string, catalog catalog.Catalog) (any, error) {
|
2024-03-13 21:02:36 +01:00
|
|
|
value, _, err := p.parsedTemplatesCache.Has(templatePath)
|
|
|
|
|
if value != nil {
|
2024-03-13 02:27:15 +01:00
|
|
|
return value, err
|
|
|
|
|
}
|
2024-03-13 21:02:36 +01:00
|
|
|
|
|
|
|
|
reader, err := utils.ReaderFromPathOrURL(templatePath, catalog)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
2025-07-09 14:47:26 -05:00
|
|
|
_ = reader.Close()
|
|
|
|
|
}()
|
2024-03-13 21:02:36 +01:00
|
|
|
|
2025-08-02 15:54:15 +05:30
|
|
|
// For local YAML files, check if preprocessing is needed
|
|
|
|
|
var data []byte
|
2024-03-13 21:02:36 +01:00
|
|
|
if fileutil.FileExists(templatePath) && config.GetTemplateFormatFromExt(templatePath) == config.YAML {
|
2025-08-02 15:54:15 +05:30
|
|
|
data, err = io.ReadAll(reader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-03-13 21:02:36 +01:00
|
|
|
data, err = yamlutil.PreProcess(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 02:27:15 +01:00
|
|
|
template := &Template{}
|
|
|
|
|
|
|
|
|
|
switch config.GetTemplateFormatFromExt(templatePath) {
|
|
|
|
|
case config.JSON:
|
2025-08-02 15:54:15 +05:30
|
|
|
if data == nil {
|
|
|
|
|
data, err = io.ReadAll(reader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-13 02:27:15 +01:00
|
|
|
err = json.Unmarshal(data, template)
|
|
|
|
|
case config.YAML:
|
2025-08-02 15:54:15 +05:30
|
|
|
if data != nil {
|
|
|
|
|
// Already read and preprocessed
|
|
|
|
|
if p.NoStrictSyntax {
|
|
|
|
|
err = yaml.Unmarshal(data, template)
|
|
|
|
|
} else {
|
|
|
|
|
err = yaml.UnmarshalStrict(data, template)
|
|
|
|
|
}
|
2024-03-13 02:27:15 +01:00
|
|
|
} else {
|
2025-08-02 15:54:15 +05:30
|
|
|
// Stream directly from reader
|
|
|
|
|
decoder := yaml.NewDecoder(reader)
|
|
|
|
|
if !p.NoStrictSyntax {
|
|
|
|
|
decoder.SetStrict(true)
|
|
|
|
|
}
|
|
|
|
|
err = decoder.Decode(template)
|
2024-03-13 02:27:15 +01:00
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
err = fmt.Errorf("failed to identify template format expected JSON or YAML but got %v", templatePath)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 15:54:15 +05:30
|
|
|
p.parsedTemplatesCache.Store(templatePath, template, nil, nil) // don't keep raw bytes to save memory
|
2024-03-13 02:27:15 +01:00
|
|
|
return template, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LoadWorkflow returns true if the workflow is valid and matches the filtering criteria.
|
|
|
|
|
func (p *Parser) LoadWorkflow(templatePath string, catalog catalog.Catalog) (bool, error) {
|
|
|
|
|
t, templateParseError := p.ParseTemplate(templatePath, catalog)
|
|
|
|
|
if templateParseError != nil {
|
|
|
|
|
return false, templateParseError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template, ok := t.(*Template)
|
|
|
|
|
if !ok {
|
|
|
|
|
panic("not a template")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(template.Workflows) > 0 {
|
|
|
|
|
if validationError := validateTemplateMandatoryFields(template); validationError != nil {
|
|
|
|
|
stats.Increment(SyntaxErrorStats)
|
|
|
|
|
return false, validationError
|
|
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|