Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/catalog/index/metadata.go
2848 views
1
package index
2
3
import (
4
"os"
5
"slices"
6
"time"
7
8
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
9
"github.com/projectdiscovery/nuclei/v3/pkg/templates"
10
"github.com/projectdiscovery/nuclei/v3/pkg/templates/types"
11
)
12
13
// Metadata contains lightweight metadata extracted from a template.
14
type Metadata struct {
15
// ID is the unique identifier of the template.
16
ID string `gob:"id"`
17
18
// FilePath is the path to the template file.
19
FilePath string `gob:"file_path"`
20
21
// ModTime is the modification time of the template file.
22
ModTime time.Time `gob:"mod_time"`
23
24
// Name is the name of the template.
25
Name string `gob:"name"`
26
27
// Authors are the authors of the template.
28
Authors []string `gob:"authors"`
29
30
// Tags are the tags associated with the template.
31
Tags []string `gob:"tags"`
32
33
// Severity is the severity level of the template.
34
Severity string `gob:"severity"`
35
36
// ProtocolType is the primary protocol type of the template.
37
ProtocolType string `gob:"protocol_type"`
38
39
// Verified indicates whether the template is verified.
40
Verified bool `gob:"verified"`
41
42
// TemplateVerifier is the verifier used for the template.
43
TemplateVerifier string `gob:"verifier,omitempty"`
44
45
// NOTE(dwisiswant0): Consider adding more fields here in the future to
46
// enhance filtering caps w/o loading full templates, such as:
47
// `has_{code,headless,file}` to indicate presence of protocol-based
48
// requests, and/or classification fields (CVE, CWE, CVSS, EPSS), if needed.
49
//
50
// For maintainers: when adding new fields, don't forget to update the
51
// Weigher logic in [NewIndex] to account for the new fields in cache weight
52
// calculation, because it affects cache eviction behavior. Also, consider
53
// the impact on existing cached data and whether a [IndexVersion] bump is
54
// needed.
55
}
56
57
// NewMetadataFromTemplate creates a new metadata object from a template.
58
func NewMetadataFromTemplate(path string, tpl *templates.Template) *Metadata {
59
return &Metadata{
60
ID: tpl.ID,
61
FilePath: path,
62
63
Name: tpl.Info.Name,
64
Authors: tpl.Info.Authors.ToSlice(),
65
Tags: tpl.Info.Tags.ToSlice(),
66
Severity: tpl.Info.SeverityHolder.Severity.String(),
67
68
ProtocolType: tpl.Type().String(),
69
70
Verified: tpl.Verified,
71
TemplateVerifier: tpl.TemplateVerifier,
72
}
73
}
74
75
// IsValid checks if the cached metadata is still valid by comparing the file
76
// modification time.
77
func (m *Metadata) IsValid() bool {
78
info, err := os.Stat(m.FilePath)
79
if err != nil {
80
return false
81
}
82
83
return m.ModTime.Equal(info.ModTime())
84
}
85
86
// MatchesSeverity checks if the metadata matches the given severity.
87
func (m *Metadata) MatchesSeverity(sev severity.Severity) bool {
88
return m.Severity == sev.String()
89
}
90
91
// MatchesProtocol checks if the metadata matches the given protocol type.
92
func (m *Metadata) MatchesProtocol(protocolType types.ProtocolType) bool {
93
return m.ProtocolType == protocolType.String()
94
}
95
96
// HasTag checks if the metadata contains the given tag.
97
func (m *Metadata) HasTag(tag string) bool {
98
return slices.Contains(m.Tags, tag)
99
}
100
101
// HasAuthor checks if the metadata contains the given author.
102
func (m *Metadata) HasAuthor(author string) bool {
103
return slices.Contains(m.Authors, author)
104
}
105
106