2024-03-13 01:27:15 +00:00
|
|
|
package templates
|
2021-10-19 22:31:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-10-20 20:14:04 +00:00
|
|
|
"fmt"
|
2021-10-19 22:31:38 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-10-17 12:14:13 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/disk"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/model"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/stringslice"
|
2023-02-25 19:47:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-10-19 22:31:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadTemplate(t *testing.T) {
|
2022-08-10 18:05:58 +00:00
|
|
|
catalog := disk.NewCatalog("")
|
2024-03-15 12:36:57 +00:00
|
|
|
p := NewParser()
|
2021-10-19 22:31:38 +00:00
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
2024-03-13 01:27:15 +00:00
|
|
|
template *Template
|
2021-10-19 22:31:38 +00:00
|
|
|
templateErr error
|
2024-03-14 23:01:09 +00:00
|
|
|
filter TagFilterConfig
|
2021-10-19 22:31:38 +00:00
|
|
|
|
|
|
|
expectedErr error
|
2023-04-27 09:57:30 +00:00
|
|
|
isValid bool
|
2021-10-19 22:31:38 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid",
|
2024-03-13 01:27:15 +00:00
|
|
|
template: &Template{
|
2021-10-19 22:31:38 +00:00
|
|
|
ID: "CVE-2021-27330",
|
|
|
|
Info: model.Info{
|
2023-04-27 09:57:30 +00:00
|
|
|
Name: "Valid template",
|
|
|
|
Authors: stringslice.StringSlice{Value: "Author"},
|
|
|
|
SeverityHolder: severity.Holder{Severity: severity.Medium},
|
2021-10-19 22:31:38 +00:00
|
|
|
},
|
|
|
|
},
|
2023-04-27 09:57:30 +00:00
|
|
|
isValid: true,
|
2021-10-19 22:31:38 +00:00
|
|
|
},
|
|
|
|
{
|
2021-10-20 20:24:11 +00:00
|
|
|
name: "emptyTemplate",
|
2024-03-13 01:27:15 +00:00
|
|
|
template: &Template{},
|
2023-04-27 09:57:30 +00:00
|
|
|
isValid: false,
|
2024-03-13 01:27:15 +00:00
|
|
|
expectedErr: errors.New("mandatory 'name' field is missing\nmandatory 'author' field is missing\nmandatory 'id' field is missing"),
|
2021-10-20 20:24:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "emptyNameWithInvalidID",
|
2024-03-13 01:27:15 +00:00
|
|
|
template: &Template{
|
2021-10-20 20:24:11 +00:00
|
|
|
ID: "invalid id",
|
|
|
|
Info: model.Info{
|
2023-04-27 09:57:30 +00:00
|
|
|
Authors: stringslice.StringSlice{Value: "Author"},
|
|
|
|
SeverityHolder: severity.Holder{Severity: severity.Medium},
|
2021-10-20 20:24:11 +00:00
|
|
|
},
|
|
|
|
},
|
2024-03-13 01:27:15 +00:00
|
|
|
expectedErr: errors.New("mandatory 'name' field is missing\ninvalid field format for 'id' (allowed format is ^([a-zA-Z0-9]+[-_])*[a-zA-Z0-9]+$)"),
|
2021-10-19 22:31:38 +00:00
|
|
|
},
|
2023-04-27 09:57:30 +00:00
|
|
|
{
|
|
|
|
name: "emptySeverity",
|
2024-03-13 01:27:15 +00:00
|
|
|
template: &Template{
|
2023-04-27 09:57:30 +00:00
|
|
|
ID: "CVE-2021-27330",
|
|
|
|
Info: model.Info{
|
|
|
|
Name: "Valid template",
|
|
|
|
Authors: stringslice.StringSlice{Value: "Author"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isValid: true,
|
|
|
|
expectedErr: errors.New("field 'severity' is missing"),
|
|
|
|
},
|
2023-05-02 09:42:55 +00:00
|
|
|
{
|
2023-08-01 18:33:43 +00:00
|
|
|
name: "template-without-severity-with-correct-filter-id",
|
2024-03-13 01:27:15 +00:00
|
|
|
template: &Template{
|
2023-05-02 09:42:55 +00:00
|
|
|
ID: "CVE-2021-27330",
|
|
|
|
Info: model.Info{
|
|
|
|
Name: "Valid template",
|
|
|
|
Authors: stringslice.StringSlice{Value: "Author"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// should be error because the template is loaded
|
|
|
|
expectedErr: errors.New("field 'severity' is missing"),
|
|
|
|
isValid: true,
|
2024-03-14 23:01:09 +00:00
|
|
|
filter: TagFilterConfig{IncludeIds: []string{"CVE-2021-27330"}},
|
2023-05-02 09:42:55 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-01 18:33:43 +00:00
|
|
|
name: "template-without-severity-with-diff-filter-id",
|
2024-03-13 01:27:15 +00:00
|
|
|
template: &Template{
|
2023-05-02 09:42:55 +00:00
|
|
|
ID: "CVE-2021-27330",
|
|
|
|
Info: model.Info{
|
|
|
|
Name: "Valid template",
|
|
|
|
Authors: stringslice.StringSlice{Value: "Author"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
isValid: false,
|
2024-03-14 23:01:09 +00:00
|
|
|
filter: TagFilterConfig{IncludeIds: []string{"another-id"}},
|
2023-05-02 09:42:55 +00:00
|
|
|
// no error because the template is not loaded
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
2021-10-19 22:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-03-13 20:02:36 +00:00
|
|
|
p.parsedTemplatesCache.Store(tc.name, tc.template, nil, tc.templateErr)
|
2021-10-19 22:31:38 +00:00
|
|
|
|
2024-03-13 01:27:15 +00:00
|
|
|
tagFilter, err := NewTagFilter(&tc.filter)
|
2022-08-25 11:22:08 +00:00
|
|
|
require.Nil(t, err)
|
2024-03-13 01:27:15 +00:00
|
|
|
success, err := p.LoadTemplate(tc.name, tagFilter, nil, catalog)
|
2021-10-19 22:31:38 +00:00
|
|
|
if tc.expectedErr == nil {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
2023-04-27 09:57:30 +00:00
|
|
|
require.ErrorContains(t, err, tc.expectedErr.Error())
|
2021-10-19 22:31:38 +00:00
|
|
|
}
|
2023-05-02 09:42:55 +00:00
|
|
|
require.Equal(t, tc.isValid, success)
|
2021-10-19 22:31:38 +00:00
|
|
|
})
|
|
|
|
}
|
2021-10-20 20:14:04 +00:00
|
|
|
|
|
|
|
t.Run("invalidTemplateID", func(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
id string
|
|
|
|
success bool
|
|
|
|
}{
|
|
|
|
{id: "A-B-C", success: true},
|
|
|
|
{id: "A-B-C-1", success: true},
|
|
|
|
{id: "CVE_2021_27330", success: true},
|
|
|
|
{id: "ABC DEF", success: false},
|
|
|
|
{id: "_-__AAA_", success: false},
|
|
|
|
{id: " CVE-2021-27330", success: false},
|
|
|
|
{id: "CVE-2021-27330 ", success: false},
|
|
|
|
{id: "CVE-2021-27330-", success: false},
|
|
|
|
{id: "-CVE-2021-27330-", success: false},
|
|
|
|
{id: "CVE-2021--27330", success: false},
|
|
|
|
{id: "CVE-2021+27330", success: false},
|
|
|
|
}
|
|
|
|
for i, tc := range tt {
|
|
|
|
name := fmt.Sprintf("regexp%d", i)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2024-03-13 01:27:15 +00:00
|
|
|
template := &Template{
|
2021-10-20 20:14:04 +00:00
|
|
|
ID: tc.id,
|
|
|
|
Info: model.Info{
|
2023-04-27 09:57:30 +00:00
|
|
|
Name: "Valid template",
|
|
|
|
Authors: stringslice.StringSlice{Value: "Author"},
|
|
|
|
SeverityHolder: severity.Holder{Severity: severity.Medium},
|
2021-10-20 20:14:04 +00:00
|
|
|
},
|
|
|
|
}
|
2024-03-13 20:02:36 +00:00
|
|
|
p.parsedTemplatesCache.Store(name, template, nil, nil)
|
2021-10-20 20:14:04 +00:00
|
|
|
|
2024-03-14 23:01:09 +00:00
|
|
|
tagFilter, err := NewTagFilter(&TagFilterConfig{})
|
2022-08-25 11:22:08 +00:00
|
|
|
require.Nil(t, err)
|
2024-03-13 01:27:15 +00:00
|
|
|
success, err := p.LoadTemplate(name, tagFilter, nil, catalog)
|
2021-10-20 20:14:04 +00:00
|
|
|
if tc.success {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, success)
|
|
|
|
} else {
|
2023-04-27 09:57:30 +00:00
|
|
|
require.ErrorContains(t, err, "invalid field format for 'id' (allowed format is ^([a-zA-Z0-9]+[-_])*[a-zA-Z0-9]+$)")
|
2021-10-20 20:14:04 +00:00
|
|
|
require.False(t, success)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-10-19 22:31:38 +00:00
|
|
|
}
|