Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/templates/parser_test.go
2070 views
1
package templates
2
3
import (
4
"errors"
5
"fmt"
6
"testing"
7
8
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/disk"
9
"github.com/projectdiscovery/nuclei/v3/pkg/model"
10
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
11
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/stringslice"
12
"github.com/stretchr/testify/require"
13
)
14
15
func TestLoadTemplate(t *testing.T) {
16
catalog := disk.NewCatalog("")
17
p := NewParser()
18
19
tt := []struct {
20
name string
21
template *Template
22
templateErr error
23
filter TagFilterConfig
24
25
expectedErr error
26
isValid bool
27
}{
28
{
29
name: "valid",
30
template: &Template{
31
ID: "CVE-2021-27330",
32
Info: model.Info{
33
Name: "Valid template",
34
Authors: stringslice.StringSlice{Value: "Author"},
35
SeverityHolder: severity.Holder{Severity: severity.Medium},
36
},
37
},
38
isValid: true,
39
},
40
{
41
name: "emptyTemplate",
42
template: &Template{},
43
isValid: false,
44
expectedErr: errors.New("cause=\"Could not load template emptyTemplate: cause=\\\"mandatory 'name' field is missing\\\"\\ncause=\\\"mandatory 'author' field is missing\\\"\\ncause=\\\"mandatory 'id' field is missing\\\"\""),
45
},
46
{
47
name: "emptyNameWithInvalidID",
48
template: &Template{
49
ID: "invalid id",
50
Info: model.Info{
51
Authors: stringslice.StringSlice{Value: "Author"},
52
SeverityHolder: severity.Holder{Severity: severity.Medium},
53
},
54
},
55
expectedErr: errors.New("cause=\"Could not load template emptyNameWithInvalidID: cause=\\\"mandatory 'name' field is missing\\\"\\ncause=\\\"invalid field format for 'id' (allowed format is ^([a-zA-Z0-9]+[-_])*[a-zA-Z0-9]+$)\\\"\""),
56
},
57
{
58
name: "emptySeverity",
59
template: &Template{
60
ID: "CVE-2021-27330",
61
Info: model.Info{
62
Name: "Valid template",
63
Authors: stringslice.StringSlice{Value: "Author"},
64
},
65
},
66
isValid: true,
67
expectedErr: errors.New("field 'severity' is missing"),
68
},
69
{
70
name: "template-without-severity-with-correct-filter-id",
71
template: &Template{
72
ID: "CVE-2021-27330",
73
Info: model.Info{
74
Name: "Valid template",
75
Authors: stringslice.StringSlice{Value: "Author"},
76
},
77
},
78
// should be error because the template is loaded
79
expectedErr: errors.New("field 'severity' is missing"),
80
isValid: true,
81
filter: TagFilterConfig{IncludeIds: []string{"CVE-2021-27330"}},
82
},
83
{
84
name: "template-without-severity-with-diff-filter-id",
85
template: &Template{
86
ID: "CVE-2021-27330",
87
Info: model.Info{
88
Name: "Valid template",
89
Authors: stringslice.StringSlice{Value: "Author"},
90
},
91
},
92
isValid: false,
93
filter: TagFilterConfig{IncludeIds: []string{"another-id"}},
94
// no error because the template is not loaded
95
expectedErr: nil,
96
},
97
}
98
99
for _, tc := range tt {
100
t.Run(tc.name, func(t *testing.T) {
101
p.parsedTemplatesCache.Store(tc.name, tc.template, nil, tc.templateErr)
102
103
tagFilter, err := NewTagFilter(&tc.filter)
104
require.Nil(t, err)
105
success, err := p.LoadTemplate(tc.name, tagFilter, nil, catalog)
106
if tc.expectedErr == nil {
107
require.NoError(t, err)
108
} else {
109
require.ErrorContains(t, err, tc.expectedErr.Error())
110
}
111
require.Equal(t, tc.isValid, success)
112
})
113
}
114
115
t.Run("invalidTemplateID", func(t *testing.T) {
116
tt := []struct {
117
id string
118
success bool
119
}{
120
{id: "A-B-C", success: true},
121
{id: "A-B-C-1", success: true},
122
{id: "CVE_2021_27330", success: true},
123
{id: "ABC DEF", success: false},
124
{id: "_-__AAA_", success: false},
125
{id: " CVE-2021-27330", success: false},
126
{id: "CVE-2021-27330 ", success: false},
127
{id: "CVE-2021-27330-", success: false},
128
{id: "-CVE-2021-27330-", success: false},
129
{id: "CVE-2021--27330", success: false},
130
{id: "CVE-2021+27330", success: false},
131
}
132
for i, tc := range tt {
133
name := fmt.Sprintf("regexp%d", i)
134
t.Run(name, func(t *testing.T) {
135
template := &Template{
136
ID: tc.id,
137
Info: model.Info{
138
Name: "Valid template",
139
Authors: stringslice.StringSlice{Value: "Author"},
140
SeverityHolder: severity.Holder{Severity: severity.Medium},
141
},
142
}
143
p.parsedTemplatesCache.Store(name, template, nil, nil)
144
145
tagFilter, err := NewTagFilter(&TagFilterConfig{})
146
require.Nil(t, err)
147
success, err := p.LoadTemplate(name, tagFilter, nil, catalog)
148
if tc.success {
149
require.NoError(t, err)
150
require.True(t, success)
151
} else {
152
require.ErrorContains(t, err, "invalid field format for 'id' (allowed format is ^([a-zA-Z0-9]+[-_])*[a-zA-Z0-9]+$)")
153
require.False(t, success)
154
}
155
})
156
}
157
})
158
}
159
160