2024-03-13 02:27:15 +01:00
package templates
2021-10-20 01:31:38 +03:00
import (
"errors"
2021-10-20 23:14:04 +03:00
"fmt"
2021-10-20 01:31:38 +03:00
"testing"
2023-10-17 17:44:13 +05:30
"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-26 03:47:47 +08:00
"github.com/stretchr/testify/require"
2021-10-20 01:31:38 +03:00
)
func TestLoadTemplate ( t * testing . T ) {
2022-08-10 23:35:58 +05:30
catalog := disk . NewCatalog ( "" )
2024-03-15 13:36:57 +01:00
p := NewParser ( )
2021-10-20 01:31:38 +03:00
tt := [ ] struct {
name string
2024-03-13 02:27:15 +01:00
template * Template
2021-10-20 01:31:38 +03:00
templateErr error
2024-03-15 00:01:09 +01:00
filter TagFilterConfig
2021-10-20 01:31:38 +03:00
expectedErr error
2023-04-27 12:57:30 +03:00
isValid bool
2021-10-20 01:31:38 +03:00
} {
{
name : "valid" ,
2024-03-13 02:27:15 +01:00
template : & Template {
2021-10-20 01:31:38 +03:00
ID : "CVE-2021-27330" ,
Info : model . Info {
2023-04-27 12:57:30 +03:00
Name : "Valid template" ,
Authors : stringslice . StringSlice { Value : "Author" } ,
SeverityHolder : severity . Holder { Severity : severity . Medium } ,
2021-10-20 01:31:38 +03:00
} ,
} ,
2023-04-27 12:57:30 +03:00
isValid : true ,
2021-10-20 01:31:38 +03:00
} ,
{
2021-10-20 23:24:11 +03:00
name : "emptyTemplate" ,
2024-03-13 02:27:15 +01:00
template : & Template { } ,
2023-04-27 12:57:30 +03:00
isValid : false ,
2025-08-20 05:28:23 +05:30
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\\\"\"" ) ,
2021-10-20 23:24:11 +03:00
} ,
{
name : "emptyNameWithInvalidID" ,
2024-03-13 02:27:15 +01:00
template : & Template {
2021-10-20 23:24:11 +03:00
ID : "invalid id" ,
Info : model . Info {
2023-04-27 12:57:30 +03:00
Authors : stringslice . StringSlice { Value : "Author" } ,
SeverityHolder : severity . Holder { Severity : severity . Medium } ,
2021-10-20 23:24:11 +03:00
} ,
} ,
2025-08-20 05:28:23 +05:30
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]+$)\\\"\"" ) ,
2021-10-20 01:31:38 +03:00
} ,
2023-04-27 12:57:30 +03:00
{
name : "emptySeverity" ,
2024-03-13 02:27:15 +01:00
template : & Template {
2023-04-27 12:57:30 +03: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 15:12:55 +05:30
{
2023-08-01 14:33:43 -04:00
name : "template-without-severity-with-correct-filter-id" ,
2024-03-13 02:27:15 +01:00
template : & Template {
2023-05-02 15:12:55 +05:30
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-15 00:01:09 +01:00
filter : TagFilterConfig { IncludeIds : [ ] string { "CVE-2021-27330" } } ,
2023-05-02 15:12:55 +05:30
} ,
{
2023-08-01 14:33:43 -04:00
name : "template-without-severity-with-diff-filter-id" ,
2024-03-13 02:27:15 +01:00
template : & Template {
2023-05-02 15:12:55 +05:30
ID : "CVE-2021-27330" ,
Info : model . Info {
Name : "Valid template" ,
Authors : stringslice . StringSlice { Value : "Author" } ,
} ,
} ,
isValid : false ,
2024-03-15 00:01:09 +01:00
filter : TagFilterConfig { IncludeIds : [ ] string { "another-id" } } ,
2023-05-02 15:12:55 +05:30
// no error because the template is not loaded
expectedErr : nil ,
} ,
2021-10-20 01:31:38 +03:00
}
for _ , tc := range tt {
t . Run ( tc . name , func ( t * testing . T ) {
2024-03-13 21:02:36 +01:00
p . parsedTemplatesCache . Store ( tc . name , tc . template , nil , tc . templateErr )
2021-10-20 01:31:38 +03:00
2024-03-13 02:27:15 +01:00
tagFilter , err := NewTagFilter ( & tc . filter )
2022-08-25 13:22:08 +02:00
require . Nil ( t , err )
2024-03-13 02:27:15 +01:00
success , err := p . LoadTemplate ( tc . name , tagFilter , nil , catalog )
2021-10-20 01:31:38 +03:00
if tc . expectedErr == nil {
require . NoError ( t , err )
} else {
2023-04-27 12:57:30 +03:00
require . ErrorContains ( t , err , tc . expectedErr . Error ( ) )
2021-10-20 01:31:38 +03:00
}
2023-05-02 15:12:55 +05:30
require . Equal ( t , tc . isValid , success )
2021-10-20 01:31:38 +03:00
} )
}
2021-10-20 23:14:04 +03: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 02:27:15 +01:00
template := & Template {
2021-10-20 23:14:04 +03:00
ID : tc . id ,
Info : model . Info {
2023-04-27 12:57:30 +03:00
Name : "Valid template" ,
Authors : stringslice . StringSlice { Value : "Author" } ,
SeverityHolder : severity . Holder { Severity : severity . Medium } ,
2021-10-20 23:14:04 +03:00
} ,
}
2024-03-13 21:02:36 +01:00
p . parsedTemplatesCache . Store ( name , template , nil , nil )
2021-10-20 23:14:04 +03:00
2024-03-15 00:01:09 +01:00
tagFilter , err := NewTagFilter ( & TagFilterConfig { } )
2022-08-25 13:22:08 +02:00
require . Nil ( t , err )
2024-03-13 02:27:15 +01:00
success , err := p . LoadTemplate ( name , tagFilter , nil , catalog )
2021-10-20 23:14:04 +03:00
if tc . success {
require . NoError ( t , err )
require . True ( t , success )
} else {
2023-04-27 12:57:30 +03:00
require . ErrorContains ( t , err , "invalid field format for 'id' (allowed format is ^([a-zA-Z0-9]+[-_])*[a-zA-Z0-9]+$)" )
2021-10-20 23:14:04 +03:00
require . False ( t , success )
}
} )
}
} )
2021-10-20 01:31:38 +03:00
}