Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regexp pattern for template-id #1151

Merged
merged 6 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions v2/pkg/parsers/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import (
"github.com/projectdiscovery/nuclei/v2/pkg/utils/stats"
)

const mandatoryFieldMissingTemplate = "mandatory '%s' field is missing"
const (
mandatoryFieldMissingTemplate = "mandatory '%s' field is missing"
invalidFieldFormatTemplate = "invalid field format for '%s' (allowed format is %s)"
)

// LoadTemplate returns true if the template is valid and matches the filtering criteria.
func LoadTemplate(templatePath string, tagFilter *filter.TagFilter, extraTags []string) (bool, error) {
Expand All @@ -30,12 +33,11 @@ func LoadTemplate(templatePath string, tagFilter *filter.TagFilter, extraTags []
return false, nil
}

templateInfo := template.Info
if validationError := validateMandatoryInfoFields(&templateInfo); validationError != nil {
if validationError := validateTemplateFields(template); validationError != nil {
return false, validationError
}

return isTemplateInfoMetadataMatch(tagFilter, &templateInfo, extraTags)
return isTemplateInfoMetadataMatch(tagFilter, &template.Info, extraTags)
}

// LoadWorkflow returns true if the workflow is valid and matches the filtering criteria.
Expand All @@ -45,10 +47,8 @@ func LoadWorkflow(templatePath string) (bool, error) {
return false, templateParseError
}

templateInfo := template.Info

if len(template.Workflows) > 0 {
if validationError := validateMandatoryInfoFields(&templateInfo); validationError != nil {
if validationError := validateTemplateFields(template); validationError != nil {
return false, validationError
}
return true, nil
Expand All @@ -71,10 +71,8 @@ func isTemplateInfoMetadataMatch(tagFilter *filter.TagFilter, templateInfo *mode
return match, err
}

func validateMandatoryInfoFields(info *model.Info) error {
if info == nil {
return fmt.Errorf(mandatoryFieldMissingTemplate, "info")
}
func validateTemplateFields(template *templates.Template) error {
info := template.Info
forgedhallpass marked this conversation as resolved.
Show resolved Hide resolved

if utils.IsBlank(info.Name) {
return fmt.Errorf(mandatoryFieldMissingTemplate, "name")
Expand All @@ -83,13 +81,23 @@ func validateMandatoryInfoFields(info *model.Info) error {
if info.Authors.IsEmpty() {
return fmt.Errorf(mandatoryFieldMissingTemplate, "author")
}

if template.ID == "" {
return fmt.Errorf(mandatoryFieldMissingTemplate, "id")
}

if !templateIDRegexp.MatchString(template.ID) {
return fmt.Errorf(invalidFieldFormatTemplate, "id", templateIDRegexp.String())
}

return nil
}

var (
parsedTemplatesCache *cache.Templates
ShouldValidate bool
fieldErrorRegexp = regexp.MustCompile(`not found in`)
templateIDRegexp = regexp.MustCompile(`^[A-Za-z0-9-_]+$`)
)

const (
Expand Down
68 changes: 68 additions & 0 deletions v2/pkg/parsers/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package parsers

import (
"errors"
"testing"

"github.com/projectdiscovery/nuclei/v2/pkg/catalog/loader/filter"
"github.com/projectdiscovery/nuclei/v2/pkg/model"
"github.com/projectdiscovery/nuclei/v2/pkg/model/types/stringslice"
"github.com/projectdiscovery/nuclei/v2/pkg/templates"
"github.com/stretchr/testify/require"
)

func TestLoadTemplate(t *testing.T) {
origTemplatesCache := parsedTemplatesCache
defer func() { parsedTemplatesCache = origTemplatesCache }()

tt := []struct {
name string
template *templates.Template
templateErr error

expectedErr error
}{
{
name: "valid",
template: &templates.Template{
ID: "CVE-2021-27330",
Info: model.Info{
Name: "Valid template",
Authors: stringslice.StringSlice{Value: "Author"},
},
},
},
{
name: "missingName",
template: &templates.Template{},
expectedErr: errors.New("mandatory 'name' field is missing"),
},
{
name: "invalidID",
template: &templates.Template{
ID: "ABC DEF",
Info: model.Info{
Name: "Invalid ID",
Authors: stringslice.StringSlice{Value: "Author"},
},
},
expectedErr: errors.New("invalid field format for 'id' (allowed format is ^[A-Za-z0-9-_]+$)"),
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
parsedTemplatesCache.Store(tc.name, tc.template, tc.templateErr)

tagFilter := filter.New(&filter.Config{})
success, err := LoadTemplate(tc.name, tagFilter, nil)
if tc.expectedErr == nil {
require.NoError(t, err)
require.True(t, success)
} else {
require.Equal(t, tc.expectedErr, err)
require.False(t, success)
}
})
}
}
2 changes: 1 addition & 1 deletion v2/pkg/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Template struct {
// examples:
// - name: ID Example
// value: "\"CVE-2021-19520\""
ID string `yaml:"id" jsonschema:"title=id of the template,description=The Unique ID for the template,example=cve-2021-19520"`
ID string `yaml:"id" jsonschema:"title=id of the template,description=The Unique ID for the template,example=cve-2021-19520,pattern=^[A-Za-z0-9-_]+$"`
// description: |
// Info contains metadata information about the template.
// examples:
Expand Down