Skip to content

Commit 39f67ed

Browse files
authored
Merge pull request #6779 from projectdiscovery/dwisiswant0/perf/cache-template-signature-verification
perf: cache template signature verification
2 parents 52bade5 + 4534e9c commit 39f67ed

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

pkg/catalog/loader/loader.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ func New(cfg *Config) (*Store, error) {
172172
// Initialize metadata index and filter (load from disk & cache for reuse)
173173
store.metadataIndex = store.loadTemplatesIndex()
174174
store.indexFilter = store.buildIndexFilter()
175+
if cfg.ExecutorOptions != nil {
176+
cfg.ExecutorOptions.TemplateVerificationCallback = store.getTemplateVerification
177+
}
175178
store.saveMetadataIndexOnce = sync.OnceFunc(func() {
176179
if store.metadataIndex == nil {
177180
return
@@ -246,6 +249,22 @@ func New(cfg *Config) (*Store, error) {
246249
return store, nil
247250
}
248251

252+
func (store *Store) getTemplateVerification(templatePath string) *protocols.TemplateVerification {
253+
if store.metadataIndex == nil {
254+
return nil
255+
}
256+
257+
metadata, found := store.metadataIndex.Get(templatePath)
258+
if !found {
259+
return nil
260+
}
261+
262+
return &protocols.TemplateVerification{
263+
Verified: metadata.Verified,
264+
Verifier: metadata.TemplateVerifier,
265+
}
266+
}
267+
249268
func handleTemplatesEditorURLs(input string) string {
250269
parsed, err := url.Parse(input)
251270
if err != nil {

pkg/protocols/protocols.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ type Executer interface {
5757
ExecuteWithResults(ctx *scan.ScanContext) ([]*output.ResultEvent, error)
5858
}
5959

60+
// TemplateVerification holds cached verification information for a template.
61+
type TemplateVerification struct {
62+
Verified bool
63+
Verifier string
64+
}
65+
6066
// ExecutorOptions contains the configuration options for executer clients
6167
type ExecutorOptions struct {
6268
// TemplateID is the ID of the template for the request
@@ -67,6 +73,9 @@ type ExecutorOptions struct {
6773
TemplateInfo model.Info
6874
// TemplateVerifier is the verifier for the template
6975
TemplateVerifier string
76+
// TemplateVerificationCallback returns cached verification info for a template path.
77+
// If it returns nil, verification should be computed normally.
78+
TemplateVerificationCallback func(templatePath string) *TemplateVerification
7079
// RawTemplate is the raw template for the request
7180
RawTemplate []byte
7281
// Output is a writer interface for writing output events from executer.
@@ -266,6 +275,7 @@ func (e *ExecutorOptions) Copy() *ExecutorOptions {
266275
TemplatePath: e.TemplatePath,
267276
TemplateInfo: e.TemplateInfo,
268277
TemplateVerifier: e.TemplateVerifier,
278+
TemplateVerificationCallback: e.TemplateVerificationCallback,
269279
RawTemplate: e.RawTemplate,
270280
Output: e.Output,
271281
Options: e.Options,

pkg/templates/compile.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,19 @@ func parseTemplate(data []byte, srcOptions *protocols.ExecutorOptions) (*Templat
580580

581581
// check if the template is verified
582582
// only valid templates can be verified or signed
583+
if options.TemplateVerificationCallback != nil && options.TemplatePath != "" {
584+
if cached := options.TemplateVerificationCallback(options.TemplatePath); cached != nil {
585+
template.Verified = cached.Verified
586+
template.TemplateVerifier = cached.Verifier
587+
options.TemplateVerifier = cached.Verifier
588+
//nolint
589+
if !(template.Verified && template.TemplateVerifier == "projectdiscovery/nuclei-templates") {
590+
template.Options.RawTemplate = data
591+
}
592+
return template, nil
593+
}
594+
}
595+
583596
var verifier *signer.TemplateSigner
584597
for _, verifier = range signer.DefaultTemplateVerifiers {
585598
template.Verified, _ = verifier.Verify(data, template)
@@ -592,10 +605,12 @@ func parseTemplate(data []byte, srcOptions *protocols.ExecutorOptions) (*Templat
592605
}
593606
}
594607
options.TemplateVerifier = template.TemplateVerifier
608+
595609
//nolint
596610
if !(template.Verified && verifier.Identifier() == "projectdiscovery/nuclei-templates") {
597611
template.Options.RawTemplate = data
598612
}
613+
599614
return template, nil
600615
}
601616

0 commit comments

Comments
 (0)