Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/templates/parser_validate.go
2070 views
1
package templates
2
3
import (
4
"errors"
5
6
"github.com/projectdiscovery/nuclei/v3/pkg/templates/types"
7
"github.com/projectdiscovery/nuclei/v3/pkg/utils"
8
"github.com/projectdiscovery/utils/errkit"
9
)
10
11
// validateTemplateMandatoryFields validates the mandatory fields of a template
12
// return error from this function will cause hard fail and not proceed further
13
func validateTemplateMandatoryFields(template *Template) error {
14
info := template.Info
15
16
var validateErrors []error
17
18
if utils.IsBlank(info.Name) {
19
validateErrors = append(validateErrors, errkit.Newf("mandatory '%s' field is missing", "name"))
20
}
21
22
if info.Authors.IsEmpty() {
23
validateErrors = append(validateErrors, errkit.Newf("mandatory '%s' field is missing", "author"))
24
}
25
26
if template.ID == "" {
27
validateErrors = append(validateErrors, errkit.Newf("mandatory '%s' field is missing", "id"))
28
} else if !ReTemplateID.MatchString(template.ID) {
29
validateErrors = append(validateErrors, errkit.Newf("invalid field format for '%s' (allowed format is %s)", "id", ReTemplateID.String()))
30
}
31
32
if len(validateErrors) > 0 {
33
return errors.Join(validateErrors...)
34
}
35
36
return nil
37
}
38
39
func isTemplateInfoMetadataMatch(tagFilter *TagFilter, template *Template, extraTags []string) (bool, error) {
40
match, err := tagFilter.Match(template, extraTags)
41
42
if err == ErrExcluded {
43
return false, ErrExcluded
44
}
45
46
return match, err
47
}
48
49
// validateTemplateOptionalFields validates the optional fields of a template
50
// return error from this function will throw a warning and proceed further
51
func validateTemplateOptionalFields(template *Template) error {
52
info := template.Info
53
54
var warnings []error
55
56
if template.Type() != types.WorkflowProtocol && utils.IsBlank(info.SeverityHolder.Severity.String()) {
57
warnings = append(warnings, errkit.Newf("field '%s' is missing", "severity"))
58
}
59
60
if len(warnings) > 0 {
61
return errors.Join(warnings...)
62
}
63
64
return nil
65
}
66
67