Template folder exists changes (#1825)

dev
Sami 2022-04-11 01:29:22 -05:00 committed by GitHub
parent 5ddc37e8d7
commit ce79a8dc57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 16 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"strings"
"github.com/projectdiscovery/nuclei/v2/pkg/testutils"
"github.com/projectdiscovery/nuclei/v2/pkg/utils"
@ -13,6 +14,8 @@ func getTemplatePath() string {
}
var templatesPathTestCases = map[string]testutils.TestCase{
//template folder path issue
"http/get.yaml": &folderPathTemplateTest{},
//cwd
"./dns/cname-fingerprint.yaml": &cwdTemplateTest{},
//relative path
@ -25,15 +28,10 @@ type cwdTemplateTest struct{}
// Execute executes a test case and returns an error if occurred
func (h *cwdTemplateTest) Execute(filePath string) error {
var routerErr error
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "8x8exch02.8x8.com", debug)
if err != nil {
return err
}
if routerErr != nil {
return routerErr
}
return expectResultsCount(results, 1)
}
@ -41,15 +39,10 @@ type relativePathTemplateTest struct{}
// Execute executes a test case and returns an error if occurred
func (h *relativePathTemplateTest) Execute(filePath string) error {
var routerErr error
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "8x8exch02.8x8.com", debug)
if err != nil {
return err
}
if routerErr != nil {
return routerErr
}
return expectResultsCount(results, 1)
}
@ -57,14 +50,23 @@ type absolutePathTemplateTest struct{}
// Execute executes a test case and returns an error if occurred
func (h *absolutePathTemplateTest) Execute(filePath string) error {
var routerErr error
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "8x8exch02.8x8.com", debug)
if err != nil {
return err
}
if routerErr != nil {
return routerErr
}
return expectResultsCount(results, 1)
}
type folderPathTemplateTest struct{}
// Execute executes a test case and returns an error if occurred
func (h *folderPathTemplateTest) Execute(filePath string) error {
results, err := testutils.RunNucleiBinaryAndGetCombinedOutput(debug, []string{"-t", filePath, "-target", "http://example.com"})
if err != nil {
return err
}
if strings.Contains(results, "installing") {
return fmt.Errorf("couldn't find template path,re-installing")
}
return nil
}

View File

@ -96,7 +96,7 @@ func (r *Runner) updateTemplates() error { // TODO this method does more than ju
ctx := context.Background()
var noTemplatesFound bool
if !fileutil.FileExists(r.templatesConfig.TemplatesDirectory) {
if !fileutil.FolderExists(r.templatesConfig.TemplatesDirectory) {
noTemplatesFound = true
}

View File

@ -93,6 +93,21 @@ func RunNucleiBinaryAndGetLoadedTemplates(nucleiBinary string, debug bool, args
}
return matches[0][1], nil
}
func RunNucleiBinaryAndGetCombinedOutput(debug bool, args []string) (string, error) {
cmd := exec.Command("./nuclei", args...)
if debug {
cmd.Args = append(cmd.Args, "-debug")
fmt.Println(cmd.String())
}
data, err := cmd.CombinedOutput()
if debug {
fmt.Println(string(data))
}
if err != nil {
return "", err
}
return string(data), nil
}
// TestCase is a single integration test case
type TestCase interface {