Repository navigation
Expand file tree
/
Copy pathparser.go
More file actions
276 lines (234 loc) · 7.86 KB
/
Copy pathparser.go
File metadata and controls
276 lines (234 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package templates
import (
"bytes"
"fmt"
"io"
"strings"
"sync"
"golang.org/x/sync/singleflight"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
"github.com/projectdiscovery/nuclei/v3/pkg/utils"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/stats"
yamlutil "github.com/projectdiscovery/nuclei/v3/pkg/utils/yaml"
"github.com/projectdiscovery/utils/errkit"
fileutil "github.com/projectdiscovery/utils/file"
)
type Parser struct {
ShouldValidate bool
NoStrictSyntax bool
// parsedTemplatesCache stores clean parsed template structures used for
// validation, filtering, and engine-local compilation. Entries also retain
// source bytes when they exactly match the parsed structure.
parsedTemplatesCache *Cache
// compiledTemplatesCache stores fully compiled templates with all protocol
// requests.
// This cache contains references to heap objects and should be purged when
// no longer needed.
compiledTemplatesCache *Cache
compiledTemplatesLoadGroup singleflight.Group
sync.Mutex
}
func NewParser() *Parser {
p := &Parser{
parsedTemplatesCache: NewCache(),
compiledTemplatesCache: NewCache(),
}
return p
}
func NewParserWithParsedCache(cache *Cache) *Parser {
return &Parser{
parsedTemplatesCache: cache,
compiledTemplatesCache: NewCache(),
}
}
// NewExecutionParser creates a parser for one engine execution. Parsed template
// data is shared with the long-lived parent, while compiled templates remain
// private because they retain execution-specific options and mutable state.
func NewExecutionParser(parent *Parser) *Parser {
if parent == nil {
return NewParser()
}
parent.Lock()
defer parent.Unlock()
return &Parser{
ShouldValidate: parent.ShouldValidate,
NoStrictSyntax: parent.NoStrictSyntax,
parsedTemplatesCache: parent.parsedTemplatesCache,
compiledTemplatesCache: NewCache(),
}
}
// Purge clears the parsed and compiled template caches. It should be called
// when the parser is no longer needed (e.g. on engine Close) so a long-running
// embedder does not retain every compiled template (a heap-heavy object) for
// the entire process lifetime.
func (p *Parser) Purge() {
p.Lock()
defer p.Unlock()
p.parsedTemplatesCache.Purge()
p.compiledTemplatesCache.Purge()
}
// PurgeCompiled releases execution-specific compiled templates without
// clearing parsed template data that may be shared by other executions.
func (p *Parser) PurgeCompiled() {
p.Lock()
defer p.Unlock()
p.compiledTemplatesCache.Purge()
}
// Cache returns the parsed templates cache
func (p *Parser) Cache() *Cache {
return p.parsedTemplatesCache
}
// CompiledCache returns the compiled templates cache
func (p *Parser) CompiledCache() *Cache {
return p.compiledTemplatesCache
}
func (p *Parser) ParsedCount() int {
p.Lock()
defer p.Unlock()
return len(p.parsedTemplatesCache.items.Map)
}
func (p *Parser) CompiledCount() int {
p.Lock()
defer p.Unlock()
return len(p.compiledTemplatesCache.items.Map)
}
func checkOpenFileError(err error) bool {
if err != nil && strings.Contains(err.Error(), "too many open files") {
panic(err)
}
return false
}
// LoadTemplate returns true if the template is valid and matches the filtering criteria.
func (p *Parser) LoadTemplate(templatePath string, t any, extraTags []string, catalog catalog.Catalog) (bool, error) {
tagFilter, ok := t.(*TagFilter)
if !ok {
panic("not a *TagFilter")
}
t, templateParseError := p.ParseTemplate(templatePath, catalog)
if templateParseError != nil {
checkOpenFileError(templateParseError)
return false, errkit.Newf("Could not load template %s: %s", templatePath, templateParseError)
}
template, ok := t.(*Template)
if !ok {
panic("not a template")
}
return MatchLoadFilters(template, templatePath, tagFilter, extraTags)
}
// MatchLoadFilters applies the checks LoadTemplate runs after parsing, so a
// caller holding parsed templates can filter them without parsing again.
func MatchLoadFilters(template *Template, templatePath string, tagFilter *TagFilter, extraTags []string) (bool, error) {
if len(template.Workflows) > 0 {
return false, nil
}
validationError := validateTemplateMandatoryFields(template)
if validationError != nil {
stats.Increment(TemplateSyntaxErrorStats)
return false, errkit.Newf("Could not load template %s: %s", templatePath, validationError)
}
ret, err := isTemplateInfoMetadataMatch(tagFilter, template, extraTags)
if err != nil {
checkOpenFileError(err)
return ret, errkit.Newf("Could not load template %s: %s", templatePath, err)
}
// if template loaded then check the template for optional fields to add warnings
if ret {
validationWarning := validateTemplateOptionalFields(template)
if validationWarning != nil {
stats.Increment(TemplateSyntaxWarningStats)
checkOpenFileError(validationWarning)
return ret, errkit.Newf("Could not load template %s: %s", templatePath, validationWarning)
}
}
return ret, nil
}
// ParseTemplate parses a template and returns a *templates.Template structure
func (p *Parser) ParseTemplate(templatePath string, catalog catalog.Catalog) (any, error) {
value, _, err := p.parsedTemplatesCache.Has(templatePath)
if value != nil {
return value, err
}
// Multiple engine executions can share the parsed cache. Coalesce their
// concurrent first access so an immutable template is read and parsed once.
// Recheck inside the flight because another caller may have populated the
// cache between the optimistic lookup above and becoming the flight leader.
key := fmt.Sprintf("%t:%s", p.NoStrictSyntax, templatePath)
loaded, loadErr, _ := p.parsedTemplatesCache.loads.Do(key, func() (any, error) {
cached, _, cachedErr := p.parsedTemplatesCache.Has(templatePath)
if cached != nil {
return cached, cachedErr
}
return p.parseTemplate(templatePath, catalog)
})
return loaded, loadErr
}
func (p *Parser) parseTemplate(templatePath string, catalog catalog.Catalog) (any, error) {
reader, err := utils.ReaderFromPathOrURL(templatePath, catalog)
if err != nil {
return nil, err
}
defer func() {
_ = reader.Close()
}()
sourceData, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
data := sourceData
cacheSource := true
// For local YAML files, check if preprocessing is needed.
if fileutil.FileExists(templatePath) && config.GetTemplateFormatFromExt(templatePath) == config.YAML {
data, err = yamlutil.PreProcess(data, templatePath)
if err != nil {
return nil, err
}
cacheSource = bytes.Equal(data, sourceData)
}
template := &Template{}
switch config.GetTemplateFormatFromExt(templatePath) {
case config.JSON:
if p.NoStrictSyntax {
err = json.Unmarshal(data, template)
} else {
err = template.unmarshalJSONStrict(data)
}
case config.YAML:
if p.NoStrictSyntax {
err = yamlutil.Unmarshal(data, template)
} else {
err = yamlutil.UnmarshalStrict(data, template)
}
default:
err = fmt.Errorf("failed to identify template format expected JSON or YAML but got %v", templatePath)
}
if err != nil {
return nil, err
}
if cacheSource {
p.parsedTemplatesCache.Store(templatePath, template, sourceData, nil)
} else {
p.parsedTemplatesCache.StoreWithoutRaw(templatePath, template, nil)
}
return template, nil
}
// LoadWorkflow returns true if the workflow is valid and matches the filtering criteria.
func (p *Parser) LoadWorkflow(templatePath string, catalog catalog.Catalog) (bool, error) {
t, templateParseError := p.ParseTemplate(templatePath, catalog)
if templateParseError != nil {
return false, templateParseError
}
template, ok := t.(*Template)
if !ok {
panic("not a template")
}
if len(template.Workflows) > 0 {
if validationError := validateTemplateMandatoryFields(template); validationError != nil {
stats.Increment(TemplateSyntaxErrorStats)
return false, validationError
}
return true, nil
}
return false, nil
}