add unit test coverage for remote tempates

This commit is contained in:
Sajad Parra 2022-01-21 20:14:50 +05:30
parent 9865b14c70
commit ba33622073
4 changed files with 61 additions and 6 deletions

View File

@ -30,7 +30,7 @@ require (
github.com/projectdiscovery/fastdialer v0.0.13
github.com/projectdiscovery/filekv v0.0.0-20210915124239-3467ef45dd08
github.com/projectdiscovery/fileutil v0.0.0-20210928100737-cab279c5d4b5
github.com/projectdiscovery/goflags v0.0.8-0.20220117072744-aa5d42bd5994
github.com/projectdiscovery/goflags v0.0.8-0.20220121110825-48035ad3ffe0
github.com/projectdiscovery/gologger v1.1.4
github.com/projectdiscovery/hmap v0.0.2-0.20210917080408-0fd7bd286bfa
github.com/projectdiscovery/interactsh v0.0.8-0.20211231143029-74e9182d2cbe

View File

@ -624,6 +624,8 @@ github.com/projectdiscovery/goflags v0.0.8-0.20211028121123-edf02bc05b1a h1:EzwV
github.com/projectdiscovery/goflags v0.0.8-0.20211028121123-edf02bc05b1a/go.mod h1:Jjwsf4eEBPXDSQI2Y+6fd3dBumJv/J1U0nmpM+hy2YY=
github.com/projectdiscovery/goflags v0.0.8-0.20220117072744-aa5d42bd5994 h1:4EuhJ+YTGA1eqMnR7zI9BLV0Wu0BvHaDKE9LbxSUFb8=
github.com/projectdiscovery/goflags v0.0.8-0.20220117072744-aa5d42bd5994/go.mod h1:Jjwsf4eEBPXDSQI2Y+6fd3dBumJv/J1U0nmpM+hy2YY=
github.com/projectdiscovery/goflags v0.0.8-0.20220121110825-48035ad3ffe0 h1:KtCp/dCsxXNdT8m0yyWc/4ou4YaKWVakAr3G03TjQCk=
github.com/projectdiscovery/goflags v0.0.8-0.20220121110825-48035ad3ffe0/go.mod h1:Jjwsf4eEBPXDSQI2Y+6fd3dBumJv/J1U0nmpM+hy2YY=
github.com/projectdiscovery/gologger v1.0.1/go.mod h1:Ok+axMqK53bWNwDSU1nTNwITLYMXMdZtRc8/y1c7sWE=
github.com/projectdiscovery/gologger v1.1.4 h1:qWxGUq7ukHWT849uGPkagPKF3yBPYAsTtMKunQ8O2VI=
github.com/projectdiscovery/gologger v1.1.4/go.mod h1:Bhb6Bdx2PV1nMaFLoXNBmHIU85iROS9y1tBuv7T5pMY=

View File

@ -1,6 +1,7 @@
package loader
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
@ -38,3 +39,55 @@ func TestLoadTemplates(t *testing.T) {
require.Equal(t, []string{templatesDirectory}, store.finalTemplates, "could not get correct templates")
})
}
func TestRemoteTemplates(t *testing.T) {
var nilStringSlice []string
type args struct {
config *Config
}
tests := []struct {
name string
args args
want *Store
wantErr bool
}{
{
name: "remote-templates-positive",
args: args{
config: &Config{
TemplateURLs: []string{"https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/master/technologies/tech-detect.yaml"},
RemoteTemplateDomainList: []string{"localhost","raw.githubusercontent.com"},
},
},
want: &Store{
finalTemplates: []string{"https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/master/technologies/tech-detect.yaml"},
},
wantErr: false,
},
{
name: "remote-templates-negative",
args: args{
config: &Config{
TemplateURLs: []string{"https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/master/technologies/tech-detect.yaml"},
RemoteTemplateDomainList: []string{"localhost"},
},
},
want: &Store{
finalTemplates: nilStringSlice,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.config)
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got.finalTemplates, tt.want.finalTemplates) {
t.Errorf("New() = %v, want %v", got.finalTemplates, tt.want.finalTemplates)
}
})
}
}

View File

@ -23,14 +23,14 @@ type RemoteContentError struct {
Error error
}
func getRemoteTemplatesAndWorkflows(templateURLs, workflowURLs, rtdl []string) ([]string, []string, error) {
func getRemoteTemplatesAndWorkflows(templateURLs, workflowURLs, remoteTemplateDomainList []string) ([]string, []string, error) {
remoteContentErrorChannel := make(chan RemoteContentError)
for _, templateURL := range templateURLs {
go getRemoteContent(templateURL, rtdl, remoteContentErrorChannel, Template)
go getRemoteContent(templateURL, remoteTemplateDomainList, remoteContentErrorChannel, Template)
}
for _, workflowURL := range workflowURLs {
go getRemoteContent(workflowURL, rtdl, remoteContentErrorChannel, Workflow)
go getRemoteContent(workflowURL, remoteTemplateDomainList, remoteContentErrorChannel, Workflow)
}
var remoteTemplateList []string
@ -56,7 +56,7 @@ func getRemoteTemplatesAndWorkflows(templateURLs, workflowURLs, rtdl []string) (
return remoteTemplateList, remoteWorkFlowList, err
}
func getRemoteContent(URL string, rtdl []string, w chan<- RemoteContentError, contentType ContentType) {
func getRemoteContent(URL string, remoteTemplateDomainList []string, w chan<- RemoteContentError, contentType ContentType) {
if strings.HasPrefix(URL, "http") && (strings.HasSuffix(URL, ".yaml") || strings.HasSuffix(URL, ".yml")) {
parsed, err := url.Parse(URL)
if err != nil {
@ -65,7 +65,7 @@ func getRemoteContent(URL string, rtdl []string, w chan<- RemoteContentError, co
}
return
}
if !stringSliceContains(rtdl, parsed.Host) {
if !stringSliceContains(remoteTemplateDomainList, parsed.Host) {
w <- RemoteContentError{
Error: errors.Errorf("Remote template URL host (%s) is not present in the `remote-template-domain` list in nuclei config", parsed.Host),
}