2021-07-15 13:41:41 +03:00
|
|
|
package parsers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2021-08-31 19:27:26 +05:30
|
|
|
"regexp"
|
2021-10-20 23:24:11 +03:00
|
|
|
"strings"
|
2021-07-19 21:04:08 +03:00
|
|
|
|
2022-08-10 23:35:58 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/catalog"
|
2021-07-15 13:41:41 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/loader/filter"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/templates"
|
2021-08-28 00:15:28 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/templates/cache"
|
2023-02-25 23:24:46 +01:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/templates/signer"
|
2021-07-15 13:41:41 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/utils"
|
2021-08-31 19:27:26 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/utils/stats"
|
2022-12-13 20:35:14 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
2021-07-15 13:41:41 +03:00
|
|
|
)
|
|
|
|
|
|
2021-10-20 01:31:38 +03:00
|
|
|
const (
|
2023-02-25 23:24:46 +01:00
|
|
|
errMandatoryFieldMissingFmt = "mandatory '%s' field is missing"
|
|
|
|
|
errInvalidFieldFmt = "invalid field format for '%s' (allowed format is %s)"
|
2021-10-20 01:31:38 +03:00
|
|
|
)
|
2021-07-15 13:41:41 +03:00
|
|
|
|
2021-08-19 15:17:25 +03:00
|
|
|
// LoadTemplate returns true if the template is valid and matches the filtering criteria.
|
2022-08-10 23:35:58 +05:30
|
|
|
func LoadTemplate(templatePath string, tagFilter *filter.TagFilter, extraTags []string, catalog catalog.Catalog) (bool, error) {
|
|
|
|
|
template, templateParseError := ParseTemplate(templatePath, catalog)
|
2021-07-15 13:41:41 +03:00
|
|
|
if templateParseError != nil {
|
|
|
|
|
return false, templateParseError
|
|
|
|
|
}
|
2021-08-19 15:17:25 +03:00
|
|
|
|
|
|
|
|
if len(template.Workflows) > 0 {
|
2021-08-19 01:58:54 +05:30
|
|
|
return false, nil
|
|
|
|
|
}
|
2021-07-15 13:41:41 +03:00
|
|
|
|
2021-10-20 01:31:38 +03:00
|
|
|
if validationError := validateTemplateFields(template); validationError != nil {
|
2021-10-25 17:42:01 +05:30
|
|
|
stats.Increment(SyntaxErrorStats)
|
2021-07-15 13:41:41 +03:00
|
|
|
return false, validationError
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 13:22:08 +02:00
|
|
|
return isTemplateInfoMetadataMatch(tagFilter, template, extraTags)
|
2021-08-19 15:17:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LoadWorkflow returns true if the workflow is valid and matches the filtering criteria.
|
2022-08-10 23:35:58 +05:30
|
|
|
func LoadWorkflow(templatePath string, catalog catalog.Catalog) (bool, error) {
|
|
|
|
|
template, templateParseError := ParseTemplate(templatePath, catalog)
|
2021-08-19 15:17:25 +03:00
|
|
|
if templateParseError != nil {
|
|
|
|
|
return false, templateParseError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(template.Workflows) > 0 {
|
2021-10-20 01:31:38 +03:00
|
|
|
if validationError := validateTemplateFields(template); validationError != nil {
|
2021-08-19 15:17:25 +03:00
|
|
|
return false, validationError
|
|
|
|
|
}
|
2021-08-28 00:19:05 +05:30
|
|
|
return true, nil
|
2021-07-15 13:41:41 +03:00
|
|
|
}
|
2021-08-19 15:17:25 +03:00
|
|
|
|
|
|
|
|
return false, nil
|
2021-07-15 13:41:41 +03:00
|
|
|
}
|
|
|
|
|
|
2022-08-25 13:22:08 +02:00
|
|
|
func isTemplateInfoMetadataMatch(tagFilter *filter.TagFilter, template *templates.Template, extraTags []string) (bool, error) {
|
|
|
|
|
match, err := tagFilter.Match(template, extraTags)
|
2021-08-19 15:17:25 +03:00
|
|
|
|
2021-07-15 13:41:41 +03:00
|
|
|
if err == filter.ErrExcluded {
|
|
|
|
|
return false, filter.ErrExcluded
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 15:17:25 +03:00
|
|
|
return match, err
|
2021-07-15 13:41:41 +03:00
|
|
|
}
|
|
|
|
|
|
2021-10-20 01:31:38 +03:00
|
|
|
func validateTemplateFields(template *templates.Template) error {
|
|
|
|
|
info := template.Info
|
2021-07-15 13:41:41 +03:00
|
|
|
|
2021-10-20 23:24:11 +03:00
|
|
|
var errors []string
|
|
|
|
|
|
2021-08-03 14:51:34 +03:00
|
|
|
if utils.IsBlank(info.Name) {
|
2023-02-25 23:24:46 +01:00
|
|
|
errors = append(errors, fmt.Sprintf(errMandatoryFieldMissingFmt, "name"))
|
2021-07-15 13:41:41 +03:00
|
|
|
}
|
|
|
|
|
|
2021-08-03 14:51:34 +03:00
|
|
|
if info.Authors.IsEmpty() {
|
2023-02-25 23:24:46 +01:00
|
|
|
errors = append(errors, fmt.Sprintf(errMandatoryFieldMissingFmt, "author"))
|
2021-07-15 13:41:41 +03:00
|
|
|
}
|
2021-10-20 01:31:38 +03:00
|
|
|
|
|
|
|
|
if template.ID == "" {
|
2023-02-25 23:24:46 +01:00
|
|
|
errors = append(errors, fmt.Sprintf(errMandatoryFieldMissingFmt, "id"))
|
2021-10-20 23:24:11 +03:00
|
|
|
} else if !templateIDRegexp.MatchString(template.ID) {
|
2023-02-25 23:24:46 +01:00
|
|
|
errors = append(errors, fmt.Sprintf(errInvalidFieldFmt, "id", templateIDRegexp.String()))
|
2021-10-20 01:31:38 +03:00
|
|
|
}
|
|
|
|
|
|
2021-10-20 23:24:11 +03:00
|
|
|
if len(errors) > 0 {
|
|
|
|
|
return fmt.Errorf(strings.Join(errors, ", "))
|
2021-10-20 01:31:38 +03:00
|
|
|
}
|
|
|
|
|
|
2021-07-15 13:41:41 +03:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 19:53:53 +05:30
|
|
|
var (
|
|
|
|
|
parsedTemplatesCache *cache.Templates
|
|
|
|
|
ShouldValidate bool
|
2022-07-13 13:30:11 +02:00
|
|
|
NoStrictSyntax bool
|
2021-10-20 22:58:36 +03:00
|
|
|
templateIDRegexp = regexp.MustCompile(`^([a-zA-Z0-9]+[-_])*[a-zA-Z0-9]+$`)
|
2021-08-31 19:53:53 +05:30
|
|
|
)
|
2021-08-31 19:27:26 +05:30
|
|
|
|
2021-08-31 21:39:20 +05:30
|
|
|
const (
|
2021-12-05 15:11:14 +01:00
|
|
|
SyntaxWarningStats = "syntax-warnings"
|
|
|
|
|
SyntaxErrorStats = "syntax-errors"
|
|
|
|
|
RuntimeWarningsStats = "runtime-warnings"
|
2021-08-31 21:39:20 +05:30
|
|
|
)
|
2021-08-28 00:27:37 +05:30
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
parsedTemplatesCache = cache.New()
|
2021-08-31 19:27:26 +05:30
|
|
|
|
2021-09-01 02:01:55 +05:30
|
|
|
stats.NewEntry(SyntaxWarningStats, "Found %d templates with syntax warning (use -validate flag for further examination)")
|
|
|
|
|
stats.NewEntry(SyntaxErrorStats, "Found %d templates with syntax error (use -validate flag for further examination)")
|
2021-12-05 15:11:14 +01:00
|
|
|
stats.NewEntry(RuntimeWarningsStats, "Found %d templates with runtime error (use -validate flag for further examination)")
|
2021-08-28 00:27:37 +05:30
|
|
|
}
|
2021-08-27 17:06:06 +03:00
|
|
|
|
2021-08-19 02:10:36 +05:30
|
|
|
// ParseTemplate parses a template and returns a *templates.Template structure
|
2022-08-10 23:35:58 +05:30
|
|
|
func ParseTemplate(templatePath string, catalog catalog.Catalog) (*templates.Template, error) {
|
2021-08-28 00:15:28 +05:30
|
|
|
if value, err := parsedTemplatesCache.Has(templatePath); value != nil {
|
|
|
|
|
return value.(*templates.Template), err
|
2021-08-27 17:06:06 +03:00
|
|
|
}
|
2022-08-10 23:35:58 +05:30
|
|
|
data, err := utils.ReadFromPathOrURL(templatePath, catalog)
|
2021-07-15 13:41:41 +03:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template := &templates.Template{}
|
2023-02-25 23:24:46 +01:00
|
|
|
|
|
|
|
|
// check if the template is verified
|
|
|
|
|
if signer.DefaultVerifier != nil {
|
|
|
|
|
template.Verified, _ = signer.Verify(signer.DefaultVerifier, data)
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-13 13:30:11 +02:00
|
|
|
if NoStrictSyntax {
|
|
|
|
|
err = yaml.Unmarshal(data, template)
|
|
|
|
|
} else {
|
|
|
|
|
err = yaml.UnmarshalStrict(data, template)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2022-07-24 14:56:06 +05:30
|
|
|
stats.Increment(SyntaxErrorStats)
|
|
|
|
|
return nil, err
|
2021-07-15 13:41:41 +03:00
|
|
|
}
|
2023-02-25 23:24:46 +01:00
|
|
|
|
2021-08-28 00:15:28 +05:30
|
|
|
parsedTemplatesCache.Store(templatePath, template, nil)
|
2021-07-15 13:41:41 +03:00
|
|
|
return template, nil
|
|
|
|
|
}
|